smartdata/dist/smartdata.classes.dbdoc.js

46 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2016-09-13 20:53:21 +00:00
"use strict";
2016-11-17 23:42:25 +00:00
/**
2016-11-18 12:20:43 +00:00
* saveable - saveable decorator to be used on class properties
2016-11-17 23:42:25 +00:00
*/
function saveable(target, key) {
console.log('called sva');
if (!target.saveableProperties) {
target.saveableProperties = [];
}
target.saveableProperties.push(key);
}
exports.saveable = saveable;
2016-09-13 20:53:21 +00:00
class DbDoc {
2016-11-17 23:42:25 +00:00
/**
* class constructor
*/
constructor() {
this.collection = this.constructor['dbCollection'];
2016-09-13 20:53:21 +00:00
}
2016-11-17 23:42:25 +00:00
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
2016-09-13 20:53:21 +00:00
save() {
2016-11-17 23:42:25 +00:00
let saveableObject = {};
for (let propertyNameString of this.saveableProperties) {
saveableObject[propertyNameString] = this[propertyNameString];
}
2016-11-18 12:20:43 +00:00
switch (this.creationType) {
case 'db':
this.collection; // TODO implement collection.update()
break;
case 'new':
this.collection.insertOne(saveableObject);
}
2016-09-13 20:53:21 +00:00
}
2016-11-17 23:42:25 +00:00
/**
* also store any referenced objects to DB
* better for data consistency
*/
2016-09-13 20:53:21 +00:00
saveDeep() {
2016-11-17 23:42:25 +00:00
this.save();
2016-09-13 20:53:21 +00:00
}
}
exports.DbDoc = DbDoc;
2016-11-18 12:20:43 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBT0E7O0dBRUc7QUFDSCxrQkFBeUIsTUFBa0IsRUFBRSxHQUFXO0lBQ3BELE9BQU8sQ0FBQyxHQUFHLENBQUMsWUFBWSxD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