65 lines
4.5 KiB
JavaScript
65 lines
4.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const plugins = require("./smartdata.plugins");
|
|
const lik_1 = require("lik");
|
|
class Db {
|
|
constructor(dbUrlArg, dbTypeArg = 'mongodb') {
|
|
this.collections = new lik_1.Objectmap();
|
|
this.dbType = dbTypeArg;
|
|
this.dbUrl = dbUrlArg;
|
|
}
|
|
// basic connection stuff ----------------------------------------------
|
|
/**
|
|
* connects to the database that was specified during instance creation
|
|
*/
|
|
connect() {
|
|
let done = plugins.q.defer();
|
|
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') {
|
|
this.db = null;
|
|
}
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* closes the connection to the databse
|
|
*/
|
|
close() {
|
|
let done = plugins.q.defer();
|
|
if (this.dbType === 'mongodb') {
|
|
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;
|
|
}
|
|
;
|
|
addCollection(dbCollectionArg) {
|
|
this.collections.add(dbCollectionArg);
|
|
}
|
|
}
|
|
exports.Db = Db;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRk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