60 lines
3.6 KiB
JavaScript
60 lines
3.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const lik_1 = require("lik");
|
|
/**
|
|
* saveable - saveable decorator to be used on class properties
|
|
*/
|
|
function svDb() {
|
|
return (target, key) => {
|
|
console.log('called sva');
|
|
if (!target.saveableProperties) {
|
|
target.saveableProperties = [];
|
|
}
|
|
target.saveableProperties.push(key);
|
|
};
|
|
}
|
|
exports.svDb = svDb;
|
|
class DbDoc {
|
|
/**
|
|
* class constructor
|
|
*/
|
|
constructor() {
|
|
this.collection = this.constructor['dbCollection'];
|
|
}
|
|
/**
|
|
* saves this instance but not any connected items
|
|
* may lead to data inconsistencies, but is faster
|
|
*/
|
|
save() {
|
|
let saveableObject = {}; // isn not exposed to outside, so any is ok here
|
|
for (let propertyNameString of this.saveableProperties) {
|
|
saveableObject[propertyNameString] = this[propertyNameString];
|
|
}
|
|
switch (this.creationType) {
|
|
case 'db':
|
|
this.collection; // TODO implement collection.update()
|
|
break;
|
|
case 'new':
|
|
this.collection.insertOne(saveableObject);
|
|
}
|
|
}
|
|
/**
|
|
* also store any referenced objects to DB
|
|
* better for data consistency
|
|
*/
|
|
saveDeep(savedMapArg = null) {
|
|
if (!savedMapArg) {
|
|
savedMapArg = new lik_1.Objectmap();
|
|
}
|
|
savedMapArg.add(this);
|
|
this.save();
|
|
for (let propertyKey in this) {
|
|
let property = this[propertyKey];
|
|
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
|
|
property.saveDeep(savedMapArg);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exports.DbDoc = DbDoc;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcH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