smartdata/dist/smartdata.classes.dbcollection.js

128 lines
9.2 KiB
JavaScript
Raw Permalink Normal View History

2016-09-11 16:01:46 +00:00
"use strict";
2016-11-17 11:20:52 +00:00
const plugins = require("./smartdata.plugins");
function Collection(db) {
return function (constructor) {
constructor['dbCollection'] = new DbCollection(constructor.name, db);
};
}
exports.Collection = Collection;
2016-09-11 16:01:46 +00:00
class DbCollection {
2016-09-12 21:50:25 +00:00
constructor(nameArg, dbArg) {
2016-11-17 21:36:12 +00:00
this.objectValidation = null;
2016-09-13 20:53:21 +00:00
this.name = nameArg;
2016-11-17 21:36:12 +00:00
this.db = dbArg;
if (this.db.dbType === 'mongodb') {
this.collection = dbArg.db.collection(nameArg);
}
else {
this.collection = new plugins.nedb();
}
2016-09-11 16:01:46 +00:00
}
/**
* adds a validation function that all newly inserted and updated objects have to pass
*/
2016-11-17 21:36:12 +00:00
addDocValidation(funcArg) {
this.objectValidation = funcArg;
}
2016-09-11 16:01:46 +00:00
/**
2016-09-12 15:31:23 +00:00
* finds an object in the DbCollection
2016-09-11 16:01:46 +00:00
*/
2016-09-13 20:53:21 +00:00
find(docMatchArg, optionsArg) {
2016-09-12 16:14:01 +00:00
let done = plugins.q.defer();
2016-11-17 21:36:12 +00:00
if (this.db.dbType === 'mongodb') {
let findCursor = this.collection.find(docMatchArg);
if (optionsArg) {
if (optionsArg.limit) {
findCursor = findCursor.limit(1);
}
2016-09-13 20:53:21 +00:00
}
2016-11-17 21:36:12 +00:00
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);
});
2016-09-13 20:53:21 +00:00
}
2016-09-12 16:14:01 +00:00
return done.promise;
2016-09-12 15:31:23 +00:00
}
/**
2016-11-17 21:36:12 +00:00
* inserts object into the DbCollection
2016-09-12 15:31:23 +00:00
*/
insertOne(docArg) {
2016-09-12 16:14:01 +00:00
let done = plugins.q.defer();
this.checkDoc(docArg).then(() => {
2016-11-17 21:36:12 +00:00
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'));
2016-09-12 16:14:01 +00:00
});
return done.promise;
2016-09-12 15:31:23 +00:00
}
2016-09-11 16:01:46 +00:00
/**
* inserts many objects at once into the DbCollection
*/
2016-09-12 15:31:23 +00:00
insertMany(docArrayArg) {
2016-09-12 16:14:01 +00:00
let done = plugins.q.defer();
let checkDocPromiseArray = [];
for (let docArg of docArrayArg) {
checkDocPromiseArray.push(this.checkDoc(docArg));
}
plugins.q.all(checkDocPromiseArray).then(() => {
2016-11-17 21:36:12 +00:00
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);
}
2016-09-12 16:14:01 +00:00
});
return done.promise;
}
2016-11-17 21:36:12 +00:00
/**
* checks a Doc for constraints
*/
checkDoc(docArg) {
2016-09-12 16:14:01 +00:00
let done = plugins.q.defer();
2016-11-17 21:36:12 +00:00
let validationResult = true;
if (this.objectValidation) {
validationResult = this.objectValidation(docArg);
}
if (validationResult) {
done.resolve();
}
else {
done.reject('validation of object did not pass');
}
2016-09-12 16:14:01 +00:00
return done.promise;
2016-09-12 15:31:23 +00:00
}
2016-09-11 16:01:46 +00:00
}
exports.DbCollection = DbCollection;
2016-11-17 21:36:12 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic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