smartdata/dist/smartdata.classes.dbdoc.js

61 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-09-13 20:53:21 +00:00
"use strict";
2017-02-25 10:37:05 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
const lik_1 = require("lik");
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
*/
2016-11-18 12:56:15 +00:00
function svDb() {
return (target, key) => {
console.log('called sva');
if (!target.saveableProperties) {
target.saveableProperties = [];
}
target.saveableProperties.push(key);
};
2016-11-17 23:42:25 +00:00
}
2016-11-18 12:56:15 +00:00
exports.svDb = svDb;
2016-09-13 20:53:21 +00:00
class DbDoc {
2016-11-17 23:42:25 +00:00
/**
* class constructor
*/
constructor() {
this.name = this.constructor['name'];
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() {
2017-06-18 17:52:54 +00:00
let saveableObject = {}; // is not exposed to outside, so any is ok here
2016-11-17 23:42:25 +00:00
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
*/
2017-02-25 10:37:05 +00:00
saveDeep(savedMapArg = null) {
if (!savedMapArg) {
savedMapArg = new lik_1.Objectmap();
}
savedMapArg.add(this);
2016-11-17 23:42:25 +00:00
this.save();
2017-02-25 10:37:05 +00:00
for (let propertyKey in this) {
let property = this[propertyKey];
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
property.saveDeep(savedMapArg);
}
}
2016-09-13 20:53:21 +00:00
}
}
exports.DbDoc =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