UNPKG

@configurator/ravendb

Version:
164 lines 6.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentStoreBase = void 0; const events_1 = require("events"); const Exceptions_1 = require("../Exceptions"); const UriUtil_1 = require("../Utility/UriUtil"); const DocumentConventions_1 = require("./Conventions/DocumentConventions"); const IndexCreation_1 = require("./Indexes/IndexCreation"); const PutIndexesOperation_1 = require("./Operations/Indexes/PutIndexesOperation"); const DocumentSubscriptions_1 = require("./Subscriptions/DocumentSubscriptions"); const TypeUtil_1 = require("../Utility/TypeUtil"); const CaseInsensitiveKeysMap_1 = require("../Primitives/CaseInsensitiveKeysMap"); const TimeSeriesOperations_1 = require("./TimeSeries/TimeSeriesOperations"); const StringUtil_1 = require("../Utility/StringUtil"); class DocumentStoreBase extends events_1.EventEmitter { constructor() { super(); this._urls = []; this._lastRaftIndexPerDatabase = CaseInsensitiveKeysMap_1.CaseInsensitiveKeysMap.create(); this._eventHandlers = []; this._subscriptions = new DocumentSubscriptions_1.DocumentSubscriptions(this); } isDisposed() { return this._disposed; } executeIndex(task, database) { this.assertInitialized(); return task.execute(this, this.conventions, database); } async executeIndexes(tasks, database) { this.assertInitialized(); const indexesToAdd = IndexCreation_1.IndexCreation.createIndexesToAdd(tasks, this.conventions); await this.maintenance .forDatabase(this.getEffectiveDatabase(database)) .send(new PutIndexesOperation_1.PutIndexesOperation(...indexesToAdd)); } get timeSeries() { if (!this._timeSeriesOperation) { this._timeSeriesOperation = new TimeSeriesOperations_1.TimeSeriesOperations(this); } return this._timeSeriesOperation; } get conventions() { if (!this._conventions) { this._conventions = new DocumentConventions_1.DocumentConventions(); } return this._conventions; } set conventions(value) { this._assertNotInitialized("conventions"); this._conventions = value; } get urls() { return this._urls; } set urls(value) { this._assertNotInitialized("urls"); if (!value || !Array.isArray(value)) { (0, Exceptions_1.throwError)("InvalidArgumentException", `Invalid urls array passed: ${value.toString()}.`); } for (let i = 0; i < value.length; i++) { if (!value[i]) { (0, Exceptions_1.throwError)("InvalidArgumentException", `Url cannot be null or undefined - url index: ${i}`); } (0, UriUtil_1.validateUri)(value[i]); value[i] = value[i].replace(/\/$/, ""); } this._urls = value; } get subscriptions() { return this._subscriptions; } getLastTransactionIndex(database) { const index = this._lastRaftIndexPerDatabase.get(database); if (!index) { return null; } return index; } setLastTransactionIndex(database, index) { if (!index) { return; } const initialValue = this._lastRaftIndexPerDatabase.get(database); const result = TypeUtil_1.TypeUtil.isUndefined(initialValue) ? index : Math.max(initialValue, index); this._lastRaftIndexPerDatabase.set(database, result); } _ensureNotDisposed() { if (this._disposed) { (0, Exceptions_1.throwError)("InvalidOperationException", "The document store has already been disposed and cannot be used"); } } assertInitialized() { if (!this._initialized) { (0, Exceptions_1.throwError)("InvalidOperationException", "You cannot open a session or access the database commands before initializing the document store. " + "Did you forget calling initialize()?"); } } _assertNotInitialized(property) { if (this._initialized) { (0, Exceptions_1.throwError)("InvalidOperationException", "You cannot set '" + property + "' after the document store has been initialized."); } } get database() { return this._database; } set database(value) { this._assertNotInitialized("database"); this._database = value; } get authOptions() { return this._authOptions; } set authOptions(value) { this._assertNotInitialized("authOptions"); this._authOptions = value; } addSessionListener(eventName, eventHandler) { this._eventHandlers.push([eventName, eventHandler]); return this; } removeSessionListener(eventName, eventHandler) { const toRemove = this._eventHandlers .filter(x => x[0] === eventName && x[1] === eventHandler)[0]; if (toRemove) { this._eventHandlers.splice(this._eventHandlers.indexOf(toRemove), 1); } } registerEvents(requestExecutorOrSession) { this._eventHandlers.forEach(([eventName, eventHandler]) => { if (eventName === "failedRequest" || eventName === "topologyUpdated" || eventName === "beforeRequest" || eventName === "succeedRequest") { requestExecutorOrSession.on(eventName, eventHandler); } else { requestExecutorOrSession.on(eventName, eventHandler); } }); } _assertValidConfiguration() { this.conventions.validate(); } getEffectiveDatabase(database) { return DocumentStoreBase.getEffectiveDatabase(this, database); } static getEffectiveDatabase(store, database) { if (!database) { database = store.database; } if (!StringUtil_1.StringUtil.isNullOrWhitespace(database)) { return database; } (0, Exceptions_1.throwError)("InvalidArgumentException", "Cannot determine database to operate on. " + "Please either specify 'database' directly as an action parameter " + "or set the default database to operate on using 'DocumentStore.database' property. " + "Did you forget to pass 'database' parameter?"); } } exports.DocumentStoreBase = DocumentStoreBase; //# sourceMappingURL=DocumentStoreBase.js.map