# promised.sqlite

> You can use async/await for sqlite3

Latest version **0.2.0** (published 2021-11-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install promised.sqlite
pnpm add promised.sqlite
yarn add promised.sqlite
bun add promised.sqlite
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-11-07 |
| First published | 2021-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 16.0.0 |
| Dependencies | 1 |
| Unpacked size | 877.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bynaki |
| Maintainers | bynaki |
| Keywords | node, typescript, module, sqlite3, async, promise |

## Links

- npm: https://www.npmjs.com/package/promised.sqlite
- Repository: https://github.com/bynaki/promised.sqlite
- Homepage: https://github.com/bynaki/promised.sqlite#readme
- Issues: https://github.com/bynaki/promised.sqlite/issues
- npm.io page: https://npm.io/package/promised.sqlite

## Dependencies (1)

- [sqlite3](https://npm.io/package/sqlite3.md) ^5.0.2

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2021-11-07
- 0.1.0 — 2021-11-07

## README

# Promised SQLite

## Introduction
You can use async/await for sqlite3.

## Install
```shell
> npm install promised.sqlite
```

## Usage

### In memory based database
```js
import {
  open,
} from 'promised.sqlite'

// open the database
const db = await open(':memory:')
console.log('Connected to the in-memory SQlite database.')

// close the database connection
await db.close()
console.log('Close the database connection.')
```

### Disk file based database
```js
import {
  open,
  OPEN_READWRITE,
} from 'promised.sqlite'

// open the database
const db = await open('./assets/chinook.db', OPEN_READWRITE)
console.log('Connected to the database.')

const row = await db.get(`SELECT PlaylistId as id,
                          Name as name
                          FROM playlists`)
console.log(row.id + '\t' + row.name)

// close the database connection
await db.close()
```

### Querying all rows with all() method
```js
import {
  open,
} from 'promised.sqlite'

const sql = `SELECT DISTINCT Name name FROM playlists
              ORDER BY name`

// open the database
const db = await open('./assets/chinook.db')

// querying all rows with all() method
const rows = await db.all(sql, [])
rows.forEach(row => {
  console.log(row.name)
})

// close the database connection
await db.close()
```

### Query the first row in the result set
```js
import {
  open,
} from 'promised.sqlite'

// open the database
const db = await open('./assets/chinook.db')
const sql = `SELECT PlaylistID id,
                  Name name
              FROM playlists
              WHERE PlaylistId = ?`
const playlistId = 1

// first row only
const row = await db.get(sql, [playlistId])
row
? console.log(row.id, row.name)
: console.log(`No playlist found with the id ${playlistId}`)

// close the database connection
await db.close()
```

### Query rows with each() method
```js
import {
  open,
} from 'promised.sqlite'

// open the database
const db = await open('./assets/chinook.db')
const sql = `SELECT FirstName firstName,
                  LastName lastName,
                  Email email
              FROM customers
              WHERE Country = ?
              ORDER BY FirstName`
const rows: string[] = []

for await (let row of db.each(sql, ['USA'])) {
  const r = `${row.firstName} ${row.lastName} - ${row.email}`
  console.log(r)
}

await db.close()
```

### Insert on row into a table
```js
import {
  open,
} from 'promised.sqlite'

const db = await open(':memory:')

// insert one row into the langs table
await db.run('CREATE TABLE langs(name text)')
const res = await db.run(`INSERT INTO langs(name) VALUES(?)`, ['C'])

// get the last insert id
console.log(`A row has been inserted with rowid ${res.lastID}`)

// close the database connection
await db.close()
```

### Insert multiple rows into a table at a time
```js
import {
  open,
} from 'promised.sqlite'

// open the database connection
const db = await open(':memory:')
const languages = ['C++', 'Python', 'Java', 'C#', 'Go']

// construct the insert statement with multiple placehoders
// based on the number of rows
const placeholders = languages.map(lan => '(?)').join(',')
const sql = 'INSERT INTO langs(name) VALUES ' + placeholders

// output the INSERT statement
console.log(sql)
await db.run('CREATE TABLE langs(name text)')
const res = await db.run(sql, languages)
console.log(`Rows inserted ${res.changes}`)

// close the database connection
db.close()
```

### Updating Data in SQLite Database from a Node.js Application
```js
import {
  open,
} from 'promised.sqlite'

// open the database connection
const db = await open(':memory:')
const languages = ['C++', 'Python', 'Java', 'C#', 'Go', 'C']

// construct the insert statement with multiple placehoders
// based on the number of rows
const placeholders = languages.map(lan => '(?)').join(',')
const sqlInsert = 'INSERT INTO langs(name) VALUES ' + placeholders

// update statement
const data = ['Ansi C', 'C']
const sqlUpdate = `UPDATE langs
              SET name = ?
              WHERE name = ?`

// create table
await db.run('CREATE TABLE langs(name text)')

// insert rows
const insertRes = await db.run(sqlInsert, languages)
console.log(`Rows inserted: ${insertRes.changes}`)

// update
const updateRes = await db.run(sqlUpdate, data)
console.log(`Row(s) updated: ${updateRes.changes}`)

// close the database connection
await db.close()
```

### Deleting Data in SQLite Database from a Node.js Application
```js
import {
  open,
} from 'promised.sqlite'

// open the database connection
const db = await open(':memory:')
const languages = ['C++', 'Python', 'Java', 'C#', 'Go']

// construct the insert statement with multiple placehoders
// based on the number of rows
const placeholders = languages.map(lan => '(?)').join(',')
const sql = 'INSERT INTO langs(name) VALUES ' + placeholders

// create table
await db.run('CREATE TABLE langs(name text)')
const insertRes = await db.run(sql, languages)
console.log(`Rows inserted: ${insertRes.changes}`)

const id = 1
// delete a row based on id
const deleteRes = await db.run(`DELETE FROM langs WHERE rowid=?`, id)
console.log(`Row(s) deleted: ${deleteRes.changes}`)

// close the database connection
await db.close()
```

## Reference

- [sqlite3 tutorial](https://www.sqlitetutorial.net/sqlite-nodejs/)

## License

Copyright (c) bynaki. All rights reserved.

Licensed under the MIT License.

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