Commit ff4cb431 authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

add postgresql engine on dist

parent 3ae93928
export { default as FileEngine } from "./file/FileEngine.js";
export { default as PostgreSqlEngine } from "./postgresql/PostgreSqlEngine.js";
......@@ -3,6 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileEngine = void 0;
exports.PostgreSqlEngine = exports.FileEngine = void 0;
var FileEngine_js_1 = require("./file/FileEngine.js"); // Language: typescript, note the extension, sould be .js! even if it is .ts for esm to work correctly
Object.defineProperty(exports, "FileEngine", { enumerable: true, get: function () { return __importDefault(FileEngine_js_1).default; } });
var PostgreSqlEngine_js_1 = require("./postgresql/PostgreSqlEngine.js");
Object.defineProperty(exports, "PostgreSqlEngine", { enumerable: true, get: function () { return __importDefault(PostgreSqlEngine_js_1).default; } });
import { AuditRecord, AuditEngine, DatabaseSettings } from '../../interfaces/index.js';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
* @class PostgreSqlEngine
* @implements AuditEngine
* @param {string} path - path to store the records
*/
export declare class PostgreSqlEngine implements AuditEngine {
#private;
/**
* @description constructor
* @param {DatabaseSettings} settings - settings for the database
* @param {string} table - table name
* @param {AuditRecord} columnNames - column names
* @memberof PostgreSqlEngine
*/
constructor(connectionString?: string, dbSettings?: DatabaseSettings);
/**
* @description Store a record in the database
* @param {AuditRecord} record - record to be stored
* @returns {AuditRecord} - the record stored
* @memberof FileEngine
* @method put
*/
put(record: AuditRecord): AuditRecord;
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns {AuditRecord}
* @memberof FileEngine
* @method get
*/
get(auditTransactionId: string): AuditRecord;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(): number;
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(): string;
}
export default PostgreSqlEngine;
"use strict";
//To check this engine you have to have a PostgreSQL database installed and running.
//The database must be configured to allow connections from the host that the application is running on.
//The database must also have the following table:
//table audit_records
// auditUnit: varchar(255);
// auditTransactionId: varchar(255);
// auditProtocol: varchar(255);
// auditTransactionDate: varchar(20);
// auditUserIp: varchar(16);
// auditUserId: varchar(255);
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _PostgreSqlEngine_table, _PostgreSqlEngine_columnNames, _PostgreSqlEngine_client;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgreSqlEngine = void 0;
//You can use Docker to run PostgreSQL for your tests.
//`docker run -d --name audit-postgres -e POSTGRES_PASSWORD=audit --network host postgres`
//`docker exec -it audit-postgres psql -U postgres -h localhost -c "CREATE DATABASE audit;"`
//`docker exec -it audit-postgres psql -U postgres -h localhost -c "CREATE TABLE audit_log (auditUnit varchar(255), auditTransactionId varchar(255), auditProtocol varchar(255), auditTransactionDate varchar(20), auditUserIp varchar(16), auditUserId varchar(255));"`
//You can also map the fields of already existent table to the fields in the audit log.
const pg_native_1 = __importDefault(require("pg-native"));
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
* @class PostgreSqlEngine
* @implements AuditEngine
* @param {string} path - path to store the records
*/
class PostgreSqlEngine {
/**
* @description constructor
* @param {DatabaseSettings} settings - settings for the database
* @param {string} table - table name
* @param {AuditRecord} columnNames - column names
* @memberof PostgreSqlEngine
*/
constructor(connectionString = "", dbSettings = {}) {
_PostgreSqlEngine_table.set(this, void 0);
_PostgreSqlEngine_columnNames.set(this, void 0);
_PostgreSqlEngine_client.set(this, void 0);
const connection = connectionString != "" ? { connectionString } : {};
__classPrivateFieldSet(this, _PostgreSqlEngine_table, dbSettings.tableName || "audit_records", "f");
__classPrivateFieldSet(this, _PostgreSqlEngine_columnNames, dbSettings.columns || {
auditUnit: "auditUnit",
auditTransactionId: "auditTransactionId",
auditProtocol: "auditProtocol",
auditTransactionDate: "auditTransactionDate",
auditUserIp: "auditUserIp",
}, "f");
__classPrivateFieldSet(this, _PostgreSqlEngine_client, new pg_native_1.default(connection), "f");
__classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").connectSync();
if (!dbSettings.tableName) {
__classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("CREATE TABLE IF NOT EXISTS " + __classPrivateFieldGet(this, _PostgreSqlEngine_table, "f") + " (" + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUnit + " varchar(255), " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditTransactionId + " varchar(255), " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditProtocol + " varchar(255), " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditTransactionDate + " varchar(20), " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUserIp + " varchar(16), " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUserId + " varchar(255));");
}
}
/**
* @description Store a record in the database
* @param {AuditRecord} record - record to be stored
* @returns {AuditRecord} - the record stored
* @memberof FileEngine
* @method put
*/
put(record) {
const data = JSON.stringify(record, null, 2);
try {
__classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("INSERT INTO " + __classPrivateFieldGet(this, _PostgreSqlEngine_table, "f") + " (" + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUnit + "," + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditTransactionId + "," + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditProtocol + "," + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditTransactionDate + "," + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUserIp + "," + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditUserId + ") \
VALUES ('" + record.auditUnit + "','" + record.auditTransactionId + "','" + record.auditProtocol + "','" + record.auditTransactionDate + "','" + record.auditUserIp + "','" + record.auditUserId + "');");
return record;
}
catch (error) {
throw error;
}
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns {AuditRecord}
* @memberof FileEngine
* @method get
*/
get(auditTransactionId) {
try {
let data = {};
const res = __classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("SELECT * FROM " + __classPrivateFieldGet(this, _PostgreSqlEngine_table, "f") + " WHERE " + __classPrivateFieldGet(this, _PostgreSqlEngine_columnNames, "f").auditTransactionId + "='" + auditTransactionId + "';");
data = res.rows[0];
return data;
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq() {
try {
__classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("CREATE SEQUENCE IF NOT EXISTS " + __classPrivateFieldGet(this, _PostgreSqlEngine_table, "f") + "_seq START 1");
const res = __classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("SELECT nextval('" + __classPrivateFieldGet(this, _PostgreSqlEngine_table, "f") + "_seq');");
return res[0].nextval;
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn() {
try {
const protocol_date = new Date().toISOString().split('T')[0];
const seqName = "SEQ" + protocol_date.replace(/-/g, '');
//create sequence for protocol_date if not exists
__classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("CREATE SEQUENCE IF NOT EXISTS " + seqName + "_seq START 1");
const res = __classPrivateFieldGet(this, _PostgreSqlEngine_client, "f").querySync("SELECT nextval('" + seqName + "_seq');");
//TODO delete past protocol_date sequences
return res[0].nextval + "/" + protocol_date;
}
catch (error) {
throw error;
}
}
}
exports.PostgreSqlEngine = PostgreSqlEngine;
_PostgreSqlEngine_table = new WeakMap(), _PostgreSqlEngine_columnNames = new WeakMap(), _PostgreSqlEngine_client = new WeakMap();
exports.default = PostgreSqlEngine;
import { AuditRecord } from './interfaces/index.js';
import { FileEngine } from './engines/index.js';
import { AuditRecord, AuditEngine } from './interfaces/index.js';
/**
* @description Use this on your app to generate and log the audit record
* @coment This is the main function of the application
......@@ -10,7 +9,7 @@ import { FileEngine } from './engines/index.js';
* @env env.HOSTIP - useful to pass the IP address of the end user automatically on docker enviroments
* @returns AuditRecord | null - The audit record generated or the one provided
*/
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: FileEngine) => AuditRecord | null;
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: AuditEngine | undefined) => AuditRecord | null;
export default generateAuditRecord;
export { FileEngine } from './engines/index.js';
export { AuditRecord };
......@@ -16,8 +16,9 @@ const index_js_1 = require("./engines/index.js");
* @env env.HOSTIP - useful to pass the IP address of the end user automatically on docker enviroments
* @returns AuditRecord | null - The audit record generated or the one provided
*/
const generateAuditRecord = (auditInit, dbEngine = new index_js_1.FileEngine("/tmp")) => {
const _db = new db_js_1.default(dbEngine);
const generateAuditRecord = (auditInit, dbEngine) => {
const dbe = dbEngine || new index_js_1.FileEngine("/tmp");
const _db = new db_js_1.default(dbe);
const auditUnit = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditUnit) || "gov.gr";
const auditTransactionId = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditTransactionId) || "" + _db.seq();
const auditProtocol = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditProtocol) || _db.pn();
......
......@@ -28,3 +28,10 @@ export declare type FS_ERROR = {
code: string;
message: string;
};
/**
* @description Schema to be used to store the audit records on real databases
*/
export declare type DatabaseSettings = {
tableName?: string;
columns?: AuditRecord;
};
export { default as FileEngine } from "./file/FileEngine.js";
export { default as PostgreSqlEngine } from "./postgresql/PostgreSqlEngine.js";
export { default as FileEngine } from "./file/FileEngine.js"; // Language: typescript, note the extension, sould be .js! even if it is .ts for esm to work correctly
export { default as PostgreSqlEngine } from "./postgresql/PostgreSqlEngine.js";
import { AuditRecord, AuditEngine, DatabaseSettings } from '../../interfaces/index.js';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
* @class PostgreSqlEngine
* @implements AuditEngine
* @param {string} path - path to store the records
*/
export declare class PostgreSqlEngine implements AuditEngine {
#private;
/**
* @description constructor
* @param {DatabaseSettings} settings - settings for the database
* @param {string} table - table name
* @param {AuditRecord} columnNames - column names
* @memberof PostgreSqlEngine
*/
constructor(connectionString?: string, dbSettings?: DatabaseSettings);
/**
* @description Store a record in the database
* @param {AuditRecord} record - record to be stored
* @returns {AuditRecord} - the record stored
* @memberof FileEngine
* @method put
*/
put(record: AuditRecord): AuditRecord;
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns {AuditRecord}
* @memberof FileEngine
* @method get
*/
get(auditTransactionId: string): AuditRecord;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(): number;
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(): string;
}
export default PostgreSqlEngine;
//To check this engine you have to have a PostgreSQL database installed and running.
//The database must be configured to allow connections from the host that the application is running on.
//The database must also have the following table:
//table audit_records
// auditUnit: varchar(255);
// auditTransactionId: varchar(255);
// auditProtocol: varchar(255);
// auditTransactionDate: varchar(20);
// auditUserIp: varchar(16);
// auditUserId: varchar(255);
//You can use Docker to run PostgreSQL for your tests.
//`docker run -d --name audit-postgres -e POSTGRES_PASSWORD=audit --network host postgres`
//`docker exec -it audit-postgres psql -U postgres -h localhost -c "CREATE DATABASE audit;"`
//`docker exec -it audit-postgres psql -U postgres -h localhost -c "CREATE TABLE audit_log (auditUnit varchar(255), auditTransactionId varchar(255), auditProtocol varchar(255), auditTransactionDate varchar(20), auditUserIp varchar(16), auditUserId varchar(255));"`
//You can also map the fields of already existent table to the fields in the audit log.
import Client from 'pg-native';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
* @class PostgreSqlEngine
* @implements AuditEngine
* @param {string} path - path to store the records
*/
export class PostgreSqlEngine {
#table;
#columnNames;
#client;
/**
* @description constructor
* @param {DatabaseSettings} settings - settings for the database
* @param {string} table - table name
* @param {AuditRecord} columnNames - column names
* @memberof PostgreSqlEngine
*/
constructor(connectionString = "", dbSettings = {}) {
const connection = connectionString != "" ? { connectionString } : {};
this.#table = dbSettings.tableName || "audit_records";
this.#columnNames = dbSettings.columns || {
auditUnit: "auditUnit",
auditTransactionId: "auditTransactionId",
auditProtocol: "auditProtocol",
auditTransactionDate: "auditTransactionDate",
auditUserIp: "auditUserIp",
};
this.#client = new Client(connection);
this.#client.connectSync();
if (!dbSettings.tableName) {
this.#client.querySync("CREATE TABLE IF NOT EXISTS " + this.#table + " (" + this.#columnNames.auditUnit + " varchar(255), " + this.#columnNames.auditTransactionId + " varchar(255), " + this.#columnNames.auditProtocol + " varchar(255), " + this.#columnNames.auditTransactionDate + " varchar(20), " + this.#columnNames.auditUserIp + " varchar(16), " + this.#columnNames.auditUserId + " varchar(255));");
}
}
/**
* @description Store a record in the database
* @param {AuditRecord} record - record to be stored
* @returns {AuditRecord} - the record stored
* @memberof FileEngine
* @method put
*/
put(record) {
const data = JSON.stringify(record, null, 2);
try {
this.#client.querySync("INSERT INTO " + this.#table + " (" + this.#columnNames.auditUnit + "," + this.#columnNames.auditTransactionId + "," + this.#columnNames.auditProtocol + "," + this.#columnNames.auditTransactionDate + "," + this.#columnNames.auditUserIp + "," + this.#columnNames.auditUserId + ") \
VALUES ('" + record.auditUnit + "','" + record.auditTransactionId + "','" + record.auditProtocol + "','" + record.auditTransactionDate + "','" + record.auditUserIp + "','" + record.auditUserId + "');");
return record;
}
catch (error) {
throw error;
}
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns {AuditRecord}
* @memberof FileEngine
* @method get
*/
get(auditTransactionId) {
try {
let data = {};
const res = this.#client.querySync("SELECT * FROM " + this.#table + " WHERE " + this.#columnNames.auditTransactionId + "='" + auditTransactionId + "';");
data = res.rows[0];
return data;
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq() {
try {
this.#client.querySync("CREATE SEQUENCE IF NOT EXISTS " + this.#table + "_seq START 1");
const res = this.#client.querySync("SELECT nextval('" + this.#table + "_seq');");
return res[0].nextval;
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn() {
try {
const protocol_date = new Date().toISOString().split('T')[0];
const seqName = "SEQ" + protocol_date.replace(/-/g, '');
//create sequence for protocol_date if not exists
this.#client.querySync("CREATE SEQUENCE IF NOT EXISTS " + seqName + "_seq START 1");
const res = this.#client.querySync("SELECT nextval('" + seqName + "_seq');");
//TODO delete past protocol_date sequences
return res[0].nextval + "/" + protocol_date;
}
catch (error) {
throw error;
}
}
}
export default PostgreSqlEngine;
import { AuditRecord } from './interfaces/index.js';
import { FileEngine } from './engines/index.js';
import { AuditRecord, AuditEngine } from './interfaces/index.js';
/**
* @description Use this on your app to generate and log the audit record
* @coment This is the main function of the application
......@@ -10,7 +9,7 @@ import { FileEngine } from './engines/index.js';
* @env env.HOSTIP - useful to pass the IP address of the end user automatically on docker enviroments
* @returns AuditRecord | null - The audit record generated or the one provided
*/
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: FileEngine) => AuditRecord | null;
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: AuditEngine | undefined) => AuditRecord | null;
export default generateAuditRecord;
export { FileEngine } from './engines/index.js';
export { AuditRecord };
......@@ -10,8 +10,9 @@ import { FileEngine } from './engines/index.js';
* @env env.HOSTIP - useful to pass the IP address of the end user automatically on docker enviroments
* @returns AuditRecord | null - The audit record generated or the one provided
*/
export const generateAuditRecord = (auditInit, dbEngine = new FileEngine("/tmp")) => {
const _db = new db(dbEngine);
export const generateAuditRecord = (auditInit, dbEngine) => {
const dbe = dbEngine || new FileEngine("/tmp");
const _db = new db(dbe);
const auditUnit = auditInit?.auditUnit || "gov.gr";
const auditTransactionId = auditInit?.auditTransactionId || "" + _db.seq();
const auditProtocol = auditInit?.auditProtocol || _db.pn();
......
......@@ -28,3 +28,10 @@ export declare type FS_ERROR = {
code: string;
message: string;
};
/**
* @description Schema to be used to store the audit records on real databases
*/
export declare type DatabaseSettings = {
tableName?: string;
columns?: AuditRecord;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment