| 17 | }; |
| 18 | |
| 19 | function addLazyLoadProperty(name, Instance, Model, property) { |
| 20 | var method = ucfirst(name); |
| 21 | var promiseFunctionPostfix = Model.settings.get('promiseFunctionPostfix'); |
| 22 | var functionNames = { |
| 23 | get: { |
| 24 | callback : "get" + method, |
| 25 | promise : "get" + method + promiseFunctionPostfix |
| 26 | }, |
| 27 | remove: { |
| 28 | callback : "remove" + method, |
| 29 | promise : "remove" + method + promiseFunctionPostfix |
| 30 | }, |
| 31 | set: { |
| 32 | callback : "set" + method, |
| 33 | promise : "set" + method + promiseFunctionPostfix |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | Object.defineProperty(Instance, functionNames.get.callback, { |
| 38 | value: function (cb) { |
| 39 | var conditions = conditionAssign(Instance, Model); |
| 40 | |
| 41 | Model.find(conditions, { identityCache: false }).only(Model.id.concat(property)).first(function (err, item) { |
| 42 | return cb(err, item ? item[property] : null); |
| 43 | }); |
| 44 | |
| 45 | return this; |
| 46 | }, |
| 47 | enumerable: false |
| 48 | }); |
| 49 | |
| 50 | Object.defineProperty(Instance, functionNames.remove.callback, { |
| 51 | value: function (cb) { |
| 52 | var conditions = conditionAssign(Instance, Model); |
| 53 | |
| 54 | Model.find(conditions, { identityCache: false }).only(Model.id.concat(property)).first(function (err, item) { |
| 55 | if (err) { |
| 56 | return cb(err); |
| 57 | } |
| 58 | if (!item) { |
| 59 | return cb(null); |
| 60 | } |
| 61 | |
| 62 | item[property] = null; |
| 63 | |
| 64 | return item.save(cb); |
| 65 | }); |
| 66 | |
| 67 | return this; |
| 68 | }, |
| 69 | enumerable: false |
| 70 | }); |
| 71 | |
| 72 | Object.defineProperty(Instance, functionNames.set.callback, { |
| 73 | value: function (data, cb) { |
| 74 | var conditions = conditionAssign(Instance, Model); |
| 75 | |
| 76 | Model.find(conditions, { identityCache: false }).first(function (err, item) { |