Database Query Generator

1.0.3 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize a database connector configuration and generate a SQL SELECT query from a JSON object using the library's `identify` and `prepareQuery` methods.

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;

view raw JSON →