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
*/
2017-06-18 17:52:54 +00:00
constructor(nameArg) {
this.collection = this.constructor['dbCollection'];
2017-06-18 17:52:54 +00:00
this.name = nameArg;
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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