Skip to content

Commit

Permalink
feat(store-sync): key decoded records by last updated block number
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaaa committed Jan 5, 2024
1 parent f6f4028 commit 40c4fd8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/store-sync/src/postgres-decoded/buildTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { buildColumn } from "./buildColumn";

export const metaColumns = {
__keyBytes: asHex("__key_bytes").primaryKey(),
__lastUpdatedBlockNumber: asBigInt("__last_updated_block_number", "numeric"),
__lastUpdatedBlockNumber: asBigInt("__last_updated_block_number", "numeric").primaryKey(),
} as const satisfies Record<string, PgColumnBuilderBase>;

type PgTableFromSchema<TKeySchema extends KeySchema, TValueSchema extends ValueSchema> = PgTableWithColumns<{
Expand Down
10 changes: 7 additions & 3 deletions packages/store-sync/src/postgres-decoded/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export async function createStorageAdapter<TConfig extends StoreConfig = StoreCo
and(
eq(internalTables.recordsTable.address, log.address),
eq(internalTables.recordsTable.tableId, log.args.tableId),
eq(internalTables.recordsTable.keyBytes, keyBytes)
eq(internalTables.recordsTable.keyBytes, keyBytes),
eq(internalTables.recordsTable.blockNumber, blockNumber)
)
)
.limit(1)
Expand Down Expand Up @@ -114,7 +115,7 @@ export async function createStorageAdapter<TConfig extends StoreConfig = StoreCo
...value,
})
.onConflictDoUpdate({
target: sqlTable.__keyBytes,
target: [sqlTable.__keyBytes, sqlTable.__lastUpdatedBlockNumber],
set: {
__lastUpdatedBlockNumber: blockNumber,
...value,
Expand All @@ -128,7 +129,10 @@ export async function createStorageAdapter<TConfig extends StoreConfig = StoreCo
key,
});

await tx.delete(sqlTable).where(eq(sqlTable.__keyBytes, keyBytes)).execute();
await tx
.delete(sqlTable)
.where(and(eq(sqlTable.__keyBytes, keyBytes), eq(sqlTable.__lastUpdatedBlockNumber, blockNumber)))
.execute();
}
}
});
Expand Down

0 comments on commit 40c4fd8

Please sign in to comment.