All Projects → mlaanderson → database-js

mlaanderson / database-js

Licence: MIT license
Common Database Interface for Node

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to database-js

Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (+1036.21%)
Mutual labels:  javascript-library, node-module
bulksearch
Lightweight and read-write optimized full text search library.
Stars: ✭ 108 (+86.21%)
Mutual labels:  javascript-library, node-module
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (+831.03%)
Mutual labels:  javascript-library, node-module
Flexsearch
Next-Generation full text search library for Browser and Node.js
Stars: ✭ 8,108 (+13879.31%)
Mutual labels:  javascript-library, node-module
Recursive Diff
A JavaScript library to find diff between two JavaScript Objects. Support for Array, Number, Date and other primitive data types.
Stars: ✭ 71 (+22.41%)
Mutual labels:  javascript-library, node-module
excel-date-to-js
Convert Excel date in integer format into JS date. Dates are stored as numbers in Excel and count the number of days since January 0, 1900 (1900 standard, for mac it is 1904, which means January 0, 1904 is the start date). Times are handled internally as numbers between 0 and 1.
Stars: ✭ 26 (-55.17%)
Mutual labels:  excel, javascript-library
SchemaMapper
A .NET class library that allows you to import data from different sources into a unified destination
Stars: ✭ 41 (-29.31%)
Mutual labels:  excel, msaccess
Xresloader
跨平台Excel导表工具(Excel=>protobuf/msgpack/lua/javascript/json/xml)
Stars: ✭ 161 (+177.59%)
Mutual labels:  excel, ini
microbundle-ts-pkg
A TypeScript npm package skeleton/starter project with microbundle, node:test and prettier
Stars: ✭ 20 (-65.52%)
Mutual labels:  node-module
javascript-strong-password-generator
JavaScript Strong Password Generator: based on Jeff Atwood's Post "Password Rules Are Bullshit".
Stars: ✭ 21 (-63.79%)
Mutual labels:  javascript-library
Excel-Timesheet
⏰ This Add-In is used to produce a timesheet file with functionality to import your Google Timeline. The standard timesheet has options for start and end dates, day of week and default start, end and break times. The Google timeline options are start and end dates, UTC selection, daylight savings time parameters and title filter for timeline ent…
Stars: ✭ 25 (-56.9%)
Mutual labels:  excel
fmtdate
MS Excel (TM) syntax for Go time/date
Stars: ✭ 95 (+63.79%)
Mutual labels:  excel
necktie
Necktie – a simple DOM binding tool
Stars: ✭ 43 (-25.86%)
Mutual labels:  javascript-library
toaststrap
A simple, lightweight JavaScript library for showing Bootstrap 5 toast popups.
Stars: ✭ 16 (-72.41%)
Mutual labels:  javascript-library
alga-js
Alga.js is javascript helper for helping build a component of any front-end web frameworks
Stars: ✭ 18 (-68.97%)
Mutual labels:  javascript-library
SSCD.js
Super Simple Collision Detection for JavaScript games!
Stars: ✭ 88 (+51.72%)
Mutual labels:  javascript-library
ChangeNumbersJs
Tiny Library for change number from a language in other language.
Stars: ✭ 14 (-75.86%)
Mutual labels:  javascript-library
rc-here-maps
React components implemented on top of Here Maps API
Stars: ✭ 16 (-72.41%)
Mutual labels:  javascript-library
react-advertising
A JavaScript library for display ads in React applications.
Stars: ✭ 50 (-13.79%)
Mutual labels:  javascript-library
ioBroker.tankerkoenig
Spritpreis Adapter für ioBroker
Stars: ✭ 29 (-50%)
Mutual labels:  node-module

database-js

Build Status npm version Mentioned in Awesome Node.js downloads

Wrapper for multiple databases with a JDBC-like connection

Database-js implements a common, promise-based interface for SQL database access. Inspired by JDBC, it uses connection strings to identify the database driver. Wrappers around native database drivers provide a unified interface to handle databases. Thus, you can change the target database by modifying the connection string. 😉

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 prior to MySQL v8 npm i database-js-mysql
MySQL2 MySQL v8+ npm i database-js-mysql2
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

Usage without async/await:

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

// CONNECTION
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
	// 👉 Change the connection string according to the database driver

// QUERY
var stmt1 = conn.prepareStatement("SELECT * FROM city WHERE name = ?");
stmt1.query("New York")
	.then( function (results) {
		console.log(results); // Display the results
	} ).catch( function (reason) {
		console.log(reason); // Some problem while performing the query
	} );

// COMMAND
var stmt2 = conn.prepareStatement("INSERT INTO city (name, population) VALUES (?, ?)");
stmt2.execute("Rio de Janeiro", 6747815)
	.then( function() { console.log( 'Inserted.' ); } )
	.catch( function(reason) { console.log('Error: ' + reason); } );

// ANOTHER COMMAND
var stmt3 = conn.prepareStatement("UPDATE city SET population = population + ? WHERE name = ?");
stmt3.execute(1, "Rio de Janeiro")
	.then( function() { console.log( 'Updated.' ); } )
	.catch( function(reason) { console.log('Error: ' + reason); } );

// CLOSING THE CONNECTION
conn.close()
	.then( function() { console.log('Closed.'); } )
	.catch( function(reason) { console.log('Error: ' + reason); } );

Async / await

Using async/await:

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

(async () => {
	let conn;
	try {
		// CONNECTION
		conn = new Connection('mysql://user:password@localhost/test');

		// QUERY
		const stmt1 = conn.prepareStatement('SELECT * FROM city WHERE name = ?');
		const results = await stmt1.query('New York');
		console.log(results);

		// COMMAND 1
		const stmt2 = conn.prepareStatement('INSERT INTO city (name, population) VALUES (?,?)');
		await stmt1.execute('Rio de Janeiro', 6747815);

		// COMMAND 2
		const stmt2 = conn.prepareStatement('UPDATE city SET population = population + ? WHERE name = ?');
		await stmt1.execute(1, 'Rio de Janeiro');
	} catch (reason) {
		console.log(reason);
	} finally {
		try {
			await conn.close();
		} catch (err) {
			console.log(err);
		}
	}
})();

Basic API

class Connection {

	/** Creates and prepares a statement with the given SQL. */
	prepareStatement(sql: string): PreparedStatement;

	/** Closes the underlying connection. */
	close(): Promise<void>;

	/** Indicates whether the underlying driver support transactions. */
	isTransactionSupported(): boolean;

	/** Returns true if the underlying driver is in a transaction, false otherwise. */
	inTransaction(): boolean;

	/**
	 * Starts a transaction (if supported).
	 *
	 * Transactions can fail to start if another transaction is already running or
	 * if the driver does not support transactions.
	 */
	beginTransaction(): Promise<boolean>;

	/**
	 * Commits a transaction (if supported).
	 *
	 * Transactions can fail to commit if no transaction was started, or if the driver
	 * does not support transactions.
	 */
	commit(): Promise<boolean>;

	/**
	 * Cancels a transaction (if supported).
	 *
	 * Transaction can fail to be rolled back no transaction was started, or if the driver
	 * does not support transactions.
	 */
	rollback(): Promise<boolean>;
}
class PreparedStatement {
	/**
	 * Performs the prepared SQL query with the given arguments.
	 * Returns a Promise with an array of rows.
	 */
	query(...args: any): Promise<Array<any>>;

	/** Executes the prepared SQL statement with the given arguments. */
	execute(... args): Promise<any>;
}

See also

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].