add first json db functions

This commit is contained in:
2023-08-15 10:42:24 +02:00
parent 1beba0131a
commit 3e7ff741a3
5 changed files with 39 additions and 31 deletions

View File

@@ -15,8 +15,9 @@ class JSONDB {
this.db = {};
}
connect ( ) {
connect () {
this.db = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) );
this.db[ 'libreevent_temp' ] = {};
setInterval( async () => {
fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) );
}, 10000 );
@@ -24,6 +25,16 @@ class JSONDB {
return 'connection';
}
async resetDB () {
this.db = {};
fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) );
}
async setupDB () {
this.db = { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {} };
fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) );
}
query ( operation, table ) {
return new Promise( ( resolve, reject ) => {
/*
@@ -67,17 +78,18 @@ class JSONDB {
- checkDataAvailability:
- operation.property (the column to search for the value),
- operation.searchQuery (the value to search for [will be sanitised by method])
- fullCustomCommand:
- operation.query (the SQL instruction to be executed) --> NOTE: This command will not be sanitised, so use only with proper sanitisation!
*/
if ( operation.command === 'getAllData' ) {
resolve( this.db[ table ] );
} else if ( operation.command === 'getFilteredData' ) {
//
} else if ( operation.command === 'fullCustomCommand' ) {
//
let ret = {};
for ( let entry in this.db[ table ] ) {
if ( this.db[ table ][ entry ][ operation.property ] == operation.searchQuery ) {
ret[ entry ] = this.db[ table ][ entry ];
}
}
return ret;
} else if ( operation.command === 'addData' ) {
//
} else if ( operation.command === 'updateData' ) {
@@ -101,4 +113,6 @@ class JSONDB {
}
} );
}
}
}
module.exports = JSONDB;

View File

@@ -20,12 +20,20 @@ class SQLDB {
}
connect ( ) {
const self = this;
this.sqlConnection.connect( function( err ) {
if ( err ) {
console.error( '[ SQL ]: An error ocurred whilst connecting: ' + err.stack );
return;
}
console.log( '[ SQL ] Connected to database successfully' );
self.sqlConnection.query( 'TRUNCATE libreevent_temp;', error => {
if ( error ) {
console.error( '[ SQL ] Unable to truncate libreevent_temp table due to the following error: ' + error.code );
} else {
console.log( '[ SQL ] Truncated temporary data table successfully' );
}
} );
return 'connection';
} );
}