smartdata/dist/smartdata.classes.dbcollection.js

70 lines
4.8 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSxxQkFDekIsQ0FBQyxDQUQ2QztBQU85QyxvQkFBMkIsRUFBTTtJQU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