@jovian/type-tools
Version:
TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.
674 lines • 35.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpstreamDatastoreMongo = exports.MongoCode = void 0;
var common_iface_1 = require("../../../src/upstream/common.iface");
var enum_util_1 = require("../../../src/common/util/enum.util");
var upstream_1 = require("../../../src/upstream");
var MongoDB = __importStar(require("mongodb"));
var src_1 = require("../../../src");
var MongoCodeEnum;
(function (MongoCodeEnum) {
MongoCodeEnum[MongoCodeEnum["CONNECTION_ERROR"] = 0] = "CONNECTION_ERROR";
MongoCodeEnum[MongoCodeEnum["CREATE_ACK_FAIL"] = 1] = "CREATE_ACK_FAIL";
MongoCodeEnum[MongoCodeEnum["CREATE_CONTENTION_INDEX"] = 2] = "CREATE_CONTENTION_INDEX";
MongoCodeEnum[MongoCodeEnum["CREATE_ERROR"] = 3] = "CREATE_ERROR";
MongoCodeEnum[MongoCodeEnum["READ_NO_GID"] = 4] = "READ_NO_GID";
MongoCodeEnum[MongoCodeEnum["UPDATE_NO_GID"] = 5] = "UPDATE_NO_GID";
MongoCodeEnum[MongoCodeEnum["UPDATE_INDEX_CONTENTION"] = 6] = "UPDATE_INDEX_CONTENTION";
MongoCodeEnum[MongoCodeEnum["UPDATE_ERROR"] = 7] = "UPDATE_ERROR";
MongoCodeEnum[MongoCodeEnum["COLLECTION_NOT_FOUND"] = 8] = "COLLECTION_NOT_FOUND";
MongoCodeEnum[MongoCodeEnum["COLLECTION_NOT_FOUND_CACHED"] = 9] = "COLLECTION_NOT_FOUND_CACHED";
MongoCodeEnum[MongoCodeEnum["COLLECTION_FETCH_ERROR"] = 10] = "COLLECTION_FETCH_ERROR";
MongoCodeEnum[MongoCodeEnum["COLLECTION_CREATE_ERROR"] = 11] = "COLLECTION_CREATE_ERROR";
MongoCodeEnum[MongoCodeEnum["COLLECTION_FETCH_REJECT"] = 12] = "COLLECTION_FETCH_REJECT";
MongoCodeEnum[MongoCodeEnum["INDEX_CREATE_CONTENTION"] = 13] = "INDEX_CREATE_CONTENTION";
MongoCodeEnum[MongoCodeEnum["INDEX_CREATE_ERROR"] = 14] = "INDEX_CREATE_ERROR";
})(MongoCodeEnum || (MongoCodeEnum = {}));
exports.MongoCode = (0, enum_util_1.ReturnCodeFamily)('MongoCode', MongoCodeEnum);
var UpstreamDatastoreMongo = (function () {
function UpstreamDatastoreMongo(config) {
var _this = this;
this.knownCollections = {};
this.indexEnsuringPromise = null;
this.dbConnAwaiter = (0, src_1.promise)(function (resolve) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2, this.dbConnResolver = resolve];
}); }); });
this.admin = {
dropCollection: function (type) { return __awaiter(_this, void 0, void 0, function () {
var typename_1, collection, collections, hasCollection, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
typename_1 = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
return [4, this.ensureCollection(type, true)];
case 1:
collection = _a.sent();
return [4, this.dbconn.listCollections().toArray()];
case 2:
collections = _a.sent();
hasCollection = collections.filter(function (c) { return c.name === typename_1; }).length > 0;
if (!hasCollection) return [3, 4];
return [4, collection.drop()];
case 3:
_a.sent();
if (this.knownCollections[typename_1]) {
delete this.knownCollections[typename_1];
}
return [2, (0, enum_util_1.ok)(true)];
case 4: return [2, (0, enum_util_1.ok)(false)];
case 5:
e_1 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_1);
}
return [2, (0, enum_util_1.ok)(false)];
case 6: return [2];
}
});
}); }
};
this.index = {
checkDefinitions: function (type) {
var typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
var collectionInfo = _this.knownCollections[typename];
return {
definitions: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.indexDefinitions,
timeSet: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.timeIndexDefinitionSet,
timeUpdated: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.timeIndexUpdated,
};
},
setDefinitions: function (type, indexDefinitions) {
var typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
var collectionInfo = _this.knownCollections[typename];
if (!collectionInfo) {
collectionInfo = _this.knownCollections[typename] = {};
}
collectionInfo.indexDefinitions = indexDefinitions;
collectionInfo.timeIndexDefinitionSet = Date.now();
},
create: function (type, indexDefinition) { return __awaiter(_this, void 0, void 0, function () {
var collection, columnsCopy, partialFilterExpression, _i, _a, columnName, indexOptions, indexOptionsArg, e_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 3, , 4]);
return [4, this.ensureCollection(type, true)];
case 1:
collection = _b.sent();
if (!indexDefinition.columns) {
return [2, (0, enum_util_1.ok)(false)];
}
columnsCopy = (0, src_1.deepCopy)(indexDefinition.columns);
partialFilterExpression = {};
for (_i = 0, _a = Object.keys(columnsCopy); _i < _a.length; _i++) {
columnName = _a[_i];
partialFilterExpression[columnName] = { $type: 'string' };
if (columnsCopy[columnName]) {
columnsCopy[columnName] = 1;
}
else {
columnsCopy[columnName] = -1;
}
}
indexOptions = { name: indexDefinition.name };
indexOptionsArg = indexDefinition.options ? indexDefinition.options : {};
indexOptionsArg = (0, src_1.deepCopy)(indexOptionsArg);
if (indexOptionsArg.unique) {
indexOptions.unique = true;
indexOptions.partialFilterExpression = partialFilterExpression;
}
Object.assign(indexOptions, indexOptionsArg);
return [4, collection.createIndex(columnsCopy, indexOptions)];
case 2:
_b.sent();
return [2, (0, enum_util_1.ok)(true)];
case 3:
e_2 = _b.sent();
if (e_2.code === 11000) {
return [2, exports.MongoCode.error('INDEX_CREATE_CONTENTION', e_2)];
}
return [2, exports.MongoCode.error('INDEX_CREATE_ERROR', e_2)];
case 4: return [2];
}
});
}); },
delete: function (type, indexDefinition) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, (0, enum_util_1.ok)(true)];
});
}); },
list: function (type) { return __awaiter(_this, void 0, void 0, function () {
var collection, _a, e_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 3, , 4]);
return [4, this.ensureCollection(type, true)];
case 1:
collection = _b.sent();
_a = enum_util_1.ok;
return [4, collection.indexes()];
case 2: return [2, _a.apply(void 0, [_b.sent()])];
case 3:
e_3 = _b.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_3);
}
return [2, (0, enum_util_1.errorResult)(e_3)];
case 4: return [2];
}
});
}); },
ensure: function (type, indexDefinitions, forceRecheck) { return __awaiter(_this, void 0, void 0, function () {
var prom;
var _this = this;
return __generator(this, function (_a) {
if (this.indexEnsuringPromise) {
return [2, this.indexEnsuringPromise];
}
prom = this.indexEnsuringPromise = (0, src_1.promise)(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
var typename, collection, collectionInfo, proms, _i, _a, indexName, indexInfo, e_4;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 9, , 10]);
if (!!this.dbconn) return [3, 2];
return [4, this.dbConnAwaiter];
case 1:
_b.sent();
_b.label = 2;
case 2:
typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
return [4, this.ensureCollection(typename, true)];
case 3:
collection = _b.sent();
collectionInfo = this.knownCollections[typename];
if (!collectionInfo) {
collectionInfo = this.knownCollections[typename] = {};
}
if (indexDefinitions) {
collectionInfo.indexDefinitions = indexDefinitions;
forceRecheck = true;
}
if (!forceRecheck && collectionInfo.timeIndexUpdated) {
return [2, resolve((0, enum_util_1.ok)(true))];
}
if (!indexDefinitions) {
indexDefinitions = collectionInfo.indexDefinitions;
}
if (!indexDefinitions || Object.keys(indexDefinitions).length === 0) {
collectionInfo.timeIndexUpdated = Date.now();
return [2, resolve((0, enum_util_1.ok)(true))];
}
proms = [];
_i = 0, _a = Object.keys(indexDefinitions);
_b.label = 4;
case 4:
if (!(_i < _a.length)) return [3, 7];
indexName = _a[_i];
return [4, collection.indexExists(indexName)];
case 5:
if (_b.sent()) {
return [3, 6];
}
indexInfo = indexDefinitions[indexName];
proms.push(this.index.create(type, indexInfo));
_b.label = 6;
case 6:
_i++;
return [3, 4];
case 7: return [4, src_1.PromUtil.allSettled(proms)];
case 8:
_b.sent();
collectionInfo.timeIndexUpdated = Date.now();
return [2, resolve((0, enum_util_1.ok)(true))];
case 9:
e_4 = _b.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_4);
}
return [2, resolve((0, enum_util_1.errorResult)(e_4))];
case 10: return [2];
}
});
}); }).finally(function () {
if (prom === _this.indexEnsuringPromise) {
_this.indexEnsuringPromise = null;
}
});
return [2, prom];
});
}); }
};
if (!config.path) {
config.path = common_iface_1.defaultUpstreamPath;
}
this.config = config;
this.initialize();
}
UpstreamDatastoreMongo.prototype.read = function (type, _gid, _v) {
return __awaiter(this, void 0, void 0, function () {
var collection, parsed, result, resultArray, e_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
if (!_gid) {
return [2, exports.MongoCode.error('READ_NO_GID')];
}
return [4, this.ensureCollection(type)];
case 1:
collection = _a.sent();
parsed = (0, common_iface_1.parseGlobalId)(_gid);
return [4, collection.find({ _id: new MongoDB.ObjectId(parsed.localId) })];
case 2:
result = _a.sent();
return [4, result.toArray()];
case 3:
resultArray = _a.sent();
return [2, (0, enum_util_1.ok)(resultArray[0])];
case 4:
e_5 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_5);
}
return [2, (0, enum_util_1.errorResult)(e_5)];
case 5: return [2];
}
});
});
};
;
UpstreamDatastoreMongo.prototype.create = function (type, target, typeVersion) {
return __awaiter(this, void 0, void 0, function () {
var collection, typename, targetAny, result, localId, _gid, e_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4, this.ensureCollection(type)];
case 1:
collection = _a.sent();
typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
targetAny = target;
targetAny._id = new MongoDB.ObjectId();
targetAny._tfn = typename;
targetAny._tv = typeVersion;
targetAny._v = 1;
targetAny._ct = Date.now();
targetAny._ut = Date.now();
return [4, collection.insertOne(target)];
case 2:
result = _a.sent();
if (!result || !result.acknowledged) {
return [2, exports.MongoCode.error('CREATE_ACK_FAIL')];
}
localId = result.insertedId.toString();
_gid = (0, common_iface_1.getGlobalId)(targetAny._tfn, this.config.path, localId);
return [2, (0, enum_util_1.ok)(_gid)];
case 3:
e_6 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_6);
}
if (e_6.code === 11000) {
return [2, exports.MongoCode.error('CREATE_CONTENTION_INDEX', e_6)];
}
return [2, exports.MongoCode.error('CREATE_ERROR', e_6)];
case 4: return [2];
}
});
});
};
;
UpstreamDatastoreMongo.prototype.update = function (type, _gid, updater) {
return __awaiter(this, void 0, void 0, function () {
var collection, parsed, result, e_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
if (!_gid) {
return [2, exports.MongoCode.error('UPDATE_NO_GID')];
}
return [4, this.ensureCollection(type)];
case 1:
collection = _a.sent();
parsed = (0, common_iface_1.parseGlobalId)(_gid);
return [4, collection.updateOne({ _id: new MongoDB.ObjectId(parsed.localId) }, this.getUpdateRubric(updater))];
case 2:
result = _a.sent();
return [2, (0, enum_util_1.ok)(result.modifiedCount ? true : false)];
case 3:
e_7 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_7);
}
return [2, (0, enum_util_1.errorResult)(e_7)];
case 4: return [2];
}
});
});
};
;
UpstreamDatastoreMongo.prototype.delete = function (type, id) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, (0, enum_util_1.ok)(false)];
});
});
};
;
UpstreamDatastoreMongo.prototype.find = function (type, matcher, limit, indexName) {
if (limit === void 0) { limit = 0; }
return __awaiter(this, void 0, void 0, function () {
var collection, parsedMatcher, result, resultArray, e_8;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4, this.ensureCollection(type)];
case 1:
collection = _a.sent();
parsedMatcher = this.getMatcherRubric(matcher);
result = indexName ? collection.find(parsedMatcher).hint(indexName) : collection.find(parsedMatcher);
if (limit) {
result = result.limit(limit);
}
return [4, result.toArray()];
case 2:
resultArray = _a.sent();
return [2, (0, enum_util_1.ok)(resultArray)];
case 3:
e_8 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_8);
}
return [2, (0, enum_util_1.errorResult)(e_8)];
case 4: return [2];
}
});
});
};
;
UpstreamDatastoreMongo.prototype.list = function (type, filter) {
return __awaiter(this, void 0, void 0, function () {
var collection, e_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.ensureCollection(type)];
case 1:
collection = _a.sent();
return [3, 3];
case 2:
e_9 = _a.sent();
if (upstream_1.Upstream.showOperationErrors) {
console.error(e_9);
}
return [2, (0, enum_util_1.errorResult)(e_9)];
case 3: return [2];
}
});
});
};
;
UpstreamDatastoreMongo.prototype.getUpdateRubric = function (updater) {
var updateRubric = {};
updateRubric.$set = {};
updateRubric.$inc = {};
if (updater.set) {
for (var _i = 0, _a = Object.keys(updater.set); _i < _a.length; _i++) {
var path = _a[_i];
updateRubric.$set[path] = updater.set[path];
}
}
if (updater.add) {
for (var _b = 0, _c = Object.keys(updater.add); _b < _c.length; _b++) {
var path = _c[_b];
updateRubric.$inc[path] = updater.add[path];
}
}
if (!updateRubric.$set._gid) {
updateRubric.$set._ut = Date.now();
updateRubric.$inc._v = 1;
}
return updateRubric;
};
UpstreamDatastoreMongo.prototype.getMatcherRubric = function (matcher) {
return matcher;
};
UpstreamDatastoreMongo.prototype.connectToMongo = function (cred) {
return __awaiter(this, void 0, void 0, function () {
var userPass, hasPort, mongoUrl, client, db, e_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
userPass = cred.username && cred.password ? "".concat(encodeURIComponent(cred.username), ":").concat(encodeURIComponent(cred.password), "@") : '';
hasPort = cred.endpoint.indexOf(':') >= 0;
mongoUrl = hasPort ? "mongodb://".concat(userPass).concat(cred.endpoint) : "mongodb+srv://".concat(userPass).concat(cred.endpoint);
client = new MongoDB.MongoClient(mongoUrl);
return [4, client.connect()];
case 1:
_a.sent();
db = client.db(cred.dbname ? cred.dbname : common_iface_1.defaultUpstreamDatabaseName);
return [2, (0, enum_util_1.ok)({ client: client, db: db })];
case 2:
e_10 = _a.sent();
return [2, exports.MongoCode.error('CONNECTION_ERROR', e_10)];
case 3: return [2];
}
});
});
};
UpstreamDatastoreMongo.prototype.initialize = function () {
return __awaiter(this, void 0, void 0, function () {
var connectResult;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.connectToMongo(this.config.endpoint.credentials)];
case 1:
connectResult = _a.sent();
if (connectResult.bad) {
return [2, (0, enum_util_1.passthru)(connectResult)];
}
this.client = connectResult.data.client;
this.dbconn = connectResult.data.db;
return [4, this.ensureEndpointMeta(this.dbconn)];
case 2:
_a.sent();
this.dbConnResolver();
return [2];
}
});
});
};
UpstreamDatastoreMongo.prototype.ensureEndpointMeta = function (db) {
return __awaiter(this, void 0, void 0, function () {
var list, metadataTableAccounted, txTableAccounted, _i, list_1, tableInfo, collection, indexes, e_11, collection, indexes, e_12;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, db.listCollections().toArray()];
case 1:
list = _a.sent();
metadataTableAccounted = false;
txTableAccounted = false;
for (_i = 0, list_1 = list; _i < list_1.length; _i++) {
tableInfo = list_1[_i];
if (tableInfo.name.startsWith('__')) {
if (tableInfo.name === common_iface_1.defaultUpstreamMetadataTable) {
metadataTableAccounted = true;
}
if (tableInfo.name === common_iface_1.defaultUpstreamTxDataTable) {
txTableAccounted = true;
}
continue;
}
if (!this.knownCollections[tableInfo.name]) {
this.knownCollections[tableInfo.name] = { collection: this.dbconn.collection(tableInfo.name) };
}
}
if (!!metadataTableAccounted) return [3, 9];
_a.label = 2;
case 2:
_a.trys.push([2, 8, , 9]);
return [4, this.dbconn.createCollection(common_iface_1.defaultUpstreamMetadataTable)];
case 3:
collection = _a.sent();
return [4, collection.listIndexes()];
case 4:
indexes = _a.sent();
return [4, indexes.toArray()];
case 5:
if (!((_a.sent()).filter(function (item) { return item.name === 'last_insert_ids'; }).length === 0)) return [3, 7];
return [4, this.index.create(common_iface_1.defaultUpstreamMetadataTable, { name: 'last_insert_ids', options: { unique: 1 }, columns: { type_name: 1 } })];
case 6:
_a.sent();
_a.label = 7;
case 7: return [3, 9];
case 8:
e_11 = _a.sent();
if (e_11.codeName !== 'NamespaceExists') {
console.error(e_11);
}
return [3, 9];
case 9:
if (!!txTableAccounted) return [3, 17];
_a.label = 10;
case 10:
_a.trys.push([10, 16, , 17]);
return [4, this.dbconn.createCollection(common_iface_1.defaultUpstreamTxDataTable)];
case 11:
collection = _a.sent();
return [4, collection.listIndexes()];
case 12:
indexes = _a.sent();
return [4, indexes.toArray()];
case 13:
if (!((_a.sent()).filter(function (item) { return item.name === 'tx_flow'; }).length === 0)) return [3, 15];
return [4, this.index.create(common_iface_1.defaultUpstreamTxDataTable, { name: 'tx_flow', options: {}, columns: { tx_id: 1 } })];
case 14:
_a.sent();
_a.label = 15;
case 15: return [3, 17];
case 16:
e_12 = _a.sent();
if (e_12.codeName !== 'NamespaceExists') {
console.error(e_12);
}
return [3, 17];
case 17: return [2, (0, enum_util_1.ok)(true)];
}
});
});
};
UpstreamDatastoreMongo.prototype.ensureCollection = function (type, skipIndexCheck) {
if (skipIndexCheck === void 0) { skipIndexCheck = false; }
return __awaiter(this, void 0, void 0, function () {
var typename, collectionInfo, _a, e_13;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!!this.dbconn) return [3, 2];
return [4, this.dbConnAwaiter];
case 1:
_b.sent();
_b.label = 2;
case 2:
typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type);
collectionInfo = this.knownCollections[typename];
if (!collectionInfo) {
collectionInfo = this.knownCollections[typename] = {};
}
if (!(!skipIndexCheck && collectionInfo.indexDefinitions && !collectionInfo.timeIndexUpdated)) return [3, 4];
return [4, this.index.ensure(type)];
case 3:
_b.sent();
_b.label = 4;
case 4:
if (collectionInfo.collection) {
return [2, collectionInfo.collection];
}
_b.label = 5;
case 5:
_b.trys.push([5, 7, , 8]);
_a = collectionInfo;
return [4, this.dbconn.createCollection(typename)];
case 6:
_a.collection = _b.sent();
return [3, 8];
case 7:
e_13 = _b.sent();
collectionInfo.collection = this.dbconn.collection(typename);
return [3, 8];
case 8: return [2, collectionInfo.collection];
}
});
});
};
return UpstreamDatastoreMongo;
}());
exports.UpstreamDatastoreMongo = UpstreamDatastoreMongo;
//# sourceMappingURL=mongodb.js.map