Fix the include with array value
This commit is contained in:
parent
bc2ce644be
commit
7e45a1fda1
|
@ -0,0 +1,142 @@
|
|||
var jdb = require('../index');
|
||||
|
||||
var User, Post, Passport, City, Street, Building;
|
||||
var nbSchemaRequests = 0;
|
||||
|
||||
setup(function () {
|
||||
|
||||
Passport.find({include: 'owner'}, function (err, passports) {
|
||||
console.log('passports.owner', passports);
|
||||
});
|
||||
|
||||
User.find({include: 'posts'}, function (err, users) {
|
||||
console.log('users.posts', users);
|
||||
});
|
||||
|
||||
Passport.find({include: {owner: 'posts'}}, function (err, passports) {
|
||||
console.log('passports.owner.posts', passports);
|
||||
});
|
||||
|
||||
Passport.find({
|
||||
include: {owner: {posts: 'author'}}
|
||||
}, function (err, passports) {
|
||||
console.log('passports.owner.posts.author', passports);
|
||||
});
|
||||
|
||||
User.find({include: ['posts', 'passports']}, function (err, users) {
|
||||
// console.log('users.passports && users.posts', users);
|
||||
users.forEach(function (user) {
|
||||
console.log('user', user);
|
||||
user.posts(function (err, posts) {
|
||||
if (posts && posts.length > 0) {
|
||||
console.log('posts', posts);
|
||||
}
|
||||
});
|
||||
user.passports(function (err, passports) {
|
||||
if (passports && passports.length > 0) {
|
||||
console.log('posts', passports);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function setup(done) {
|
||||
var db = new jdb.DataSource({connector: 'memory'});
|
||||
City = db.define('City');
|
||||
Street = db.define('Street');
|
||||
Building = db.define('Building');
|
||||
User = db.define('User', {
|
||||
name: String,
|
||||
age: Number
|
||||
});
|
||||
Passport = db.define('Passport', {
|
||||
number: String
|
||||
});
|
||||
Post = db.define('Post', {
|
||||
title: String
|
||||
});
|
||||
|
||||
Passport.belongsTo('owner', {model: User});
|
||||
User.hasMany('passports', {foreignKey: 'ownerId'});
|
||||
User.hasMany('posts', {foreignKey: 'userId'});
|
||||
Post.belongsTo('author', {model: User, foreignKey: 'userId'});
|
||||
|
||||
db.automigrate(function () {
|
||||
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}
|
||||
],
|
||||
function (items) {
|
||||
createdUsers = items;
|
||||
createPassports();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function createPassports() {
|
||||
clearAndCreate(
|
||||
Passport,
|
||||
[
|
||||
{number: '1', ownerId: createdUsers[0].id},
|
||||
{number: '2', ownerId: createdUsers[1].id},
|
||||
{number: '3'}
|
||||
],
|
||||
function (items) {
|
||||
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'}
|
||||
],
|
||||
function (items) {
|
||||
createdPosts = items;
|
||||
done();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function clearAndCreate(model, data, callback) {
|
||||
var createdItems = [];
|
||||
model.destroyAll(function () {
|
||||
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++;
|
||||
}
|
||||
}
|
4
index.js
4
index.js
|
@ -1,5 +1,3 @@
|
|||
var fs = require('fs');
|
||||
|
||||
exports.ModelBuilder = exports.LDL = require('./lib/model-builder.js').ModelBuilder;
|
||||
exports.DataSource = exports.Schema = require('./lib/datasource.js').DataSource;
|
||||
exports.ModelBaseClass = require('./lib/model.js');
|
||||
|
@ -14,7 +12,7 @@ exports.__defineGetter__('BaseSQL', function () {
|
|||
|
||||
|
||||
exports.__defineGetter__('version', function () {
|
||||
return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version;
|
||||
return require('./package.json').version;
|
||||
});
|
||||
|
||||
var commonTest = './test/common_test';
|
||||
|
|
|
@ -557,7 +557,7 @@ DataAccessObject.find = function find(params, cb) {
|
|||
var includes = params.include || [];
|
||||
if (typeof includes === 'string') {
|
||||
includes = [includes];
|
||||
} else if (typeof includes === 'object') {
|
||||
} else if (!Array.isArray(includes) && typeof includes === 'object') {
|
||||
includes = Object.keys(includes);
|
||||
}
|
||||
includes.forEach(function (inc) {
|
||||
|
|
|
@ -117,8 +117,15 @@ describe('include', function () {
|
|||
// The relation should be promoted as the 'owner' property
|
||||
user.should.have.property('posts');
|
||||
user.should.have.property('passports');
|
||||
|
||||
var userObj = user.toJSON();
|
||||
userObj.should.have.property('posts');
|
||||
userObj.should.have.property('passports');
|
||||
userObj.posts.should.be.an.instanceOf(Array);
|
||||
userObj.passports.should.be.an.instanceOf(Array);
|
||||
|
||||
// The __cachedRelations should be removed from json output
|
||||
user.toJSON().should.not.have.property('__cachedRelations');
|
||||
userObj.should.not.have.property('__cachedRelations');
|
||||
|
||||
user.__cachedRelations.should.have.property('posts');
|
||||
user.__cachedRelations.should.have.property('passports');
|
||||
|
|
Loading…
Reference in New Issue