mirror of
https://github.com/janishutz/libreevent.git
synced 2026-04-29 14:29:25 +02:00
Revert "Restructuring for new way of installing libreevent"
This reverts commit 688b0616cc.
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* libreevent - db.js
|
||||
*
|
||||
* Created by Janis Hutz 03/26/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const path = require( 'path' );
|
||||
const fs = require( 'fs' );
|
||||
|
||||
const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../config/settings.config.json' ) ) );
|
||||
|
||||
const dbRef = {
|
||||
'user': 'libreevent_users',
|
||||
'admin': 'libreevent_admin',
|
||||
'order': 'libreevent_orders',
|
||||
'users': 'libreevent_users',
|
||||
'orders': 'libreevent_orders',
|
||||
'temp': 'libreevent_temp',
|
||||
'processingOrders': 'libreevent_processing_orders'
|
||||
};
|
||||
|
||||
const letters = [ ',', '{' ];
|
||||
|
||||
let dbh;
|
||||
|
||||
if ( settings.db === 'mysql' ) {
|
||||
const dbsoft = require( './mysqldb.js' );
|
||||
dbh = new dbsoft();
|
||||
dbh.connect();
|
||||
} else {
|
||||
const dbsoft = require( './jsondb.js' );
|
||||
dbh = new dbsoft();
|
||||
dbh.connect();
|
||||
}
|
||||
|
||||
module.exports.initDB = () => {
|
||||
( async() => {
|
||||
console.log( '[ DB ] Setting up...' );
|
||||
await dbh.resetDB();
|
||||
setTimeout( () => {
|
||||
dbh.setupDB();
|
||||
}, 2000 );
|
||||
console.log( '[ DB ] Setting up complete!' );
|
||||
} )();
|
||||
};
|
||||
|
||||
module.exports.reset = () => {
|
||||
console.log( '[ DB ] Resetting...' );
|
||||
this.writeJSONData( 'booked', {} );
|
||||
this.writeJSONData( 'eventDrafts', {} );
|
||||
this.writeJSONData( 'events', {} );
|
||||
this.writeJSONData( 'locations', {} );
|
||||
this.writeJSONData( 'events', {} );
|
||||
this.writeJSONData( 'seatplan', {} );
|
||||
this.writeJSONData( 'tickets', {} );
|
||||
console.log( '[ DB ] Reset complete!' );
|
||||
};
|
||||
|
||||
module.exports.getDataSimple = ( db, column, searchQuery ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dbh.query( { 'command': 'getFilteredData', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( data => {
|
||||
resolve( data );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.getData = ( db ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dbh.query( { 'command': 'getAllData' }, dbRef[ db ] ).then( data => {
|
||||
resolve( data );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.writeDataSimple = ( db, column, searchQuery, data ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dbh.query( { 'command': 'checkDataAvailability', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( res => {
|
||||
if ( res.length > 0 ) {
|
||||
dbh.query( { 'command': 'updateData', 'property': column, 'searchQuery': searchQuery, 'newValues': data }, dbRef[ db ] ).then( dat => {
|
||||
resolve( dat );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} else {
|
||||
dbh.query( { 'command': 'addData', 'data': data }, dbRef[ db ] ).then( dat => {
|
||||
resolve( dat );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
}
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.deleteDataSimple = ( db, column, searchQuery ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dbh.query( { 'command': 'deleteData', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( dat => {
|
||||
resolve( dat );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.checkDataAvailability = ( db, column, searchQuery ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dbh.query( { 'command': 'checkDataAvailability', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( res => {
|
||||
if ( res.length > 0 ) {
|
||||
resolve( true );
|
||||
} else {
|
||||
resolve( false );
|
||||
}
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
|
||||
module.exports.getJSONData = ( file ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fs.readFile( path.join( __dirname + '/../../data/' + file + '.json' ), ( error, data ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
} else {
|
||||
if ( data.byteLength > 0 ) {
|
||||
resolve( JSON.parse( data ) ?? {} );
|
||||
} else {
|
||||
resolve( { } );
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.getJSONDataSimple = ( file, identifier ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fs.readFile( path.join( __dirname + '/../../data/' + file + '.json' ), ( error, data ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
} else {
|
||||
if ( data.byteLength > 0 ) {
|
||||
resolve( JSON.parse( data )[ identifier ] ?? {} );
|
||||
} else {
|
||||
resolve( { } );
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.getJSONDataSync = ( file ) => {
|
||||
return JSON.parse( fs.readFileSync( path.join( __dirname + '/../../' + file ) ) );
|
||||
};
|
||||
|
||||
module.exports.writeJSONDataSimple = ( db, identifier, values ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fs.readFile( path.join( __dirname + '/../../data/' + db + '.json' ), ( error, data ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
} else {
|
||||
let dat = {};
|
||||
if ( data.byteLength > 0 ) {
|
||||
dat = JSON.parse( data ) ?? {};
|
||||
}
|
||||
dat[ identifier ] = values;
|
||||
fs.writeFile( path.join( __dirname + '/../../data/' + db + '.json' ), JSON.stringify( dat ), ( error ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
}
|
||||
resolve( true );
|
||||
} );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.writeJSONData = ( db, data ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fs.writeFile( path.join( __dirname + '/../../data/' + db + '.json' ), JSON.stringify( data ), ( error ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
} else {
|
||||
resolve( true );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.deleteJSONDataSimple = ( db, identifier ) => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fs.readFile( path.join( __dirname + '/../../data/' + db + '.json' ), ( error, data ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
} else {
|
||||
let dat = {};
|
||||
if ( data.byteLength > 0 ) {
|
||||
dat = JSON.parse( data ) ?? {};
|
||||
}
|
||||
delete dat[ identifier ];
|
||||
fs.writeFile( path.join( __dirname + '/../../data/' + db + '.json' ), JSON.stringify( dat ), ( error ) => {
|
||||
if ( error ) {
|
||||
reject( 'Error occurred: Error trace: ' + error );
|
||||
}
|
||||
resolve( true );
|
||||
} );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
module.exports.saveSettings = ( settings ) => {
|
||||
const settingsString = JSON.stringify( settings );
|
||||
let settingsToSave = '';
|
||||
for ( let letter in settingsString ) {
|
||||
if ( letters.includes( settingsString[ letter ] ) ) {
|
||||
settingsToSave += settingsString[ letter ] + '\n\t';
|
||||
} else if ( settingsString[ letter ] === '}' ) {
|
||||
settingsToSave += '\n' + settingsString[ letter ];
|
||||
} else {
|
||||
settingsToSave += settingsString[ letter ];
|
||||
}
|
||||
}
|
||||
fs.writeFileSync( path.join( __dirname + '/../../config/settings.config.json' ), settingsToSave );
|
||||
};
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* libreevent - jsondb.js
|
||||
*
|
||||
* Created by Janis Hutz 07/11/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const fs = require( 'fs' );
|
||||
const path = require( 'path' );
|
||||
|
||||
class JSONDB {
|
||||
constructor () {
|
||||
this.db = {};
|
||||
this.dbIndex = { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0, 'libreevent_processing_orders': 0 };
|
||||
this.isSaving = false;
|
||||
this.awaitingSaving = true;
|
||||
}
|
||||
|
||||
connect () {
|
||||
let data = {};
|
||||
try {
|
||||
JSON.parse( fs.readFileSync( path.join( __dirname + '/../../data/db.json' ) ) );
|
||||
} catch ( err ) {
|
||||
console.error( '[ JSON-DB ] CRITICAL INITIALIZATION FAILURE!' + err );
|
||||
throw ( 'JSONDB failed to start!' );
|
||||
}
|
||||
this.db = data[ 'db' ] ?? { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {}, 'libreevent_processing_orders': {} };
|
||||
this.dbIndex = data[ 'index' ] ?? { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0, 'libreevent_processing_orders': 0 };
|
||||
this.db[ 'libreevent_temp' ] = {};
|
||||
this.saveToDisk();
|
||||
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( { 'db': this.db, 'index': this.dbIndex } ), ( 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 = { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {}, 'libreevent_processing_orders': {} };
|
||||
this.dbIndex = { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0, 'libreevent_processing_orders': 0 };
|
||||
fs.writeFile( path.join( __dirname + '/../../data/db.json' ), JSON.stringify( { 'db': this.db, 'index': this.dbIndex } ) );
|
||||
}
|
||||
|
||||
async setupDB () {
|
||||
this.db = { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {}, 'libreevent_processing_orders': {} };
|
||||
this.dbIndex = { 'libreevent_temp': 0, 'libreevent_admin': 0, 'libreevent_orders': 0, 'libreevent_users': 0, 'libreevent_processing_orders': 0 };
|
||||
fs.writeFile( path.join( __dirname + '/../../data/db.json' ), JSON.stringify( { 'db': this.db, 'index': this.dbIndex } ) );
|
||||
}
|
||||
|
||||
query ( operation, table ) {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
/*
|
||||
Possible operation.command values (all need the table argument of the method call):
|
||||
- getAllData: no additional instructions needed
|
||||
|
||||
- getFilteredData:
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
|
||||
- InnerJoin (Select values that match in both tables):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- LeftJoin (Select values in first table and return all corresponding values of second table):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- RightJoin (Select values in second table and return all corresponding values of first table):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- addData:
|
||||
- operation.data (key-value pair with all data as values and column to insert into as key)
|
||||
|
||||
- updateData:
|
||||
- operation.newValues (a object with keys being the column and value being the value to be inserted into that column, values are being
|
||||
sanitised by the function)
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
|
||||
- checkDataAvailability:
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
*/
|
||||
|
||||
if ( operation.command === 'getAllData' ) {
|
||||
let ret = [];
|
||||
for ( let entry in this.db[ table ] ) {
|
||||
ret.push( this.db[ table ][ entry ] );
|
||||
}
|
||||
resolve( ret );
|
||||
} 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 ) {
|
||||
ret.push( this.db[ table ][ entry ] );
|
||||
}
|
||||
}
|
||||
resolve( ret );
|
||||
} else if ( operation.command === 'addData' ) {
|
||||
this.dbIndex[ table ] += 1;
|
||||
this.db[ table ][ this.dbIndex[ table ] ] = operation.data;
|
||||
this.saveToDisk();
|
||||
resolve( true );
|
||||
} 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.newValues ) {
|
||||
this.db[ table ][ entry ][ changed ] = operation.newValues[ changed ];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.saveToDisk();
|
||||
resolve( true );
|
||||
} 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();
|
||||
resolve( true );
|
||||
} else if ( operation.command === 'InnerJoin' ) {
|
||||
// TODO: Finish those when actually needed
|
||||
} else if ( operation.command === 'LeftJoin' ) {
|
||||
//
|
||||
} else if ( operation.command === 'RightJoin' ) {
|
||||
//
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = JSONDB;
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* libreevent - mysqldb.js
|
||||
*
|
||||
* Created by Janis Hutz 07/12/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const mysql = require( 'mysql' );
|
||||
const fs = require( 'fs' );
|
||||
const path = require( 'path' );
|
||||
|
||||
// If the connection does not work for you, you will need to add your ip
|
||||
// to the whitelist of the database
|
||||
|
||||
class SQLDB {
|
||||
constructor ( ) {
|
||||
this.sqlConnection = mysql.createConnection( JSON.parse( fs.readFileSync( path.join( __dirname + '/../../config/db.config.json' ) ) ) );
|
||||
}
|
||||
|
||||
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';
|
||||
} );
|
||||
}
|
||||
|
||||
disconnect ( ) {
|
||||
this.sqlConnection.end();
|
||||
}
|
||||
|
||||
async resetDB ( ) {
|
||||
this.sqlConnection.query( 'DROP TABLE libreevent_orders;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_BAD_TABLE_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'DROP TABLE libreevent_users;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_BAD_TABLE_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'DROP TABLE libreevent_admin;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_BAD_TABLE_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'DROP TABLE libreevent_temp;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_BAD_TABLE_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'DROP TABLE libreevent_processing_orders;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_BAD_TABLE_ERROR' ) throw error;
|
||||
return 'done';
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
async setupDB ( ) {
|
||||
this.sqlConnection.query( 'SELECT @@default_storage_engine;', ( error, results ) => {
|
||||
if ( error ) throw error;
|
||||
if ( results[ 0 ][ '@@default_storage_engine' ] !== 'InnoDB' ) throw 'DB HAS TO USE InnoDB!';
|
||||
} );
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_users ( account_id INT ( 10 ) NOT NULL AUTO_INCREMENT, email TINYTEXT NOT NULL, pass TEXT, name TEXT, first_name TEXT, two_fa TINYTEXT, user_data VARCHAR( 60000 ), mail_confirmed TINYTEXT, marketing TINYTEXT, PRIMARY KEY ( account_id ) ) ENGINE=INNODB;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_orders ( order_id INT ( 10 ) NOT NULL AUTO_INCREMENT, order_name TINYTEXT, account_id INT ( 10 ) NOT NULL, tickets VARCHAR( 60000 ), processed TINYTEXT, timestamp TINYTEXT, PRIMARY KEY ( order_id ), FOREIGN KEY ( account_id ) REFERENCES libreevent_users( account_id ) ) ENGINE=INNODB;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_admin ( account_id INT NOT NULL AUTO_INCREMENT, email TINYTEXT, pass TEXT, permissions VARCHAR( 1000 ), username TINYTEXT, two_fa TINYTEXT, PRIMARY KEY ( account_id ) );', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_temp ( entry_id INT NOT NULL AUTO_INCREMENT, user_id TINYTEXT, data VARCHAR( 60000 ), timestamp TINYTEXT, PRIMARY KEY ( entry_id ) );', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_processing_orders ( entry_id INT NOT NULL AUTO_INCREMENT, user_id TINYTEXT, data VARCHAR( 60000 ), timestamp TINYTEXT, PRIMARY KEY ( entry_id ) );', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
return 'DONE';
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
query ( operation, table ) {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
/*
|
||||
Possible operation.command values (all need the table argument of the method call):
|
||||
- getAllData: no additional instructions needed
|
||||
|
||||
- getFilteredData:
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
|
||||
- InnerJoin (Select values that match in both tables):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- LeftJoin (Select values in first table and return all corresponding values of second table):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- RightJoin (Select values in second table and return all corresponding values of first table):
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
- operation.selection (The columns of both tables to be selected, e.g. users.name, orders.id)
|
||||
- operation.secondTable (The second table to perform Join operation with)
|
||||
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
|
||||
|
||||
- addData:
|
||||
- operation.data (key-value pair with all data as values and column to insert into as key)
|
||||
|
||||
- deleteData:
|
||||
- operation.property (the column to search for the value)
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
|
||||
- updateData:
|
||||
- operation.newValues (a object with keys being the column and value being the value to be inserted into that column, values are being
|
||||
sanitised by the function)
|
||||
- operation.property (the column to search for the value),
|
||||
- operation.searchQuery (the value to search for [will be sanitised by method])
|
||||
|
||||
- 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!
|
||||
*/
|
||||
let command = '';
|
||||
if ( operation.command === 'getAllData' ) {
|
||||
command = 'SELECT * FROM ' + table;
|
||||
} 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;
|
||||
} else if ( operation.command === 'addData' ) {
|
||||
let keys = '';
|
||||
let values = '';
|
||||
for ( let key in operation.data ) {
|
||||
keys += String( key ) + ', ';
|
||||
values += this.sqlConnection.escape( String( operation.data[ key ] ) ) + ', ' ;
|
||||
}
|
||||
command = 'INSERT INTO ' + table + ' (' + keys.slice( 0, keys.length - 2 ) + ') VALUES (' + values.slice( 0, values.length - 2 ) + ');';
|
||||
} else if ( operation.command === 'updateData' ) {
|
||||
if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' );
|
||||
else {
|
||||
command = 'UPDATE ' + table + ' SET ';
|
||||
let updatedValues = '';
|
||||
for ( let value in operation.newValues ) {
|
||||
updatedValues += value + ' = ' + this.sqlConnection.escape( String( operation.newValues[ value ] ) ) + ', ';
|
||||
}
|
||||
command += updatedValues.slice( 0, updatedValues.length - 2 );
|
||||
command += ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery );
|
||||
}
|
||||
} else if ( operation.command === 'deleteData' ) {
|
||||
if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' );
|
||||
else {
|
||||
command = 'DELETE FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery );
|
||||
}
|
||||
} else if ( operation.command === 'InnerJoin' ) {
|
||||
command = 'SELECT ' + operation.selection + ' FROM ' + table + ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ) + ' INNER JOIN ' + operation.secondTable + ' ON ' + operation.matchingParam;
|
||||
} else if ( operation.command === 'LeftJoin' ) {
|
||||
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;
|
||||
}
|
||||
this.sqlConnection.query( command, ( error, results ) => {
|
||||
if ( error ) reject( error );
|
||||
resolve( results );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SQLDB;
|
||||
Reference in New Issue
Block a user