node-database-connectors: JSON to SQL Query Builder

1.7.20 · active · verified Wed Apr 22

The `node-database-connectors` library provides a programmatic interface for converting structured JSON objects into database-specific SQL queries. It aims to abstract the direct SQL string construction process, offering a declarative approach to database interactions within Node.js applications. The library supports generating various SQL statements, including SELECT, INSERT, UPDATE, and DELETE, by interpreting JSON representations of tables, fields, aliases, join conditions, sorting, and filtering logic. Currently at version 1.7.20, it demonstrates a consistent release cadence with minor updates. Key differentiators include its focus on a JSON-based query language, its support for complex SQL operations and join conditions, and its ability to simplify application-level database logic by providing a structured, configuration-driven method for query construction.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure a database connection, define a JSON-based SELECT query, and generate the corresponding SQL string using `node-database-connectors`.

const connectionIdentifier = require('node-database-connectors');

// Sample database configuration
const sampleConfig = {
  type: "database",
  engine: 'MyISAM',
  databaseType: 'mysql',
  database: 'test_db',
  host: "localhost",
  port: "3306",
  user: "root",
  password: process.env.DB_PASSWORD ?? '', // Use environment variables for sensitive data
  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 {
  const objConnection = connectionIdentifier.identify(sampleConfig);
  const generatedQuery = objConnection.prepareQuery(jsonQuery);
  console.log('Generated SQL Query:\n', generatedQuery);
} catch (error) {
  console.error('Error generating query:', error.message);
}

// Expected Output (similar to README example):
// SELECT ``.`pk_tableID` as `pk`,``.`refNumber`
// FROM `tbl_SampleMaster` as SM
// WHERE (``.`pk_id` = '1')
// ORDER BY `refNumber` ASC;

view raw JSON →