UNPKG

@platform/fsdb.mongo

Version:

Standard IDb abstraction over mongodb.

247 lines (246 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MongoStore = void 0; var tslib_1 = require("tslib"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var common_1 = require("../common"); var MongoClient = common_1.mongodb.MongoClient; var MongoStore = (function () { function MongoStore(args) { this._dispose$ = new rxjs_1.Subject(); this.dispose$ = this._dispose$.pipe(operators_1.share()); this.isConnected = false; var uri = args.uri; this.client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); this._args = args; if (args.connect) { this.ensureConnected(); } } MongoStore.create = function (args) { return new MongoStore(args); }; MongoStore.prototype.dispose = function () { this.client.close(); this.isConnected = false; this._dispose$.next(); this._dispose$.complete(); }; Object.defineProperty(MongoStore.prototype, "isDisposed", { get: function () { return this._dispose$.isStopped; }, enumerable: false, configurable: true }); MongoStore.prototype.ensureConnected = function () { var _this = this; this.throwIfDisposed('ensureConnected'); return new Promise(function (resolve, reject) { if (_this.isConnected) { return resolve(); } _this.client.connect(function (err) { if (err) { reject(err); } _this.db = _this.client.db(_this._args.db); _this.collection = _this.db.collection(_this._args.collection); _this.isConnected = true; resolve(); }); }); }; MongoStore.prototype.insert = function (doc, options) { if (options === void 0) { options = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.throwIfDisposed('insert'); return [4, this.insertMany([doc], options)]; case 1: return [2, (_a.sent())[0]]; } }); }); }; MongoStore.prototype.insertMany = function (docs, options) { var _this = this; if (options === void 0) { options = {}; } this.throwIfDisposed('insertMany'); return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); if (common_1.defaultValue(options.escapeKeys, true)) { docs = common_1.keys.encodeObjectKeys(docs); } this.collection.insertMany(docs, function (err, res) { if (err) { reject(err); } else { resolve(res.ops); } }); return [2]; } }); }); }); }; MongoStore.prototype.updateOne = function (query, update, options) { var _this = this; if (options === void 0) { options = {}; } this.throwIfDisposed('update'); return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); this.collection.updateOne(query, { $set: update }, options, function (err, res) { if (err) { reject(err); } else { var upsert = res.upsertedCount > 0; var modified = upsert ? res.upsertedCount > 0 : res.modifiedCount > 0; var doc = upsert ? tslib_1.__assign(tslib_1.__assign({}, update), { _id: res.upsertedId._id.toString() }) : undefined; resolve({ modified: modified, upsert: upsert, doc: doc }); } }); return [2]; } }); }); }); }; MongoStore.prototype.find = function (query) { var _this = this; this.throwIfDisposed('find'); return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var cursor, docs_1, error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); cursor = this.collection.find(query); docs_1 = []; return [4, cursor.forEach(function (doc) { return docs_1.push(doc); })]; case 3: _a.sent(); resolve(docs_1); return [3, 5]; case 4: error_1 = _a.sent(); reject(error_1); return [3, 5]; case 5: return [2]; } }); }); }); }; MongoStore.prototype.findOne = function (query) { var _this = this; this.throwIfDisposed('findOne'); return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); this.collection.findOne(query, function (err, res) { if (err) { reject(err); } else { var doc = res ? tslib_1.__assign(tslib_1.__assign({}, res), { _id: res._id.toString() }) : undefined; doc = common_1.keys.decodeObjectKeys(doc); resolve(doc); } }); return [2]; } }); }); }); }; MongoStore.prototype.remove = function (query, options) { var _this = this; if (options === void 0) { options = {}; } this.throwIfDisposed('remove'); return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var multi, single, error_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); multi = options.multi; single = !Boolean(multi); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4, this.collection.remove(query, { single: single })]; case 3: _a.sent(); resolve(); return [3, 5]; case 4: error_2 = _a.sent(); reject(error_2); return [3, 5]; case 5: return [2]; } }); }); }); }; MongoStore.prototype.drop = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var exists; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); return [4, this.exists()]; case 2: exists = _a.sent(); if (!exists) return [3, 4]; return [4, this.collection.drop()]; case 3: _a.sent(); _a.label = 4; case 4: return [2]; } }); }); }; MongoStore.prototype.exists = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var name; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.ensureConnected()]; case 1: _a.sent(); name = this._args.collection; return [4, this.db.collections()]; case 2: return [2, (_a.sent()).some(function (c) { return c.collectionName === name; })]; } }); }); }; MongoStore.prototype.throwIfDisposed = function (action) { if (this.isDisposed) { throw new Error("Cannot " + action + " because MongoStore is disposed."); } }; return MongoStore; }()); exports.MongoStore = MongoStore;