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,9 +15,6 @@ const cookieParser = require( 'cookie-parser' );
const http = require( 'http' ); const http = require( 'http' );
const fs = require( 'fs' ); const fs = require( 'fs' );
const token = require( './backend/token.js' ); const token = require( './backend/token.js' );
// const pm = require( './backend/plugins/manager.js' );
// const pluginManager = new pm();
console.log( ` console.log( `
@@ -38,20 +35,18 @@ _ _ _ _
==> You are running Version V1.0.0 ==> You are running Version V1.0.0
==> You should deploy this on a server!
Below you can see all important things that happen during operation. Below you can see all important things that happen during operation.
libreevent logs all errors in the console such that they appear in the libreevent logs all errors in the console such that they appear in the
log files when running it with an output pipe. log files when running it with an output pipe (which you should definitely do)
To do this run the following command when starting libreevent:
'node app.js > libreevent_log.txt'
` ); ` );
console.log( '[ Server ] loading settings' ); console.log( '[ Server ] loading settings' );
const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/config/settings.config.json' ) ) ); const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/config/settings.config.json' ) ) );
// const mail = require( './backend/mail/mailSender.js' );
// const mailManager = new mail();
console.log( '[ Server ] Setting up static routes' ); console.log( '[ Server ] Setting up static routes' );
if ( settings.init ) { if ( settings.init ) {
app.use( express.static( 'webapp/main/dist' ) ); app.use( express.static( 'webapp/main/dist' ) );
@@ -72,8 +67,6 @@ app.use( expressSession( {
} }
} ) ); } ) );
// app.use( bodyParser.urlencoded( { extended: false } ) );
// app.use( bodyParser.json() );
app.use( cookieParser() ); app.use( cookieParser() );
let file = path.join( __dirname + '/webapp/main/dist/index.html' ); let file = path.join( __dirname + '/webapp/main/dist/index.html' );
@@ -85,17 +78,12 @@ if ( settings.init ) {
require( './backend/userAPIRoutes.js' )( app, settings ); // admin api routes require( './backend/userAPIRoutes.js' )( app, settings ); // admin api routes
require( './backend/userRoutes.js' )( app, settings ); // user routes require( './backend/userRoutes.js' )( app, settings ); // user routes
require( './backend/payments/paymentRoutes.js' )( app, settings ); // payment routes require( './backend/payments/paymentRoutes.js' )( app, settings ); // payment routes
console.log( '[ Server ] loading plugins' ); require( './backend/plugins/pluginLoader.js' )( app, settings ); // plugin loader
require( './backend/plugins/pluginLoader.js' )( app, settings );
} else { } else {
require( './setup/setupRoutes.js' )( app, settings ); // setup routes require( './setup/setupRoutes.js' )( app, settings ); // setup routes
file = path.join( __dirname + '/webapp/setup/dist/index.html' ); file = path.join( __dirname + '/webapp/setup/dist/index.html' );
} }
// TODO: load dynamically
// require( './backend/plugins/payments/stripe/stripeRoutes.js' )( app, settings ); // stripe routes
// require( './backend/plugins/payments/payrexx/payrexxRoutes.js' )( app, settings ); // payrexx routes
app.use( ( request, response ) => { app.use( ( request, response ) => {
response.sendFile( file ); response.sendFile( file );
} ); } );

View File

@@ -15,8 +15,9 @@ class JSONDB {
this.db = {}; this.db = {};
} }
connect ( ) { connect () {
this.db = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) ); this.db = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) );
this.db[ 'libreevent_temp' ] = {};
setInterval( async () => { setInterval( async () => {
fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) );
}, 10000 ); }, 10000 );
@@ -24,6 +25,16 @@ class JSONDB {
return 'connection'; 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 ) { query ( operation, table ) {
return new Promise( ( resolve, reject ) => { return new Promise( ( resolve, reject ) => {
/* /*
@@ -67,17 +78,18 @@ class JSONDB {
- checkDataAvailability: - checkDataAvailability:
- operation.property (the column to search for the value), - operation.property (the column to search for the value),
- operation.searchQuery (the value to search for [will be sanitised by method]) - 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' ) { if ( operation.command === 'getAllData' ) {
resolve( this.db[ table ] ); resolve( this.db[ table ] );
} else if ( operation.command === 'getFilteredData' ) { } else if ( operation.command === 'getFilteredData' ) {
// let ret = {};
} else if ( operation.command === 'fullCustomCommand' ) { 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 === 'addData' ) {
// //
} else if ( operation.command === 'updateData' ) { } else if ( operation.command === 'updateData' ) {
@@ -102,3 +114,5 @@ class JSONDB {
} ); } );
} }
} }
module.exports = JSONDB;

View File

@@ -20,12 +20,20 @@ class SQLDB {
} }
connect ( ) { connect ( ) {
const self = this;
this.sqlConnection.connect( function( err ) { this.sqlConnection.connect( function( err ) {
if ( err ) { if ( err ) {
console.error( '[ SQL ]: An error ocurred whilst connecting: ' + err.stack ); console.error( '[ SQL ]: An error ocurred whilst connecting: ' + err.stack );
return; return;
} }
console.log( '[ SQL ] Connected to database successfully' ); 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'; return 'connection';
} ); } );
} }

View File

@@ -14,13 +14,11 @@
class PluginManager { class PluginManager {
constructor () {} constructor () {}
install ( plugin ) { loadSettings ( plugin ) {
} }
update () {} saveSettings ( plugin, settings ) {
uninstall ( plugin ) {
} }
} }

View File

@@ -11,11 +11,11 @@ const fs = require( 'fs' );
const path = require( 'path' ); const path = require( 'path' );
module.exports = ( app, settings ) => { module.exports = ( app, settings ) => {
console.log( '\n\n[ Plugin Loader ] Loading plugins\n' );
let otherPlugins = fs.readdirSync( path.join( __dirname + '/others' ) ); let otherPlugins = fs.readdirSync( path.join( __dirname + '/others' ) );
console.log( '\n\n' );
for ( let plugin in otherPlugins ) { for ( let plugin in otherPlugins ) {
console.log( '[ Plugin Loader ] Loaded plugin "' + otherPlugins[ plugin ] + '"' );
require( './others/' + otherPlugins[ plugin ] + '/' + otherPlugins[ plugin ] + 'Routes.js' )( app, settings ); require( './others/' + otherPlugins[ plugin ] + '/' + otherPlugins[ plugin ] + 'Routes.js' )( app, settings );
console.log( '[ Plugin Loader ] Loaded plugin "' + otherPlugins[ plugin ] + '"' );
} }
require( './payments/' + settings.payments + '/' + settings.payments + 'Routes.js' )( app, settings ); require( './payments/' + settings.payments + '/' + settings.payments + 'Routes.js' )( app, settings );