smartdata/dist/smartdata.classes.db.js

56 lines
3.8 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 {
2017-06-18 17:52:54 +00:00
constructor(dbUrlArg) {
2016-09-13 20:53:21 +00:00
this.collections = new lik_1.Objectmap();
2016-11-17 11:20:52 +00:00
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() {
2017-06-18 17:52:54 +00:00
let done = plugins.smartq.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);
});
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() {
2017-06-18 17:52:54 +00:00
let done = plugins.smartq.defer();
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) {
2017-06-18 17:52:54 +00:00
let done = plugins.smartq.defer();
2016-09-13 20:53:21 +00:00
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-06-18 17:52:54 +00:00
//# sourceMappingURL=data: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