Skip to content

Commit

Permalink
Drizzle SQLite adapter: Revert previous optimization for D1 compatibi…
Browse files Browse the repository at this point in the history
…lity (#1500)
  • Loading branch information
pilcrowonpaper authored Mar 21, 2024
1 parent ddc7c4d commit fd66ddb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/adapter-drizzle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @lucia-auth/adapter-drizzle

## 1.0.6

- Revert previous optimization for D1 support.

## 1.0.5

- Fix table types ([#1495](https://github.com/lucia-auth/lucia/pull/1495)).
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lucia-auth/adapter-drizzle",
"version": "1.0.5",
"version": "1.0.6",
"description": "Drizzle ORM adapter for Lucia",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
33 changes: 23 additions & 10 deletions packages/adapter-drizzle/src/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ export class DrizzleMySQLAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
// https://github.com/drizzle-team/drizzle-orm/issues/555
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
}

private async getSession(sessionId: string): Promise<DatabaseSession | null> {
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.select()
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return [null, null];
return [
transformIntoDatabaseSession(result[0].session),
transformIntoDatabaseUser(result[0].user)
];
if (result.length !== 1) return null;
return transformIntoDatabaseSession(result[0]);
}

public async getUserSessions(userId: UserId): Promise<DatabaseSession[]> {
Expand All @@ -56,6 +58,17 @@ export class DrizzleMySQLAdapter implements Adapter {
});
}

private async getUserFromSessionId(sessionId: string): Promise<DatabaseUser | null> {
const { _, $inferInsert, $inferSelect, getSQL, ...userColumns } = this.userTable;
const result = await this.db
.select(userColumns)
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return null;
return transformIntoDatabaseUser(result[0]);
}

public async setSession(session: DatabaseSession): Promise<void> {
await this.db.insert(this.sessionTable).values({
id: session.id,
Expand Down

1 comment on commit fd66ddb

@jdnavarro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should've been applied to sqlite driver, not the mysql one, right?

Please sign in to comment.