# @libsql/sqlite3

> node-sqlite3-compatible API for libSQL

Latest version **0.3.1** (published 2023-06-12) · MIT license · 39.8K weekly downloads

## Install

```sh
npm install @libsql/sqlite3
pnpm add @libsql/sqlite3
yarn add @libsql/sqlite3
bun add @libsql/sqlite3
```

## Health

**Score 35/100 (D)** — status: abandoned.

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

Warnings: no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2023-06-12 |
| First published | 2023-03-13 |
| Weekly downloads | 39.8K |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | just_one_more_time, honzasp, penberg |
| Keywords | libsql, database, sqlite, serverless, vercel, netlify, lambda |

## Links

- npm: https://www.npmjs.com/package/@libsql/sqlite3
- Repository: https://github.com/libsql/libsql-node-sqlite3
- Homepage: https://github.com/libsql/libsql-node-sqlite3#readme
- Issues: https://github.com/libsql/libsql-node-sqlite3/issues
- npm.io page: https://npm.io/package/@libsql/sqlite3

## Dependencies (1)

- [@libsql/hrana-client](https://npm.io/package/@libsql/hrana-client.md) ^0.4.1

## Alternatives

- [@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
- [database-js2](https://npm.io/package/database-js2.md) — 28 weekly downloads

## Recent versions

- 0.3.1 (latest) — 2023-06-12
- 0.3.0 — 2023-06-12
- 0.2.2 — 2023-06-12
- 0.2.1 — 2023-05-12
- 0.2.0 — 2023-04-25
- 0.1.4 — 2023-03-31
- 0.1.3 — 2023-03-28
- 0.1.2 — 2023-03-15
- 0.1.1 — 2023-03-14
- 0.1.0 — 2023-03-13

## README

# `sqlite3` wrapper for libSQL

This package is a drop-in replacement of the Node package [`sqlite3`](https://www.npmjs.com/package/sqlite3) for use with [sqld](https://github.com/libsql/sqld) (the libSQL server mode). Instead of opening a local SQLite database, this package connects to sqld using a WebSocket using the [Hrana protocol](https://github.com/libsql/hrana-client-ts).

## Usage

You can get many applications that use the `sqlite3` package work with sqld just by replacing `require('sqlite3')` with `require('@libsql/sqlite3')`, and using a `libsql://` URL instead of a filename:

```javascript
const sqlite3 = require('@libsql/sqlite3').verbose();
const db = new sqlite3.Database('libsql://localhost:2023?tls=0');

db.serialize(() => {
    db.run('CREATE TABLE lorem (info TEXT)');

    const stmt = db.prepare('INSERT INTO lorem VALUES (?)');
    for (let i = 0; i < 10; i++) {
        stmt.run('Ipsum ' + i);
    }
    stmt.finalize();

    db.each('SELECT rowid AS id, info FROM lorem', (err, row) => {
        console.log(row.id + ': ' + row.info);
    });
});

db.close();
```

### URL

The library accepts the same URL schemas as [`@libsql/client`][libsql-client-ts]:

- `http://` and `https://` connect to a libsql server over HTTP,
- `ws://` and `wss://` connect to the server over WebSockets,
- `libsql://` connects to the server using the default protocol (which is now HTTP). `libsql://` URLs use TLS by default, but you can use `?tls=0` to disable TLS (e.g. when you run your own instance of the server locally).

To use a JWT for authentication, you can use the `authToken` query parameter (for example,
`ws://localhost?authToken=<token>`).

You can also pass a `file:` URL to `new sqlite3.Database()` to use the original `sqlite3` package. The returned database will be a `Database` from `sqlite3`, not the `Database` from `@libsql/sqlite3`. You will need to install `sqlite3` yourself, this package does not depend on `sqlite3`.

[libsql-client-ts]: https://github.com/libsql/libsql-client-ts

### Usage with Knex

You can use this package with Knex.js by replacing the `sqlite3` package in the SQLite dialect.

#### JavaScript

```javascript
const Knex = require("knex");
const Client_SQLite3 = require("knex/lib/dialects/sqlite3");

class Client_Libsql extends Client_SQLite3 {
    _driver() {
        return require("@libsql/sqlite3");
    }
}

const knex = Knex({
    client: Client_Libsql,
    connection: {
        filename: url,
    },
});
```

#### TypeScript

```typescript
import { Knex, knex } from "knex";
const Client_SQLite3 = require("knex/lib/dialects/sqlite3");

class Client_Libsql extends Client_SQLite3 {
    _driver() {
        return require("@libsql/sqlite3");
    }
}
const db = knex({
    client: Client_Libsql as any,
    connection: {
        filename: url,
    },
});
```

## Unsupported features

Most APIs exposed by `sqlite3` should work as expected, but the following features are not implemented:

- Flags passed to `new Database()` (they are ignored)
- `Database.configure()`
- `Database.loadExtension()`
- `Database.interrupt()`

## License

This project is licensed under the MIT license.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in `@libsql/sqlite3` by you, shall be licensed as MIT, without any additional terms or conditions.

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