loopback-datasource-juggler/examples/inclusion.js

132 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-04-06 14:51:49 +00:00
// Copyright IBM Corp. 2014,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
2014-02-14 01:43:28 +00:00
var jdb = require('../index');
var User, Post, Passport, City, Street, Building;
var nbSchemaRequests = 0;
setup(function() {
Passport.find({ include: 'owner' }, function(err, passports) {
2014-02-14 01:43:28 +00:00
console.log('passports.owner', passports);
});
User.find({ include: 'posts' }, function(err, users) {
2014-02-14 01:43:28 +00:00
console.log('users.posts', users);
});
Passport.find({ include: { owner: 'posts' }}, function(err, passports) {
2014-02-14 01:43:28 +00:00
console.log('passports.owner.posts', passports);
});
Passport.find({
include: { owner: { posts: 'author' }},
}, function(err, passports) {
2014-02-14 01:43:28 +00:00
console.log('passports.owner.posts.author', passports);
});
User.find({ include: ['posts', 'passports'] }, function(err, users) {
2014-02-14 07:42:21 +00:00
console.log('users.passports && users.posts', users);
2014-02-14 01:43:28 +00:00
});
});
function setup(done) {
var db = new jdb.DataSource({ connector: 'memory' });
2014-02-14 01:43:28 +00:00
City = db.define('City');
Street = db.define('Street');
Building = db.define('Building');
User = db.define('User', {
name: String,
age: Number,
2014-02-14 01:43:28 +00:00
});
Passport = db.define('Passport', {
number: String,
2014-02-14 01:43:28 +00:00
});
Post = db.define('Post', {
title: String,
2014-02-14 01:43:28 +00:00
});
Passport.belongsTo('owner', { model: User });
User.hasMany('passports', { foreignKey: 'ownerId' });
User.hasMany('posts', { foreignKey: 'userId' });
Post.belongsTo('author', { model: User, foreignKey: 'userId' });
2014-02-14 01:43:28 +00:00
db.automigrate(function() {
2014-02-14 01:43:28 +00:00
var createdUsers = [];
var createdPassports = [];
var createdPosts = [];
createUsers();
function createUsers() {
clearAndCreate(
User,
[
{ name: 'User A', age: 21 },
{ name: 'User B', age: 22 },
{ name: 'User C', age: 23 },
{ name: 'User D', age: 24 },
{ name: 'User E', age: 25 },
2014-02-14 01:43:28 +00:00
],
function(items) {
2014-02-14 01:43:28 +00:00
createdUsers = items;
createPassports();
}
);
}
function createPassports() {
clearAndCreate(
Passport,
[
{ number: '1', ownerId: createdUsers[0].id },
{ number: '2', ownerId: createdUsers[1].id },
{ number: '3' },
2014-02-14 01:43:28 +00:00
],
function(items) {
2014-02-14 01:43:28 +00:00
createdPassports = items;
createPosts();
}
);
}
function createPosts() {
clearAndCreate(
Post,
[
{ title: 'Post A', userId: createdUsers[0].id },
{ title: 'Post B', userId: createdUsers[0].id },
{ title: 'Post C', userId: createdUsers[0].id },
{ title: 'Post D', userId: createdUsers[1].id },
{ title: 'Post E' },
2014-02-14 01:43:28 +00:00
],
function(items) {
2014-02-14 01:43:28 +00:00
createdPosts = items;
done();
}
);
}
});
}
function clearAndCreate(model, data, callback) {
var createdItems = [];
model.destroyAll(function() {
2014-02-14 01:43:28 +00:00
nextItem(null, null);
});
var itemIndex = 0;
function nextItem(err, lastItem) {
if (lastItem !== null) {
createdItems.push(lastItem);
}
if (itemIndex >= data.length) {
callback(createdItems);
return;
}
model.create(data[itemIndex], nextItem);
itemIndex++;
}
}