smartdata/dist/smartdata.classes.dbcollection.js

103 lines
7.2 KiB
JavaScript
Raw Permalink 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 plugins = require("./smartdata.plugins");
function Collection(db) {
return function (constructor) {
constructor['dbCollection'] = new DbTable(constructor, db);
2017-07-15 22:11:03 +00:00
};
}
exports.Collection = Collection;
class DbTable {
2017-07-15 22:11:03 +00:00
constructor(collectedClassArg, dbArg) {
this.objectValidation = null;
// tell the collection where it belongs
this.tableName = collectedClassArg.name;
2017-07-15 22:11:03 +00:00
this.db = dbArg;
// tell the db class about it (important since Db uses different systems under the hood)
this.db.addTable(this);
2017-07-15 22:11:03 +00:00
}
init() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.table) {
// connect this instance to a RethinkDB table
const availableTables = yield plugins.rethinkDb
.db(this.db.dbName)
.tableList()
.run(this.db.dbConnection);
if (availableTables.indexOf(this.tableName)) {
yield plugins.rethinkDb
.db(this.db.dbName)
.tableCreate(this.tableName)
.run(this.db.dbConnection);
}
}
this.table = plugins.rethinkDb.table(this.tableName);
});
}
2017-07-15 22:11:03 +00:00
/**
* adds a validation function that all newly inserted and updated objects have to pass
*/
addDocValidation(funcArg) {
this.objectValidation = funcArg;
}
/**
* finds an object in the DbCollection
*/
find() {
return __awaiter(this, void 0, void 0, function* () {
yield this.init();
return yield plugins.rethinkDb.table(this.tableName).filter({}).run(this.db.dbConnection);
2017-07-15 22:11:03 +00:00
});
}
/**
* create an object in the database
2017-07-15 22:11:03 +00:00
*/
insert(dbDocArg) {
return __awaiter(this, void 0, void 0, function* () {
yield this.init();
yield this.checkDoc(dbDocArg);
return yield plugins.rethinkDb.table(this.tableName).insert(dbDocArg.createSavableObject()).run(this.db.dbConnection);
2017-07-15 22:11:03 +00:00
});
}
/**
* inserts object into the DbCollection
2017-07-15 22:11:03 +00:00
*/
update(dbDocArg) {
return __awaiter(this, void 0, void 0, function* () {
yield this.init();
yield this.checkDoc(dbDocArg);
console.log(this.tableName, dbDocArg.createSavableObject());
return yield plugins.rethinkDb.table(this.tableName).update(dbDocArg.createSavableObject()).run(this.db.dbConnection);
2017-07-15 22:11:03 +00:00
});
}
/**
* checks a Doc for constraints
* if this.objectValidation is not set it passes.
2017-07-15 22:11:03 +00:00
*/
checkDoc(docArg) {
let done = plugins.smartq.defer();
let validationResult = true;
if (this.objectValidation) {
validationResult = this.objectValidation(docArg);
}
if (validationResult) {
done.resolve();
}
else {
done.reject('validation of object did not pass');
}
return done.promise;
}
extractKey(writeResult) {
}
2017-07-15 22:11:03 +00:00
}
exports.DbTable = DbTable;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSwrQ0FBOEM7QUFlOUMsb0JBQTRCLEVBQU07SUFDaEMsTUFBTSxDQUFDLFVBQVUsV0FBVztRQUMxQixXQUFXLENBQUUsY0FBYyxDQUFFLEdBQUcsSUFBSSxPQUFPLENBQUMsV0FBVyxFQUFFLEVBQUUsQ0FBQyxDQUFBO0lBQzlELENBQUMsQ0FBQTtBQUNILENBQUM7QUFKRCxnQ0FJQztBQUVEO0lBVUUsWUFBYSxpQkFBK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