UNPKG

typecql

Version:

ORM for CQL databases.

111 lines 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeleteFactory = void 0; const formatKeysWithSettings_1 = require("../misc/functions/formating/formatKeysWithSettings"); const specialWhere_1 = require("../misc/classes/specialWhere"); class DeleteFactory { constructor(tableName, settings) { this.tableName = tableName; this.settings = settings; } createDeleteQuery(parameters) { const select = this.createSelectQuery(parameters?.['fields']); const where = this.createWhereQuery(parameters?.['where']); const usingQuery = this.createUsingQuery(parameters?.timestamp); const conditions = this.createIFQuery(parameters?.['if']); const allowFiltering = parameters.allowFiltering === true ? 'ALLOW FILTERING' : ''; return `${select} ${usingQuery} ${where} ${conditions} ${allowFiltering};`; } createUsingQuery(timestamp) { if (!timestamp) return ''; return `USING TIMESTAMP ${timestamp}`; } createSelectQuery(fields) { if (!fields?.length || fields?.length === 0) return `DELETE FROM "${this.tableName}" `; const chosenColumns = fields .map((item) => `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({ key: item, forQuery: true, tableName: this.tableName, connSettings: this.settings, })}`) .join(', '); return `DELETE ${chosenColumns} FROM "${this.tableName}" `; } 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.DeleteFactory = DeleteFactory; //# sourceMappingURL=delete.factory.js.map