Skip to content

Commit

Permalink
Added libsql adapter (#680)
Browse files Browse the repository at this point in the history
Co-authored-by: pilcrowOnPaper <[email protected]>
Co-authored-by: pilcrowOnPaper <[email protected]>
  • Loading branch information
3 people authored Jul 5, 2023
1 parent b8c7332 commit 3e42405
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .auri/$t9fgucw6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/adapter-sqlite" # package name
type: "minor" # "major", "minor", "patch"
---

Add libSQL adapter
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const auth = lucia({
### Adapters for database drivers and ORMs

- [`better-sqlite3`](/database-adapters/better-sqlite3): SQLite
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
Expand Down
1 change: 1 addition & 0 deletions documentation-v2/content/main/1.basics/0.database.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ There are 2 types of adapters provided by Lucia: Regular adapters, and session a
We currently provide the following adapters:

- [`better-sqlite3`](/database-adapters/better-sqlite3)
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [Mongoose](/database-adapters/mongoose)
- [`mysql2`](/database-adapters/mysql2)
Expand Down
103 changes: 103 additions & 0 deletions documentation-v2/content/main/2.database-adapters/libsql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
menuTitle: "libSQL"
title: "libSQL adapter"
description: "Learn how to use libSQL with Lucia"
---

Adapter for [libSQL](https://github.com/libsql/libsql) provided by the SQLite adapter package.

```ts
import { libSQL } from "@lucia-auth/adapter-sqlite";
```

```ts
const libSQL: (
client: Client,
tableNames: {
user: string;
key: string;
session: string;
}
) => InitializeAdapter<Adapter>;
```

##### Parameters

Table names are automatically escaped.

| name | type | description |
| -------------------- | -------- | ------------------ |
| `client` | `Client` | Database client |
| `tableNames.user` | `string` | User table name |
| `tableNames.key` | `string` | Key table name |
| `tableNames.session` | `string` | Session table name |

## Installation

```
npm i @lucia-auth/adapter-sqlite
pnpm add @lucia-auth/adapter-sqlite
yarn add @lucia-auth/adapter-sqlite
```

## Usage

```ts
import { lucia } from "lucia";
import { libsql } from "@lucia-auth/adapter-sqlite";
import { createClient } from "@libsql/client";

const db = createClient({
url: "file:test/main.db"
});

const auth = lucia({
adapter: libsql(db, {
user: "user",
key: "user_key",
session: "user_session"
})
// ...
});
```

## libSQL schema

You can choose any table names, just make sure to define them in the adapter argument.

### User table

You can add additional columns to store user attributes.

```sql
CREATE TABLE user (
id VARCHAR(31) NOT NULL PRIMARY KEY
);
```

### Key table

Make sure to update the foreign key statement if you change the user table name.

```sql
CREATE TABLE user_key (
id VARCHAR(255) NOT NULL PRIMARY KEY,
user_id VARCHAR(15) NOT NULL,
hashed_password VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES user(id)
);
```

### Session table

You can add additional columns to store session attributes. Make sure to update the foreign key statement if you change the user table name.

```sql
CREATE TABLE user_session (
id VARCHAR(127) NOT NULL PRIMARY KEY,
user_id VARCHAR(15) NOT NULL,
active_expires BIGINT NOT NULL,
idle_expires BIGINT NOT NULL,
FOREIGN KEY (user_id) REFERENCES user(id)
);
```
8 changes: 8 additions & 0 deletions packages/adapter-sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SQLite adapter for Lucia
## Supported drivers

- [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)
- [libSQL](https://github.com/libsql/libsql) (Turso)

## Installation

Expand Down Expand Up @@ -57,3 +58,10 @@ Finally, run:
```
pnpm test.d1
```


### libSQL

```
pnpm test.libsql
```
9 changes: 7 additions & 2 deletions packages/adapter-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"build": "shx rm -rf ./dist/* && tsc",
"auri.build": "pnpm build",
"test.better-sqlite3": "tsx test/better-sqlite3/index.ts",
"test.d1": "tsx test/d1/index.ts"
"test.d1": "tsx test/d1/index.ts",
"test.libsql": "tsx test/libsql/index.ts"
},
"keywords": [
"lucia",
"lucia",
"auth",
"better-sqlite3",
Expand All @@ -40,15 +40,20 @@
},
"peerDependencies": {
"better-sqlite3": "^8.0.0",
"@libsql/client": "^0.2.1",
"lucia": "2.0.0-beta.4"
},
"peerDependenciesMeta": {
"better-sqlite3": {
"optional": true
},
"@libsql/client": {
"optional": true
}
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230518.0",
"@libsql/client": "^0.2.1",
"@lucia-auth/adapter-test": "latest",
"@miniflare/d1": "^2.14.0",
"@types/better-sqlite3": "^7.6.3",
Expand Down
Loading

0 comments on commit 3e42405

Please sign in to comment.