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

Refactor to async due to raj highly demand

parent f5bb24a1
......@@ -9,7 +9,7 @@ import { AuditRecord, AuditEngine } from './interfaces/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?: AuditEngine | undefined) => AuditRecord | null;
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: AuditEngine | undefined) => Promise<AuditRecord | null>;
export default generateAuditRecord;
export { FileEngine, PostgreSqlEngine } from './engines/index.js';
export { AuditRecord, AuditEngine, FS_ERROR } from './interfaces/index.js';
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
......@@ -16,32 +25,32 @@ 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) => {
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();
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";
const auditRecord = {
auditUnit,
auditTransactionId,
auditProtocol,
auditTransactionDate,
auditUserIp,
auditUserId
};
const generateAuditRecord = (auditInit, dbEngine) => __awaiter(void 0, void 0, void 0, function* () {
try {
return _db.put(auditRecord);
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) || "" + (yield _db.seq());
const auditProtocol = (auditInit === null || auditInit === void 0 ? void 0 : auditInit.auditProtocol) || (yield _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";
const auditRecord = {
auditUnit,
auditTransactionId,
auditProtocol,
auditTransactionDate,
auditUserIp,
auditUserId
};
return yield _db.put(auditRecord);
}
catch (error) {
const err = error;
console.log(err.code, err.message);
return null;
}
};
});
exports.generateAuditRecord = generateAuditRecord;
exports.default = exports.generateAuditRecord;
var index_js_2 = require("./engines/index.js");
......
......@@ -16,10 +16,10 @@ export declare type AuditRecord = {
* @interface AuditEngine
*/
export interface AuditEngine {
put: (record: AuditRecord) => AuditRecord;
get: (auditTransactionId: string) => AuditRecord;
seq: (path?: string) => number;
pn: (path?: string) => string;
put: (record: AuditRecord) => AuditRecord | Promise<AuditRecord>;
get: (auditTransactionId: string) => AuditRecord | Promise<AuditRecord>;
seq: (path?: string) => number | Promise<number>;
pn: (path?: string) => string | Promise<string>;
}
/**
* @description FileSystem errors
......
......@@ -11,27 +11,27 @@ export declare class db {
/**
* @description Store a record in the database
* @param record
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method put
*/
put(record: AuditRecord): AuditRecord;
put(record: AuditRecord): Promise<AuditRecord>;
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method get
*/
get(auditTransactionId: string): AuditRecord;
get(auditTransactionId: string): Promise<AuditRecord>;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @returns Promise<number>
* @memberof db
* @method seq
*/
seq(path?: string): number;
seq(path?: string): Promise<number>;
/**
* @description Generate a new protocol string
* @param path
......@@ -39,6 +39,6 @@ export declare class db {
* @memberof db
* @method pn
*/
pn(path?: string): string;
pn(path?: string): Promise<string>;
}
export default db;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.db = void 0;
/**
......@@ -14,36 +23,42 @@ class db {
/**
* @description Store a record in the database
* @param record
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method put
*/
put(record) {
if (!record.auditTransactionId)
throw new Error("record.auditTransactionId is required");
return this.engine.put(record);
return __awaiter(this, void 0, void 0, function* () {
if (!record.auditTransactionId)
throw new Error("record.auditTransactionId is required");
return yield this.engine.put(record);
});
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method get
*/
get(auditTransactionId) {
if (!auditTransactionId)
throw new Error("auditTransactionId is required");
return this.engine.get(auditTransactionId);
return __awaiter(this, void 0, void 0, function* () {
if (!auditTransactionId)
throw new Error("auditTransactionId is required");
return yield this.engine.get(auditTransactionId);
});
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @returns Promise<number>
* @memberof db
* @method seq
*/
seq(path) {
return this.engine.seq(path);
return __awaiter(this, void 0, void 0, function* () {
return yield this.engine.seq(path);
});
}
/**
* @description Generate a new protocol string
......@@ -53,7 +68,9 @@ class db {
* @method pn
*/
pn(path) {
return this.engine.pn(path);
return __awaiter(this, void 0, void 0, function* () {
return yield this.engine.pn(path);
});
}
}
exports.db = db;
......
......@@ -9,7 +9,7 @@ import { AuditRecord, AuditEngine } from './interfaces/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?: AuditEngine | undefined) => AuditRecord | null;
export declare const generateAuditRecord: (auditInit?: AuditRecord | undefined, dbEngine?: AuditEngine | undefined) => Promise<AuditRecord | null>;
export default generateAuditRecord;
export { FileEngine, PostgreSqlEngine } from './engines/index.js';
export { AuditRecord, AuditEngine, FS_ERROR } from './interfaces/index.js';
......@@ -10,25 +10,25 @@ 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) => {
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();
const auditTransactionDate = auditInit?.auditTransactionDate || new Date().toISOString().split('.')[0] + "Z";
const auditUserIp = auditInit?.auditUserIp || process.env.HOSTIP || "127.0.0.1";
const auditUserId = auditInit?.auditUserId || "system";
const auditRecord = {
auditUnit,
auditTransactionId,
auditProtocol,
auditTransactionDate,
auditUserIp,
auditUserId
};
export const generateAuditRecord = async (auditInit, dbEngine) => {
try {
return _db.put(auditRecord);
const dbe = dbEngine || new FileEngine("/tmp");
const _db = new db(dbe);
const auditUnit = auditInit?.auditUnit || "gov.gr";
const auditTransactionId = auditInit?.auditTransactionId || "" + await _db.seq();
const auditProtocol = auditInit?.auditProtocol || await _db.pn();
const auditTransactionDate = auditInit?.auditTransactionDate || new Date().toISOString().split('.')[0] + "Z";
const auditUserIp = auditInit?.auditUserIp || process.env.HOSTIP || "127.0.0.1";
const auditUserId = auditInit?.auditUserId || "system";
const auditRecord = {
auditUnit,
auditTransactionId,
auditProtocol,
auditTransactionDate,
auditUserIp,
auditUserId
};
return await _db.put(auditRecord);
}
catch (error) {
const err = error;
......
......@@ -16,10 +16,10 @@ export declare type AuditRecord = {
* @interface AuditEngine
*/
export interface AuditEngine {
put: (record: AuditRecord) => AuditRecord;
get: (auditTransactionId: string) => AuditRecord;
seq: (path?: string) => number;
pn: (path?: string) => string;
put: (record: AuditRecord) => AuditRecord | Promise<AuditRecord>;
get: (auditTransactionId: string) => AuditRecord | Promise<AuditRecord>;
seq: (path?: string) => number | Promise<number>;
pn: (path?: string) => string | Promise<string>;
}
/**
* @description FileSystem errors
......
......@@ -11,27 +11,27 @@ export declare class db {
/**
* @description Store a record in the database
* @param record
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method put
*/
put(record: AuditRecord): AuditRecord;
put(record: AuditRecord): Promise<AuditRecord>;
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method get
*/
get(auditTransactionId: string): AuditRecord;
get(auditTransactionId: string): Promise<AuditRecord>;
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @returns Promise<number>
* @memberof db
* @method seq
*/
seq(path?: string): number;
seq(path?: string): Promise<number>;
/**
* @description Generate a new protocol string
* @param path
......@@ -39,6 +39,6 @@ export declare class db {
* @memberof db
* @method pn
*/
pn(path?: string): string;
pn(path?: string): Promise<string>;
}
export default db;
......@@ -12,36 +12,36 @@ export class db {
/**
* @description Store a record in the database
* @param record
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method put
*/
put(record) {
async put(record) {
if (!record.auditTransactionId)
throw new Error("record.auditTransactionId is required");
return this.engine.put(record);
return await this.engine.put(record);
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method get
*/
get(auditTransactionId) {
async get(auditTransactionId) {
if (!auditTransactionId)
throw new Error("auditTransactionId is required");
return this.engine.get(auditTransactionId);
return await this.engine.get(auditTransactionId);
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @returns Promise<number>
* @memberof db
* @method seq
*/
seq(path) {
return this.engine.seq(path);
async seq(path) {
return await this.engine.seq(path);
}
/**
* @description Generate a new protocol string
......@@ -50,8 +50,8 @@ export class db {
* @memberof db
* @method pn
*/
pn(path) {
return this.engine.pn(path);
async pn(path) {
return await this.engine.pn(path);
}
}
export default db;
......@@ -12,12 +12,13 @@ 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?: AuditRecord | undefined, dbEngine?:AuditEngine) => {
export const generateAuditRecord = async (auditInit?: AuditRecord | undefined, dbEngine?:AuditEngine) => {
try {
const dbe:AuditEngine = 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();
const auditTransactionId = auditInit?.auditTransactionId || "" + await _db.seq();
const auditProtocol = auditInit?.auditProtocol || await _db.pn();
const auditTransactionDate = auditInit?.auditTransactionDate || new Date().toISOString().split('.')[0] + "Z"
const auditUserIp = auditInit?.auditUserIp || process.env.HOSTIP || "127.0.0.1";
const auditUserId = auditInit?.auditUserId || "system";
......@@ -29,15 +30,12 @@ export const generateAuditRecord = (auditInit?: AuditRecord | undefined, dbEngin
auditUserIp,
auditUserId
}
try {
return _db.put(auditRecord);
return await _db.put(auditRecord);
} catch (error) {
const err = error as FS_ERROR;
console.log(err.code,err.message);
return null;
}
}
export default generateAuditRecord;
......
......@@ -17,10 +17,10 @@ export type AuditRecord = {
* @interface AuditEngine
*/
export interface AuditEngine {
put: (record: AuditRecord) => AuditRecord;
get: (auditTransactionId: string) => AuditRecord;
seq: (path?: string) => number; //Generate a new sequence number
pn: (path?: string) => string; //Generate a new protocol string
put: (record: AuditRecord) => AuditRecord | Promise<AuditRecord>;
get: (auditTransactionId: string) => AuditRecord | Promise<AuditRecord>;
seq: (path?: string) => number | Promise<number>; //Generate a new sequence number
pn: (path?: string) => string | Promise<string>; //Generate a new protocol string
}
/**
......
......@@ -15,36 +15,36 @@ export class db {
/**
* @description Store a record in the database
* @param record
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method put
*/
put(record: AuditRecord): AuditRecord {
async put(record: AuditRecord): Promise<AuditRecord> {
if (!record.auditTransactionId) throw new Error("record.auditTransactionId is required");
return this.engine.put(record);
return await this.engine.put(record);
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns AuditRecord
* @returns Promise<AuditRecord>
* @memberof db
* @method get
*/
get(auditTransactionId: string): AuditRecord {
async get(auditTransactionId: string): Promise<AuditRecord> {
if (!auditTransactionId) throw new Error("auditTransactionId is required");
return this.engine.get(auditTransactionId);
return await this.engine.get(auditTransactionId);
}
/**
* @description Generate a new sequence number
* @param path
* @returns number
* @returns Promise<number>
* @memberof db
* @method seq
*/
seq(path?: string): number {
return this.engine.seq(path);
async seq(path?: string): Promise<number> {
return await this.engine.seq(path);
}
/**
......@@ -54,8 +54,8 @@ export class db {
* @memberof db
* @method pn
*/
pn(path?: string): string {
return this.engine.pn(path);
async pn(path?: string): Promise<string> {
return await this.engine.pn(path);
}
}
......
import { generateAuditRecord } from '../dist/esm/index.js'
import { FileEngine } from '../dist/esm/engines/index.js';
const test = async () => {
generateAuditRecord({},new FileEngine('/tmp/audit'));
await generateAuditRecord({},new FileEngine('/tmp/audit'));
}
test();
const generateAuditRecord = require('../dist/cjs/index').default;
const { FileEngine } = require('../dist/cjs/engines/index');
const test = () => {
const ar = generateAuditRecord();
const test = async () => {
const ar = await generateAuditRecord();
console.log(ar);
}
const test2 = () => {
const ar = generateAuditRecord(undefined, new FileEngine('/tmp'));
const test2 = async () => {
const ar = await generateAuditRecord(undefined, new FileEngine('/tmp'));
console.log(ar);
}
......
import {generateAuditRecord} from '../dist/esm/index.js'
import { FileEngine } from '../dist/esm/engines/index.js';
const test = () => {
const ar = generateAuditRecord();
const test = async () => {
const ar = await generateAuditRecord();
console.log(ar);
}
const test2 = () => {
const ar = generateAuditRecord(undefined, new FileEngine('/tmp'));
const test2 = async () => {
const ar = await generateAuditRecord(undefined, new FileEngine('/tmp'));
console.log(ar);
}
......
import generateAuditRecord from '../src/index'
import {FileEngine} from '../src/engines/index';
const test = () => {
const ar = generateAuditRecord();
const test = async () => {
const ar = await generateAuditRecord();
console.log(ar);
}
const test2 = () => {
const ar = generateAuditRecord(undefined, new FileEngine('/tmp'));
const test2 = async () => {
const ar = await generateAuditRecord(undefined, new FileEngine('/tmp'));
console.log(ar);
}
......
import generateAuditRecord from '../src/index'
import {PostgreSqlEngine} from '../src/engines/index';
const test = () => {
const ar = generateAuditRecord({}, new PostgreSqlEngine('postgres://postgres:postgres@localhost:5432/postgres'));
const test = async () => {
const ar = await generateAuditRecord({}, new PostgreSqlEngine('postgres://postgres:postgres@localhost:5432/postgres'));
console.log(ar);
}
......
Markdown is supported
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