UNPKG

@riskmgmt/forerunnerdb

Version:

Forerunnerdb Angular 5 injectable service

230 lines (229 loc) 10.1 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { Injectable } from "@angular/core"; import { LogService } from "@riskmgmt/logger"; import * as _ from "lodash"; import { DocumentDbService } from "./documentdb.service"; var ForerunnerDB = require("forerunnerdb/js/builds/core+persist"); var ForerunnerDbService = /** @class */ (function (_super) { __extends(ForerunnerDbService, _super); function ForerunnerDbService(settings, logService) { var _this = _super.call(this) || this; _this.settings = settings; _this.activeCollectionScopes = []; _this.logger = logService.create("Service:ForerunnerDb"); _this.logger.info("ForerunnerDbService created"); if (!settings.dbName) { throw Error("ForerunnerDbService: Must supply a name for the database"); } _this.fdb = new ForerunnerDB(settings.dbName); _this.db = _this.fdb.db("fdb"); return _this; } ForerunnerDbService.prototype.beginCollectionScope = function (collection) { this.logger.info("beginCollectionScope called"); this.logger.debug("beginCollectionScope: collection:", collection); if (this.isCollectionScopeCurrent(collection)) { return; } this.logger.debug("beginCollectionScope: pushing collection onto active stack", collection); this.activeCollectionScopes.push(collection); this.logger.debug("beginCollectionScope: stack:", this.activeCollectionScopes); }; ForerunnerDbService.prototype.endCollectionScope = function (collection) { this.logger.info("endCollectionScope called"); this.logger.debug("endCollectionScope: collection", collection); if (this.isCollectionScopeCurrent(collection)) { this.logger.debug("endCollectionScope: removing collection from active stack", collection); _.remove(this.activeCollectionScopes, function (item) { return item === collection; }); this.logger.debug("endCollectionScope: stack:", this.activeCollectionScopes); } }; ForerunnerDbService.prototype.remove = function (collection, id) { var _this = this; this.logger.info("remove called"); this.logger.info("remove: params: ", collection, id); return this.get(collection, id).then(function (file) { _this.db.collection(collection).remove({ _id: id }); return null; }) .then(function () { return _this.saveCollection(collection); }) .catch(function (err) { _this.logger.error(err); }); }; ForerunnerDbService.prototype.clean = function (collection) { var _this = this; this.logger.info("clean called"); this.logger.debug("clean: params:", collection); return this.loadCollection(collection) .then(function () { return _this.db.collection(collection).remove(); }); }; ForerunnerDbService.prototype.getMany = function (collection, filter) { var _this = this; this.logger.info("getMany called"); this.logger.debug("getMany: params:", collection, filter); return this.loadCollection(collection) .then(function () { return _this.db.collection(collection).find(filter); }) .then(function (list) { _this.logger.debug("getMany: list:", list); return _.map(list, function (row) { return row; }); }); }; ForerunnerDbService.prototype.get = function (collection, id) { var _this = this; this.logger.info("get called"); this.logger.debug("get: params:", collection, id); return this.loadCollection(collection) .then(function () { return _this.db.collection(collection).find({ _id: id }); }) .then(function (list) { return _.map(list, function (row) { return row; }); }) .then(function (items) { return _.first(items); }); }; ForerunnerDbService.prototype.save = function (collection, obj) { var _this = this; this.logger.info("save called"); this.logger.debug("save: params:", collection, obj); var id = this.makeid(); var now = new Date().toISOString(); var object = { metadata: { createdAt: now, updatedAt: now } }; _.merge(object, obj); var newItem = { _id: id, item: object }; this.upsert(this.db.collection(collection), id, newItem); return Promise.resolve(obj) .then(function () { return _this.saveCollection(collection); }) .then(function () { return newItem; }); }; ForerunnerDbService.prototype.update = function (collection, obj) { var _this = this; this.logger.info("update called"); this.logger.debug("update: params:", collection, obj); if (!obj || !obj.item) { throw new Error("forerunner data: update: obj or obj.item is null"); } var now = new Date().toISOString(); if (!obj.item.metadata) { var object = { metadata: { createdAt: now, updatedAt: now } }; _.merge(obj.item, object); } obj.item.metadata.updatedAt = now; this.upsert(this.db.collection(collection), obj._id, { _id: obj._id, item: obj.item }); return Promise.resolve(obj) .then(function (item) { return _this.saveCollection(collection); }) .then(function () { return obj; }); }; ForerunnerDbService.prototype.getSize = function (collections) { this.logger.info("getSize called"); this.logger.debug("getSize: params:", collections); var size = 0; for (var col in collections) { if (collections.hasOwnProperty(col)) { size += JSON.stringify(this.db.collection(collections[col]).find()).length; } } return size; }; ForerunnerDbService.prototype.upsert = function (collection, id, doc) { var _this = this; this.logger.info("upsert called"); this.logger.debug("upsert: params:", collection, id, doc); return this.loadCollection(collection) .then(function () { return _this.db.collection(collection).find({ _id: id }); }) .then(function (list) { var col = _this.db.collection(collection); if (list.length === 1) { col.update({ _id: id }, { $overwrite: { item: doc.item } }); } else { col.insert(doc); } return null; }) .then(function () { return _this.saveCollection(collection); }); }; ForerunnerDbService.prototype.loadCollection = function (name) { var _this = this; this.logger.info("loadCollection called"); this.logger.debug("loadCollection: params:", name); return new Promise(function (fulfill, reject) { if (_this.isCollectionScopeCurrent(name)) { _this.logger.debug("loadCollection: collection " + name + " is configured to skip loading"); fulfill(name); // skip loading } else { _this.logger.debug("loadCollection: loading collection " + name); _this.db.collection(name).load(function (err) { if (err) { reject(err); } else { fulfill(name); } }); } }); }; ForerunnerDbService.prototype.saveCollection = function (name) { var _this = this; this.logger.info("saveCollection called"); this.logger.debug("saveCollection: params:", name); return new Promise(function (fulfill, reject) { if (_this.isCollectionScopeCurrent(name)) { _this.logger.debug("saveCollection: collection " + name + " is configured to skip saving"); fulfill(name); // skip saving } else { _this.logger.debug("saveCollection: saving collection " + name); _this.db.collection(name).save(function (err) { if (err) { reject(err); } else { fulfill(name); } }); } }); }; ForerunnerDbService.prototype.makeid = function () { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)); } return text; }; ForerunnerDbService.prototype.isCollectionScopeCurrent = function (collection) { var included = _.includes(this.activeCollectionScopes, collection); this.logger.debug("isCollectionScopeCurrent: included:", included); return included; }; ForerunnerDbService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ ForerunnerDbService.ctorParameters = function () { return [ { type: ForerunnerDbSettings, }, { type: LogService, }, ]; }; return ForerunnerDbService; }(DocumentDbService)); export { ForerunnerDbService }; var ForerunnerDbSettings = /** @class */ (function () { function ForerunnerDbSettings(dbName) { if (dbName === void 0) { dbName = null; } this.dbName = dbName; } return ForerunnerDbSettings; }()); export { ForerunnerDbSettings };