Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
DigiGov-OSS
AuditRecordDB
Commits
6aa372e4
Commit
6aa372e4
authored
Feb 03, 2022
by
Panagiotis Skarvelis
Browse files
refactoring to allow new db engine with params
parent
d3903331
Changes
39
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
6aa372e4
...
...
@@ -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 exa
m
ple.
dist/cjs/
lib
/FileEngine.d.ts
→
dist/cjs/
engines/file
/FileEngine.d.ts
View file @
6aa372e4
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
;
dist/cjs/
lib
/FileEngine.js
→
dist/cjs/
engines/file
/FileEngine.js
View file @
6aa372e4
...
...
@@ -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
();
...
...
dist/cjs/
lib
/protocol.d.ts
→
dist/cjs/
engines/file
/protocol.d.ts
View file @
6aa372e4
File moved
dist/cjs/
lib
/protocol.js
→
dist/cjs/
engines/file
/protocol.js
View file @
6aa372e4
File moved
dist/cjs/
lib
/sequence.d.ts
→
dist/cjs/
engines/file
/sequence.d.ts
View file @
6aa372e4
File moved
dist/cjs/
lib
/sequence.js
→
dist/cjs/
engines/file
/sequence.js
View file @
6aa372e4
File moved
dist/cjs/engines/index.d.ts
0 → 100644
View file @
6aa372e4
export
{
default
as
FileEngine
}
from
"
./file/FileEngine.js
"
;
dist/cjs/engines/index.js
0 → 100644
View file @
6aa372e4
"
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
;
}
});
dist/cjs/index.d.ts
View file @
6aa372e4
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
};
dist/cjs/index.js
View file @
6aa372e4
...
...
@@ -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
db
Engine
.
put
(
auditRecord
);
return
_
db
.
put
(
auditRecord
);
}
catch
(
error
)
{
const
err
=
error
;
...
...
dist/cjs/interfaces/index.d.ts
View file @
6aa372e4
...
...
@@ -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
...
...
dist/cjs/lib/db.d.ts
View file @
6aa372e4
...
...
@@ -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
;
dist/cjs/lib/db.js
View file @
6aa372e4
...
...
@@ -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
;
dist/esm/
lib
/FileEngine.d.ts
→
dist/esm/
engines/file
/FileEngine.d.ts
View file @
6aa372e4
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
;
dist/esm/
lib
/FileEngine.js
→
dist/esm/
engines/file
/FileEngine.js
View file @
6aa372e4
//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
;
dist/esm/
lib
/protocol.d.ts
→
dist/esm/
engines/file
/protocol.d.ts
View file @
6aa372e4
File moved
dist/esm/
lib
/protocol.js
→
dist/esm/
engines/file
/protocol.js
View file @
6aa372e4
File moved
dist/esm/
lib
/sequence.d.ts
→
dist/esm/
engines/file
/sequence.d.ts
View file @
6aa372e4
File moved
dist/esm/
lib
/sequence.js
→
dist/esm/
engines/file
/sequence.js
View file @
6aa372e4
File moved
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment