2013-07-30 21:10:03 +00:00
|
|
|
var DataSource = require('../../loopback-datasource-juggler').DataSource;
|
|
|
|
var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
|
2013-05-17 17:54:14 +00:00
|
|
|
var ds = new DataSource('memory');
|
2013-05-17 21:41:04 +00:00
|
|
|
|
2013-05-17 17:54:14 +00:00
|
|
|
// define models
|
|
|
|
var Post = ds.define('Post', {
|
2013-05-17 21:41:04 +00:00
|
|
|
title: { type: String, length: 255 },
|
|
|
|
content: { type: DataSource.Text },
|
|
|
|
date: { type: Date, default: function () {
|
|
|
|
return new Date;
|
|
|
|
} },
|
|
|
|
timestamp: { type: Number, default: Date.now },
|
2013-05-17 17:54:14 +00:00
|
|
|
published: { type: Boolean, default: false, index: true }
|
|
|
|
});
|
|
|
|
|
|
|
|
// simplier way to describe model
|
|
|
|
var User = ds.define('User', {
|
2013-05-17 21:41:04 +00:00
|
|
|
name: String,
|
|
|
|
bio: DataSource.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
age: Number
|
2013-05-17 17:54:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var Group = ds.define('Group', {name: String});
|
|
|
|
|
|
|
|
// define any custom method
|
|
|
|
User.prototype.getNameAndAge = function () {
|
|
|
|
return this.name + ', ' + this.age;
|
|
|
|
};
|
|
|
|
|
|
|
|
var user = new User({name: 'Joe'});
|
|
|
|
console.log(user);
|
|
|
|
|
2013-05-17 21:41:04 +00:00
|
|
|
// console.log(ds.models);
|
|
|
|
// console.log(ds.definitions);
|
|
|
|
|
|
|
|
// setup relationships
|
|
|
|
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
|
|
|
|
|
|
|
|
// creates instance methods:
|
|
|
|
// user.posts(conds)
|
|
|
|
// user.posts.build(data) // like new Post({userId: user.id});
|
|
|
|
// user.posts.create(data) // build and save
|
|
|
|
|
|
|
|
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
|
|
|
// creates instance methods:
|
|
|
|
// post.author(callback) -- getter when called with function
|
|
|
|
// post.author() -- sync getter when called without params
|
|
|
|
// post.author(user) -- setter when called with object
|
|
|
|
|
|
|
|
User.hasAndBelongsToMany('groups');
|
|
|
|
|
|
|
|
var user2 = new User({name: 'Smith'});
|
|
|
|
user2.save(function (err) {
|
|
|
|
console.log(user2);
|
|
|
|
var post = user2.posts.build({title: 'Hello world'});
|
|
|
|
post.save(function(err, data) {
|
|
|
|
console.log(err ? err: data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Post.findOne({where: {published: false}, order: 'date DESC'}, function (err, data) {
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
User.create({name: 'Jeff'}, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(data);
|
|
|
|
var post = data.posts.build({title: 'My Post'});
|
|
|
|
console.log(post);
|
|
|
|
});
|
|
|
|
|
|
|
|
User.create({name: 'Ray'}, function (err, data) {
|
|
|
|
console.log(data);
|
|
|
|
});
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-05-17 23:21:12 +00:00
|
|
|
var Article = ds.define('Article', {title: String});
|
|
|
|
var Tag = ds.define('Tag', {name: String});
|
|
|
|
Article.hasAndBelongsToMany('tags');
|
|
|
|
|
|
|
|
Article.create(function(e, article) {
|
|
|
|
article.tags.create({name: 'popular'}, function (err, data) {
|
|
|
|
Article.findOne(function(e, article) {
|
|
|
|
article.tags(function(e, tags) {
|
|
|
|
console.log(tags);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-05-24 00:29:03 +00:00
|
|
|
// should be able to attach a data source to an existing model
|
2013-05-24 05:20:20 +00:00
|
|
|
var modelBuilder = new ModelBuilder();
|
2013-05-17 17:54:14 +00:00
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
Color = modelBuilder.define('Color', {
|
2013-05-24 00:29:03 +00:00
|
|
|
name: String
|
|
|
|
});
|
|
|
|
|
|
|
|
// attach
|
|
|
|
ds.attach(Color);
|
|
|
|
|
|
|
|
Color.create({name: 'red'});
|
|
|
|
Color.create({name: 'green'});
|
|
|
|
Color.create({name: 'blue'});
|
|
|
|
|
|
|
|
Color.all(function (err, colors) {
|
|
|
|
console.log(colors);
|
|
|
|
});
|
2013-05-17 17:54:14 +00:00
|
|
|
|