loopback-datasource-juggler/examples/datasource-app.js

118 lines
2.9 KiB
JavaScript
Raw Normal View History

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', {
2014-01-24 17:09:53 +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 },
published: { type: Boolean, default: false, index: true }
2013-05-17 17:54:14 +00:00
});
// simplier way to describe model
var User = ds.define('User', {
2014-01-24 17:09:53 +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 () {
2014-01-24 17:09:53 +00:00
return this.name + ', ' + this.age;
2013-05-17 17:54:14 +00:00
};
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');
2014-03-01 06:29:22 +00:00
var user2 = new User({name: 'Smith', age: 14});
2013-05-17 21:41:04 +00:00
user2.save(function (err) {
2014-01-24 17:09:53 +00:00
console.log(user2);
var post = user2.posts.build({title: 'Hello world'});
post.save(function (err, data) {
console.log(err ? err : data);
});
2013-05-17 21:41:04 +00:00
});
Post.findOne({where: {published: false}, order: 'date DESC'}, function (err, data) {
2014-01-24 17:09:53 +00:00
console.log(data);
2013-05-17 21:41:04 +00:00
});
2014-03-01 06:29:22 +00:00
User.create({name: 'Jeff', age: 12}, function (err, data) {
2014-01-24 17:09:53 +00:00
if (err) {
console.log(err);
return;
}
console.log(data);
var post = data.posts.build({title: 'My Post'});
console.log(post);
2013-05-17 21:41:04 +00:00
});
User.create({name: 'Ray'}, function (err, data) {
2014-01-24 17:09:53 +00:00
console.log(data);
2013-05-17 21:41:04 +00:00
});
2013-05-17 17:54:14 +00:00
2014-03-01 06:29:22 +00:00
User.scope('minors', {where: {age: {lte: 16}}, include: 'posts'});
2014-01-24 17:09:53 +00:00
User.minors(function (err, kids) {
console.log('Kids: ', kids);
});
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');
2014-01-24 17:09:53 +00:00
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 23:21:12 +00:00
});
2014-01-24 17:09:53 +00:00
});
2013-05-17 23:21:12 +00:00
});
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