# database-js2

> Common Database Interface

Latest version **1.4.2** (published 2018-01-26) · MIT license · 28 weekly downloads

## Install

```sh
npm install database-js2
pnpm add database-js2
yarn add database-js2
bun add database-js2
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.4.2 |
| Published | 2018-01-26 |
| First published | 2017-07-24 |
| Weekly downloads | 28 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | Michael Anderson |
| Maintainers | thiagodp, manderson10 |
| Keywords | database, database-js, javascript, adodb, firebase, ini, mysql, postgres, sqlite |

## Links

- npm: https://www.npmjs.com/package/database-js2
- Repository: https://github.com/mlaanderson/database-js
- Homepage: https://github.com/mlaanderson/database-js#readme
- Issues: https://github.com/mlaanderson/database-js/issues
- npm.io page: https://npm.io/package/database-js2

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 1.4.2 (latest) — 2018-01-26
- 1.4.1 — 2018-01-26
- 3.0.0 — 2018-01-26
- 1.4.0 — 2018-01-25
- 1.3.2 — 2017-12-12
- 1.3.1 — 2017-12-12
- 1.3.0 — 2017-12-11
- 1.2.2 — 2017-08-30
- 1.2.1 — 2017-08-29
- 1.2.0 — 2017-08-18
- 1.1.0 — 2017-08-01
- 1.0.2 — 2017-07-31
- 1.0.1 — 2017-07-31
- 1.0.0 — 2017-07-31
- 0.2.1 — 2017-07-30
- … 9 more at https://npm.io/package/database-js2/versions

## README

# Database-js

__DEPRECATED__: Database-js2 has claimed the [Database-js](https://www.npmjs.com/package/database-js) package name on NPM. This package will continue to get bug fixes, but will not get new features. All new development will be published to the [Database-js](https://www.npmjs.com/package/database-js) package.

* [Install](#install)
* [API](//github.com/mlaanderson/database-js/wiki/API)
* [Examples](//github.com/mlaanderson/database-js/wiki/Examples)
* [Drivers](//github.com/mlaanderson/database-js/wiki/Drivers)
* [In the Browser](//github.com/mlaanderson/database-js/wiki/Browsers)

## About
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.

## Drivers

Currently available drivers:
- [MySQL](//github.com/mlaanderson/database-js-mysql)
- [PostgreSQL](//github.com/mlaanderson/database-js-postgresql)
- [SQLite](//github.com/mlaanderson/database-js-sqlite)
- [ActiveX Data Objects](//github.com/mlaanderson/database-js-adodb)
- [Firebase](//github.com/mlaanderson/database-js-firebase)
- [INI files](//github.com/mlaanderson/database-js-ini)
- [Excel files](//github.com/mlaanderson/database-js-xlsx)
- [CSV files](//github.com/mlaanderson/database-js-csv)
- [JSON files](//github.com/thiagodp/database-js-json)

[See here](https://github.com/mlaanderson/database-js/wiki/Drivers#implementing-a-new-driver) how to add a new driver.

## Install

```shell
npm install database-js2
```

## Usage

### SQLite
```javascript
var Connection = require('database-js2').Connection;

var conn = new Connection("sqlite:///path/to/test.sqlite");

var statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
statement.query("South Dakota").then((results) => {
    console.log(results);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
}).catch((reason) => {
    console.log(reaons);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
});
```

### MySQL
```javascript
var Connection = require('database-js2').Connection;

var conn = new Connection("mysql://user:password@localhost/test");

var statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
statement.query("South Dakota").then((results) => {
    console.log(results);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
}).catch((reason) => {
    console.log(reaons);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
});
```

### PostgreSQL
```javascript
var Connection = require('database-js2').Connection;

var conn = new Connection("postgres://user:password@localhost/test");

var statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
statement.query("South Dakota").then((results) => {
    console.log(results);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
}).catch((reason) => {
    console.log(reaons);
    conn.close().then(() => {
        process.exit(0);
    }).catch((reason) => {
        console.log(reason);
        process.exit(1);
    });
});
```

Notice that in all three examples, the only difference is the connection URL.

### ES7 Compatibility: async
Because database-js is built on Promises, it works very well with ES7 async functions. Compare the following ES7 code to the SQLite code from above. They accomplish the same thing.
```javascript
var Connection = require('database-js2').Connection;

(async function() {
    let conn, statement, results;
    
    try {
        conn = new Connection("sqlite:///path/to/test.sqlite");
        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);
    }
})();
```

## License

[MIT](https://github.com/mlaanderson/database-js/blob/master/LICENSE) (c) [mlaanderson](https://github.com/mlaanderson)

---
_Source: https://npm.io/package/database-js2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
