Added "TEST" operation to verify that the configured database is available. Test op is still WIP, doesn't return a useful response
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
node_modules
|
||||
.env
|
||||
.env
|
||||
client.js
|
||||
server.js
|
||||
70
readme.md
70
readme.md
@@ -2,7 +2,7 @@
|
||||
This set of modules is intended to serve as a general purpose toolkit for building Node.js applications with a database-type agnostic SQL backend. The audience at creation is myself, as I wanted a reusable module for communicating with my SQL backends in Node.js projects.
|
||||
|
||||
## Features
|
||||
At the moment, CRUD operations against Microsoft SQL Server and MariaDB servers are supported. I plan to implement support for interoperability with Postgres, Oracle, and MongoDB as well to provide the most possible utility in this package. An example implementation is available in the included server.js:
|
||||
At the moment, CRUD operations against Microsoft SQL Server and MariaDB servers are supported. I plan to implement support for interoperability with Postgres, Oracle, and MongoDB as well to provide the most possible utility in this package. An example implementation server.js:
|
||||
|
||||
```javascript
|
||||
// server.js
|
||||
@@ -35,9 +35,13 @@ app.listen(PORT, () => {
|
||||
});
|
||||
```
|
||||
|
||||
Additionally, the script `client.js` which runs simulated interactions with the server.js contains the following example for a request to the sample API route:
|
||||
Additionally, here's an example client.js script to test the database connector:
|
||||
|
||||
```javascript
|
||||
// client.js
|
||||
|
||||
const axios = require('axios');
|
||||
const generateUUID = require('uuid').v4;
|
||||
async function testQuery(table, operation, params) {
|
||||
try {
|
||||
const response = await axios.post('http://localhost:3100/api/query', {
|
||||
@@ -50,14 +54,68 @@ async function testQuery(table, operation, params) {
|
||||
console.error('Error executing query:', error.response ? error.response.data : error.message);
|
||||
}
|
||||
}
|
||||
...
|
||||
const ID = generateUUID();
|
||||
|
||||
// Example usage:
|
||||
(async () => {
|
||||
// CREATE: Ensure all required fields are present
|
||||
const ID1 = generateUUID();
|
||||
const ID2 = generateUUID();
|
||||
const ID3 = generateUUID();
|
||||
|
||||
await testQuery('Test', 'CREATE', {
|
||||
Name: 'Test Item',
|
||||
Text: 'A sample item created with ID: ' + ID,
|
||||
ID: ID
|
||||
Text: 'A sample item created with ID: ' + ID1,
|
||||
ID: ID1
|
||||
// Add other necessary fields here based on your schema
|
||||
});
|
||||
await testQuery('Test', 'CREATE', {
|
||||
Name: 'Test Item',
|
||||
Text: 'A sample item created with ID: ' + ID2,
|
||||
ID: ID2
|
||||
// Add other necessary fields here based on your schema
|
||||
});
|
||||
await testQuery('Test', 'CREATE', {
|
||||
Name: 'Test Item',
|
||||
Text: 'A sample item created wth ID: ' + ID3,
|
||||
ID: ID3
|
||||
// Add other necessary fields here based on your schema
|
||||
});
|
||||
await testQuery('Test', 'CREATE', {
|
||||
Name: 'Test Item',
|
||||
Text: 'A sample item created with ID: ' + ID4,
|
||||
ID: ID4
|
||||
// Add other necessary fields here based on your schema
|
||||
});
|
||||
|
||||
// UPDATE: Ensure both the ID and at least one field to update are present. ID must be the LAST field.
|
||||
await testQuery('Test', 'UPDATE', {
|
||||
Name: 'Tested item name',
|
||||
Text: 'Updated item text successfully for item: ' + ID2,
|
||||
ID: ID2
|
||||
});
|
||||
|
||||
// READ: Ensure the ID is present
|
||||
await testQuery('Test', 'READ', {
|
||||
ID : ID1
|
||||
} );
|
||||
|
||||
// READ: See all rows in a table
|
||||
await testQuery('Test', 'READ', {
|
||||
Name: 'Tested item name',
|
||||
});
|
||||
|
||||
// READ: See all rows in a table
|
||||
await testQuery('Test', 'READ', {
|
||||
partialMatch: true,
|
||||
Name: 'Test',
|
||||
});
|
||||
|
||||
|
||||
// DELETE: Ensure the ID is present
|
||||
await testQuery('Test', 'DELETE', {
|
||||
ID: ID3
|
||||
});
|
||||
})();
|
||||
```
|
||||
|
||||
The following environment variables are used to control the operation of the DB connector:
|
||||
|
||||
@@ -33,6 +33,9 @@ async function generateQuery(table, operation, params) {
|
||||
validateCompareParams(params);
|
||||
query = generateCompareQuery(DB_TYPE, table, params);
|
||||
break; */
|
||||
case 'TEST':
|
||||
query = generateTestQuery(DB_TYPE);
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unsupported operation');
|
||||
}
|
||||
@@ -266,6 +269,26 @@ async function generateDeleteQuery(dbType, table, params) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a TEST SQL query string based on database type, table, and parameters.
|
||||
*
|
||||
* @param {string} dbType - Type of database (e.g., 'MSSQL', 'ORACLE').
|
||||
* @returns {Promise<string>} The constructed DELETE SQL query string.
|
||||
*/
|
||||
async function generateTestQuery(dbType) {
|
||||
switch (dbType.toUpperCase()) {
|
||||
case 'MSSQL':
|
||||
return `SELECT @@SPID;`;
|
||||
case 'POSTGRES':
|
||||
return `SELECT pg_backend_pid();`; // Use $1 for PostgreSQL
|
||||
case 'MARIADB':
|
||||
return `SELECT CONNECTION_ID();`;
|
||||
case 'ORACLE':
|
||||
return `SELECT SYS_CONTEXT('USERENV', 'SESSIONID') FROM dual;`;
|
||||
default:
|
||||
throw new Error('Unsupported database type!');
|
||||
}
|
||||
}
|
||||
/*
|
||||
*
|
||||
* I intend to implement "compare" functionality but haven't yet decided how to do so. This is some spitballed code that I came up with for this exercise, and I hope to complete work on it in the future.
|
||||
|
||||
@@ -58,10 +58,13 @@ async function executeQuery(table, operation, params) {
|
||||
case 'READ':
|
||||
return result.recordset || [];
|
||||
case 'UPDATE':
|
||||
return { success: true, affectedRows: result.affectedRows };
|
||||
case 'DELETE':
|
||||
return { success: true, affectedRows: result.affectedRows };
|
||||
case 'CREATE':
|
||||
return { success: true };
|
||||
case 'TEST':
|
||||
return result.recordset || [];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -143,4 +146,4 @@ async function executePostgresQuery(connection, query, params) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { executeQuery };
|
||||
module.exports = { executeQuery };
|
||||
Reference in New Issue
Block a user