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

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 16:13:48 +00:00
const DataSource = require('../../loopback-datasource-juggler').DataSource;
const ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
const ds = new DataSource('memory');
2013-05-17 21:41:04 +00:00
2013-05-17 17:54:14 +00:00
// define models
2018-12-07 16:13:48 +00:00
const Post = ds.define('Post', {
2016-08-19 17:46:59 +00:00
title: {type: String, length: 255},
content: {type: DataSource.Text},
date: {type: Date, default: function() {
2014-01-24 17:09:53 +00:00
return new Date;
2016-08-19 17:46:59 +00:00
}},
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
2018-12-07 16:13:48 +00:00
const User = ds.define('User', {
2014-01-24 17:09:53 +00:00
name: String,
bio: DataSource.Text,
approved: Boolean,
joinedAt: Date,
2016-04-01 11:48:17 +00:00
age: Number,
2013-05-17 17:54:14 +00:00
});
2018-12-07 16:13:48 +00:00
const Group = ds.define('Group', {name: String});
2013-05-17 17:54:14 +00:00
// define any custom method
2016-04-01 11:48:17 +00:00
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
};
2018-12-07 16:13:48 +00:00
const user = new User({name: 'Joe'});
2013-05-17 17:54:14 +00:00
console.log(user);
2013-05-17 21:41:04 +00:00
// console.log(ds.models);
// console.log(ds.definitions);
// setup relationships
2016-08-19 17:46:59 +00:00
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
2013-05-17 21:41:04 +00:00
// creates instance methods:
// user.posts(conds)
// user.posts.build(data) // like new Post({userId: user.id});
// user.posts.create(data) // build and save
2016-08-19 17:46:59 +00:00
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
2013-05-17 21:41:04 +00:00
// 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');
2018-12-07 16:13:48 +00:00
const user2 = new User({name: 'Smith', age: 14});
2016-04-01 11:48:17 +00:00
user2.save(function(err) {
2014-01-24 17:09:53 +00:00
console.log(user2);
2018-12-07 16:13:48 +00:00
const post = user2.posts.build({title: 'Hello world'});
2016-04-01 11:48:17 +00:00
post.save(function(err, data) {
2014-01-24 17:09:53 +00:00
console.log(err ? err : data);
});
2013-05-17 21:41:04 +00:00
});
2016-08-19 17:46:59 +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
});
2016-08-19 17:46:59 +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);
2018-12-07 16:13:48 +00:00
const post = data.posts.build({title: 'My Post'});
2014-01-24 17:09:53 +00:00
console.log(post);
2013-05-17 21:41:04 +00:00
});
2016-08-19 17:46:59 +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
2016-08-19 17:46:59 +00:00
User.scope('minors', {where: {age: {lte: 16}}, include: 'posts'});
2016-04-01 11:48:17 +00:00
User.minors(function(err, kids) {
2014-01-24 17:09:53 +00:00
console.log('Kids: ', kids);
});
2018-12-07 16:13:48 +00:00
const Article = ds.define('Article', {title: String});
const Tag = ds.define('Tag', {name: String});
2013-05-17 23:21:12 +00:00
Article.hasAndBelongsToMany('tags');
2016-04-01 11:48:17 +00:00
Article.create(function(e, article) {
2016-08-19 17:46:59 +00:00
article.tags.create({name: 'popular'}, function(err, data) {
2016-04-01 11:48:17 +00:00
Article.findOne(function(e, article) {
article.tags(function(e, tags) {
2014-01-24 17:09:53 +00:00
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
2018-12-07 16:13:48 +00:00
const modelBuilder = new ModelBuilder();
2013-05-17 17:54:14 +00:00
const Color = modelBuilder.define('Color', {
2016-04-01 11:48:17 +00:00
name: String,
2013-05-24 00:29:03 +00:00
});
// attach
ds.attach(Color);
2016-08-19 17:46:59 +00:00
Color.create({name: 'red'});
Color.create({name: 'green'});
Color.create({name: 'blue'});
2013-05-24 00:29:03 +00:00
2016-04-01 11:48:17 +00:00
Color.all(function(err, colors) {
2013-05-24 00:29:03 +00:00
console.log(colors);
});
2013-05-17 17:54:14 +00:00