-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhas-many-association.js
90 lines (82 loc) · 3.06 KB
/
has-many-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
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
/*eslint no-var: "off"*/
// noinspection ES6ConvertVarToLetConst
var {LangUtils} = require('@themost/common');
var _ = require('lodash');
var {QueryUtils} = require('@themost/query');
var types = require('./types');
var {DataQueryable} = require('./data-queryable');
/**
* @class
* @constructor
* @augments DataQueryable
* @param {DataObject} obj - A DataObject instance that represents the parent data object
* @param {string|*} association - A string that represents the name of the field which holds association mapping or the association mapping itself.
* @property {DataObject} parent - Gets or sets the parent data object
* @property {DataAssociationMapping} mapping - Gets or sets the mapping definition of this data object association.
*/
function HasManyAssociation(obj, association)
{
/**
* @type {DataObject}
* @private
*/
var _parent = obj;
/**
* Gets or sets the parent data object
* @type DataObject
*/
Object.defineProperty(this, 'parent', { get: function () {
return _parent;
}, set: function (value) {
_parent = value;
}, configurable: false, enumerable: false});
var self = this;
if (typeof association === 'string') {
//infer mapping from field name
//set relation mapping
if (self.parent!=null) {
var model = self.parent.getModel();
if (model!=null)
self.mapping = model.inferMapping(association);
}
}
else if (typeof association === 'object' && association !=null) {
//get the specified mapping
if (association instanceof types.DataAssociationMapping)
self.mapping = association;
else
self.mapping = _.assign(new types.DataAssociationMapping(), association);
}
var q = null;
//override query property
Object.defineProperty(this, 'query', {
get:function() {
//if query is already defined
if (q!=null)
//return this query
return q;
if (typeof self.mapping === 'undefined' || self.mapping==null)
throw new Error('Data association mapping cannot be empty at this context.');
//prepare query by selecting the foreign key of the related object
q = QueryUtils.query(self.model.viewAdapter).where(self.mapping.childField).equal(self.parent[self.mapping.parentField]).prepare();
return q;
}, configurable:false, enumerable:false
});
var m = null;
//override model property
Object.defineProperty(this, 'model', {
get:function() {
//if query is already defined
if (m!=null)
//return this query
return m;
m = self.parent.context.model(self.mapping.childModel);
return m;
}, configurable:false, enumerable:false
});
}
LangUtils.inherits(HasManyAssociation, DataQueryable);
module.exports = {
HasManyAssociation
};