From 4208ad44a2b17f1037058d789b8a1de08c4b6271 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Fri, 18 Aug 2023 09:28:54 +0200 Subject: [PATCH] json db almost done --- src/server/backend/db/jsondb.js | 55 +++++++++++++++++++++++++------- src/server/backend/db/mysqldb.js | 4 +-- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/server/backend/db/jsondb.js b/src/server/backend/db/jsondb.js index 73c2eb2..b5ffe6b 100644 --- a/src/server/backend/db/jsondb.js +++ b/src/server/backend/db/jsondb.js @@ -13,18 +13,39 @@ const path = require( 'path' ); class JSONDB { constructor () { this.db = {}; + this.dbIndex = { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0 }; + this.isSaving = false; + this.awaitingSaving = true; } connect () { - this.db = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) ); + let data = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) ); + this.db = data[ 'db' ] ?? { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {} }; + this.dbIndex = data[ 'index' ] ?? { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0 }; this.db[ 'libreevent_temp' ] = {}; - setInterval( async () => { - fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); - }, 10000 ); console.log( '[ JSON-DB ] Database initialized successfully' ); return 'connection'; } + async saveToDisk () { + if ( !this.isSaving ) { + this.awaitingSaving = false; + this.save(); + } else { + this.awaitingSaving = true; + } + } + + save () { + fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ), ( err ) => { + if ( err ) console.error( '[ JSON-DB ] An error occurred during saving: ' + err ); + this.isSaving = false; + if ( this.awaitingSaving ) { + this.saveToDisk(); + } + } ); + } + async resetDB () { this.db = {}; fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); @@ -82,7 +103,7 @@ class JSONDB { if ( operation.command === 'getAllData' ) { resolve( this.db[ table ] ); - } else if ( operation.command === 'getFilteredData' ) { + } else if ( operation.command === 'getFilteredData' || operation.command === 'checkDataAvailability' ) { let ret = {}; for ( let entry in this.db[ table ] ) { if ( this.db[ table ][ entry ][ operation.property ] == operation.searchQuery ) { @@ -91,25 +112,37 @@ class JSONDB { } return ret; } else if ( operation.command === 'addData' ) { - // + this.dbIndex[ table ] += 1; + this.db[ table ][ this.dbIndex[ table ] ] = operation.data; + this.saveToDisk(); } else if ( operation.command === 'updateData' ) { if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' ); else { - // + for ( let entry in this.db[ table ] ) { + if ( this.db[ table ][ entry ][ operation.property ] == operation.searchQuery ) { + for ( let changed in operation.data ) { + this.db[ table ][ entry ][ changed ] = operation.data[ changed ]; + } + } + } } + this.saveToDisk(); } else if ( operation.command === 'deleteData' ) { if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' ); else { - // + for ( let entry in this.db[ table ] ) { + if ( this.db[ table ][ entry ][ operation.property ] == operation.searchQuery ) { + delete this.db[ table ][ entry ]; + } + } } + this.saveToDisk(); } else if ( operation.command === 'InnerJoin' ) { - // + // TODO: Finish those when actually needed } else if ( operation.command === 'LeftJoin' ) { // } else if ( operation.command === 'RightJoin' ) { // - } else if ( operation.command === 'checkDataAvailability' ) { - // } } ); } diff --git a/src/server/backend/db/mysqldb.js b/src/server/backend/db/mysqldb.js index 75a6881..5a98bda 100644 --- a/src/server/backend/db/mysqldb.js +++ b/src/server/backend/db/mysqldb.js @@ -132,7 +132,7 @@ class SQLDB { let command = ''; if ( operation.command === 'getAllData' ) { command = 'SELECT * FROM ' + table; - } else if ( operation.command === 'getFilteredData' ) { + } else if ( operation.command === 'getFilteredData' || operation.command === 'checkDataAvailability' ) { command = 'SELECT * FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ); } else if ( operation.command === 'fullCustomCommand' ) { command = operation.query; @@ -166,8 +166,6 @@ class SQLDB { command = 'SELECT ' + operation.selection + ' FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ) + ' LEFT JOIN ' + operation.secondTable + ' ON ' + operation.matchingParam; } else if ( operation.command === 'RightJoin' ) { command = 'SELECT ' + operation.selection + ' FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ) + ' RIGHT JOIN ' + operation.secondTable + ' ON ' + operation.matchingParam; - } else if ( operation.command === 'checkDataAvailability' ) { - command = 'SELECT * FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ); } this.sqlConnection.query( command, ( error, results ) => { if ( error ) reject( error );