53 lines
3.6 KiB
JavaScript
53 lines
3.6 KiB
JavaScript
"use strict";
|
|
const plugins = require('./smartdata.plugins');
|
|
const lik_1 = require('lik');
|
|
class Db {
|
|
constructor(dbUrl) {
|
|
this.collections = new lik_1.Objectmap();
|
|
this.dbUrl = dbUrl;
|
|
}
|
|
// basic connection stuff ----------------------------------------------
|
|
/**
|
|
* connects to the database that was specified during instance creation
|
|
*/
|
|
connect() {
|
|
let done = plugins.q.defer();
|
|
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
|
if (err) {
|
|
console.log(err);
|
|
}
|
|
plugins.assert.equal(null, err);
|
|
this.db = db;
|
|
plugins.beautylog.success(`connected to database at ${this.dbUrl}`);
|
|
done.resolve(this.db);
|
|
});
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* closes the connection to the databse
|
|
*/
|
|
close() {
|
|
let done = plugins.q.defer();
|
|
this.db.close();
|
|
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
|
|
done.resolve();
|
|
return done.promise;
|
|
}
|
|
// advanced communication with the database --------------------------------
|
|
/**
|
|
* gets a collection by name: string
|
|
*/
|
|
getCollectionByName(nameArg) {
|
|
let done = plugins.q.defer();
|
|
let resultCollection = this.collections.find((dbCollectionArg) => {
|
|
return dbCollectionArg.name === nameArg;
|
|
});
|
|
if (resultCollection !== null) {
|
|
done.resolve(resultCollection);
|
|
}
|
|
return done.promise;
|
|
}
|
|
;
|
|
}
|
|
exports.Db = Db;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0scUJBQ3pCLENBQUMsQ0FENkM7QUFDOUMsc0JBQTBCLEtBRTFCLENB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