70 lines
4.7 KiB
JavaScript
70 lines
4.7 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.name = nameArg;
|
|
this.collection = dbArg.db.collection(nameArg);
|
|
}
|
|
/**
|
|
* adds a validation function that all newly inserted and updated objects have to pass
|
|
*/
|
|
addObjectValidation(funcArg) { }
|
|
/**
|
|
* finds an object in the DbCollection
|
|
*/
|
|
find(docMatchArg, optionsArg) {
|
|
let done = plugins.q.defer();
|
|
let findCursor = this.collection.find(docMatchArg);
|
|
if (optionsArg) {
|
|
if (optionsArg.limit) {
|
|
findCursor = findCursor.limit(1);
|
|
}
|
|
}
|
|
findCursor.toArray((err, docs) => {
|
|
if (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(() => {
|
|
this.collection.insertOne(docArg)
|
|
.then(() => { done.resolve(); });
|
|
});
|
|
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(() => {
|
|
this.collection.insertMany(docArrayArg)
|
|
.then(() => { done.resolve(); });
|
|
});
|
|
return done.promise;
|
|
}
|
|
checkDoc(doc) {
|
|
let done = plugins.q.defer();
|
|
done.resolve();
|
|
return done.promise;
|
|
}
|
|
}
|
|
exports.DbCollection = DbCollection;
|
|
//# sourceMappingURL=data:application/json;base64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