Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issues with sessions w/ drizzle orm #1773

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pages/sessions/basic-api/drizzle-orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { drizzle } from "drizzle-orm/mysql2";
import type { InferSelectModel } from "drizzle-orm";

const connection = await mysql.createConnection();
const db = drizzle(connection);
export const db = drizzle(connection);

export const userTable = mysqlTable("user", {
id: int("id").primaryKey().autoincrement()
Expand Down Expand Up @@ -50,7 +50,7 @@ import { drizzle } from "drizzle-orm/node-postgres";
import type { InferSelectModel } from "drizzle-orm";

const pool = new pg.Pool();
const db = drizzle(pool);
export const db = drizzle(pool);

export const userTable = pgTable("user", {
id: serial("id").primaryKey()
Expand Down Expand Up @@ -81,7 +81,7 @@ import { drizzle } from "drizzle-orm/better-sqlite3";
import type { InferSelectModel } from "drizzle-orm";

const sqliteDB = sqlite(":memory:");
const db = drizzle(sqliteDB);
export const db = drizzle(sqliteDB);

export const userTable = sqliteTable("user", {
id: integer("id").primaryKey()
Expand Down Expand Up @@ -168,6 +168,8 @@ import { eq } from "drizzle-orm";
import { encodeBase32LowerCaseNoPadding, encodeHexLowerCase } from "@oslojs/encoding";
import { sha256 } from "@oslojs/crypto/sha2";

import type { User, Session } from "./db.js";

// ...

export async function createSession(token: string, userId: number): Promise<Session> {
Expand Down Expand Up @@ -197,6 +199,8 @@ import { eq } from "drizzle-orm";
import { encodeBase32LowerCaseNoPadding, encodeHexLowerCase } from "@oslojs/encoding";
import { sha256 } from "@oslojs/crypto/sha2";

import type { User, Session } from "./db.js";

// ...

export async function validateSessionToken(token: string): Promise<SessionValidationResult> {
Expand Down Expand Up @@ -295,7 +299,7 @@ export async function validateSessionToken(token: string): Promise<SessionValida
return { session, user };
}

export async function invalidateSession(sessionId: string): void {
export async function invalidateSession(sessionId: string): Promise<void> {
await db.delete(sessionTable).where(eq(sessionTable.id, sessionId));
}

Expand Down