https://github.com/marchellodev/authjs-adapter-sql
Mysql/Postgres adapter for Auth.js
https://github.com/marchellodev/authjs-adapter-sql
Last synced: about 2 months ago
JSON representation
Mysql/Postgres adapter for Auth.js
- Host: GitHub
- URL: https://github.com/marchellodev/authjs-adapter-sql
- Owner: marchellodev
- License: isc
- Created: 2023-06-10T17:54:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-10T18:17:01.000Z (about 3 years ago)
- Last Synced: 2025-12-31T04:58:59.774Z (6 months ago)
- Language: TypeScript
- Homepage:
- Size: 435 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Sql Adapter - NextAuth.js / Auth.js
[](https://github.com/roelandmoors/authjs-adapter-sql/actions/workflows/mysql_test.yml)
[](https://github.com/roelandmoors/authjs-adapter-sql/actions/workflows/postgres_test.yml)
---
This adapter uses plain sql statements to integrate with [Authjs](https://authjs.dev/).
Support for Mysql ([mysql2](https://github.com/sidorares/node-mysql2)) and Postgres ([pg-promise](https://github.com/vitaly-t/pg-promise)).
Also works with the [PlanetScale Serverless Driver](https://github.com/planetscale/database-js), [Neon Serverless Driver](https://github.com/neondatabase/serverless), [Kysely](https://kysely-org.github.io/kysely/), [Knex](https://knexjs.org/) and [Slonik](https://github.com/gajus/slonik).
You can create a custom helper function to support other drivers if needed.
## Configuration
You can set an optional table prefix/postgres schema.
```ts
const config = {
prefix: "auth_", //or for example 'auth.' for a postgres schema
};
export default NextAuth({
adapter: SqlAdapter(helpers, config),
providers: [],
});
```
## Default vs named imports
If you have problems with default imports in modules, than you can also try named imports.
Instead of this:
```ts
import SqlAdapter from "authjs-adapter-sql";
import buildMysql2Helpers from "authjs-adapter-sql/mysql2";
```
try this:
```ts
import { SqlAdapter } from "authjs-adapter-sql";
import { buildMysql2Helpers } from "authjs-adapter-sql/mysql2";
```
## How to use with Mysql2
Install:
```
npm i authjs-adapter-sql mysql2
```
use [mysql.sql](mysql.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import * as mysql from "mysql2/promise";
import buildMysql2Helpers from "authjs-adapter-sql/mysql2";
function getConnection() {
return mysql.createConnection({
host: "127.0.0.1",
user: "root",
database: "fancydb",
});
}
// you can create your own helpers for custom logic
const mysql2Helpers = buildMysql2Helpers(getConnection);
export default NextAuth({
adapter: SqlAdapter(mysql2Helpers),
providers: [],
});
```
When using the new app dir in NextJs you may need this:
```ts
const nextConfig = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ["mysql2"],
},
};
```
## How to use with PlanetScale serverless driver
Install:
```
npm i authjs-adapter-sql @planetscale/database
```
use [mysql.sql](mysql.sql) to create the tables.
```ts
import SqlAdapter from "authjs-adapter-sql";
import buildPlanetScaleHelpers from "authjs-adapter-sql/planetscale";
const client = new Client(config);
// you can create your own helpers for custom logic
const psHelpers = buildPlanetScaleHelpers(client);
export default NextAuth({
adapter: SqlAdapter(psHelpers),
providers: [],
});
```
- https://planetscale.com/docs/learn/operating-without-foreign-key-constraints
- https://planetscale.com/docs/tutorials/planetscale-serverless-driver
## How to use with pg-promise
Install:
```
npm i authjs-adapter-sql pg-promise
```
use [postgres.sql](postgres.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import pgPromise from "pg-promise";
import buildPgPromiseHelpers from "../src/pg-promise";
const pgp = pgPromise();
const db = pgp("postgres://postgres:postgres@localhost:5432/postgres");
function getConnection() {
return db;
}
// you can create your own helpers for custom logic
const pgHelpers = buildPgPromiseHelpers(getConnection);
export default NextAuth({
adapter: SqlAdapter(pgHelpers),
providers: [],
});
```
## How to use with Neon Serverless Driver
Install:
```
npm i authjs-adapter-sql @neondatabase/serverless
```
use [postgres.sql](postgres.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import buildNeonHelpers from "authjs-adapter-sql/neon";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
// you can create your own helpers for custom logic
const helpers = buildNeonHelpers(pool);
export default NextAuth({
adapter: SqlAdapter(helpers),
providers: [],
});
```
## How to use with Kysely
Install:
```
npm i kysely mysql2 (or pg)
```
use [mysql.sql](mysql.sql) or [postgres.sql](postgres.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import { Kysely, MysqlDialect } from "kysely";
import buildKyselyHelpers from "authjs-adapter-sql/kysely";
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
const db = new Kysely({
dialect: new MysqlDialect({
pool,
}),
});
// you can create your own helpers for custom logic
const helpers = buildKyselyHelpers(db, "mysql"); //or postgres
export default NextAuth({
adapter: SqlAdapter(helpers),
providers: [],
});
```
## How to use with Knex
Install:
```
npm i knex mysql2 (or pg)
```
use [mysql.sql](mysql.sql) or [postgres.sql](postgres.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import { Knex, knex } from "knex";
import buildKnexHelpers from "authjs-adapter-sql/knex";
const config: Knex.Config = {
client: "mysql2",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
},
};
const knexInstance = knex(config);
// you can create your own helpers for custom logic
const helpers = buildKnexHelpers(knexInstance, "mysql"); //or postgres
export default NextAuth({
adapter: SqlAdapter(helpers),
providers: [],
});
```
## How to use with Slonik
Install:
```
npm i slonik pg
```
use [postgres.sql](postgres.sql) to create the tables.
(you can add foreign keys if needed)
```ts
import SqlAdapter from "authjs-adapter-sql";
import buildSlonikHelpers from "authjs-adapter-sql/slonik";
import { createPool } from "slonik";
const poolPromise = createPool("postgres://postgres:postgres@localhost:5432/postgres");
function getConnection() {
return poolPromise;
}
// you can create your own helpers for custom logic
const helpers = buildSlonikHelpers(getConnection);
export default NextAuth({
adapter: SqlAdapter(helpers),
providers: [],
});
```