2016-09-12 21:50:25 +00:00
|
|
|
"use strict";
|
2017-06-23 09:40:20 +00:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
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");
|
2017-06-23 09:40:20 +00:00
|
|
|
const smartdata_classes_dbobjectdoc_1 = require("./smartdata.classes.dbobjectdoc");
|
2016-09-12 21:50:25 +00:00
|
|
|
class Db {
|
2017-06-18 17:52:54 +00:00
|
|
|
constructor(dbUrlArg) {
|
2017-06-23 09:40:20 +00:00
|
|
|
this.classCollections = new lik_1.Objectmap();
|
|
|
|
this.objectCollections = 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 --------------------------------
|
|
|
|
/**
|
2017-06-23 09:40:20 +00:00
|
|
|
* gets a class based collection by name: string
|
2016-09-13 20:53:21 +00:00
|
|
|
*/
|
2017-06-23 09:40:20 +00:00
|
|
|
getClassCollectionByName(nameArg) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let resultCollection = this.classCollections.find((dbCollectionArg) => {
|
|
|
|
return dbCollectionArg.name === nameArg;
|
|
|
|
});
|
|
|
|
return resultCollection;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* gets an object collection by name
|
|
|
|
*/
|
|
|
|
getObjectCollectionByName(nameArg, dbArg, makeNewArg = false) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let resultCollection = this.objectCollections.find((dbCollectionArg) => {
|
|
|
|
return dbCollectionArg.name === nameArg;
|
|
|
|
});
|
|
|
|
if (!resultCollection && makeNewArg) {
|
|
|
|
resultCollection = smartdata_classes_dbobjectdoc_1.getObjectDoc(nameArg, this).collection;
|
|
|
|
return resultCollection;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return resultCollection;
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
});
|
|
|
|
}
|
2016-09-13 23:02:11 +00:00
|
|
|
addCollection(dbCollectionArg) {
|
2017-06-23 09:40:20 +00:00
|
|
|
this.classCollections.add(dbCollectionArg);
|
2016-09-13 23:02:11 +00:00
|
|
|
}
|
2016-09-12 21:50:25 +00:00
|
|
|
}
|
|
|
|
exports.Db = Db;
|
2017-06-23 09:40:20 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQUcvQixtRkFBOEQ7QUFPOUQ7SUFPRSxZQUFhLFFBQWdCO1FBSDdCLHFCQUFnQixHQUFHLElBQUksZUFBUyxFQUFxQixDQUFBO1FBQ3JELHNCQUFpQixHQUFHLElBQUksZUFBUyxFQUFxQixDQUFBO1FBR3BELElBQUksQ0FBQyxLQUFLLEdBQUcsUUFBUSxDQUFBO0lBQ3ZCLENBQUM7SUFFRCx3RUFBd0U7SUFFeEU7O09BRUc7SUFDSCxPQUFPO1FBQ0wsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUNqQyxPQUFPLENBQUMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLEdBQUcsRUFBRSxFQUFFO1lBQ3RELEVBQUUsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7Z0JBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQTtZQUFDLENBQUM7WUFDN0IsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxFQUFFL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