typecql
Version:
ORM for CQL databases.
116 lines • 5.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionEntity = void 0;
const mergePrimaryKeysDefaults_1 = require("../misc/functions/formating/mergePrimaryKeysDefaults");
const objectValuesSafe_1 = require("../misc/functions/formating/objectValuesSafe");
const replaceUppersInObject_1 = require("../misc/functions/formating/replaceUppersInObject");
const specialWhere_1 = require("../misc/classes/specialWhere");
const specialSet_1 = require("../misc/classes/specialSet");
const getDefaultTableValues_1 = require("../misc/functions/formating/getDefaultTableValues");
class TransactionEntity {
constructor(tableName, batchQueries, writeFactory, deleteFactory, updateFactory, settings, client) {
this.tableName = tableName;
this.batchQueries = batchQueries;
this.writeFactory = writeFactory;
this.deleteFactory = deleteFactory;
this.updateFactory = updateFactory;
this.settings = settings;
this.client = client;
}
/**
* Transactional version of <a href='https://typecql.com/#Insert%20-%20insert'>insert</a> method.
* @param data object or array of objects to be inserted in database.
* @param options additional options.
* @link https://typecql.com/#Transactions
*/
insert(data, options) {
if (!Array.isArray(data)) {
const clonedData = structuredClone(data);
(0, getDefaultTableValues_1.getTableDefaultValuesObject)(clonedData, this.tableName);
(0, mergePrimaryKeysDefaults_1.mergePrimaryKeysDefaults)(clonedData, this.tableName);
const query = this.writeFactory.InsertQuery(clonedData, options);
this.batchQueries.push({ query, params: (0, objectValuesSafe_1.objectValuesSafe)(clonedData) });
return options?.returning
? (0, replaceUppersInObject_1.formatObjectWithSettings)(clonedData, this.tableName, this.settings, this.client)
: undefined;
}
const datas = structuredClone(data);
const result = datas.map((item) => {
(0, getDefaultTableValues_1.getTableDefaultValuesObject)(item, this.tableName);
(0, mergePrimaryKeysDefaults_1.mergePrimaryKeysDefaults)(item, this.tableName);
const query = this.writeFactory.InsertQuery(item, options);
this.batchQueries.push({ query, params: (0, objectValuesSafe_1.objectValuesSafe)(item) });
return options?.returning
? (0, replaceUppersInObject_1.formatObjectWithSettings)(item, this.tableName, this.settings, this.client)
: undefined;
});
return options?.returning ? result : undefined;
}
/**
* Transactional version of <a href='https://typecql.com/#Update%20-%20update'>update</a> method.
* @param parameters - update query parameters.
* @param partialObject object with fields as update values.
* @link https://typecql.com/#Transactions
*/
update(parameters, partialObject) {
const query = this.updateFactory.createUpdateQuery(parameters, partialObject);
const preparedValues = [];
for (const item of [
...(0, objectValuesSafe_1.objectValuesSafe)(partialObject),
...(0, objectValuesSafe_1.objectValuesSafe)(parameters?.where),
...(0, objectValuesSafe_1.objectValuesSafe)(parameters?.if),
]) {
if (item instanceof specialSet_1.SpecialSet) {
if (typeof item.values !== 'undefined') {
preparedValues.push(item.values);
}
}
else if (item instanceof specialWhere_1.SpecialWhere) {
if (Array.isArray(item.values)) {
preparedValues.push(...item.values);
continue;
}
if (typeof item.values !== 'undefined') {
preparedValues.push(item.values);
}
}
else {
if (typeof item !== 'undefined') {
preparedValues.push(item);
}
}
}
this.batchQueries.push({ query, params: preparedValues });
return;
}
/**
* Transactional version of <a href='https://typecql.com/#Delete%20-%20delete'>delete</a> method.
* @param parameters - delete query parameters.
* @link https://typecql.com/#Transactions
*/
delete(parameters) {
const preparedValues = [];
for (const item of [
...(0, objectValuesSafe_1.objectValuesSafe)(parameters?.where),
...(0, objectValuesSafe_1.objectValuesSafe)(parameters?.if),
]) {
if (item instanceof specialWhere_1.SpecialWhere) {
if (Array.isArray(item.values)) {
preparedValues.push(...item.values);
continue;
}
if (typeof item.values !== 'undefined') {
preparedValues.push(item.values);
continue;
}
}
if (typeof item !== 'undefined') {
preparedValues.push(item);
}
}
const query = this.deleteFactory.createDeleteQuery(parameters);
this.batchQueries.push({ query, params: preparedValues });
}
}
exports.TransactionEntity = TransactionEntity;
//# sourceMappingURL=transaction.entity.js.map