Skip to content

Commit

Permalink
fix(adapter-drizzle): get session and user in one query (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryScaletta authored Feb 6, 2024
1 parent 1196b67 commit 901355e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 77 deletions.
38 changes: 13 additions & 25 deletions packages/adapter-drizzle/src/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ export class DrizzleMySQLAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.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)
];
}

public async getUserSessions(userId: string): Promise<DatabaseSession[]> {
Expand Down Expand Up @@ -69,26 +77,6 @@ export class DrizzleMySQLAdapter implements Adapter {
public async deleteExpiredSessions(): Promise<void> {
await this.db.delete(this.sessionTable).where(lte(this.sessionTable.expiresAt, new Date()));
}

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

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]);
}
}

export type MySQLUserTable = MySqlTableWithColumns<{
Expand Down
38 changes: 13 additions & 25 deletions packages/adapter-drizzle/src/drivers/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ export class DrizzlePostgreSQLAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.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)
];
}

public async getUserSessions(userId: string): Promise<DatabaseSession[]> {
Expand Down Expand Up @@ -69,26 +77,6 @@ export class DrizzlePostgreSQLAdapter implements Adapter {
public async deleteExpiredSessions(): Promise<void> {
await this.db.delete(this.sessionTable).where(lte(this.sessionTable.expiresAt, new Date()));
}

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

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]);
}
}

export type PostgreSQLUserTable = PgTableWithColumns<{
Expand Down
41 changes: 14 additions & 27 deletions packages/adapter-drizzle/src/drivers/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ export class DrizzleSQLiteAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId))
.get();
if (!result) return [null, null];
return [
transformIntoDatabaseSession(result.session),
transformIntoDatabaseUser(result.user)
];
}

public async getUserSessions(userId: string): Promise<DatabaseSession[]> {
Expand Down Expand Up @@ -80,28 +89,6 @@ export class DrizzleSQLiteAdapter implements Adapter {
.delete(this.sessionTable)
.where(lte(this.sessionTable.expiresAt, Math.floor(Date.now() / 1000)));
}

private async getSession(sessionId: string): Promise<DatabaseSession | null> {
const result = await this.db
.select()
.from(this.sessionTable)
.where(eq(this.sessionTable.id, sessionId))
.get();
if (!result) return null;
return transformIntoDatabaseSession(result);
}

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))
.get();
if (!result) return null;
return transformIntoDatabaseUser(result);
}
}

export type SQLiteUserTable = SQLiteTableWithColumns<{
Expand Down

0 comments on commit 901355e

Please sign in to comment.