2016-09-11 16:01:46 +00:00
|
|
|
"use strict";
|
2016-11-17 11:20:52 +00:00
|
|
|
const plugins = require("./smartdata.plugins");
|
2016-09-13 23:02:11 +00:00
|
|
|
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-09-13 20:53:21 +00:00
|
|
|
this.name = nameArg;
|
2016-09-12 21:50:25 +00:00
|
|
|
this.collection = dbArg.db.collection(nameArg);
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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-13 20:53:21 +00:00
|
|
|
find(docMatchArg, optionsArg) {
|
2016-09-12 16:14:01 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-09-13 20:53:21 +00:00
|
|
|
let findCursor = this.collection.find(docMatchArg);
|
|
|
|
if (optionsArg) {
|
|
|
|
if (optionsArg.limit) {
|
|
|
|
findCursor = findCursor.limit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
findCursor.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-11-17 11:20:52 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBOEM7QUFPOUMsb0JBQTJCLEVBQU07SUFDN0IsTUFBTSxDQUFDLFVBQVMsV0FBVztRQUN2QixXQUFXLENBQUMsY0FBYyx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