WIP! A BB-style forum, on the ATmosphere! We're still working... we'll be back soon when we have something to show off!
node typescript hono htmx atproto
4
fork

Configure Feed

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

refactor(db): extract reusable transaction types (#9)

Extract Transaction and DbOrTransaction types from inline definitions
in the indexer to shared type exports in @atbb/db package.

This improves code clarity and reusability by:
- Eliminating complex inline type expressions
- Providing a single source of truth for transaction types
- Adding comprehensive documentation with usage examples
- Making these types available to all consumers of @atbb/db

The Transaction type extracts the transaction callback parameter type
from Drizzle's database instance. The DbOrTransaction union type is
useful for helper functions that can work with either a database
instance or an active transaction context.

authored by

Malpercio and committed by
GitHub
c35e9dc8 be221a2a

+33 -5
+5 -5
apps/appview/src/lib/indexer.ts
··· 3 3 CommitDeleteEvent, 4 4 CommitUpdateEvent, 5 5 } from "@skyware/jetstream"; 6 - import type { Database } from "@atbb/db"; 6 + import type { Database, DbOrTransaction } from "@atbb/db"; 7 7 import { 8 8 posts, 9 9 forums, ··· 59 59 * Ensure a user exists in the database. Creates if not exists. 60 60 * @param dbOrTx - Database instance or transaction 61 61 */ 62 - async function ensureUser(did: string, dbOrTx: Database | Parameters<Parameters<Database['transaction']>[0]>[0] = db) { 62 + async function ensureUser(did: string, dbOrTx: DbOrTransaction = db) { 63 63 try { 64 64 const existing = await dbOrTx.select().from(users).where(eq(users.did, did)).limit(1); 65 65 ··· 83 83 */ 84 84 async function getForumIdByUri( 85 85 forumUri: string, 86 - dbOrTx: Database | Parameters<Parameters<Database['transaction']>[0]>[0] = db 86 + dbOrTx: DbOrTransaction = db 87 87 ): Promise<bigint | null> { 88 88 const parsed = parseAtUri(forumUri); 89 89 if (!parsed) return null; ··· 104 104 */ 105 105 async function getForumIdByDid( 106 106 forumDid: string, 107 - dbOrTx: Database | Parameters<Parameters<Database['transaction']>[0]>[0] = db 107 + dbOrTx: DbOrTransaction = db 108 108 ): Promise<bigint | null> { 109 109 try { 110 110 const result = await dbOrTx ··· 126 126 */ 127 127 async function getPostIdByUri( 128 128 postUri: string, 129 - dbOrTx: Database | Parameters<Parameters<Database['transaction']>[0]>[0] = db 129 + dbOrTx: DbOrTransaction = db 130 130 ): Promise<bigint | null> { 131 131 const parsed = parseAtUri(postUri); 132 132 if (!parsed) return null;
+28
packages/db/src/index.ts
··· 9 9 10 10 export type Database = ReturnType<typeof createDb>; 11 11 12 + /** 13 + * Transaction type extracted from Drizzle's database instance. 14 + * Use this when you need to work with a transaction object directly. 15 + */ 16 + export type Transaction = Parameters<Parameters<Database['transaction']>[0]>[0]; 17 + 18 + /** 19 + * Union type for functions that need to work with either the database 20 + * instance or an active transaction. This is useful for helper functions 21 + * that can be called both standalone and within a transaction context. 22 + * 23 + * Example: 24 + * ```typescript 25 + * async function getUser(id: string, dbOrTx: DbOrTransaction = db) { 26 + * return dbOrTx.select().from(users).where(eq(users.id, id)); 27 + * } 28 + * 29 + * // Can be called standalone 30 + * await getUser('123', db); 31 + * 32 + * // Or within a transaction 33 + * await db.transaction(async (tx) => { 34 + * await getUser('123', tx); 35 + * }); 36 + * ``` 37 + */ 38 + export type DbOrTransaction = Database | Transaction; 39 + 12 40 export * from "./schema.js";