@szegedsw/lib-node
Version:
A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.
97 lines • 4.47 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Database = void 0;
const knex_1 = __importDefault(require("knex"));
const logger_1 = require("../logger/logger");
class Database {
static init(config) {
logger_1.Logger.info("Initializing database module...");
Database.Connector = knex_1.default(config);
// - Database.Connector.on("query", (action) => {
// - Logger.trace("query string: " + action.sql + "\nbindings: " + action.bindings, true);
// });
logger_1.Logger.info("Database module loaded.");
return Database.Connector;
}
/**
* @method table_create used to create a table in database.
* Table will be created only if it does not exist. If you want to create the table anyway, use the `forceDrop` flag.
* @param tablename table name in database
* @param creator this is the table creator function
* @param forceDrop Force table drop if it exists then create it. If it is set to false, table won't be created if already exists.
*/
static async table_create(tablename, creator, forceDrop = false) {
const res = await Database.Connector.schema.hasTable(tablename).then((succ) => succ, (err) => err);
if (typeof res === "boolean" && res) {
if (forceDrop) {
await Database.Connector.schema.dropTable(tablename).then(() => {
logger_1.Logger.info(`Table was dropped successfully: ${tablename}`);
}, (err) => {
logger_1.Logger.error(`Failed to drop table: ${tablename} See below for details.\n\n${err}`, "table_force_drop", true);
});
}
}
if ((typeof res === "boolean" && !res) || (typeof res === "boolean" && res && forceDrop)) {
await Database.Connector.schema
.createTable(tablename, (tb) => {
creator(tb);
})
.then(() => {
logger_1.Logger.info(`Table was createad successfully: ${tablename}`);
}, (error) => {
logger_1.Logger.error(`${tablename}(table) creation has failed! See below for details.\n\n${error}`, "table_create", true);
});
}
}
/**
* @method insert data into database table
* @param tablename tablename for insertion
* @param binds obje {column_name: value}
* @param checkUniqueness whether to check if row exists.
* "Duplicate key" error can be suppressed with it, because we try to insert only if it does not exists.
* @returns Promise is rejected on error. Promise resolves to true only if insertion was successful.
* Resolves to false only if param(checkUniqueness) was requested and insertion would be resulted in error.
*/
static async insert(tablename, binds, checkUniqueness = false) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
let count = 0;
if (checkUniqueness) {
const result = await Database.Connector(tablename)
.count()
.where(binds)
.then((succ) => {
var _a;
if (succ && succ.length > 0 && ((_a = succ[0]) === null || _a === void 0 ? void 0 : _a.count)) {
count = +succ[0].count;
}
return true;
}, (err) => {
logger_1.Logger.error(`${err}`, `${tablename}_insert_failed`, true);
return err;
});
if (typeof result === "string") {
reject(result);
}
}
if (!checkUniqueness || (checkUniqueness && count === 0)) {
await Database.Connector(tablename)
.insert(binds)
.then(() => {
resolve(true);
}, (err) => {
logger_1.Logger.error(`${err}`, `${tablename}_insert_failed`, true);
reject(err);
});
}
else {
resolve(false);
}
});
}
}
exports.Database = Database;
//# sourceMappingURL=database.js.map