typecql
Version:
ORM for CQL databases.
87 lines • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryFactory = void 0;
const formatKeysWithSettings_1 = require("../misc/functions/formating/formatKeysWithSettings");
const specialWhere_1 = require("../misc/classes/specialWhere");
class QueryFactory {
constructor(tableName, settings) {
this.tableName = tableName;
this.settings = settings;
}
createCountQuery(whereFields) {
const where = this.whereFieldsFactory(whereFields);
return `SELECT COUNT(1) AS "count" FROM "${this.tableName}" ${where};`;
}
createQuery(selectFields, whereFields, limit, orderBy, allowFiltering) {
let query = this.selectFieldsFactory(selectFields) +
this.whereFieldsFactory(whereFields);
if (allowFiltering)
query += ' ALLOW FILTERING ';
if (orderBy && orderBy.length > 0)
query += ` ORDER BY ${orderBy
.map((item) => `${item.field.toString()} ${item.dir}`)
.join(', ')} `;
return query + ';';
}
selectFieldsFactory(selectFields) {
if (!selectFields?.length)
return `SELECT * FROM "${this.tableName}" `;
const selectQuery = 'SELECT ';
const formattedFields = selectFields.map((field) => {
if (field.alias) {
return `${(0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: field.field.toString(),
forQuery: true,
tableName: this.tableName,
connSettings: this.settings,
})} AS "${field.alias}"`;
}
else {
return (0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: field.field.toString(),
forQuery: true,
tableName: this.tableName,
connSettings: this.settings,
});
}
});
return (selectQuery + formattedFields.join(', ') + ` FROM ${this.tableName} `);
}
whereFieldsFactory(where) {
if (!where || 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 ');
}
}
exports.QueryFactory = QueryFactory;
//# sourceMappingURL=query.factory.js.map