smartdata/dist/smartdata.classes.dbcollection.js

84 lines
5.4 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");
function Collection(db) {
return function (constructor) {
constructor['dbCollection'] = new DbTable(constructor, db);
};
}
exports.Collection = Collection;
class DbTable {
constructor(collectedClassArg, dbArg) {
this.objectValidation = null;
// tell the collection where it belongs
this.name = collectedClassArg.name;
this.db = dbArg;
// connect this instance to a RethinkDB table
this.table = plugins.rethinkDb.db(this.db.dbName).table(this.name);
// tell the db class about it (important since Db uses different systems under the hood)
this.db.addTable(this);
}
/**
* 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(docMatchArg, optionsArg) {
return __awaiter(this, void 0, void 0, function* () {
});
}
/**
* inserts object into the DbCollection
*/
insertOne(docArg) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkDoc(docArg);
});
}
/**
* inserts many objects at once into the DbCollection
*/
insertMany(docArrayArg) {
let done = plugins.smartq.defer();
let checkDocPromiseArray = [];
for (let docArg of docArrayArg) {
checkDocPromiseArray.push(this.checkDoc(docArg));
}
Promise.all(checkDocPromiseArray).then(() => {
this.table.insertMany(docArrayArg)
.then(() => { done.resolve(); });
});
return done.promise;
}
/**
* checks a Doc for constraints
*/
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');
}
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