database-js CSV Driver
database-js-csv is a driver designed to integrate CSV files into the database-js ecosystem, allowing developers to perform SQL-like queries directly on CSV data. It operates as a wrapper around the jl-sql-api package, which provides the core SQL parsing functionality for JavaScript object streams. The overarching database-js project aims to offer a unified, promise-based interface for various data sources, including traditional SQL databases, JSON, Excel, and CSV, via standardized connection strings. While the core database-js library (version 0.5.1) and its underlying jl-sql-api haven't seen significant updates since mid-2021, indicating a maintenance phase, database-js-csv itself is at version 1.0.0. The primary advantage of this package lies in its ability to enable SQL querying on flat-file data, simplifying data access without requiring a dedicated database server.
Common errors
-
Error: Driver not found for connection string: csv:///test.csv?headers=true&overwriteOnExecute=true
cause The `database-js-csv` driver was not loaded or registered with the core `database-js` library.fixAdd `require('database-js-csv');` to your application's entry point to ensure the driver is initialized, typically after requiring `database-js` itself. -
TypeError: Cannot read properties of undefined (reading 'Connection')
cause You are trying to destructure `Connection` from a module that doesn't export it, or the `database-js` package was not installed or imported correctly.fixEnsure `npm install database-js` is run and that you are importing `Connection` from `database-js` using `const { Connection } = require('database-js');` or `const Connection = require('database-js').Connection;`. -
Error: CSV parsing error: Expected a header row but 'headers=true' was specified and the file is empty or malformed.
cause The CSV file specified by the connection string either does not exist, is empty, or its format does not match the `headers=true` expectation.fixVerify the CSV file exists at the specified path, contains data, and has a header row if `headers=true` is used in the connection string. Check for correct delimiters or line endings.
Warnings
- gotcha The example in the package's README uses `require('database-js2')`. This appears to be a typo or an outdated reference; the correct dependency for the core API is `database-js`. Always use `require('database-js')`.
- gotcha The `database-js-csv` package must be explicitly `require()`d to register its driver with the `database-js` core, even if you don't assign its exports to a variable. Without this, `database-js` will not recognize the `csv:///` connection string.
- gotcha The underlying `jl-sql-api` package, which provides the SQL parsing, has not been actively maintained for several years (last update ~2 years ago). This may limit support for advanced SQL features or lead to unpatched bugs in the SQL parsing logic.
- gotcha When specifying file paths in the connection string (e.g., `csv:///path/to/file.csv`), ensure they are correctly escaped and formatted for a URL. Relative paths might behave differently depending on the execution context and operating system.
- gotcha All data read from CSV files is initially treated as strings. While `jl-sql-api` has some type detection capabilities (e.g., `detectNumbers`, `detectDates`), explicit type casting within SQL queries or post-processing in JavaScript might be necessary for accurate data handling.
Install
-
npm install database-js-csv -
yarn add database-js-csv -
pnpm add database-js-csv
Imports
- Connection
import { Connection } from 'database-js'const Connection = require('database-js').Connection; - Database
import { Database } from 'database-js'const { Database } = require('database-js'); - Side-effect import for driver registration
import 'database-js-csv';
require('database-js-csv');
Quickstart
const fs = require('fs');
const { Database } = require('database-js');
// IMPORTANT: Require the driver to register it with database-js
require('database-js-csv');
const csvContent = `user_name,email,role\nsecret_user,secret@example.com,admin\nnot_so_secret_user,notsosecret@example.com,user\n`;
const csvFilePath = './test.csv';
// Create a dummy CSV file for the example
fs.writeFileSync(csvFilePath, csvContent);
(async () => {
let connection;
try {
// The 'csv:///' prefix activates the database-js-csv driver
connection = new Database(`csv://${csvFilePath}?headers=true&overwriteOnExecute=true`);
let statement = await connection.prepareStatement("SELECT user_name, email FROM CSV WHERE user_name = ?");
let rows = await statement.query('not_so_secret_user');
console.log('Query result for not_so_secret_user:', rows);
statement = await connection.prepareStatement("INSERT INTO CSV (user_name, email, role) VALUES (?, ?, ?)");
await statement.execute('new_user', 'new@example.com', 'guest');
console.log('Inserted new user.');
// Query all data to see the inserted user
statement = await connection.prepareStatement("SELECT * FROM CSV");
rows = await statement.query();
console.log('All data after insert:', rows);
} catch (error) {
console.error('Error:', error);
} finally {
if (connection) {
await connection.close();
}
// Clean up the dummy CSV file
fs.unlinkSync(csvFilePath);
console.log('Cleaned up test.csv');
}
})();