JSON File Driver for Database-JS
raw JSON →database-js-json is a driver for the database-js2 ecosystem, enabling SQL-like querying of local JSON files. It wraps the jl-sql-api library to provide its core parsing and querying capabilities. The current stable version is 1.2.1, with releases adhering to Semantic Versioning. This package distinguishes itself by allowing developers to treat JSON documents as a data source queryable via a familiar SQL syntax, integrating seamlessly with the database-js2 connection abstraction. Key features include configurable character sets and file existence checks through URL-style connection string options. It's designed for scenarios requiring direct interaction with JSON data files using a standardized database interface, primarily for read operations.
Common errors
error Error: ENOENT: no such file or directory, open 'test.json' ↓
?checkOnConnect=false to the connection string to bypass the check. error Error: Protocol 'json' not found ↓
database-js-json is installed (npm install database-js-json) and explicitly required or imported in your application's entry point: require('database-js-json');. error Error: SQL parse error: Unexpected token: <token> ↓
jl-sql-api documentation for supported grammar and operations. Ensure proper quotation for string literals and valid identifiers. Warnings
breaking This driver is specifically designed for `database-js2`, a modern fork of `database-js`. Attempting to use it with the original `database-js` package will result in errors due to incompatible APIs and protocol registration mechanisms. ↓
gotcha Configuration options (e.g., `charset`, `checkOnConnect`) are passed directly in the connection string as URL query parameters. For nested options originating from `jl-sql-api`, dot notation is required (e.g., `sortOptions.inMemoryBufferSize`). ↓
gotcha By default (`checkOnConnect=true`), the driver will throw an error if the specified JSON file does not exist upon connection. This can be problematic if the file is expected to be created later or if its initial absence should be ignored. ↓
Install
npm install database-js-json yarn add database-js-json pnpm add database-js-json Imports
- Connection wrong
import { Connection } from 'database-js';correctimport { Connection } from 'database-js2'; - Connection wrong
var Connection = require('database-js').Connection;correctconst { Connection } = require('database-js2'); - (Side Effect) wrong
import { JsonConnection } from 'database-js-json';correctrequire('database-js-json');
Quickstart
const { Connection } = require('database-js2');
require('database-js-json'); // Ensure the driver is loaded and registers itself
(async () => {
// Create a dummy JSON file for demonstration
require('fs').writeFileSync('test.json', JSON.stringify([
{ "id": 1, "user_name": "alpha_user", "email": "alpha@example.com" },
{ "id": 2, "user_name": "beta_user", "email": "beta@example.com" },
{ "id": 3, "user_name": "not_so_secret_user", "email": "secret@example.com" }
], null, 2));
const connection = new Connection( 'json:///test.json' );
try {
console.log('Connecting to json:///test.json...');
// Query with a prepared statement
let statement = await connection.prepareStatement("SELECT * WHERE user_name = ?");
let rows = await statement.query('not_so_secret_user');
console.log('Query result for "not_so_secret_user":', rows);
console.log(`Found ${rows.length} user(s) matching the criteria.`);
// Fetch all data
let allRows = await connection.query("SELECT id, user_name FROM * LIMIT 2");
console.log('First 2 entries:', allRows);
} catch (error) {
console.error('An error occurred:', error);
} finally {
await connection.close();
console.log('Connection closed.');
// Clean up the dummy file
require('fs').unlinkSync('test.json');
}
} )();