typecql
Version:
ORM for CQL databases.
174 lines • 6.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Entity = void 0;
const typecql_decorator_1 = require("../meta/typecql.decorator");
const formatKeysWithSettings_1 = require("../misc/functions/formating/formatKeysWithSettings");
const typecql_decorator_functions_1 = require("../meta/typecql.decorator.functions");
const mergePrimaryKeysDefaults_1 = require("../misc/functions/formating/mergePrimaryKeysDefaults");
const settings_1 = require("../misc/utils/settings");
const GlobalClient_1 = require("../misc/utils/GlobalClient");
const getDefaultTableValues_1 = require("../misc/functions/formating/getDefaultTableValues");
const cceMap = new WeakMap();
const clientMap = new WeakMap();
class Entity {
/**
* @internal
*/
getCCE() {
return cceMap.get(this);
}
/**
* @internal
*/
// eslint-disable-line
setCCE(value) {
cceMap.set(this, value);
}
/**
* @internal
*/
getClient() {
return clientMap.get(this);
}
/**
* @internal
*/
// eslint-disable-line
setClient(client) {
clientMap.set(this, client);
}
async save() {
const tableName = Reflect.getMetadata(typecql_decorator_1.TABLE_NAME_KEY, this.constructor)
?.name;
let client = this.getClient();
if (!client) {
client = GlobalClient_1.GlobalClients.clientsByName.get('default');
}
if (!client) {
throw new Error('[TYPECQL] - [ERROR]: No client found!');
}
let settings = this.getCCE();
if (!settings) {
settings = settings_1.GlobalSettings.get(client).camelCaseEnabled;
}
const primaryKeys = [];
const columns = (0, typecql_decorator_functions_1.getColumns)(tableName);
for (let [key, value] of Object.entries(columns)) {
if (value?.['isPrimary'] === true && !key.startsWith('incode:')) {
primaryKeys.push(key);
}
}
const newObj = Object.assign({}, this);
(0, getDefaultTableValues_1.getTableDefaultValuesObject)(newObj, tableName);
const whereKeys = [];
const whereValues = [];
const updateKeys = [];
const updateValues = [];
Object.entries(newObj).forEach(([key, value]) => {
this[key] = value;
if (primaryKeys.includes(key)) {
whereKeys.push(`${(0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: key,
forQuery: true,
tableName: tableName,
connSettings: {
camelCaseEnabled: settings,
},
})} = ?`);
whereValues.push(value);
}
else {
updateKeys.push(`${(0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: key,
forQuery: true,
tableName: tableName,
connSettings: {
camelCaseEnabled: settings,
},
})} = ?`);
updateValues.push(value);
}
});
const query = `UPDATE "${tableName}" SET ${updateKeys.join(', ')} WHERE ${whereKeys.join(' AND ')}`;
await client.execute(query, [...updateValues, ...whereValues], {
prepare: true,
});
}
async create() {
const tableName = Reflect.getMetadata(typecql_decorator_1.TABLE_NAME_KEY, this.constructor)
?.name;
let client = this.getClient();
if (!client) {
client = GlobalClient_1.GlobalClients.clientsByName.get('default');
}
let settings = this.getCCE();
if (!settings) {
settings = settings_1.GlobalSettings.get(client).camelCaseEnabled;
}
if (!client) {
throw new Error('[TYPECQL] - [ERROR]: No client found!');
}
const newObj = Object.assign({}, this);
(0, getDefaultTableValues_1.getTableDefaultValuesObject)(newObj, tableName);
(0, mergePrimaryKeysDefaults_1.mergePrimaryKeysDefaults)(newObj, tableName);
const insertKeys = [];
const insertValues = [];
Object.entries(newObj).forEach(([key, value]) => {
this[key] = value;
insertKeys.push((0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: key,
forQuery: true,
tableName: tableName,
connSettings: {
camelCaseEnabled: settings,
},
}));
insertValues.push(value);
});
const query = `INSERT INTO "${tableName}" (${insertKeys.join(', ')}) VALUES (${insertValues.map((_) => '?')})`;
await client.execute(query, [...insertValues], {
prepare: true,
});
}
async delete() {
const tableName = Reflect.getMetadata(typecql_decorator_1.TABLE_NAME_KEY, this.constructor)
?.name;
let client = this.getClient();
if (!client) {
client = GlobalClient_1.GlobalClients.clientsByName.get('default');
}
let settings = this.getCCE();
if (!settings) {
settings = settings_1.GlobalSettings.get(client).camelCaseEnabled;
}
if (!client) {
throw new Error('[TYPECQL] - [ERROR]: No client found!');
}
const primaryKeys = [];
const columns = (0, typecql_decorator_functions_1.getColumns)(tableName);
for (let [key, value] of Object.entries(columns)) {
if (value?.['isPrimary'] === true && !key.startsWith('incode:')) {
primaryKeys.push(key);
}
}
const queryUpdate = [];
const queryValues = [];
Object.entries(this).forEach(([key, value]) => {
if (!primaryKeys.includes(key.replace(/"/g, '')))
return;
queryUpdate.push(`${(0, formatKeysWithSettings_1.formatKeysWithSettings)({
key: key,
forQuery: true,
tableName: tableName,
connSettings: {
camelCaseEnabled: settings,
},
})} = ?`);
queryValues.push(value);
});
const query = `DELETE FROM "${tableName}" WHERE ${queryUpdate.join(' AND ')}`;
await client.execute(query, queryValues, { prepare: true });
}
}
exports.Entity = Entity;
//# sourceMappingURL=Entity.js.map