2016-04-01 22:25:16 +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
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const jdb = require('../index');
|
2014-02-14 01:43:28 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let User, Post, Passport, City, Street, Building;
|
|
|
|
const nbSchemaRequests = 0;
|
2014-02-14 01:43:28 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
setup(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Passport.find({include: 'owner'}, function(err, passports) {
|
2014-02-14 01:43:28 +00:00
|
|
|
console.log('passports.owner', passports);
|
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({include: 'posts'}, function(err, users) {
|
2014-02-14 01:43:28 +00:00
|
|
|
console.log('users.posts', users);
|
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Passport.find({include: {owner: 'posts'}}, function(err, passports) {
|
2014-02-14 01:43:28 +00:00
|
|
|
console.log('passports.owner.posts', passports);
|
|
|
|
});
|
|
|
|
|
|
|
|
Passport.find({
|
2016-08-19 17:46:59 +00:00
|
|
|
include: {owner: {posts: 'author'}},
|
2016-04-01 11:48:17 +00:00
|
|
|
}, function(err, passports) {
|
2014-02-14 01:43:28 +00:00
|
|
|
console.log('passports.owner.posts.author', passports);
|
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
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) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const 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,
|
2016-04-01 11:48:17 +00:00
|
|
|
age: Number,
|
2014-02-14 01:43:28 +00:00
|
|
|
});
|
|
|
|
Passport = db.define('Passport', {
|
2016-04-01 11:48:17 +00:00
|
|
|
number: String,
|
2014-02-14 01:43:28 +00:00
|
|
|
});
|
|
|
|
Post = db.define('Post', {
|
2016-04-01 11:48:17 +00:00
|
|
|
title: String,
|
2014-02-14 01:43:28 +00:00
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +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
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
db.automigrate(function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let createdUsers = [];
|
|
|
|
let createdPassports = [];
|
|
|
|
let createdPosts = [];
|
2014-02-14 01:43:28 +00:00
|
|
|
createUsers();
|
|
|
|
function createUsers() {
|
|
|
|
clearAndCreate(
|
|
|
|
User,
|
|
|
|
[
|
2016-08-19 17:46:59 +00:00
|
|
|
{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
|
|
|
],
|
2016-04-01 11:48:17 +00:00
|
|
|
function(items) {
|
2014-02-14 01:43:28 +00:00
|
|
|
createdUsers = items;
|
|
|
|
createPassports();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPassports() {
|
|
|
|
clearAndCreate(
|
|
|
|
Passport,
|
|
|
|
[
|
2016-08-19 17:46:59 +00:00
|
|
|
{number: '1', ownerId: createdUsers[0].id},
|
|
|
|
{number: '2', ownerId: createdUsers[1].id},
|
|
|
|
{number: '3'},
|
2014-02-14 01:43:28 +00:00
|
|
|
],
|
2016-04-01 11:48:17 +00:00
|
|
|
function(items) {
|
2014-02-14 01:43:28 +00:00
|
|
|
createdPassports = items;
|
|
|
|
createPosts();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPosts() {
|
|
|
|
clearAndCreate(
|
|
|
|
Post,
|
|
|
|
[
|
2016-08-19 17:46:59 +00:00
|
|
|
{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
|
|
|
],
|
2016-04-01 11:48:17 +00:00
|
|
|
function(items) {
|
2014-02-14 01:43:28 +00:00
|
|
|
createdPosts = items;
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearAndCreate(model, data, callback) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const createdItems = [];
|
2016-04-01 11:48:17 +00:00
|
|
|
model.destroyAll(function() {
|
2014-02-14 01:43:28 +00:00
|
|
|
nextItem(null, null);
|
|
|
|
});
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let itemIndex = 0;
|
2014-02-14 01:43:28 +00:00
|
|
|
|
|
|
|
function nextItem(err, lastItem) {
|
|
|
|
if (lastItem !== null) {
|
|
|
|
createdItems.push(lastItem);
|
|
|
|
}
|
|
|
|
if (itemIndex >= data.length) {
|
|
|
|
callback(createdItems);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
model.create(data[itemIndex], nextItem);
|
|
|
|
itemIndex++;
|
|
|
|
}
|
|
|
|
}
|