2016-09-11 16:01:46 +00:00
|
|
|
"use strict";
|
2016-09-12 16:14:01 +00:00
|
|
|
const plugins = require('./smartdata.plugins');
|
2016-09-11 16:01:46 +00:00
|
|
|
class DbCollection {
|
|
|
|
constructor(nameArg, dbConnectionArg) {
|
|
|
|
this.collection = dbConnectionArg.db.collection(nameArg);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* adds a validation function that all newly inserted and updated objects have to pass
|
|
|
|
*/
|
2016-09-12 15:31:23 +00:00
|
|
|
addObjectValidation(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-12 15:31:23 +00:00
|
|
|
find(docMatchArg) {
|
2016-09-12 16:14:01 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-09-12 19:36:26 +00:00
|
|
|
this.collection.find(docMatchArg).toArray((err, docs) => {
|
2016-09-12 16:14:01 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
done.resolve(docs);
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-09-12 15:31:23 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* inserts object into the DbCollection
|
|
|
|
*/
|
|
|
|
insertOne(docArg) {
|
2016-09-12 16:14:01 +00:00
|
|
|
let done = plugins.q.defer();
|
|
|
|
this.checkDoc(docArg).then(() => {
|
|
|
|
this.collection.insertOne(docArg)
|
|
|
|
.then(() => { done.resolve(); });
|
|
|
|
});
|
|
|
|
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-09-12 19:36:26 +00:00
|
|
|
this.collection.insertMany(docArrayArg)
|
|
|
|
.then(() => { done.resolve(); });
|
2016-09-12 16:14:01 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
checkDoc(doc) {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
2016-09-12 15:31:23 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|
|
|
|
exports.DbCollection = DbCollection;
|
2016-09-12 19:36:26 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSxxQkFDekIsQ0FBQyxDQUQ2QztBQUc5QztJQUVJLFlBQVksT0FBZSxFQUFFLGVBQTZCO1FBQ3RELElBQUksQ0FBQyxVQUFVLEdBQUcsZUFBZSxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsT0FBTyxDQUFDLENBQUE7SUFDNUQsQ0FBQztJQUVEOztPQUVHO0lBQ0gsbUJBQW1CLENBQUMsT0FBTyxJQUFJLENBQUM7SUFFaEM7O09BRUc7SUFDSCxJQUFJLENBQUMsV0FBYztRQUNmLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFPLENBQUE7UUFDakMsSUFBSSxDQUFDLFVBQVUsQ0FBQyxJQUFJLENBQUMsV0FBVyxD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