116 lines
7.6 KiB
JavaScript
116 lines
7.6 KiB
JavaScript
"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());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const plugins = require("./smartdata.plugins");
|
|
/**
|
|
* This is a decorator that will tell the decorated class what dbTable to use
|
|
* @param db
|
|
*/
|
|
function Table(db) {
|
|
return function (constructor) {
|
|
constructor["dbTable"] = new DbTable(constructor, db);
|
|
};
|
|
}
|
|
exports.Table = Table;
|
|
class DbTable {
|
|
constructor(collectedClassArg, dbArg) {
|
|
this.objectValidation = null;
|
|
// tell the collection where it belongs
|
|
this.tableName = collectedClassArg.name;
|
|
this.db = dbArg;
|
|
// tell the db class about it (important since Db uses different systems under the hood)
|
|
this.db.addTable(this);
|
|
}
|
|
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);
|
|
});
|
|
}
|
|
/**
|
|
* 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(filterObject) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
yield this.init();
|
|
let cursor = yield plugins.rethinkDb
|
|
.table(this.tableName)
|
|
.filter(filterObject)
|
|
.run(this.db.dbConnection);
|
|
return yield cursor.toArray();
|
|
});
|
|
}
|
|
/**
|
|
* create an object in the database
|
|
*/
|
|
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);
|
|
});
|
|
}
|
|
/**
|
|
* inserts object into the DbCollection
|
|
*/
|
|
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);
|
|
});
|
|
}
|
|
/**
|
|
* checks a Doc for constraints
|
|
* if this.objectValidation is not set it passes.
|
|
*/
|
|
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) { }
|
|
}
|
|
exports.DbTable = DbTable;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJ0YWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZGF0YS5jbGFzc2VzLmRidGFibGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7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