# @comdec_/node_sqlite3

> A small node package to create basic sqlite binding for nodeJS using python

Latest version **1.1.1** (published 2022-06-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install @comdec_/node_sqlite3
pnpm add @comdec_/node_sqlite3
yarn add @comdec_/node_sqlite3
bun add @comdec_/node_sqlite3
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-06-23 |
| First published | 2022-06-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Comdec_ |
| Maintainers | comdec_ |
| Keywords | sqlite, sqlite3, python, sql, async |

## Links

- npm: https://www.npmjs.com/package/@comdec_/node_sqlite3
- Repository: https://github.com/Comdec35000/node_sqlite3
- Homepage: https://github.com/Comdec35000/node_sqlite3#readme
- Issues: https://github.com/Comdec35000/node_sqlite3/issues
- npm.io page: https://npm.io/package/@comdec_/node_sqlite3

## Dependencies (1)

- [ascii-table](https://npm.io/package/ascii-table.md) ^0.0.9

## 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

- 1.1.1 (latest) — 2022-06-23
- 1.1.0 — 2022-06-23
- 1.0.0 — 2022-06-23

## README

# Node SQLite3

### Summary
- [Node SQLite3](#node-sqlite3)
    - [Summary](#summary)
  - [Introduction](#introduction)
  - [Requirement](#requirement)
  - [Installation](#installation)
  - [QuickStart](#quickstart)
      - [Connecting](#connecting)
      - [Running SQL](#running-sql)
      - [Adding arguments](#adding-arguments)
      - [Running multiple queries](#running-multiple-queries)
      - [Running SQL files](#running-sql-files)
  - [Contribution and issues](#contribution-and-issues)
      - [Contributors](#contributors)

## Introduction
Node SQLite is a small NodeJS package to create simple SQLite3 bindings using the pyhton standard libray.  
It allow you to run easly SQL queries and mutation with one single async function. Like so :

```js
const sqlite = require("node_sqlite3");

const connection = new sqlite.Connection("path/to/db/file");
const { rows } = await connection.runSql("SELECT * FROM mytable WHERE test > 1");
```

## Requirement

+ You have to install [`python3.8`](https://www.python.org/downloads/) or higher
+ You have to install [`nodejs v16.8.0`](https://nodejs.org/en/download/) or higher

## Installation

```sh
npm i @comdec_/node_sqlite3
# You can either install the package with npm or yarn
yarn add @comdec_/node_sqlite3
```

Then import the module at the top of your file :

```js
const sqlite = require("@comdec_/node_sqlite3");
```

## QuickStart

#### Connecting
You can connect to your databae simply by importing the package and instantiating the connection :
```js
const sqlite = require("@comdec_/node_sqlite3");

const connection = new sqlite.Connection("path/to/db/file");
```

#### Running SQL
Then, using this connection, you can run queries and mutations using the `runSql` function : 
```js
const { rows } = await connection.runSql("SELECT * FROM mytable WHERE test > 1");
```

This function is async and takes as parameter a `string`, witch represents your query or your mutation. It will return a Promise witch contain a `QueryResponse`.

The response can either contain an error, nothing or an `Array` of rows, witch you can access with the `rows` attribute of `QueryResponse`.

#### Adding arguments
You can aslo add argument to your query/mutation. To do so, simply add an array with these arguments, the function will automatically replace each "*?*" with the corresponding index argument : 
```js
// This is equal to run this : INSERT INTO users(name, email, phone) VALUES ("John Doe", "example@domain.com", "+33 6 12 34 56 78");
await connection.runSql(
  "INSERT INTO users(name, email, phone) VALUES (?, ?, ?);", 
  ["John Doe", "example@domain.com", "+33 6 12 34 56 78"]
);
```

#### Running multiple queries
Running the same query or mutation multiple times with differents armuments is sometimes boring, but you can use the `Connection#runMany` method to be more efficient :
```js
// This is equal to run this : INSERT INTO users(name, email, phone) VALUES ("John Doe", "example@domain.com", "+33 6 12 34 56 78");
await connection.runMany(
  "INSERT INTO users(name, id) VALUES (?, ?);", 
  [
    ["John Doe", 1],
    ["John Doe", 2],
    ["John Doe", 3]
  ]
);

console.log((await connection.runSql("SELECT * FROM users")).toString());
// .---------------.
// |   name   | id |
// |----------|----|
// | John Doe |  1 |
// | John Doe |  2 |
// | John Doe |  3 |
// '---------------'
```
The method return an array witch contain all of the `QueryResponse` of each SQL mutation/query.

#### Running SQL files
You can also run entire SQL files : 
index.js : 
```js
await connection.runSql("CREATE TABLE IF NOT EXISTS users (name VARCHAR(40), id INT);")
await connection.runFile('./test.sql');
  
console.log((await connection.runSql("SELECT * FROM users")).toString());
```

test.sql :
```sql
INSERT INTO users(name, id) VALUES ("John Doe", 3);

INSERT INTO users(name, id) VALUES ("John Doe", 4);

INSERT INTO users(name, id) VALUES (
  "John Doe", 5
);
```

This will print 
```
.---------------.
|   name   | id |
|----------|----|
| John Doe |  3 |
| John Doe |  4 |
| John Doe |  5 |
'---------------'
```

The method takes as argument the file path (relative or absolute) witch represents the location of the file you want to execute and return an array witch contain all of the `QueryResponse` of each SQL mutation/query.

## Contribution and issues
If you encounter an issue or want to ask for a feature, feel free to [create an issue](https://github.com/Comdec35000/node_sqlite3/issues/new)

If you want to contribute, you can create some pull requests to fix issues, all the help is welcome !

#### Contributors 
<a href="https://github.com/comdec35000/node_sqlite3/graphs/contributors">
  <img src="https://contributors-img.web.app/image?repo=comdec35000/node_sqlite3" />
</a>

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