UNPKG

typecql

Version:

ORM for CQL databases.

149 lines 6.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateFactory = void 0; const formatKeysWithSettings_1 = require("../misc/functions/formating/formatKeysWithSettings"); const specialWhere_1 = require("../misc/classes/specialWhere"); const specialSet_1 = require("../misc/classes/specialSet"); class UpdateFactory { constructor(tableName, settings) { this.tableName = tableName; this.settings = settings; } createUpdateQuery(parameters, partialObject) { const set = this.createUpdateFieldsQuery(partialObject); const usingQuery = this.createUsingQuery(parameters?.ttl, parameters?.timestamp); const where = this.createWhereQuery(parameters?.['where']); const conditions = this.createIFQuery(parameters?.['if']); const allowFiltering = parameters.allowFiltering === true ? 'ALLOW FILTERING' : ''; return `UPDATE "${this.tableName}" ${usingQuery} ${set} ${where} ${conditions} ${allowFiltering};`; } createUsingQuery(ttl, timestamp) { if (typeof ttl !== 'undefined' && typeof timestamp !== 'undefined') { return `USING TTL ${ttl} AND TIMESTAMP ${timestamp}`; } else if (typeof timestamp !== 'undefined') { return `USING TIMESTAMP ${timestamp}`; } else if (typeof ttl !== 'undefined') { return `USING TTL ${ttl}`; } return ''; } createUpdateFieldsQuery(fields = {}) { const entries = Object.entries(fields); if (entries.length === 0) { throw new Error('[TYPECQL] - No fields were provided to update!'); } const chosenColumns = []; entries.forEach(([key, value]) => { if (key.includes('[') && key.includes(']')) { const index = key.indexOf('['); const realKey = key.slice(0, index); const additionalData = key.slice(index); const newKey = `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: realKey, forQuery: false, tableName: this.tableName, connSettings: this.settings, })}${additionalData}`; //todo здесь проверить на кавычки! const newValue = value instanceof specialSet_1.SpecialSet ? value.formatSet(newKey) : '?'; return chosenColumns.push(`${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: realKey, forQuery: true, tableName: this.tableName, connSettings: this.settings, })}${additionalData} = ${newValue}`); } const newValue = value instanceof specialSet_1.SpecialSet ? value.formatSet((0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })) : '?'; return chosenColumns.push(`${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })} = ${newValue}`); }); return `SET ${chosenColumns.join(`, `)}`; } createWhereQuery(where = {}) { if (Object.keys(where)?.length === 0) return ''; const statements = Object.entries(where).map(([key, value]) => { if (value instanceof specialWhere_1.SpecialWhere) { return `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })} ${value.structure}`; } else if (value instanceof specialWhere_1.SpecialWhereAnd) { const fillArray = []; value?.specials?.forEach((special) => { fillArray.push((0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, }) + ' ' + (special?.['structure'] ? special['structure'] : '= ?')); }); return fillArray.join(' AND '); } return `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })} = ?`; }); return 'WHERE ' + statements.join(' AND '); } createIFQuery(parameters = {}) { if (parameters?.['exists'] === true) return 'IF EXISTS'; if (!parameters || Object.keys(parameters)?.length === 0) return ''; const statements = Object.entries(parameters).map(([key, value]) => { if (value instanceof specialWhere_1.SpecialWhere) { return `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })} ${value.structure}`; } else if (value instanceof specialWhere_1.SpecialWhereAnd) { const fillArray = []; value?.specials?.forEach((special) => { fillArray.push((0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, }) + ' ' + (special?.['structure'] ? special['structure'] : '= ?')); }); return fillArray.join(' AND '); } return `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: key, forQuery: true, tableName: this.tableName, connSettings: this.settings, })} = ?`; }); return 'IF ' + statements.join(' AND '); } } exports.UpdateFactory = UpdateFactory; //# sourceMappingURL=update.factory.js.map