A social knowledge tool for researchers built on ATProto
at development 37 lines 1.1 kB view raw
1import { 2 NodeSavedState, 3 NodeSavedStateStore, 4} from '@atproto/oauth-client-node'; 5import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'; 6import { eq } from 'drizzle-orm'; 7import { authState } from '../repositories/schema/authState.sql'; 8 9export class DrizzleStateStore implements NodeSavedStateStore { 10 constructor(private db: PostgresJsDatabase) {} 11 12 async get(key: string): Promise<NodeSavedState | undefined> { 13 const result = await this.db 14 .select() 15 .from(authState) 16 .where(eq(authState.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].state) as NodeSavedState; 23 } 24 25 async set(key: string, val: NodeSavedState): Promise<void> { 26 const state = JSON.stringify(val); 27 28 await this.db.insert(authState).values({ key, state }).onConflictDoUpdate({ 29 target: authState.key, 30 set: { state }, 31 }); 32 } 33 34 async del(key: string): Promise<void> { 35 await this.db.delete(authState).where(eq(authState.key, key)); 36 } 37}