UNPKG

typecql

Version:

ORM for CQL databases.

59 lines 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Transaction = void 0; const RegisteredEntities_1 = require("../misc/utils/RegisteredEntities"); const typecql_decorator_functions_1 = require("../meta/typecql.decorator.functions"); const transaction_entity_1 = require("./transaction.entity"); const settings_1 = require("../misc/utils/settings"); const GlobalFactories_1 = require("../factory/global/GlobalFactories"); class Transaction { constructor(client) { this.client = client; this.batchQueries = []; } /** * Of methods returns transaction instance of entity. * @param entity - registered entity with metadata. * @link https://typecql.com/#Transactions */ of(entity) { const repository = RegisteredEntities_1.RegisteredEntities.getRepository(entity); if (!repository) { throw new Error(`[TYPECQL] - Repository with table name ${(0, typecql_decorator_functions_1.getTableName)(entity)} was not provided or was registered with another connection!`); } const tableName = (0, typecql_decorator_functions_1.getTableName)(entity); const settings = settings_1.GlobalSettings.get(this.client); const { writeFactory, updateFactory, deleteFactory } = GlobalFactories_1.GlobalFactories.factories.get(this.client).get(tableName); return new transaction_entity_1.TransactionEntity(tableName, this.batchQueries, writeFactory, deleteFactory, updateFactory, settings, this.client); } /** * Method that executes transaction with provided queries and data. * @link https://typecql.com/#Transactions */ async executeTransaction(options = {}) { if (options?.experimental === true) { const queries = []; const params = []; for (const batchQuery of this.batchQueries) { queries.push(batchQuery.query); params.push(batchQuery.params); } const mergedQueries = `BEGIN ${options.isCounterBatch ? 'COUNTER' : ''} BATCH ${queries.join(' ')} APPLY BATCH`; const result = await this.client.execute(mergedQueries, params.flat(), { prepare: true, consistency: options?.consistency, }); return { wasApplied: result?.wasApplied() }; } const result = await this.client.batch(this.batchQueries, { prepare: true, counter: options?.isCounterBatch, consistency: options?.consistency, }); return { wasApplied: result?.wasApplied() }; } } exports.Transaction = Transaction; //# sourceMappingURL=base-transaction.js.map