smartdata/dist/smartdata.classes.db.js

65 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-09-12 21:50:25 +00:00
"use strict";
2017-02-25 10:37:05 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
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;
}
;
addCollection(dbCollectionArg) {
this.collections.add(dbCollectionArg);
}
2016-09-12 21:50:25 +00:00
}
exports.Db = Db;
2017-02-25 10:37:05 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLCtDQUE4QztBQUM5Qyw2QkFBK0I7QUFjL0I7SUFPRSxZQUFZLFFBQWdCLEVBQUUsWUFBcUIsU0FBUztRQUY1RCxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFxQixDQUFBO1FBRzlDLElBQUksQ0FBQyxNQUFNLEdBQUcsU0FBUyxDQUFBO1FBQ3ZCLElBQUksQ0FBQyxLQUFLLEdBQUcsUUFBUSxDQUFBO0lBQ3ZCLENBQUM7SUFFRCx3RUFBd0U7SUFFeEU7O09BRUc7SUFDSCxPQUFPO1FBQ0wsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUM1QixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsTUFBTSxLQUFLLFNBQVMsQ0FBQyxDQUFDLENBQUM7WUFDOUIsT0FBTyxDQUFDLE9BQU8sQ0FBQyxXQUFXLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxLQUFLLEVBQUUsQ0FBQyxHQUFHLEVBQUUsRUFBRTtnQkFDdEQsRUFBRSxDQUFDLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztvQkFBQyxPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFBO2dCQUFDLENBQUM7Z0JBQzdCLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLElBQUksRUFBRSxHQUFHLENBQUMsQ0FBQTtnQk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