2017-07-15 22:11:03 +00:00
|
|
|
"use strict";
|
2018-01-12 00:22:58 +00:00
|
|
|
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) => {
|
2018-01-14 16:32:04 +00:00
|
|
|
console.log("called sva");
|
2017-07-15 22:11:03 +00:00
|
|
|
if (!target.saveableProperties) {
|
|
|
|
target.saveableProperties = [];
|
|
|
|
}
|
|
|
|
target.saveableProperties.push(key);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.svDb = svDb;
|
|
|
|
class DbDoc {
|
|
|
|
/**
|
|
|
|
* class constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
2018-01-12 00:22:58 +00:00
|
|
|
/**
|
|
|
|
* how the Doc in memory was created, may prove useful later.
|
|
|
|
*/
|
2018-01-14 16:32:04 +00:00
|
|
|
this.creationStatus = "new";
|
|
|
|
this.name = this.constructor["name"];
|
|
|
|
this.collection = this.constructor["dbTable"];
|
|
|
|
}
|
|
|
|
static getInstances(filterArg) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let self = this; // fool typesystem
|
|
|
|
let referenceTable = self.dbTable;
|
|
|
|
const foundDocs = yield referenceTable.find(filterArg);
|
|
|
|
const returnArray = [];
|
|
|
|
for (let item of foundDocs) {
|
|
|
|
let newInstance = new this();
|
|
|
|
for (let key in item) {
|
|
|
|
if (key !== 'id') {
|
|
|
|
newInstance[key] = item[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
returnArray.push(newInstance);
|
|
|
|
}
|
|
|
|
return returnArray;
|
|
|
|
});
|
2017-07-15 22:11:03 +00:00
|
|
|
}
|
2018-01-15 23:40:08 +00:00
|
|
|
static getInstance(filterArg) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let result = yield this.getInstances(filterArg);
|
|
|
|
if (result && result.length > 0) {
|
|
|
|
return result[0];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-07-15 22:11:03 +00:00
|
|
|
/**
|
|
|
|
* saves this instance but not any connected items
|
|
|
|
* may lead to data inconsistencies, but is faster
|
|
|
|
*/
|
|
|
|
save() {
|
2018-01-12 00:22:58 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let self = this;
|
|
|
|
switch (this.creationStatus) {
|
2018-01-14 16:32:04 +00:00
|
|
|
case "db":
|
2018-01-12 00:22:58 +00:00
|
|
|
yield this.collection.update(self);
|
|
|
|
break;
|
2018-01-14 16:32:04 +00:00
|
|
|
case "new":
|
2018-01-12 00:22:58 +00:00
|
|
|
let writeResult = yield this.collection.insert(self);
|
2018-01-14 16:32:04 +00:00
|
|
|
this.creationStatus = "db";
|
2018-01-12 00:22:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-14 16:32:04 +00:00
|
|
|
console.error("neither new nor in db?");
|
2018-01-12 00:22:58 +00:00
|
|
|
}
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 00:22:58 +00:00
|
|
|
createSavableObject() {
|
|
|
|
let saveableObject = {}; // is not exposed to outside, so any is ok here
|
|
|
|
for (let propertyNameString of this.saveableProperties) {
|
|
|
|
saveableObject[propertyNameString] = this[propertyNameString];
|
|
|
|
}
|
|
|
|
return saveableObject;
|
|
|
|
}
|
2017-07-15 22:11:03 +00:00
|
|
|
}
|
|
|
|
exports.DbDoc = DbDoc;
|
2018-01-15 23:40:08 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBRUEsNkJBQWdDO0FBT2hDOztHQUVHO0FBQ0g7SUFDRSxNQUFNLENBQUMsQ0FBQyxNQUFrQixFQUFFLEdBQVcsRUFBRSxFQUFFO1FBQ3pDLE9BQU8sQ0FBQyxHQUFHLENBQUMsWUFBWSxDQUFDLENBQUM7UUFDMUIsRUFBRSxDQUFDLENBQUMsQ0FBQyxNQUFNLENBQUMsa0JBQWtCLENBQUMsQ0FBQyxDQUFDO1lBQy9CLE1BQU0sQ0FBQyxrQkFBa0IsR0FBRyxFQUFFLENBQUM7UUFDakMsQ0FBQztRQUNELE1BQU0sQ0FBQyxrQkFBa0IsQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUM7SUFDdEMsQ0FBQyxDQUFDO0FBQ0osQ0FBQztBQVJELG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