-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathExtend.js
More file actions
240 lines (205 loc) · 6.48 KB
/
Copy pathExtend.js
File metadata and controls
240 lines (205 loc) · 6.48 KB
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
var _ = require('lodash');
var ORMError = require("../Error");
var Settings = require("../Settings");
var Singleton = require("../Singleton");
var util = require("../Utilities");
exports.prepare = function (db, Model, associations) {
Model.extendsTo = function (name, properties, opts) {
opts = opts || {};
var assocName = opts.name || ucfirst(name);
var association = {
name : name,
table : opts.table || (Model.table + '_' + name),
reversed : opts.reversed,
autoFetch : opts.autoFetch || false,
autoFetchLimit : opts.autoFetchLimit || 2,
field : util.wrapFieldObject({
field: opts.field, model: Model, altName: Model.table
}) || util.formatField(Model, Model.table, false, false),
getAccessor : opts.getAccessor || ("get" + assocName),
setAccessor : opts.setAccessor || ("set" + assocName),
hasAccessor : opts.hasAccessor || ("has" + assocName),
delAccessor : opts.delAccessor || ("remove" + assocName)
};
var newproperties = _.cloneDeep(properties);
for (var k in association.field) {
newproperties[k] = association.field[k];
}
var modelOpts = _.extend(
_.pick(opts, 'cache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'),
{
id : Object.keys(association.field),
extension : true,
}
);
association.model = db.define(association.table, newproperties, modelOpts);
association.model.hasOne(Model.table, Model, { extension: true, field: association.field });
associations.push(association);
Model["findBy" + assocName] = function () {
var cb = null, conditions = null, options = {};
for (var i = 0; i < arguments.length; i++) {
switch (typeof arguments[i]) {
case "function":
cb = arguments[i];
break;
case "object":
if (conditions === null) {
conditions = arguments[i];
} else {
options = arguments[i];
}
break;
}
}
if (conditions === null) {
throw new ORMError(".findBy(" + assocName + ") is missing a conditions object", 'PARAM_MISMATCH');
}
options.__merge = {
from : { table: association.model.table, field: Object.keys(association.field) },
to : { table: Model.table, field: Model.id },
where : [ association.model.table, conditions ],
table : Model.table
};
options.extra = [];
if (typeof cb == "function") {
return Model.find({}, options, cb);
}
return Model.find({}, options);
};
return association.model;
};
};
exports.extend = function (Model, Instance, Driver, associations, opts) {
for (var i = 0; i < associations.length; i++) {
extendInstance(Model, Instance, Driver, associations[i], opts);
}
};
exports.autoFetch = function (Instance, associations, opts, cb) {
if (associations.length === 0) {
return cb();
}
var pending = associations.length;
var autoFetchDone = function autoFetchDone() {
pending -= 1;
if (pending === 0) {
return cb();
}
};
for (var i = 0; i < associations.length; i++) {
autoFetchInstance(Instance, associations[i], opts, autoFetchDone);
}
};
function extendInstance(Model, Instance, Driver, association, opts) {
Object.defineProperty(Instance, association.hasAccessor, {
value : function (cb) {
if (!Instance[Model.id]) {
cb(new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table }));
} else {
association.model.get(util.values(Instance, Model.id), function (err, extension) {
return cb(err, !err && extension ? true : false);
});
}
return this;
},
enumerable : false
});
Object.defineProperty(Instance, association.getAccessor, {
value: function (opts, cb) {
if (typeof opts == "function") {
cb = opts;
opts = {};
}
if (!Instance[Model.id]) {
cb(new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table }));
} else {
association.model.get(util.values(Instance, Model.id), opts, cb);
}
return this;
},
enumerable : false
});
Object.defineProperty(Instance, association.setAccessor, {
value : function (Extension, cb) {
Instance.save(function (err) {
if (err) {
return cb(err);
}
Instance[association.delAccessor](function (err) {
if (err) {
return cb(err);
}
var fields = Object.keys(association.field);
if (!Extension.isInstance) {
Extension = new association.model(Extension);
}
for (var i = 0; i < Model.id.length; i++) {
Extension[fields[i]] = Instance[Model.id[i]];
}
Extension.save(cb);
});
});
return this;
},
enumerable : false
});
Object.defineProperty(Instance, association.delAccessor, {
value : function (cb) {
if (!Instance[Model.id]) {
cb(new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table }));
} else {
var conditions = {};
var fields = Object.keys(association.field);
for (var i = 0; i < Model.id.length; i++) {
conditions[fields[i]] = Instance[Model.id[i]];
}
association.model.find(conditions, function (err, extensions) {
if (err) {
return cb(err);
}
var pending = extensions.length;
for (var i = 0; i < extensions.length; i++) {
Singleton.clear(extensions[i].__singleton_uid());
extensions[i].remove(function () {
if (--pending === 0) {
return cb();
}
});
}
if (pending === 0) {
return cb();
}
});
}
return this;
},
enumerable : false
});
}
function autoFetchInstance(Instance, association, opts, cb) {
if (!Instance.saved()) {
return cb();
}
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
opts.autoFetchLimit = association.autoFetchLimit;
}
// autoFetchLimit has been reached -
// opts.autoFetch is present, so it overrides association.autoFetch
// opts.autoFetch not present, use value from original assn definition
if (opts.autoFetchLimit === 0 || (typeof opts.autoFetch !== "undefined" && ! opts.autoFetch)
|| (typeof opts.autoFetch === "undefined" && !association.autoFetch)) {
return cb();
}
if (Instance.isPersisted()) {
Instance[association.getAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 }, function (err, Assoc) {
if (!err) {
Instance[association.name] = Assoc;
}
return cb();
});
} else {
return cb();
}
}
function ucfirst(text) {
return text[0].toUpperCase() + text.substr(1);
}