2016-09-12 21:50:25 +00:00
|
|
|
"use strict";
|
2016-11-17 11:20:52 +00:00
|
|
|
const plugins = require("./smartdata.plugins");
|
|
|
|
const lik_1 = require("lik");
|
2016-09-12 21:50:25 +00:00
|
|
|
class Db {
|
2016-11-17 11:20:52 +00:00
|
|
|
constructor(dbUrlArg, dbTypeArg = 'mongodb') {
|
2016-09-13 20:53:21 +00:00
|
|
|
this.collections = new lik_1.Objectmap();
|
2016-11-17 11:20:52 +00:00
|
|
|
this.dbType = dbTypeArg;
|
|
|
|
this.dbUrl = dbUrlArg;
|
2016-09-12 21:50:25 +00:00
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
// basic connection stuff ----------------------------------------------
|
|
|
|
/**
|
|
|
|
* connects to the database that was specified during instance creation
|
|
|
|
*/
|
2016-09-12 21:50:25 +00:00
|
|
|
connect() {
|
|
|
|
let done = plugins.q.defer();
|
2016-11-17 11:20:52 +00:00
|
|
|
if (this.dbType === 'mongodb') {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (this.dbType === 'nedb') {
|
2016-11-17 21:36:12 +00:00
|
|
|
this.db = null;
|
2016-11-17 11:20:52 +00:00
|
|
|
}
|
2016-09-12 21:50:25 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
/**
|
|
|
|
* closes the connection to the databse
|
|
|
|
*/
|
2016-09-12 21:50:25 +00:00
|
|
|
close() {
|
|
|
|
let done = plugins.q.defer();
|
2016-11-17 21:36:12 +00:00
|
|
|
if (this.dbType === 'mongodb') {
|
|
|
|
this.db.close();
|
|
|
|
}
|
2016-09-12 21:50:25 +00:00
|
|
|
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
;
|
2016-09-13 23:02:11 +00:00
|
|
|
addCollection(dbCollectionArg) {
|
|
|
|
this.collections.add(dbCollectionArg);
|
|
|
|
}
|
2016-09-12 21:50:25 +00:00
|
|
|
}
|
|
|
|
exports.Db = Db;
|
2016-11-17 21:36:12 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQWMvQjtJQU9JLFlBQVksUUFBZ0IsRUFBRSxZQUFxQixTQUFTO1FBRjVELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQXFCLENBQUE7UUFHNUMsSUFBSSxDQUFDLE1BQU0sR0FBRyxTQUFTLENBQUE7UUFDdkIsSUFBSSxDQUFDLEtBQUssR0FBRyxRQUFRLENBQUE7SUFDekIsQ0FBQztJQUVELHdFQUF3RTtJQUV4RTs7T0FFRztJQUNILE9BQU87UUFDSCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQU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