@digigov-oss/gsis-audit-record-db
Version:
JSON file storage database for use with audit mechanism of GSIS
89 lines (88 loc) • 2.5 kB
JavaScript
//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
* @class FileEngine
* @implements AuditEngine
* @param {string} path - path to store the records
* @param {PNRESETTYPES} pnreset - protocol number sequence type
*/
export class FileEngine {
#path;
#pnreset;
constructor(path, pnreset) {
this.#path = path ? path : "/tmp";
this.#pnreset = pnreset || "innumerable";
}
/**
* @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) {
const data = JSON.stringify(record, null, 2);
try {
fs.writeFileSync(this.#path + '/record-' + record.auditTransactionId + '.json', data);
return record;
}
catch (error) {
throw error;
}
}
/**
* @description Get a record from the database
* @param auditTransactionId
* @returns {AuditRecord}
* @memberof FileEngine
* @method get
*/
get(auditTransactionId) {
try {
const data = fs.readFileSync(this.#path + '/record-' + auditTransactionId + '.json', 'utf8');
return JSON.parse(data);
}
catch (error) {
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 type: SEQTYPES
* @param path: string
* @returns string
* @memberof FileEngine
* @method protocol
*/
pn(reset, path) {
const protocol_save_path = path || this.#path;
const protocol_reset = reset || this.#pnreset;
try {
return protocol(protocol_save_path, protocol_reset);
}
catch (error) {
throw error;
}
}
}
export default FileEngine;