A social knowledge tool for researchers built on ATProto
1import {
2 NodeSavedSession,
3 NodeSavedSessionStore,
4} from '@atproto/oauth-client-node';
5import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
6import { eq } from 'drizzle-orm';
7import { authSession } from '../repositories/schema/authSession.sql';
8
9export class DrizzleSessionStore implements NodeSavedSessionStore {
10 constructor(private db: PostgresJsDatabase) {}
11
12 async get(key: string): Promise<NodeSavedSession | undefined> {
13 const result = await this.db
14 .select()
15 .from(authSession)
16 .where(eq(authSession.key, key))
17 .limit(1);
18
19 if (result.length === 0) return undefined;
20 if (!result[0]) return undefined;
21
22 return JSON.parse(result[0].session) as NodeSavedSession;
23 }
24
25 async set(key: string, val: NodeSavedSession): Promise<void> {
26 const session = JSON.stringify(val);
27
28 await this.db
29 .insert(authSession)
30 .values({ key, session })
31 .onConflictDoUpdate({
32 target: authSession.key,
33 set: { session },
34 });
35 }
36
37 async del(key: string): Promise<void> {
38 await this.db.delete(authSession).where(eq(authSession.key, key));
39 }
40}