From a243d058809231106434c411a82321dd328ba5d3 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Mon, 11 Aug 2014 14:43:51 +0200 Subject: [PATCH] Enable DL definition of embedsMany + referencesMany --- lib/datasource.js | 3 ++- test/loopback-dl.test.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/datasource.js b/lib/datasource.js index b488ce72..9602ec4f 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -3,6 +3,7 @@ */ var ModelBuilder = require('./model-builder.js').ModelBuilder; var ModelDefinition = require('./model-definition.js'); +var RelationDefinition = require('./relation-definition.js'); var jutil = require('./jutil'); var utils = require('./utils'); var ModelBaseClass = require('./model.js'); @@ -364,7 +365,7 @@ function isModelClass(cls) { return cls.prototype instanceof ModelBaseClass; } -DataSource.relationTypes = ['belongsTo', 'hasMany', 'hasAndBelongsToMany', 'hasOne']; +DataSource.relationTypes = Object.keys(RelationDefinition.RelationTypes); function isModelDataSourceAttached(model) { return model && (!model.settings.unresolved) && (model.dataSource instanceof DataSource); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 18f0e0ad..f388e50e 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -903,6 +903,26 @@ describe('Load models with relations', function () { assert(Post.relations['user']); done(); }); + + it('should set up referencesMany relations', function (done) { + var ds = new DataSource('memory'); + + var Post = ds.define('Post', {userId: Number, content: String}); + var User = ds.define('User', {name: String}, {relations: {posts: {type: 'referencesMany', model: 'Post'}}}); + + assert(User.relations['posts']); + done(); + }); + + it('should set up embedsMany relations', function (done) { + var ds = new DataSource('memory'); + + var Post = ds.define('Post', {userId: Number, content: String}); + var User = ds.define('User', {name: String}, {relations: {posts: {type: 'embedsMany', model: 'Post'}}}); + + assert(User.relations['posts']); + done(); + }); it('should set up foreign key with the correct type', function (done) { var ds = new DataSource('memory');