Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

database-js

Build Status npm version Mentioned in Awesome Node.js

Wrapper for multiple databases with a JDBC-like connection

Database-js implements a common, promise-based interface for SQL database access. Inspired by Java, it uses connection strings to identify the database driver. Wrappers around native database drivers provide a unified interface to handle databases. Thus, you don't need to modify your code (except the connection string) to change your database! 😉

Database-js has built-in prepared statements, even if the underlying driver does not support them. It is built on Promises, so it works well with ES7 async code.

Contents

Install

npm install database-js

Drivers

Driver (wrapper) Note Installation
ActiveX Data Objects Windows only npm i database-js-adodb
CSV files npm i database-js-csv
Excel files npm i database-js-xlsx
Firebase npm i database-js-firebase
INI files npm i database-js-ini
JSON files npm i database-js-json
MySQL npm i database-js-mysql
MS SQL Server npm i database-js-mssql
PostgreSQL npm i database-js-postgres
SQLite npm i database-js-sqlite

See here how to add a new driver.

Usage

var Connection = require('database-js').Connection;

// 👉 Change the connection URL according to the database you need to connect
var conn =
	new Connection("sqlite:///path/to/test.sqlite"); // SQLite
	// new Connection("mysql://user:password@localhost/test"); // MySQL
	// new Connection("postgres://user:password@localhost/test"); // PostgreSQL
	// new Connection( < ANOTHER URL HERE > ); // see the drivers

var statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
statement.query("South Dakota")
	.then((results) => {
		console.log(results); // Display the results
		conn.close() // Close the database connection
			.then(() => {
				process.exit(0); // Success!
			}).catch((reason) => {
				console.log(reason); // Some problem when closing the connection
				process.exit(1);
			});
	}).catch((reason) => {
		console.log(reason); // Some problem while performing the query
		conn.close() // Close the connection
			.then(() => {
				process.exit(0); // Success!
			}).catch((reason) => {
				console.log(reason); // Some problem when closing the connection
				process.exit(1);
			});
	});

Async / await

Because database-js is built on Promises, it works very well with async/await. Compare the following code to the code from above. They accomplish the same thing.

var Connection = require('database-js').Connection;

(async function() {
    let conn, statement, results;
    try {
        conn = new Connection("sqlite:///path/to/test.sqlite"); // Just change the connection URL for a different database
        statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
        results = await statement.query("South Dakota");
        console.log(results);
    } catch (reason) {
        console.log(reason);
    } finally {
        if (conn) {
            await conn.close();
        }
        process.exit(0);
    }
})();

See also

codeceptjs-dbhelper - Allows to use database-js inside CodeceptJS to setup tests that access databases.

License

MIT