Database Query Generator
The `database-connectors` package is a Node.js library designed to convert structured JSON objects into database-specific SQL queries. It provides functionality to generate common SQL statements such as SELECT, INSERT, UPDATE, and DELETE, accommodating complex structures like table joins and conditional filtering. The current stable version, 1.0.3, was last published in 2015, indicating that the library is no longer actively maintained. Its core differentiation lies in abstracting SQL syntax behind a programmatic JSON interface, allowing developers to define query logic without directly writing raw SQL, thereby aiming to simplify data access layers and standardize query construction for MySQL-like databases. This focus on query preparation, rather than direct database execution, implies it is a utility for building queries to be passed to a separate database client.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module (ESM) context (e.g., in a file with `"type": "module"` in package.json or a `.mjs` file).fixChange your file to use CommonJS (e.g., ensure it's a `.js` file without `"type": "module"`) or use dynamic import: `import('database-connectors').then(module => { /* use module.default */ })`. -
TypeError: connectionIdentifier.identify is not a function
cause This usually happens if the package was imported incorrectly, or if the `database-connectors` package itself doesn't export an `identify` method directly (though the README suggests it does). Could also happen if `require()` fails silently or returns an empty object.fixEnsure you are using `const connectionIdentifier = require('database-connectors');` and not a named import. Verify the package is correctly installed. -
Error: Cannot find module 'node-database-connectors'
cause The `require()` statement uses the module name `node-database-connectors` as shown in the README, but the actual npm package name is `database-connectors`.fixUpdate your `require()` statement to `const connectionIdentifier = require('database-connectors');`
Warnings
- breaking The package has not been updated since 2015 (v1.0.3), meaning it lacks support for modern JavaScript features (ESM) and may not be compatible with newer Node.js versions or database systems/standards. Security vulnerabilities are unlikely to be patched.
- gotcha The README examples use `require('node-database-connectors')`, but the actual npm package is `database-connectors`. Using the name `node-database-connectors` will result in a 'Cannot find module' error.
- deprecated This library is CommonJS-only (`require`). It does not support ES Modules (`import`), which is the standard module system for modern JavaScript development in Node.js and browsers.
- gotcha The generated SQL syntax might be outdated or not fully compatible with advanced features or specific dialects of modern database versions beyond basic MySQL. For example, `engine: 'MyISAM'` is an outdated MySQL storage engine, and the SQL generated may not optimize for newer features.
Install
-
npm install database-connectors -
yarn add database-connectors -
pnpm add database-connectors
Imports
- default export (object with identify method)
import connectionIdentifier from 'database-connectors';
const connectionIdentifier = require('database-connectors'); - identify
const { identify } = require('database-connectors');const connectionIdentifier = require('database-connectors'); const objConnection = connectionIdentifier.identify(sampleConfig); - prepareQuery
const connectionIdentifier = require('database-connectors'); const objConnection = connectionIdentifier.identify(sampleConfig); const query = objConnection.prepareQuery(jsonQuery);
Quickstart
const connectionIdentifier = require('database-connectors');
// Sample database configuration (MySQL assumed based on README)
const sampleConfig = {
type: "database",
engine: 'MyISAM',
databaseType: 'mysql',
database: 'test_db',
host: "localhost",
port: "3306",
user: "root",
password: process.env.DB_PASSWORD ?? '', // Use environment variable for password
cacheResponse: false
};
// Sample JSON query for a SELECT statement
const jsonQuery = {
table: "tbl_SampleMaster",
alias: "SM",
select: [
{ field: 'pk_tableID', alias: 'pk' },
{ field: 'refNumber' }
],
sortby: [{ field: 'refNumber' }],
filter: {
AND: [{ field: 'pk_id', operator: 'EQ', value: '1' }]
}
};
try {
// Identify the connection type and get the connector object
const objConnection = connectionIdentifier.identify(sampleConfig);
// Prepare the SQL query from the JSON object
const query = objConnection.prepareQuery(jsonQuery);
console.log('Generated SQL Query:');
console.log(query);
} catch (error) {
console.error('Error generating query:', error.message);
}
// Example output for the above:
// SELECT ``.`pk_tableID` as `pk`,``.`refNumber`
// FROM `tbl_SampleMaster` as SM
// WHERE (``.`pk_id` = '1')
// ORDER BY `refNumber` ASC;