Commit 7e902a0e authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

add pg native and test

parent 0560678d
......@@ -7,7 +7,9 @@
"node": ">=16.13.1"
},
"author": "Panagiotis Skarvelis <pskarvelis@admin.grnet.gr>",
"dependencies": {},
"dependencies": {
"pg-native": "^3.0.0"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.grnet.gr/digigov-oss/auditRecordDB.git"
......@@ -28,6 +30,7 @@
"test": "node --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm ./test/test.ts",
"testesm": "node ./test/test.mjs",
"testcjs": "node ./test/test.cjs",
"testPostgreSql": "node --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm ./test/testLocalPostgreSql.ts",
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig.commonjs.json && ./setType.sh"
},
"eslintConfig": {
......
declare module 'pg-native';
\ No newline at end of file
//TODO
//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_log
//table audit_records
// auditUnit: varchar(255);
// auditTransactionId: varchar(255);
// auditProtocol: varchar(255);
......@@ -17,5 +16,123 @@
//You can also map the fields of already existent table to the fields in the audit log.
import Client from 'pg-native';
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 class PostgreSqlEngine implements AuditEngine {
#table: string;
#columnNames:AuditRecord;
#client: any;
/**
* @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={}) {
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: AuditRecord): AuditRecord {
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: string): AuditRecord {
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():number {
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(): string {
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;
\ No newline at end of file
import generateAuditRecord from '../src/index'
import {PostgreSqlEngine} from '../src/engines/index';
const test = () => {
const ar = generateAuditRecord({}, new PostgreSqlEngine('postgres://postgres:postgres@localhost:5432/postgres'));
console.log(ar);
}
console.log("Audit Record Generator, simple test on postgresql");
test();
......@@ -59,6 +59,18 @@ arg@^4.1.0:
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
bindings@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"
core-util-is@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85"
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
......@@ -69,6 +81,21 @@ diff@^4.0.1:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
inherits@~2.0.1:
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=
json5@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
......@@ -76,6 +103,14 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"
libpq@^1.7.0:
version "1.8.9"
resolved "https://registry.yarnpkg.com/libpq/-/libpq-1.8.9.tgz#6e0c6eecb176f6656ad092d67cc0131980cba897"
integrity sha512-herU0STiW3+/XBoYRycKKf49O9hBKK0JbdC2QmvdC5pyCSu8prb9idpn5bUSbxj8XwcEsWPWWWwTDZE9ZTwJ7g==
dependencies:
bindings "1.5.0"
nan "^2.14.0"
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
......@@ -86,6 +121,73 @@ minimist@^1.2.0:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
nan@^2.14.0:
version "2.15.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee"
integrity sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==
pg-int8@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c"
integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==
pg-native@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pg-native/-/pg-native-3.0.0.tgz#20c64e651e20b28f5c060b3823522d1c8c4429c3"
integrity sha512-qZZyywXJ8O4lbiIN7mn6vXIow1fd3QZFqzRe+uET/SZIXvCa3HBooXQA4ZU8EQX8Ae6SmaYtDGLp5DwU+8vrfg==
dependencies:
libpq "^1.7.0"
pg-types "^1.12.1"
readable-stream "1.0.31"
pg-types@^1.12.1:
version "1.13.0"
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-1.13.0.tgz#75f490b8a8abf75f1386ef5ec4455ecf6b345c63"
integrity sha512-lfKli0Gkl/+za/+b6lzENajczwZHc7D5kiUCZfgm914jipD2kIOIvEkAhZ8GrW3/TUoP9w8FHjwpPObBye5KQQ==
dependencies:
pg-int8 "1.0.1"
postgres-array "~1.0.0"
postgres-bytea "~1.0.0"
postgres-date "~1.0.0"
postgres-interval "^1.1.0"
postgres-array@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-1.0.3.tgz#c561fc3b266b21451fc6555384f4986d78ec80f5"
integrity sha512-5wClXrAP0+78mcsNX3/ithQ5exKvCyK5lr5NEEEeGwwM6NJdQgzIJBVxLvRW+huFpX92F2QnZ5CcokH0VhK2qQ==
postgres-bytea@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=
postgres-date@~1.0.0:
version "1.0.7"
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.7.tgz#51bc086006005e5061c591cee727f2531bf641a8"
integrity sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==
postgres-interval@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/postgres-interval/-/postgres-interval-1.2.0.tgz#b460c82cb1587507788819a06aa0fffdb3544695"
integrity sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==
dependencies:
xtend "^4.0.0"
readable-stream@1.0.31:
version "1.0.31"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.31.tgz#8f2502e0bc9e3b0da1b94520aabb4e2603ecafae"
integrity sha1-jyUC4LyeOw2huUUgqrtOJgPsr64=
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"
string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
integrity sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=
strip-bom@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
......@@ -124,6 +226,11 @@ typescript@^4.5.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8"
integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg==
xtend@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
......
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