A build your own ATProto adventure, OAuth already figured out for you.
at main 1.4 kB view raw
1// A key value key to the database that is mostly used for atproto session storage and state storage during the oauth session creation 2// The "stores" are divided up by a where on the store type so it can share the same interface just with that 3 4import { db } from './db'; 5import { keyValueStore } from './db/schema'; 6import { and, eq } from 'drizzle-orm'; 7 8export const SESSION_STORE = 'sessions'; 9export const STATE_STORE = 'states'; 10 11export class Cache { 12 13 db: typeof db; 14 cacheName: string; 15 16 constructor(database: typeof db, cacheName: string) { 17 this.db = database; 18 this.cacheName = cacheName; 19 } 20 21 async get(key: string) { 22 const result = await this.db.select().from(keyValueStore).where(and( 23 eq(keyValueStore.key, key), 24 eq(keyValueStore.storeName, this.cacheName) 25 )).limit(1); 26 if(result.length > 0){ 27 return result[0].value; 28 } 29 return null; 30 } 31 32 async set(key: string, value: string) { 33 return this.db.insert(keyValueStore) 34 .values({ key, value, storeName: this.cacheName, createdAt: new Date() }) 35 .onConflictDoUpdate({ 36 target: keyValueStore.key, 37 set: { value, createdAt: new Date() } 38 }); 39 } 40 41 async delete(key: string) { 42 return this.db.delete(keyValueStore).where(eq(keyValueStore.key, key)); 43 } 44 45}