a tool for shared writing and social publishing

use supabase pool instead of pg and run identity check in parralel

+20 -15
+20 -15
app/api/inngest/functions/index_follows.ts
··· 3 3 import { createIdentity } from "actions/createIdentity"; 4 4 import { drizzle } from "drizzle-orm/node-postgres"; 5 5 import { inngest } from "../client"; 6 - import { Client } from "pg"; 6 + import { pool } from "supabase/pool"; 7 7 8 8 export const index_follows = inngest.createFunction( 9 9 { ··· 45 45 cursor = page.cursor || null; 46 46 if (!cursor) hasMore = false; 47 47 } 48 - await step.run("check-if-identity-exists", async () => { 49 - let { data: exists } = await supabaseServerClient 50 - .from("identities") 51 - .select() 52 - .eq("atp_did", event.data.did); 53 - if (!exists) { 54 - let client = new Client({ connectionString: process.env.DB_URL }); 55 - let db = drizzle(client); 56 - await createIdentity(db, { atp_did: event.data.did }); 57 - client.end(); 58 - } 59 - }); 60 48 let existingFollows: string[] = []; 61 49 const batchSize = 100; 62 50 let batchNumber = 0; 63 51 64 52 // Create all check batches in parallel 65 - const checkBatches = []; 53 + const checkBatches: Promise<any>[] = [ 54 + step.run("check-if-identity-exists", async () => { 55 + let { data: exists } = await supabaseServerClient 56 + .from("identities") 57 + .select() 58 + .eq("atp_did", event.data.did) 59 + .single(); 60 + if (!exists) { 61 + const client = await pool.connect(); 62 + let db = drizzle(client); 63 + let identity = await createIdentity(db, { atp_did: event.data.did }); 64 + client.release(); 65 + return identity; 66 + } 67 + }), 68 + ]; 66 69 for (let i = 0; i < follows.length; i += batchSize) { 67 70 const batch = follows.slice(i, i + batchSize); 68 71 checkBatches.push( ··· 104 107 follows: f, 105 108 })); 106 109 107 - await supabaseServerClient.from("bsky_follows").upsert(insertData); 110 + return await supabaseServerClient 111 + .from("bsky_follows") 112 + .upsert(insertData); 108 113 }), 109 114 ); 110 115 insertBatchNumber++;