128 lines
9.2 KiB
JavaScript
128 lines
9.2 KiB
JavaScript
"use strict";
|
|
const plugins = require("./smartdata.plugins");
|
|
function Collection(db) {
|
|
return function (constructor) {
|
|
constructor['dbCollection'] = new DbCollection(constructor.name, db);
|
|
};
|
|
}
|
|
exports.Collection = Collection;
|
|
class DbCollection {
|
|
constructor(nameArg, dbArg) {
|
|
this.objectValidation = null;
|
|
this.name = nameArg;
|
|
this.db = dbArg;
|
|
if (this.db.dbType === 'mongodb') {
|
|
this.collection = dbArg.db.collection(nameArg);
|
|
}
|
|
else {
|
|
this.collection = new plugins.nedb();
|
|
}
|
|
}
|
|
/**
|
|
* 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.q.defer();
|
|
if (this.db.dbType === 'mongodb') {
|
|
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);
|
|
});
|
|
}
|
|
else if (this.db.dbType === 'nedb') {
|
|
this.collection.find(docMatchArg, (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.q.defer();
|
|
this.checkDoc(docArg).then(() => {
|
|
if (this.db.dbType === 'mongodb') {
|
|
this.collection.insertOne(docArg)
|
|
.then(() => { done.resolve(); });
|
|
}
|
|
else if (this.db.dbType === 'nedb') {
|
|
this.collection.insert(docArg, (err, newDoc) => {
|
|
if (err) {
|
|
done.reject(err);
|
|
throw err;
|
|
}
|
|
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.q.defer();
|
|
let checkDocPromiseArray = [];
|
|
for (let docArg of docArrayArg) {
|
|
checkDocPromiseArray.push(this.checkDoc(docArg));
|
|
}
|
|
plugins.q.all(checkDocPromiseArray).then(() => {
|
|
if (this.db.dbType === 'mongodb') {
|
|
this.collection.insertMany(docArrayArg)
|
|
.then(() => { done.resolve(); });
|
|
}
|
|
else if (this.db.dbType === 'nedb') {
|
|
let paramArray = plugins.lodash.concat(docArrayArg, (err, newDoc) => {
|
|
if (err) {
|
|
done.reject(err);
|
|
throw err;
|
|
}
|
|
done.resolve();
|
|
});
|
|
this.collection.insert.apply(null, paramArray);
|
|
}
|
|
});
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* checks a Doc for constraints
|
|
*/
|
|
checkDoc(docArg) {
|
|
let done = plugins.q.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.DbCollection = DbCollection;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBOEM7QUFXOUMsb0JBQTJCLEVBQU07SUFDN0IsTUFBTSxDQUFDLFVBQVUsV0FBVztRQ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