smartdata/dist/smartdata.classes.dbcollection.js

95 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-07-15 22:11:03 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartdata.plugins");
function Collection(db) {
return function (constructor) {
constructor['dbCollection'] = new DbCollection(constructor, db);
};
}
exports.Collection = Collection;
class DbCollection {
constructor(collectedClassArg, dbArg) {
this.objectValidation = null;
// tell the collection where it belongs
this.collectedClass = collectedClassArg;
this.name = collectedClassArg.name;
this.db = dbArg;
// make sure it actually exists
this.collection = dbArg.db.collection(this.name);
// tell the db class about it (important since Db uses different systems under the hood)
this.db.addCollection(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) {
let done = plugins.smartq.defer();
let findCursor = this.collection.find(docMatchArg);
if (optionsArg) {
if (optionsArg.limit) {
findCursor = findCursor.limit(1);
}
}
findCursor.toArray((err, docs) => {
if (err) {
done.reject(err);
throw err;
}
done.resolve(docs);
});
return done.promise;
}
/**
* inserts object into the DbCollection
*/
insertOne(docArg) {
let done = plugins.smartq.defer();
this.checkDoc(docArg).then(() => {
this.collection.insertOne(docArg)
.then(() => { done.resolve(); });
}, () => {
done.reject(new Error('one the docs did not pass validation'));
});
return done.promise;
}
/**
* 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.collection.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');
}
return done.promise;
}
}
exports.DbColl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