a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

use update cache ctx

Squashed commit of the following:

commit 4f85bed4916bf575e8561e45c0105892601257d4
Author: Jared Pereira <jared@awarm.space>
Date: Wed Aug 27 09:27:49 2025 +0900

use cacheContext and apply facts individually

+45 -39
+8 -2
app/api/rpc/[command]/push.ts
··· 9 9 import { drizzle } from "drizzle-orm/node-postgres"; 10 10 import { Lock } from "src/utils/lock"; 11 11 import { pool } from "supabase/pool"; 12 - import { serverMutationContext } from "src/replicache/serverMutationContext"; 13 12 14 13 const mutationV0Schema = z.object({ 15 14 id: z.number(), ··· 86 85 .select() 87 86 .from(permission_token_rights) 88 87 .where(eq(permission_token_rights.token, token.id)); 88 + let { getContext, flush } = cachedServerMutationContext( 89 + tx, 90 + token.id, 91 + token_rights, 92 + ); 93 + 89 94 let lastMutations = new Map<string, number>(); 90 95 console.log(pushRequest.mutations.map((m) => m.name)); 91 96 for (let mutation of pushRequest.mutations) { ··· 97 102 continue; 98 103 } 99 104 try { 100 - let ctx = serverMutationContext(tx, token.id, token_rights); 105 + let ctx = getContext(mutation.clientID, mutation.id); 101 106 await mutations[name](mutation.args as any, ctx); 102 107 } catch (e) { 103 108 console.log( ··· 124 129 target: replicache_clients.client_id, 125 130 set: { last_mutation: sql`excluded.last_mutation` }, 126 131 }); 132 + await flush(); 127 133 }); 128 134 timeProcessingMutations = performance.now() - start; 129 135
+37 -37
src/replicache/cachedServerMutationContext.ts
··· 175 175 let factWrites = writeCache.flatMap((f) => 176 176 f.type === "del" ? [] : [f.fact], 177 177 ); 178 - if (factWrites.length > 0) 179 - await tx 180 - .insert(facts) 181 - .values( 182 - await Promise.all( 183 - factWrites.map(async (f) => { 184 - let attribute = Attributes[f.attribute as Attribute]; 185 - let data = f.data; 186 - if ( 187 - attribute.type === "text" && 188 - attribute.cardinality === "one" 189 - ) { 190 - let values = Object.values( 191 - textAttributeWriteCache[`${f.entity}-${f.attribute}`] || {}, 192 - ); 193 - if (values.length > 0) { 194 - let existingFact = await scanIndex.eav(f.entity, f.attribute); 195 - if (existingFact[0]) values.push(existingFact[0].data.value); 196 - let updateBytes = Y.mergeUpdates( 197 - values.map((v) => base64.toByteArray(v)), 198 - ); 199 - data.value = base64.fromByteArray(updateBytes); 200 - } 201 - } 178 + if (factWrites.length > 0) { 179 + for (let f of factWrites) { 180 + let attribute = Attributes[f.attribute as Attribute]; 181 + let data = f.data; 182 + if (attribute.type === "text" && attribute.cardinality === "one") { 183 + let values = Object.values( 184 + textAttributeWriteCache[`${f.entity}-${f.attribute}`] || {}, 185 + ); 186 + if (values.length > 0) { 187 + let existingFact = await scanIndex.eav(f.entity, f.attribute); 188 + if (existingFact[0]) values.push(existingFact[0].data.value); 189 + let updateBytes = Y.mergeUpdates( 190 + values.map((v) => base64.toByteArray(v)), 191 + ); 192 + data.value = base64.fromByteArray(updateBytes); 193 + } 194 + } 202 195 203 - return { 204 - id: f.id, 205 - entity: f.entity, 206 - data: driz.sql`${data}::jsonb`, 207 - attribute: f.attribute, 208 - }; 209 - }), 210 - ), 211 - ) 212 - .onConflictDoUpdate({ 213 - target: facts.id, 214 - set: { data: driz.sql`excluded.data` }, 215 - }); 196 + await tx.transaction((tx2) => 197 + tx2 198 + .insert(facts) 199 + .values({ 200 + id: f.id, 201 + entity: f.entity, 202 + data: driz.sql`${data}::jsonb`, 203 + attribute: f.attribute, 204 + }) 205 + .onConflictDoUpdate({ 206 + target: facts.id, 207 + set: { data: driz.sql`excluded.data` }, 208 + }) 209 + .catch((e) => 210 + console.log(`error on inserting fact: `, JSON.stringify(e)), 211 + ), 212 + ); 213 + } 214 + } 216 215 if (deleteEntitiesCache.length > 0) 217 216 await tx 218 217 .delete(entities) ··· 242 241 eavCache.clear(); 243 242 permissionsCache = {}; 244 243 entitiesCache = []; 244 + permissionsCache = {}; 245 245 deleteEntitiesCache = []; 246 246 }; 247 247