This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
/
association.js
506 lines (471 loc) · 20.6 KB
/
association.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
var associations = require("../associations"),
oneToMany = associations.oneToMany,
manyToOne = associations.manyToOne,
oneToOne = associations.oneToOne,
manyToMany = associations.manyToMany,
fetch = associations.fetch,
comb = require("comb"),
asyncArray = comb.async.array,
Promise = comb.Promise,
PromiseList = comb.PromiseList;
var RECIPROCAL_ASSOC = {
"oneToOne": ["manyToOne"],
"manyToOne": ["oneToMany", "oneToOne"],
"oneToMany": ["manyToOne"],
"manyToMany": ["manyToMany"]
};
exports.AssociationPlugin = comb.define(null, {
instance: {
/**@lends patio.Model.prototype*/
/**
* @ignore
* <p>Plugin to expose association capability.</p>
*
* The associations exposed include
*
* <ul>
* <li><b>oneToMany</b> - Foreign key in associated model's table points to this
* model's primary key. Each current model object can be associated with
* more than one associated model objects. Each associated model object
* can be associated with only one current model object.</li>
* <li><b>manyToOne</b> - Foreign key in current model's table points to
* associated model's primary key. Each associated model object can
* be associated with more than one current model objects. Each current
* model object can be associated with only one associated model object.</li>
* <li><b>oneToOne</b> - Similar to one_to_many in terms of foreign keys, but
* only one object is associated to the current object through the
* association. The methods created are similar to many_to_one, except
* that the one_to_one setter method saves the passed object./li>
* <li><b>manyToMany</b> - A join table is used that has a foreign key that points
* to this model's primary key and a foreign key that points to the
* associated model's primary key. Each current model object can be
* associated with many associated model objects, and each associated
* model object can be associated with many current model objects./li>
* </ul>
*
* @constructs
*
*/
constructor: function () {
if (comb.isUndefinedOrNull(this.__associations)) {
this.__associations = {};
}
this._super(arguments);
},
reload: function () {
this.__associations = {};
return this._super(arguments);
},
/**@ignore*/
getters: {
/**@lends patio.Model.prototype*/
/**
* List of associations on the {@link patio.Model}
* @field
* @ignoreCode
*/
associations: function () {
return this._static.associations;
},
/**
* Returns true if this {@link patio.Model} has associations.
* @field
* @ignoreCode
*/
hasAssociations: function () {
return this._static.hasAssociations;
}
}
},
static: {
/**@lends patio.Model*/
__associations: null,
/**
* Set to false to prevent an event from being emitted when an association is added to the model
* @default true
*/
emitOnAssociationAdd: true,
/**
* @borrows _Association.fetch as fetch
*/
fetchType: fetch,
/**
* String for to signify an association as one to one.
* @const
*/
ONE_TO_ONE: "oneToOne",
/**
* String for to signify an association as one to many.
* @const
*/
ONE_TO_MANY: "oneToMany",
/**
* String for to signify an association as many to one.
* @const
*/
MANY_TO_ONE: "manyToOne",
/**
* String for to signify an association as many to many.
* @const
*/
MANY_TO_MANY: "manyToMany",
/**
*Creates a ONE_TO_MANY association.
* See {@link patio.plugins.AssociationPlugin.associate} for options.
*
* @example
*
*
* //define the BiologicalFather model
* patio.addModel("biologicalFather", {
* static:{
* init:function () {
* this._super("arguments");
* this.oneToMany("children");
* }
* }
* });
*
*
* //define Child model
* patio.addModel("child", {
* static:{
* init:function () {
* this._super("arguments");
* this.manyToOne("biologicalFather");
* }
* }
* });
*
*/
oneToMany: function (name, options, filter) {
return this.associate(this.ONE_TO_MANY, name, options, filter);
},
/* Allows eager loading of an association. This does an extra SQL query for the association.
* It will load any association singular or plural.
*
* @example
*
* Person.eager('company').one()
* { id: 1,
* name: 'Obi-Wan',
* company: {
* id: 1,
* name: 'Jedi council'
* }
* }
*
* Person.eager(['emails', 'phones', 'company']).limit(2).all()
* [{ id: 1,
* name: 'Obi-Wan',
* emails: ['obi@gmail.com', 'obi@jedi.com'],
* phones: ['911', '888-991-0991'],
* company: {
* id: 1,
* name: 'Jedi council'
* }
* },
* { id: 2,
* name: 'Luke',
* emails: ['luke@gmail.com', 'luke@jedi.com'],
* phones: ['911', '888-991-0992'],
* company: {
* id: 1,
* name: 'Jedi council'
* }
* }]
*
*/
eager: function(associations) {
var model = new this(),
includes = [],
associationsObj = {};
if (Array.isArray(associations)) {
includes = includes.concat(associations);
} else if(associations) {
includes.push(associations);
}
includes.forEach(function(association) {
associationsObj[association] = function(parent) {
if (!parent[association]) {
throw new Error("Association of " + association + " not found");
}
return parent[association];
};
});
return model.dataset.eager(associationsObj);
},
/**
* Creates a MANY_TO_ONE association.
* See {@link patio.plugins.AssociationPlugin.oneToMany}.
* See {@link patio.plugins.AssociationPlugin.associate}
*/
manyToOne: function (name, options, filter) {
return this.associate(this.MANY_TO_ONE, name, options, filter);
},
/**
* Creates a ONE_TO_ONE relationship between models.
* See {@link patio.plugins.AssociationPlugin.associate} for options.
*
* @example
*
* patio.addModel("state", {
* static:{
* init:function () {
* this._super("arguments");
* this.oneToOne("capital");
* }
* }
* });
*
* patio.addModel("capital", {
* static:{
* init:function () {
* this._super("arguments");
* this.manyToOne("state");
* }
* }
* });
*/
oneToOne: function (name, options, filter) {
return this.associate(this.ONE_TO_ONE, name, options, filter);
},
/**
* Creates a MANY_TO_MANY relationship between models.
* See {@link patio.plugins.AssociationPlugin.associate} for options.
*
* @example
*
* patio.addModel("class", {
* static:{
* init:function(){
* this._super("arguments");
* this.manyToMany("students", {fetchType:this.fetchType.EAGER, order : [sql.firstName.desc(), sql.lastName.desc()]});
* }
* }
* });
* patio.addModel("student", {
* instance:{
* enroll:function(clas){
* if (comb.isArray(clas)) {
* return this.addClasses(clas);
* } else {
* return this.addClass(clas);
* }
* }
* },
* static:{
* init:function(){
* this._super("arguments");
* this.manyToMany("classes", {fetchType:this.fetchType.EAGER, order : sql.name.desc()});
* }
* }
});
*
*/
manyToMany: function (name, options, filter) {
return this.associate(this.MANY_TO_MANY, name, options, filter);
},
/**
* Associates a related model with the current model. The following types are
* supported:
*
* <ul>
* <li><b>oneToMany</b> - Foreign key in associated model's table points to this
* model's primary key. Each current model object can be associated with
* more than one associated model objects. Each associated model object
* can be associated with only one current model object.</li>
* <li><b>manyToOne</b> - Foreign key in current model's table points to
* associated model's primary key. Each associated model object can
* be associated with more than one current model objects. Each current
* model object can be associated with only one associated model object.</li>
* <li><b>oneToOne</b> - Similar to one_to_many in terms of foreign keys, but
* only one object is associated to the current object through the
* association. The methods created are similar to many_to_one, except
* that the one_to_one setter method saves the passed object./li>
* <li><b>manyToMany</b> - A join table is used that has a foreign key that points
* to this model's primary key and a foreign key that points to the
* associated model's primary key. Each current model object can be
* associated with many associated model objects, and each associated
* model object can be associated with many current model objects.</li>
* </ul>
*
* @param {patio.Model.ONE_TO_ONE|patio.Model.ONE_TO_MANY|patio.Model.MANY_TO_ONE|patio.Model.MANY_TO_MANY} type the
* type of association that is to be created.
* @param {String} name the name of the association, the name specified will be exposed as a property on instances
* of the model.
@param {Object} [options] additional options.
* The following options can be supplied:
* <ul>
* <li><b>model</b> - The associated class or its name. If not given, uses the association's name,
* which is singularized unless the type is MANY_TO_ONE or ONE_TO_ONE</li>
* <li><b>query</b> - The conditions to use to filter the association, can be any argument passed
* to {@link patio.Dataset#filter}.</li>
* <li><b>dataset</b> - A function that is called in the scope of the model and called with the model as the
* first argument. The function must return a dataset that can be used as the base for all dataset
* operations.<b>NOTE:</b>The dataset returned will have all options applied to it.</li>
* <li><b>distinct</b> Use the DISTINCT clause when selecting associated objects.
* See {@link patio.Dataset#distinct}.</li>
* <li><b>limit</b> : Limit the number of records to the provided value. Use
* an array with two elements for the value to specify a limit (first element) and an
* offset (second element). See {@link patio.Dataset#limit}.</li>
* <li><b>order</b> : the column/s order the association dataset by. Can be a
* one or more columns.
* See {@link patio.Dataset#order}.</li>
* <li><b>readOnly</b> : Do not add a setter method (for MANY_TO_ONE or ONE_TO_ONE associations),
* or add/remove/removeAll methods (for ONE_TO_MANY and MANY_TO_MANY associations).</li>
* <li><b>select</b> : the columns to select. Defaults to the associated class's
* tableName.* in a MANY_TO_MANY association, which means it doesn't include the attributes from the
* join table. If you want to include the join table attributes, you can
* use this option, but beware that the join table attributes can clash with
* attributes from the model table, so you should alias any attributes that have
* the same name in both the join table and the associated table.</li>
* </ul>
* ManyToOne additional options:
* <ul>
* <li><b>key</b> : foreignKey in current model's table that references
* associated model's primary key. Defaults to : "{tableName}Id". Can use an
* array of strings for a composite key association.</li>
* <li><b>primaryKey</b> : column in the associated table that the <b>key</b> option references.
* Defaults to the primary key of the associated table. Can use an
* array of strings for a composite key association.</li>
* </ul>
* OneToMany and OneToOne additional options:
* <ul>
* <li><b>key</b> : foreign key in associated model's table that references
* current model's primary key, as a string. Defaults to
* "{thisTableName}Id". Can use an array of columns for a composite key association.</li>
* <li><b>primaryKey</b> : column in the current table that <b>key</b> option references.
* Defaults to primary key of the current table. Can use an array of strings for a
* composite key association.</li>
* </ul>
*
* ManyToMany additional options:
* <ul>
* <li><b>joinTable</b> : name of table that includes the foreign keys to both
* the current model and the associated model. Defaults to the name
* of current model and name of associated model, pluralized,
* sorted, and joined with '' and camelized.
* <li><b>leftKey</b> : foreign key in join table that points to current model's
* primary key. Defaults to :"{tableName}Id".
* Can use an array of strings for a composite key association.
* <li><b>leftPrimaryKey</b> - column in current table that <b>leftKey</b> points to.
* Defaults to primary key of current table. Can use an array of strings for a
* composite key association.
* <li><b>rightKey</b> : foreign key in join table that points to associated
* model's primary key. Defaults to Defaults to :"{associatedTableName}Id".
* Can use an array of strings for a composite key association.
* <li><b>rightPrimaryKey</b> : column in associated table that <b>rightKey</b> points to.
* Defaults to primary key of the associated table. Can use an
* array of strings for a composite key association.
* </ul>
* @param {Function} [filter] optional function to filter the dataset after all other options have been applied.
*
*/
associate: function (type, name, options, filter) {
if (comb.isFunction(options)) {
filter = options;
options = {};
}
this.__associations = this.__associations || {manyToMany: {}, oneToMany: {}, manyToOne: {}, oneToOne: {}};
var assoc = new associations[type](comb.merge({model: name}, options), this.patio, filter);
assoc.inject(this, name);
this.__associations[type][name] = assoc;
this.emit("associate", type, this);
return this;
},
sync: function (cb) {
if (!this.synced && this.hasAssociations) {
var self = this;
return this._super().chain(function () {
var associations = self.__associations;
return asyncArray(Object.keys(associations)).map(function (type) {
var types = associations[type];
return asyncArray(Object.keys(types)).map(function (name) {
return types[name].model.sync();
}, 1);
}, 1);
});
} else {
return this._super(arguments);
}
},
__isReciprocalAssociation: function (assoc, pAssoc) {
var keys = assoc._getAssociationKey(), leftKey = keys[0], rightKey = keys[1];
var pKeys = pAssoc._getAssociationKey(), pLeftKey = pKeys[0], pRightKey = pKeys[1];
return leftKey.every(function (k, i) {
return pRightKey[i] === k;
}) && rightKey.every(function (k, i) {
return pLeftKey[i] === k;
}) && assoc.parent === pAssoc.model;
},
_findAssociation: function (assoc) {
var ret = null;
if (!comb.isEmpty(this.__associations)) {
var type = assoc.type, recipTypes = RECIPROCAL_ASSOC[type];
for (var i in recipTypes) {
var recipType = recipTypes[i];
var potentialAssociations = this.__associations[recipType];
var found = false;
for (var j in potentialAssociations) {
var pAssoc = potentialAssociations[j];
if (this.__isReciprocalAssociation(assoc, pAssoc)) {
ret = [i, pAssoc], found = true;
break;
}
}
if (found) {
break;
}
}
}
return ret;
},
_clearAssociationsForType: function (type, clazz, model) {
this._findAssociationsForType(type, clazz).forEach(function (assoc) {
assoc._clearAssociations(model);
});
},
_reloadAssociationsForType: function (type, clazz, model) {
var pl = this._findAssociationsForType(type, clazz).map(function (assoc) {
return assoc._forceReloadAssociations(model);
});
return (pl.length ? new PromiseList(pl) : new Promise().callback()).promise();
},
_findAssociationsForType: function (type, clazz) {
var associations = this.__associations[type], ret = [];
for (var i in associations) {
var assoc = associations[i];
if (assoc.model === clazz) {
ret.push(assoc);
}
}
return ret;
},
/**@ignore*/
getters: {
/**@lends patio.plugins.AssociationPlugin*/
/**
* A list of associated model names.
* @field
* @type String[]
*/
associations: function () {
var ret = [], assoc = this.__associations;
if (assoc != null) {
Object.keys(assoc).forEach(function (k) {
ret = ret.concat(Object.keys(assoc[k]));
});
}
return ret;
},
/**
* Returns true if this model has associations.
* @field
* @type Boolean
*/
hasAssociations: function () {
return this.associations.length > 0;
}
}
}});