Blame view
node_modules/loopback-connector-remote/lib/relations.js
7.77 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
// Copyright IBM Corp. 2014,2016. All Rights Reserved. // Node module: loopback-connector-remote // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT /*! * Dependencies */ var relation = require('loopback-datasource-juggler/lib/relation-definition'); var RelationDefinition = relation.RelationDefinition; module.exports = RelationMixin; /** * RelationMixin class. Use to define relationships between models. * * @class RelationMixin */ function RelationMixin() { } /** * Define a "one to many" relationship by specifying the model name * * Examples: * ``` * User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'}); * ``` * * ``` * Book.hasMany(Chapter); * ``` * Or, equivalently: * ``` * Book.hasMany('chapters', {model: Chapter}); * ``` * * Query and create related models: * * ```js * Book.create(function(err, book) { * * // Create a chapter instance ready to be saved in the data source. * var chapter = book.chapters.build({name: 'Chapter 1'}); * * // Save the new chapter * chapter.save(); * * // you can also call the Chapter.create method with the `chapters` property * // which will build a chapter instance and save the it in the data source. * book.chapters.create({name: 'Chapter 2'}, function(err, savedChapter) { * // this callback is optional * }); * * // Query chapters for the book * book.chapters(function(err, chapters) { * // all chapters with bookId = book.id * console.log(chapters); * }); * * book.chapters({where: {name: 'test'}, function(err, chapters) { * // All chapters with bookId = book.id and name = 'test' * console.log(chapters); * }); * }); *``` * @param {Object|String} modelTo Model object (or String name of model) to * which you are creating the relationship. * @options {Object} parameters Configuration parameters; see below. * @property {String} as Name of the property in the referring model that * corresponds to the foreign key field in the related model. * @property {String} foreignKey Property name of foreign key field. * @property {Object} model Model object */ RelationMixin.hasMany = function hasMany(modelTo, params) { var def = RelationDefinition.hasMany(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; /** * Declare "belongsTo" relation that sets up a one-to-one connection with * another model, such that each instance of the declaring model "belongs * to" one instance of the other model. * * For example, if an application includes users and posts, and each post can be * written by exactly one user. The following code specifies that `Post` has a * reference called `author` to the `User` model via the `userId` property of * `Post` as the foreign key. * ``` * Post.belongsTo(User, {as: 'author', foreignKey: 'userId'}); * ``` * You can then access the author in one of the following styles. * Get the User object for the post author asynchronously: * ``` * post.author(callback); * ``` * Get the User object for the post author synchronously: * ``` * post.author(); * Set the author to be the given user: * ``` * post.author(user) * ``` * Examples: * * Suppose the model Post has a *belongsTo* relationship with User (the author * of the post). You could declare it this way: * ```js * Post.belongsTo(User, {as: 'author', foreignKey: 'userId'}); * ``` * * When a post is loaded, you can load the related author with: * ```js * post.author(function(err, user) { * // the user variable is your user object * }); * ``` * * The related object is cached, so if later you try to get again the author, no * additional request will be made. But there is an optional boolean parameter * in first position that set whether or not you want to reload the cache: * ```js * post.author(true, function(err, user) { * // The user is reloaded, even if it was already cached. * }); * ``` * This optional parameter default value is false, so the related object will * be loaded from cache if available. * * @param {Class|String} modelTo Model object (or String name of model) * to which you are creating the relationship. * @options {Object} params Configuration parameters; see below. * @property {String} as Name of the property in the referring model that * corresponds to the foreign key field in the related model. * @property {String} foreignKey Name of foreign key property. * */ RelationMixin.belongsTo = function(modelTo, params) { var def = RelationDefinition.belongsTo(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; /** * A hasAndBelongsToMany relation creates a direct many-to-many connection with * another model, with no intervening model. For example, if your application * includes users and groups, with each group having many users and each user * appearing in many groups, you could declare the models this way: * ``` * User.hasAndBelongsToMany('groups', {model: Group, foreignKey: 'groupId'}); * ``` * Then, to get the groups to which the user belongs: * ``` * user.groups(callback); * ``` * Create a new group and connect it with the user: * ``` * user.groups.create(data, callback); * ``` * Connect an existing group with the user: * ``` * user.groups.add(group, callback); * ``` * Remove the user from the group: * ``` * user.groups.remove(group, callback); * ``` * * @param {String|Object} modelTo Model object (or String name of model) to * which you are creating the relationship. * @options {Object} params Configuration parameters; see below. * @property {String} as Name of the property in the referring model that * corresponds to the foreign key field in the related model. * @property {String} foreignKey Property name of foreign key field. * @property {Object} model Model object */ RelationMixin.hasAndBelongsToMany = function hasAndBelongsToMany(modelTo, params) { var def = RelationDefinition.hasAndBelongsToMany(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; RelationMixin.hasOne = function hasOne(modelTo, params) { var def = RelationDefinition.hasOne(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; RelationMixin.referencesMany = function referencesMany(modelTo, params) { var def = RelationDefinition.referencesMany(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; RelationMixin.embedsOne = function embedsOne(modelTo, params) { var def = RelationDefinition.embedsOne(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; RelationMixin.embedsMany = function embedsMany(modelTo, params) { var def = RelationDefinition.embedsMany(this, modelTo, params); this.dataSource.adapter.resolve(this); defineRelationProperty(this, def); }; function defineRelationProperty(modelClass, def) { Object.defineProperty(modelClass.prototype, def.name, { get: function() { var that = this; var scope = function() { return that['__get__' + def.name].apply(that, arguments); }; scope.count = function() { return that['__count__' + def.name].apply(that, arguments); }; scope.create = function() { return that['__create__' + def.name].apply(that, arguments); }; scope.deleteById = scope.destroyById = function() { return that['__destroyById__' + def.name].apply(that, arguments); }; scope.exists = function() { return that['__exists__' + def.name].apply(that, arguments); }; scope.findById = function() { return that['__findById__' + def.name].apply(that, arguments); }; return scope; } }); } |