Skip to content

Commit c30a20b

Browse files
authored
add collection id to important logs (#655)
* add collection id to important logs Signed-off-by: Marc MacLeod <[email protected]> * add changeset Signed-off-by: Marc MacLeod <[email protected]> * update test snapshot Signed-off-by: Marc MacLeod <[email protected]> --------- Signed-off-by: Marc MacLeod <[email protected]>
1 parent b5d4981 commit c30a20b

File tree

7 files changed

+61
-30
lines changed

7 files changed

+61
-30
lines changed

.changeset/fresh-yaks-lay.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tanstack/electric-db-collection": patch
3+
"@tanstack/db": patch
4+
---
5+
6+
prefix logs and errors with collection id, when available

packages/db/src/collection/change-events.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import type { BasicExpression } from "../query/ir.js"
2020
export interface CollectionLike<
2121
T extends object = Record<string, unknown>,
2222
TKey extends string | number = string | number,
23-
> extends Pick<Collection<T, TKey>, `get` | `has` | `entries` | `indexes`> {}
23+
> extends Pick<
24+
Collection<T, TKey>,
25+
`get` | `has` | `entries` | `indexes` | `id`
26+
> {}
2427

2528
/**
2629
* Returns the current state of the collection as an array of changes
@@ -109,7 +112,7 @@ export function currentStateAsChanges<
109112
} catch (error) {
110113
// If anything goes wrong with the where clause, fall back to full scan
111114
console.warn(
112-
`Error processing where clause, falling back to full scan:`,
115+
`${collection.id ? `[${collection.id}] ` : ``}Error processing where clause, falling back to full scan:`,
113116
error
114117
)
115118

packages/db/src/collection/lifecycle.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ export class CollectionLifecycleManager<
111111
if (newStatus === `ready` && !this.indexes.isIndexesResolved) {
112112
// Resolve indexes asynchronously without blocking
113113
this.indexes.resolveAllIndexes().catch((error) => {
114-
console.warn(`Failed to resolve indexes:`, error)
114+
console.warn(
115+
`${this.config.id ? `[${this.config.id}] ` : ``}Failed to resolve indexes:`,
116+
error
117+
)
115118
})
116119
}
117120

packages/db/src/indexes/auto-index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export function ensureIndexForField<
5858
options: compareFn ? { compareFn, compareOptions } : {},
5959
})
6060
} catch (error) {
61-
console.warn(`Failed to create auto-index for field "${fieldName}":`, error)
61+
console.warn(
62+
`${collection.id ? `[${collection.id}] ` : ``}Failed to create auto-index for field "${fieldName}":`,
63+
error
64+
)
6265
}
6366
}
6467

packages/electric-db-collection/src/electric.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export function electricCollectionOptions(
176176
const sync = createElectricSync<any>(config.shapeOptions, {
177177
seenTxids,
178178
seenSnapshots,
179+
collectionId: config.id,
179180
})
180181

181182
/**
@@ -188,9 +189,12 @@ export function electricCollectionOptions(
188189
txId: Txid,
189190
timeout: number = 30000
190191
): Promise<boolean> => {
191-
debug(`awaitTxId called with txid %d`, txId)
192+
debug(
193+
`${config.id ? `[${config.id}] ` : ``}awaitTxId called with txid %d`,
194+
txId
195+
)
192196
if (typeof txId !== `number`) {
193-
throw new ExpectedNumberInAwaitTxIdError(typeof txId)
197+
throw new ExpectedNumberInAwaitTxIdError(typeof txId, config.id)
194198
}
195199

196200
// First check if the txid is in the seenTxids store
@@ -207,12 +211,15 @@ export function electricCollectionOptions(
207211
const timeoutId = setTimeout(() => {
208212
unsubscribeSeenTxids()
209213
unsubscribeSeenSnapshots()
210-
reject(new TimeoutWaitingForTxIdError(txId))
214+
reject(new TimeoutWaitingForTxIdError(txId, config.id))
211215
}, timeout)
212216

213217
const unsubscribeSeenTxids = seenTxids.subscribe(() => {
214218
if (seenTxids.state.has(txId)) {
215-
debug(`awaitTxId found match for txid %o`, txId)
219+
debug(
220+
`${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o`,
221+
txId
222+
)
216223
clearTimeout(timeoutId)
217224
unsubscribeSeenTxids()
218225
unsubscribeSeenSnapshots()
@@ -226,7 +233,7 @@ export function electricCollectionOptions(
226233
)
227234
if (visibleSnapshot) {
228235
debug(
229-
`awaitTxId found match for txid %o in snapshot %o`,
236+
`${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o in snapshot %o`,
230237
txId,
231238
visibleSnapshot
232239
)
@@ -249,7 +256,7 @@ export function electricCollectionOptions(
249256
const txid = handlerResult.txid
250257

251258
if (!txid) {
252-
throw new ElectricInsertHandlerMustReturnTxIdError()
259+
throw new ElectricInsertHandlerMustReturnTxIdError(config.id)
253260
}
254261

255262
// Handle both single txid and array of txids
@@ -272,7 +279,7 @@ export function electricCollectionOptions(
272279
const txid = handlerResult.txid
273280

274281
if (!txid) {
275-
throw new ElectricUpdateHandlerMustReturnTxIdError()
282+
throw new ElectricUpdateHandlerMustReturnTxIdError(config.id)
276283
}
277284

278285
// Handle both single txid and array of txids
@@ -290,7 +297,7 @@ export function electricCollectionOptions(
290297
? async (params: DeleteMutationFnParams<any>) => {
291298
const handlerResult = await config.onDelete!(params)
292299
if (!handlerResult.txid) {
293-
throw new ElectricDeleteHandlerMustReturnTxIdError()
300+
throw new ElectricDeleteHandlerMustReturnTxIdError(config.id)
294301
}
295302

296303
// Handle both single txid and array of txids
@@ -333,10 +340,10 @@ function createElectricSync<T extends Row<unknown>>(
333340
options: {
334341
seenTxids: Store<Set<Txid>>
335342
seenSnapshots: Store<Array<PostgresSnapshot>>
343+
collectionId?: string
336344
}
337345
): SyncConfig<T> {
338-
const { seenTxids } = options
339-
const { seenSnapshots } = options
346+
const { seenTxids, seenSnapshots, collectionId } = options
340347

341348
// Store for the relation schema information
342349
const relationSchema = new Store<string | undefined>(undefined)
@@ -445,7 +452,7 @@ function createElectricSync<T extends Row<unknown>>(
445452
hasUpToDate = true
446453
} else if (isMustRefetchMessage(message)) {
447454
debug(
448-
`Received must-refetch message, starting transaction with truncate`
455+
`${collectionId ? `[${collectionId}] ` : ``}Received must-refetch message, starting transaction with truncate`
449456
)
450457

451458
// Start a transaction and truncate the collection
@@ -475,7 +482,10 @@ function createElectricSync<T extends Row<unknown>>(
475482
seenTxids.setState((currentTxids) => {
476483
const clonedSeen = new Set<Txid>(currentTxids)
477484
if (newTxids.size > 0) {
478-
debug(`new txids synced from pg %O`, Array.from(newTxids))
485+
debug(
486+
`${collectionId ? `[${collectionId}] ` : ``}new txids synced from pg %O`,
487+
Array.from(newTxids)
488+
)
479489
}
480490
newTxids.forEach((txid) => clonedSeen.add(txid))
481491
newTxids.clear()
@@ -486,7 +496,10 @@ function createElectricSync<T extends Row<unknown>>(
486496
seenSnapshots.setState((currentSnapshots) => {
487497
const seen = [...currentSnapshots, ...newSnapshots]
488498
newSnapshots.forEach((snapshot) =>
489-
debug(`new snapshot synced from pg %o`, snapshot)
499+
debug(
500+
`${collectionId ? `[${collectionId}] ` : ``}new snapshot synced from pg %o`,
501+
snapshot
502+
)
490503
)
491504
newSnapshots.length = 0
492505
return seen

packages/electric-db-collection/src/errors.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,51 @@ import { TanStackDBError } from "@tanstack/db"
22

33
// Electric DB Collection Errors
44
export class ElectricDBCollectionError extends TanStackDBError {
5-
constructor(message: string) {
6-
super(message)
5+
constructor(message: string, collectionId?: string) {
6+
super(`${collectionId ? `[${collectionId}] ` : ``}${message}`)
77
this.name = `ElectricDBCollectionError`
88
}
99
}
1010

1111
export class ExpectedNumberInAwaitTxIdError extends ElectricDBCollectionError {
12-
constructor(txIdType: string) {
13-
super(`Expected number in awaitTxId, received ${txIdType}`)
12+
constructor(txIdType: string, collectionId?: string) {
13+
super(`Expected number in awaitTxId, received ${txIdType}`, collectionId)
1414
this.name = `ExpectedNumberInAwaitTxIdError`
1515
}
1616
}
1717

1818
export class TimeoutWaitingForTxIdError extends ElectricDBCollectionError {
19-
constructor(txId: number) {
20-
super(`Timeout waiting for txId: ${txId}`)
19+
constructor(txId: number, collectionId?: string) {
20+
super(`Timeout waiting for txId: ${txId}`, collectionId)
2121
this.name = `TimeoutWaitingForTxIdError`
2222
}
2323
}
2424

2525
export class ElectricInsertHandlerMustReturnTxIdError extends ElectricDBCollectionError {
26-
constructor() {
26+
constructor(collectionId?: string) {
2727
super(
28-
`Electric collection onInsert handler must return a txid or array of txids`
28+
`Electric collection onInsert handler must return a txid or array of txids`,
29+
collectionId
2930
)
3031
this.name = `ElectricInsertHandlerMustReturnTxIdError`
3132
}
3233
}
3334

3435
export class ElectricUpdateHandlerMustReturnTxIdError extends ElectricDBCollectionError {
35-
constructor() {
36+
constructor(collectionId?: string) {
3637
super(
37-
`Electric collection onUpdate handler must return a txid or array of txids`
38+
`Electric collection onUpdate handler must return a txid or array of txids`,
39+
collectionId
3840
)
3941
this.name = `ElectricUpdateHandlerMustReturnTxIdError`
4042
}
4143
}
4244

4345
export class ElectricDeleteHandlerMustReturnTxIdError extends ElectricDBCollectionError {
44-
constructor() {
46+
constructor(collectionId?: string) {
4547
super(
46-
`Electric collection onDelete handler must return a txid or array of txids`
48+
`Electric collection onDelete handler must return a txid or array of txids`,
49+
collectionId
4750
)
4851
this.name = `ElectricDeleteHandlerMustReturnTxIdError`
4952
}

packages/electric-db-collection/tests/electric.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ describe(`Electric Integration`, () => {
303303
// @ts-expect-error
304304
collection.utils.awaitTxId(`123`)
305305
).rejects.toThrowErrorMatchingInlineSnapshot(
306-
`[ExpectedNumberInAwaitTxIdError: Expected number in awaitTxId, received string]`
306+
`[ExpectedNumberInAwaitTxIdError: [test] Expected number in awaitTxId, received string]`
307307
)
308308

309309
// The txid should be tracked and awaitTxId should resolve immediately

0 commit comments

Comments
 (0)