Commit 6aa372e4 authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

refactoring to allow new db engine with params

parent d3903331
......@@ -11,7 +11,7 @@ Also provides a way for automating protocol numbers and transaction Id's, if you
import auditRecordDB from 'AuditRecordDB';
const main = () =>{
//returns generated AuditRecord, that record stored by default in `/tmp` folder SOULD change this for production
//returns generated AuditRecord, that record stored by default in `/tmp` folder using `FileEngine` SOULD change this for production
console.log(auditRecordDB({auditUnit:'DigiGov'}))
/*
{
......@@ -27,6 +27,6 @@ const main = () =>{
```
# AuditEngine
For now only file system storage engine is implement, but you can extend to store to 'real' database, look at `FileEngine.ts` for exaple.
For now only file system storage engine is implement, but you can extend to store to 'real' database, look at `FileEngine.ts` for example.
import { AuditRecord, AuditEngine } from '../interfaces/index.js';
import { AuditRecord, AuditEngine } from '../../interfaces/index.js';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
......@@ -25,5 +25,21 @@ export declare class FileEngine implements AuditEngine {
* @method get
*/
get(auditTransactionId: string): AuditRecord;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(path?: string): number;
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(path?: string): string;
}
export default FileEngine;
......@@ -18,6 +18,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.FileEngine = void 0;
//Use File System as DB storage
const fs_1 = __importDefault(require("fs"));
const protocol_js_1 = __importDefault(require("./protocol.js"));
const sequence_js_1 = __importDefault(require("./sequence.js"));
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
......@@ -63,6 +65,38 @@ class FileEngine {
throw error;
}
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(path) {
const sequence_save_path = path || __classPrivateFieldGet(this, _FileEngine_path, "f");
try {
return (0, sequence_js_1.default)(sequence_save_path + "/sequence", sequence_save_path + "/sequence");
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(path) {
const protocol_save_path = path || __classPrivateFieldGet(this, _FileEngine_path, "f");
try {
return (0, protocol_js_1.default)(protocol_save_path);
}
catch (error) {
throw error;
}
}
}
exports.FileEngine = FileEngine;
_FileEngine_path = new WeakMap();
......
export { default as FileEngine } from "./file/FileEngine.js";
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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; } });
import { AuditRecord } from './interfaces/index.js';
import { FileEngine } from './engines/index.js';
/**
* @description Use this on your app to generate and log the audit record
* @coment This is the main function of the application
* @comment The audit record is stored in the database,
* @comment you can provide a custom audit record, or use the generated one
* @param auditInit - The audit record to be stored, if is empty a new one will be generated
* @param storagePath - The path where the audit record will be stored, default is the tmp directory
* @param storagePath - The path where the audit record will be stored, default is the tmp directory SOULD change this for production
* @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, storagePath?: string) => AuditRecord | null;
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: FileEngine) => AuditRecord | null;
export default generateAuditRecord;
export { AuditRecord };
......@@ -4,24 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAuditRecord = void 0;
const protocol_js_1 = __importDefault(require("./lib/protocol.js"));
const sequence_js_1 = __importDefault(require("./lib/sequence.js"));
const db_js_1 = __importDefault(require("./lib/db.js"));
const FileEngine_js_1 = __importDefault(require("./lib/FileEngine.js"));
const index_js_1 = require("./engines/index.js");
/**
* @description Use this on your app to generate and log the audit record
* @coment This is the main function of the application
* @comment The audit record is stored in the database,
* @comment you can provide a custom audit record, or use the generated one
* @param auditInit - The audit record to be stored, if is empty a new one will be generated
* @param storagePath - The path where the audit record will be stored, default is the tmp directory
* @param storagePath - The path where the audit record will be stored, default is the tmp directory SOULD change this for production
* @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, storagePath = "/tmp") => {
const generateAuditRecord = (auditInit, dbEngine = new index_js_1.FileEngine("/tmp")) => {
const _db = new db_js_1.default(dbEngine);
const auditUnit = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditUnit) || "gov.gr";
const auditTransactionId = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditTransactionId) || "" + (0, sequence_js_1.default)(storagePath + "/sequence", storagePath + "/sequence");
const auditProtocol = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditProtocol) || (0, protocol_js_1.default)(storagePath);
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();
const auditTransactionDate = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditTransactionDate) || new Date().toISOString().split('.')[0] + "Z";
const auditUserIp = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditUserIp) || process.env.HOSTIP || "127.0.0.1";
const auditUserId = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditUserId) || "system";
......@@ -33,9 +32,8 @@ const generateAuditRecord = (auditInit, storagePath = "/tmp") => {
auditUserIp,
auditUserId
};
const dbEngine = new db_js_1.default(new FileEngine_js_1.default(storagePath));
try {
return dbEngine.put(auditRecord);
return _db.put(auditRecord);
}
catch (error) {
const err = error;
......
......@@ -18,6 +18,8 @@ export declare type AuditRecord = {
export interface AuditEngine {
put: (record: AuditRecord) => AuditRecord;
get: (auditTransactionId: string) => AuditRecord;
seq: (path?: string) => number;
pn: (path?: string) => string;
}
/**
* @description FileSystem errors
......
......@@ -24,5 +24,21 @@ export declare class db {
* @method get
*/
get(auditTransactionId: string): AuditRecord;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof db
* @method seq
*/
seq(path?: string): number;
/**
* @description Generate a new protocol string
* @param path
* @returns string
* @memberof db
* @method pn
*/
pn(path?: string): string;
}
export default db;
......@@ -35,6 +35,26 @@ class db {
throw new Error("auditTransactionId is required");
return this.engine.get(auditTransactionId);
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof db
* @method seq
*/
seq(path) {
return this.engine.seq(path);
}
/**
* @description Generate a new protocol string
* @param path
* @returns string
* @memberof db
* @method pn
*/
pn(path) {
return this.engine.pn(path);
}
}
exports.db = db;
exports.default = db;
import { AuditRecord, AuditEngine } from '../interfaces/index.js';
import { AuditRecord, AuditEngine } from '../../interfaces/index.js';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
......@@ -25,5 +25,21 @@ export declare class FileEngine implements AuditEngine {
* @method get
*/
get(auditTransactionId: string): AuditRecord;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(path?: string): number;
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(path?: string): string;
}
export default FileEngine;
//Use File System as DB storage
import fs from 'fs';
import protocol from './protocol.js';
import sequence from './sequence.js';
/**
* @description AuditEngine implementation
* @note This class is used to implement the methods that must be implemented by the AuditEngine
......@@ -45,5 +47,37 @@ export class FileEngine {
throw error;
}
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @memberof FileEngine
* @method seq
*/
seq(path) {
const sequence_save_path = path || this.#path;
try {
return sequence(sequence_save_path + "/sequence", sequence_save_path + "/sequence");
}
catch (error) {
throw error;
}
}
/**
* @description Generate a new protocol number
* @param path
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(path) {
const protocol_save_path = path || this.#path;
try {
return protocol(protocol_save_path);
}
catch (error) {
throw error;
}
}
}
export default FileEngine;
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