lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
9.46k stars 485 forks source link

v3: Drizzle Adapter #1265

Closed pilcrowonpaper closed 11 months ago

pilcrowonpaper commented 11 months ago

Description

I made this issue mostly to gauge interest.

This is for v3, but It would look something like this:

import { drizzle } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";
import { text, integer, sqliteTable } from "drizzle-orm/sqlite-core";

export const sqlite = new Database("sqlite.db");
export const db = drizzle(sqlite);

export const userTable = sqliteTable("user", {
  id: text("id").notNull().primaryKey(),
  camelCaseField: text("snake_case_column"),
});

export const sessionTable = sqliteTable("session", {
  id: text("id").notNull().primaryKey(),
  expiresAt: integer("expires_at").notNull(),
  userId: text("user_id")
    .notNull()
    .references(() => userTable.id),
});
import { DrizzleAdapter } from "@lucia-auth/adapter-drizzle";
import { Lucia } from "lucia";
import { db, userTable, sessionTable } from "./db.js";

export const auth = new Lucia(new DrizzleAdapter(db, sessionTable, userTable), {
  getUserAttributes: (attributes) => {
    return {
      camelCaseField: attributes.camelCaseField,
    };
  },
});

This comes with the benefit of attributes passed to getUserAttributes() option being the Drizzle field (camelCaseField), not the underlying column (snake_case_column).

I'm still on the fence on this one since it's already quite simple to set up a project with Drizzle. We already support most drivers that Drizzle do You do have to rely on the underlying database columns, and not Drizzle fields though.

import { BetterSqlite3Adapter } from "@lucia-auth/adapter-sqlite";
import { Lucia } from "lucia";
import { sqlite } from "./db.js";

export const auth = new Lucia(
  new BetterSqlite3Adapter(sqlite, {
    user: "user",
    session: "user_session",
  }),
  {
    getUserAttributes: (attributes) => {
      return {
        camelCaseField: attributes.snake_case_column,
      };
    },
  }
);

Another aspect that's making me questioning this is that we'd need to build 3 adapters, one for SQLite, MySQL, and Postgres. Not sure if that's worth it.

ekobedevon commented 11 months ago

As long as the option to use the current option I don't think adding an adapter would be a problem. But at the same time as someone who uses Lucia & dribble in the same project, the current setup is so simple and require less dependencies is always good imo

dromzeh commented 11 months ago

this would be really useful tbh as it's pretty tedious dealing with the camelCase and snake_case "inconsistencies" sometimes also - don't mind contributing towards the adapters for sqlite and mysql if needed, thank you for your work :)

AlexisWalravens commented 11 months ago

I think this would be very useful in the case you want to use the new http proxy drivers for either mysql / postgres / sqlite: https://orm.drizzle.team/docs/quick-mysql/http-proxy

The docs is a bit lacking but it is better explained in the release notes: https://github.com/drizzle-team/drizzle-orm/releases/tag/0.29.0

🎉 New MySQL Proxy Driver A new driver has been released, allowing you to create your own implementation for an HTTP driver using a MySQL database. You can find usage examples in the ./examples/mysql-proxy folder

You need to implement two endpoints on your server that will be used for queries and migrations(Migrate endpoint is optional and only if you want to use drizzle migrations). Both the server and driver implementation are up to you, so you are not restricted in any way. You can add custom mappings, logging, and much more

You can find both server and driver implementation examples in the ./examples/mysql-proxy folder

Also I currently use Lucia and Drizzle in a project of mine and it's a bit weird to have both a pool connection and a client connection from Drizzle, the pool connection is used by Lucia but I use the client connection for everything else.

Anyways thanks for considering doing it!

pilcrowonpaper commented 11 months ago

Done with #1269

Only available for v3