Commit 1b3980ee authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

ousting postgresql

parent 455039ff
......@@ -35,75 +35,11 @@ console.log(auditRecordDB({},new FileEngine('/tmp/auditRecords')))
}
```
### Prostgresql
To work with Postgresql, you need to use `PostgresqlEngine` instead of `FileEngine`
Moreover, have to install the native `libpg` library for Postgresql.
On macOS: brew install libpq
On Ubuntu/Debian: apt-get install libpq-dev g++ make
On RHEL/CentOS: yum install postgresql-devel
```
//you can use the PostgresSqlEngine via enviroment variables
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const main = () =>{
process.env.PGHOST='localhost'
process.env.PGUSER='dbuser'
process.env.PGPASSWORD='secretpassword'
process.env.PGDATABASE='audit'
process.env.PGPORT='5432'
console.log(auditRecordDB({},new PostgresSqlEngine()))
}
```
```
//or via connection string
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const connectionString = 'postgresql://dbuser:secretpassword@localhost:5432/audit'
const main = () =>{
console.log(auditRecordDB({},new PostgresSqlEngine()))
}
```
```
//The postgresql engine asumes that you have already create the table `audit_records` in the database, and that the table has the following columns:
//auditUnit: text
//auditTransactionId: text
//auditProtocol: text
//auditTransactionDate: text
//auditUserIp: text
//auditUserId: text
//
//if you have already a table on Postgresql you can use it by mapping the columns to the AuditRecord
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const connectionString = 'postgresql://dbuser:secretpassword@localhost:5432/audit'
const main = () =>{
console.log(auditRecordDB({},new PostgresSqlEngine({
tableName:'audit_records',
columns:{
auditUnit:'audit_unit',
auditTransactionId:'audit_transaction_id',
auditProtocol:'audit_protocol',
auditTransactionDate:'audit_transaction_date',
auditUserIp:'audit_user_ip',
auditUserId:'audit_user_id'
}
})))
}
```
# AuditEngine
By default, the file storage engine `FileEngine` is used.
Please keep in mind to change the storage path to app needs.
The app can also use `PostgreSqlEngine`.
To make it work, pass environment variables according to the documentation of the LIBPGSQL library. https://www.postgresql.org/docs/9.1/libpq-envars.html
Please remember that protocol and/or transaction id `sequences` will be created in the database if not provided.
Each day a new sequence will be created for protocol needs as `protyyyymmdd_seq`.
So if you do not need to keep tracking on application, you may need to clean up those sequences from time to time using a cron job.
Your app can also use `PostgreSqlEngine` provided from `@digigov-oss/auditrecord-postgresql-engine` module.
Look at `FileEngine.ts` and `PostgreSqlEngine.ts` for examples to extend the store to another 'real' database.
Look at `FileEngine.ts` here and `PostgreSqlEngine.ts` of the above module for examples to extend the store to another 'real' database.
#!/bin/bash
#Used at post-installation to create fake modules if optionalDepenetdencies are not installed
NPMROOT=`npm root`
PGNATIVEPATH=${NPMROOT%%node_modules*}node_modules/pg-native
echo
echo "Creating fake modules for optional dependencies"
echo
echo "check $PGNATIVEPATH for existance"
[[ ! -f $PGNATIVEPATH/index.js ]] && bash -c "mkdir -p ${PGNATIVEPATH} && touch ${PGNATIVEPATH}/index.js" && echo "${PGNATIVEPATH} created" || exit 0
......@@ -7,9 +7,6 @@
"node": ">=16.13.1"
},
"author": "Panagiotis Skarvelis <pskarvelis@admin.grnet.gr>",
"optionalDependencies": {
"pg-native": "^3.0.0"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.grnet.gr/digigov-oss/auditRecordDB.git"
......@@ -31,8 +28,7 @@
"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",
"postinstall": "./fixOptionalDepentecies.sh"
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig.commonjs.json && ./setType.sh"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
......
declare module 'pg-native';
\ No newline at end of file
import generateAuditRecord from '../src/index'
import {PostgreSqlEngine} from '../src/engines/index';
const test = async () => {
const ar = await generateAuditRecord({}, new PostgreSqlEngine('postgres://postgres:postgres@localhost:5432/postgres'));
console.log(ar);
}
console.log("Audit Record Generator, simple test on postgresql");
test();
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