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

Function extendInstance

lib/Associations/One.js:151–294  ·  view source on GitHub ↗
(Model, Instance, Driver, association)

Source from the content-addressed store, hash-verified

149};
150
151function extendInstance(Model, Instance, Driver, association) {
152 var promiseFunctionPostfix = Model.settings.get('promiseFunctionPostfix');
153 Object.defineProperty(Instance, association.hasAccessor, {
154 value: function (opts, cb) {
155 if (typeof opts === "function") {
156 cb = opts;
157 opts = {};
158 }
159
160 if (util.hasValues(Instance, Object.keys(association.field))) {
161 association.model.get(util.values(Instance, Object.keys(association.field)), opts, function (err, instance) {
162 return cb(err, instance ? true : false);
163 });
164 } else {
165 cb(null, false);
166 }
167
168 return this;
169 },
170 enumerable: false,
171 writable: true
172 });
173 Object.defineProperty(Instance, association.getAccessor, {
174 value: function (opts, cb) {
175 if (typeof opts === "function") {
176 cb = opts;
177 opts = {};
178 }
179
180 var saveAndReturn = function (err, Assoc) {
181 if (!err) {
182 Instance[association.name] = Assoc;
183 }
184
185 return cb(err, Assoc);
186 };
187
188 if (association.reversed) {
189 if (util.hasValues(Instance, Model.id)) {
190 if (typeof cb !== "function") {
191 return association.model.find(util.getConditions(Model, Object.keys(association.field), Instance), opts);
192 }
193 association.model.find(util.getConditions(Model, Object.keys(association.field), Instance), opts, saveAndReturn);
194 } else {
195 cb(null);
196 }
197 } else {
198 if (Instance.isShell()) {
199 Model.get(util.values(Instance, Model.id), function (err, instance) {
200 if (err || !util.hasValues(instance, Object.keys(association.field))) {
201 return cb(null);
202 }
203 association.model.get(util.values(instance, Object.keys(association.field)), opts, saveAndReturn);
204 });
205 } else if (util.hasValues(Instance, Object.keys(association.field))) {
206 association.model.get(util.values(Instance, Object.keys(association.field)), opts, saveAndReturn);
207 } else {
208 cb(null);

Callers 1

One.jsFile · 0.70

Calls 5

cbFunction · 0.85
getMethod · 0.65
findMethod · 0.65
saveMethod · 0.65
nextFunction · 0.50

Tested by

no test coverage detected