Common Database Interface for Node
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.vscode
examples
lib
.editorconfig
.gitignore
.jshintrc
.npmignore
.travis.yml
LICENSE
README.md
_config.yml
index.d.ts
index.js
package-lock.json
package.json
test.js
test.sqlite

README.md

database-js

Build Status npm version Mentioned in Awesome Node.js

Wrapper for multiple databases with a JDBC-like connection

Database-js was started to implement a common, promise-based interface for SQL database access. The concept is to copy the Java pattern of using connection strings to identify the driver. Then provide wrappers around the implemented functionality to commonize the syntax and results.

Thus if SQLite, MySQL and PostgreSQL all have a database named test with a table named states we can access the data the same way.

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

Currently available drivers:

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