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

Function addInstanceProperty

lib/Instance.js:488–536  ·  view source on GitHub ↗
(key)

Source from the content-addressed store, hash-verified

486 }
487
488 var addInstanceProperty = function (key) {
489 var defaultValue = undefined;
490 var prop = Model.allProperties[key];
491
492 // This code was first added, and then commented out in a later commit.
493 // Its presence doesn't affect tests, so I'm just gonna log if it ever gets called.
494 // If someone complains about noise, we know it does something, and figure it out then.
495 if (instance.hasOwnProperty(key)) console.log("Overwriting instance property");
496
497 if (key in opts.data) {
498 defaultValue = opts.data[key];
499 } else if (prop && 'defaultValue' in prop) {
500 defaultValue = prop.defaultValue;
501 }
502
503 setInstanceProperty(key, defaultValue);
504
505 Object.defineProperty(instance, key, {
506 get: function () {
507 var val = opts.data[key];
508
509 if (val === undefined) {
510 return null;
511 } else {
512 return opts.data[key];
513 }
514 },
515 set: function (val) {
516 if (prop.key === true) {
517 if (prop.type == 'serial' && opts.data[key] != null) {
518 return;
519 } else {
520 opts.originalKeyValues[prop.name] = opts.data[prop.name];
521 }
522 }
523
524 if (!setInstanceProperty(key, val)) {
525 return;
526 }
527
528 if (opts.autoSave) {
529 saveInstanceProperty(key, val);
530 } else if (opts.changes.indexOf(key) === -1) {
531 opts.changes.push(key);
532 }
533 },
534 enumerable: !(prop && !prop.enumerable)
535 });
536 };
537 var addInstanceExtraProperty = function (key) {
538 if (!instance.hasOwnProperty("extra")) {
539 instance.extra = {};

Callers 1

InstanceFunction · 0.85

Calls 2

setInstancePropertyFunction · 0.85
saveInstancePropertyFunction · 0.85

Tested by

no test coverage detected