smartdata/dist/smartdata.classes.dbdoc.js

83 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-07-15 22:11:03 +00:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
2017-07-15 22:11:03 +00:00
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() {
/**
* how the Doc in memory was created, may prove useful later.
*/
this.creationStatus = 'new';
2017-07-15 22:11:03 +00:00
this.name = this.constructor['name'];
this.collection = this.constructor['dbCollection'];
}
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
save() {
return __awaiter(this, void 0, void 0, function* () {
let self = this;
switch (this.creationStatus) {
case 'db':
yield this.collection.update(self);
break;
case 'new':
let writeResult = yield this.collection.insert(self);
this.creationStatus = 'db';
break;
default:
console.error('neither new nor in db?');
}
});
2017-07-15 22:11:03 +00:00
}
/**
* 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);
}
}
}
createSavableObject() {
let saveableObject = {}; // is not exposed to outside, so any is ok here
for (let propertyNameString of this.saveableProperties) {
saveableObject[propertyNameString] = this[propertyNameString];
}
return saveable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