MCPcopy Create free account
hub / github.com/dresende/node-orm2 / extendInstance

Function extendInstance

lib/Associations/Many.js:133–489  ·  view source on GitHub ↗
(Model, Instance, Driver, association, opts, createInstance)

Source from the content-addressed store, hash-verified

131};
132
133function extendInstance(Model, Instance, Driver, association, opts, createInstance) {
134 var promiseFunctionPostfix = Model.settings.get('promiseFunctionPostfix');
135
136 if (Model.settings.get("instance.cascadeRemove")) {
137 Instance.on("beforeRemove", function () {
138 Instance[association.delAccessor]();
139 });
140 }
141
142 function adjustForMapsTo(options) {
143 // Loop through the (cloned) association model id fields ... some of them may've been mapped to different
144 // names in the actual database - if so update to the mapped database column name
145 for(var i=0; i<options.__merge.to.field.length; i++) {
146 var idProp = association.model.properties[options.__merge.to.field[i]];
147 if(idProp && idProp.mapsTo) {
148 options.__merge.to.field[i] = idProp.mapsTo;
149 }
150 }
151 }
152
153 Object.defineProperty(Instance, association.hasAccessor, {
154 value: function () {
155 var Instances = Array.prototype.slice.apply(arguments);
156 var cb = Instances.pop();
157 var conditions = {}, options = {};
158
159 if (Instances.length) {
160 if (Array.isArray(Instances[0])) {
161 Instances = Instances[0];
162 }
163 }
164 if (Driver.hasMany) {
165 return Driver.hasMany(Model, association).has(Instance, Instances, conditions, cb);
166 }
167
168 options.autoFetchLimit = 0;
169 options.__merge = {
170 from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
171 to: { table: association.model.table, field: association.model.id.slice(0) }, // clone model id
172 where: [ association.mergeTable, {} ]
173 };
174
175 adjustForMapsTo(options);
176
177 options.extra = association.props;
178 options.extra_info = {
179 table: association.mergeTable,
180 id: util.values(Instance, Model.id),
181 id_prop: Object.keys(association.mergeId),
182 assoc_prop: Object.keys(association.mergeAssocId)
183 };
184
185 util.populateConditions(Model, Object.keys(association.mergeId), Instance, options.__merge.where[1]);
186
187 for (var i = 0; i < Instances.length; i++) {
188 util.populateConditions(association.model, Object.keys(association.mergeAssocId), Instances[i], options.__merge.where[1], false);
189 }
190

Callers 1

Many.jsFile · 0.70

Calls 8

adjustForMapsToFunction · 0.85
cbFunction · 0.85
runFunction · 0.85
onMethod · 0.80
getMethod · 0.65
findMethod · 0.65
saveMethod · 0.65
nextFunction · 0.50

Tested by

no test coverage detected