Compare commits
3 Commits
dependabot
...
integratio
| Author | SHA1 | Date | |
|---|---|---|---|
| e3c74c1c99 | |||
| a33c465dc5 | |||
| 3bd117e70d |
87
client.js
87
client.js
@@ -1,87 +0,0 @@
|
||||
// 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', {
|
||||
table,
|
||||
operation,
|
||||
params
|
||||
});
|
||||
console.log(response.data);
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error.response ? error.response.data : error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
(async () => {
|
||||
// CREATE: Ensure all required fields are present
|
||||
const ID1 = generateUUID();
|
||||
const ID2 = generateUUID();
|
||||
const ID3 = generateUUID();
|
||||
const ID4 = generateUUID();
|
||||
|
||||
await testQuery('Test', 'CREATE', {
|
||||
Name: 'Test Item',
|
||||
Text: 'A sample item created with ID: ' + ID1,
|
||||
ID: ID1, // Assuming an ID is needed for identification or to prevent conflicts
|
||||
// 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, // Assuming an ID is needed for identification or to prevent conflicts
|
||||
// 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, // Assuming an ID is needed for identification or to prevent conflicts
|
||||
// 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, // Assuming an ID is needed for identification or to prevent conflicts
|
||||
// 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
|
||||
});
|
||||
/*
|
||||
// COMPARE: Ensure both compareTable and idField are present
|
||||
const compareParams = {
|
||||
compareTable: 'Test2',
|
||||
idField: 'ID',
|
||||
ID: '1'
|
||||
};
|
||||
await testQuery('Test', 'COMPARE', compareParams);*/
|
||||
})();
|
||||
2162
package-lock.json
generated
2162
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"axios": "^1.11.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^17.2.1",
|
||||
"express": "^5.1.0",
|
||||
"mssql": "^11.0.1",
|
||||
"mysql2": "^3.14.3",
|
||||
"oracledb": "^6.9.0",
|
||||
"pg": "^8.16.3"
|
||||
}
|
||||
}
|
||||
134
readme.md
134
readme.md
@@ -1,134 +0,0 @@
|
||||
## JS DB Connector Service Guide
|
||||
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 server.js:
|
||||
|
||||
```javascript
|
||||
// server.js
|
||||
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const queryExecutor = require('./services/dbconnector/queryExecutor');
|
||||
const cors = require('cors');
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
/**
|
||||
* Example route to demonstrate CRUD operations.
|
||||
*/
|
||||
app.post('/api/query', async (req, res) => {
|
||||
try {
|
||||
const { table, operation, params } = req.body;
|
||||
const result = await queryExecutor.executeQuery(table, operation, params);
|
||||
res.status(200).json(result);
|
||||
} catch (err) {
|
||||
console.error('Error executing query:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3100;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on port ${PORT}`);
|
||||
});
|
||||
```
|
||||
|
||||
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', {
|
||||
table,
|
||||
operation,
|
||||
params
|
||||
});
|
||||
console.log(response.data);
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error.response ? error.response.data : error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 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: ' + 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:
|
||||
```
|
||||
DB_USER= # A user with permissions to manage the database
|
||||
DB_PASSWORD= # Password for DB_USER
|
||||
DB_SERVER= # Hostname or IP of the database server
|
||||
DB_PORT= # Port if not using default for the database type
|
||||
DB_DATABASE= # Database to be used. As written, only a single database is supported
|
||||
DB_TYPE= # MSSQL and MARIADB are implemented. ORACLEDB and POSTGRES are planned.
|
||||
DB_TRUST_CERT= # Trust the local system SSL certificate, useful for developing against MSSQL server.
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
To test the function of these modules, run "node server.js" in the project root, and post to http://localhost:3100/api/query. You can instead run client.js to automatically attempt each query type against the default server URL.
|
||||
28
server.js
28
server.js
@@ -1,28 +0,0 @@
|
||||
// server.js
|
||||
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const queryExecutor = require('./services/dbconnector/queryExecutor');
|
||||
const cors = require('cors');
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
/**
|
||||
* Example route to demonstrate CRUD operations.
|
||||
*/
|
||||
app.post('/api/query', async (req, res) => {
|
||||
try {
|
||||
const { table, operation, params } = req.body;
|
||||
const result = await queryExecutor.executeQuery(table, operation, params);
|
||||
res.status(200).json(result);
|
||||
} catch (err) {
|
||||
console.error('Error executing query:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3100;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on port ${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user