pbtsdb 0.6.3 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -2
- package/dist/{chunk-RZISAGM7.js → chunk-7DHFWXB3.js} +53 -12
- package/dist/chunk-7DHFWXB3.js.map +1 -0
- package/dist/core.d.ts +20 -3
- package/dist/core.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/llms.txt +28 -1
- package/package.json +2 -2
- package/dist/chunk-RZISAGM7.js.map +0 -1
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ npm install pbtsdb pocketbase @tanstack/react-query @tanstack/react-db @tanstack
|
|
|
39
39
|
|
|
40
40
|
### Peer Dependencies
|
|
41
41
|
|
|
42
|
-
- `pocketbase` >= 0.
|
|
42
|
+
- `pocketbase` >= 0.22.0
|
|
43
43
|
- `@tanstack/react-query` >= 5.0.0
|
|
44
44
|
- `@tanstack/react-db` >= 0.1.0
|
|
45
45
|
- `@tanstack/query-db-collection` >= 1.0.0
|
|
@@ -304,13 +304,18 @@ const { data } = useLiveQuery((q) =>
|
|
|
304
304
|
The main function for creating type-safe collections. Uses a curried API for better type inference.
|
|
305
305
|
|
|
306
306
|
```typescript
|
|
307
|
-
const c = createCollection<Schema>(
|
|
307
|
+
const c = createCollection<Schema>(
|
|
308
|
+
pb: PocketBase,
|
|
309
|
+
queryClient: QueryClient,
|
|
310
|
+
factoryOptions?: CreateCollectionFactoryOptions
|
|
311
|
+
);
|
|
308
312
|
const collection = c(collectionName: string, options?: CreateCollectionOptions);
|
|
309
313
|
```
|
|
310
314
|
|
|
311
315
|
**Parameters:**
|
|
312
316
|
- `pb` - PocketBase instance
|
|
313
317
|
- `queryClient` - TanStack Query QueryClient instance
|
|
318
|
+
- `factoryOptions` - Optional configuration applied to every collection this factory builds (see [Subscription Options](#subscription-options))
|
|
314
319
|
- `collectionName` - Name of the PocketBase collection
|
|
315
320
|
- `options` - Optional configuration
|
|
316
321
|
|
|
@@ -494,6 +499,41 @@ await collection.waitForSubscription(); // Wait with default 5s timeout
|
|
|
494
499
|
await collection.waitForSubscription(10000); // Wait with custom timeout (ms)
|
|
495
500
|
```
|
|
496
501
|
|
|
502
|
+
#### Subscription Options
|
|
503
|
+
|
|
504
|
+
Pass `subscribeOptions` as the third `createCollection` argument to attach extra
|
|
505
|
+
options — `headers`, `filter`, `expand`, `fields` — to every real-time
|
|
506
|
+
subscription the factory creates.
|
|
507
|
+
|
|
508
|
+
```typescript
|
|
509
|
+
const c = createCollection<Schema>(pb, queryClient, {
|
|
510
|
+
subscribeOptions: () => {
|
|
511
|
+
const token = getShareToken();
|
|
512
|
+
return token ? { headers: { 'X-Share-Token': token } } : undefined;
|
|
513
|
+
},
|
|
514
|
+
});
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
It is a **getter, not a static object**, and that matters. Subscriptions restart
|
|
518
|
+
on reconnect and whenever the subscriber count rises from zero, so a value read
|
|
519
|
+
once would go stale exactly when it counts — for example when a visitor
|
|
520
|
+
authenticates mid-session and should stop presenting an anonymous token.
|
|
521
|
+
|
|
522
|
+
Returning `undefined` subscribes with no extra options, identical to omitting
|
|
523
|
+
the option entirely.
|
|
524
|
+
|
|
525
|
+
The common use is authorizing anonymous access. PocketBase snakecases header
|
|
526
|
+
names the same way for real-time as for REST, so `X-Share-Token` is readable in
|
|
527
|
+
a collection rule as `@request.headers.x_share_token` — a single rule covers
|
|
528
|
+
both transports:
|
|
529
|
+
|
|
530
|
+
```
|
|
531
|
+
@request.auth.id != "" || @request.headers.x_share_token = "..."
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
Setting the matching header on REST requests remains the application's job, via
|
|
535
|
+
`pb.beforeSend`. Requires `pocketbase >= 0.22.0`.
|
|
536
|
+
|
|
497
537
|
### Utility Functions
|
|
498
538
|
|
|
499
539
|
#### newRecordId()
|
|
@@ -132,7 +132,7 @@ function convertToPocketBaseSort(orderBy) {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
// src/collection.ts
|
|
135
|
-
function createCollection(pb, queryClient) {
|
|
135
|
+
function createCollection(pb, queryClient, factoryOptions) {
|
|
136
136
|
return (collectionName, options) => {
|
|
137
137
|
const expandStores = options?.expand;
|
|
138
138
|
const expandString = expandStores ? Object.keys(expandStores).sort().join(",") : void 0;
|
|
@@ -184,18 +184,54 @@ function createCollection(pb, queryClient) {
|
|
|
184
184
|
expand: expandString
|
|
185
185
|
});
|
|
186
186
|
}
|
|
187
|
+
const inFlightConfirmedIds = /* @__PURE__ */ new Set();
|
|
188
|
+
function markConfirmedPresent(records) {
|
|
189
|
+
if (inFlightConfirmedIds.size === 0) return;
|
|
190
|
+
for (const record of records) {
|
|
191
|
+
const id = record?.id;
|
|
192
|
+
if (typeof id !== "string") continue;
|
|
193
|
+
for (const confirmed of inFlightConfirmedIds) confirmed.add(id);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function withRowsConfirmedMidFlight(items, confirmedMidFlight) {
|
|
197
|
+
if (confirmedMidFlight.size === 0) return items;
|
|
198
|
+
const resultIds = new Set(
|
|
199
|
+
items.map((item) => item?.id)
|
|
200
|
+
);
|
|
201
|
+
const mergedIds = [...confirmedMidFlight].filter(
|
|
202
|
+
(id) => !resultIds.has(id) && collection._state.syncedData.has(id)
|
|
203
|
+
);
|
|
204
|
+
if (mergedIds.length === 0) return items;
|
|
205
|
+
logger.debug("Merging rows confirmed while fetch was in flight", {
|
|
206
|
+
collectionName,
|
|
207
|
+
ids: mergedIds
|
|
208
|
+
});
|
|
209
|
+
return [
|
|
210
|
+
...items,
|
|
211
|
+
...mergedIds.map((id) => collection._state.syncedData.get(id))
|
|
212
|
+
];
|
|
213
|
+
}
|
|
187
214
|
async function fetchRecords(loadOptions, queryKey) {
|
|
188
|
-
|
|
215
|
+
const confirmedMidFlight = /* @__PURE__ */ new Set();
|
|
216
|
+
inFlightConfirmedIds.add(confirmedMidFlight);
|
|
189
217
|
try {
|
|
190
|
-
items
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
218
|
+
let items;
|
|
219
|
+
try {
|
|
220
|
+
items = await fetchItems(loadOptions);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
if (ignoreAutoCancellation && error instanceof Error && error.message.includes("autocancelled")) {
|
|
223
|
+
return withRowsConfirmedMidFlight(
|
|
224
|
+
queryClient.getQueryData(queryKey ?? [collectionName]) ?? [],
|
|
225
|
+
confirmedMidFlight
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
throw error;
|
|
194
229
|
}
|
|
195
|
-
|
|
230
|
+
await upsertExpandedRelations(items);
|
|
231
|
+
return withRowsConfirmedMidFlight(items, confirmedMidFlight);
|
|
232
|
+
} finally {
|
|
233
|
+
inFlightConfirmedIds.delete(confirmedMidFlight);
|
|
196
234
|
}
|
|
197
|
-
await upsertExpandedRelations(items);
|
|
198
|
-
return items;
|
|
199
235
|
}
|
|
200
236
|
const collectionOptions = queryCollectionOptions({
|
|
201
237
|
...options?.collectionOptions,
|
|
@@ -320,6 +356,7 @@ function createCollection(pb, queryClient) {
|
|
|
320
356
|
return treatEqualAsStale ? incoming <= current : incoming < current;
|
|
321
357
|
}
|
|
322
358
|
function writeServerRecords(records) {
|
|
359
|
+
markConfirmedPresent(records);
|
|
323
360
|
if (!collection.utils || !collection.isReady()) return;
|
|
324
361
|
const fresh = records.filter((record) => !isStaleServerRecord(record));
|
|
325
362
|
if (fresh.length === 0) return;
|
|
@@ -338,8 +375,12 @@ function createCollection(pb, queryClient) {
|
|
|
338
375
|
let isSubscribed = false;
|
|
339
376
|
let subscriptionPromise = null;
|
|
340
377
|
let subscriptionResolve = null;
|
|
378
|
+
function markEchoPresence(event) {
|
|
379
|
+
if (event.action !== "delete") markConfirmedPresent([event.record]);
|
|
380
|
+
}
|
|
341
381
|
const handleRealtimeEvent = (event) => {
|
|
342
382
|
if (!collection.utils) return;
|
|
383
|
+
markEchoPresence(event);
|
|
343
384
|
if (isStaleEcho(event)) return;
|
|
344
385
|
try {
|
|
345
386
|
writeOwn(
|
|
@@ -380,7 +421,7 @@ function createCollection(pb, queryClient) {
|
|
|
380
421
|
});
|
|
381
422
|
}
|
|
382
423
|
try {
|
|
383
|
-
unsubscribeFn = await pb.collection(collectionName).subscribe("*", handleRealtimeEvent);
|
|
424
|
+
unsubscribeFn = await pb.collection(collectionName).subscribe("*", handleRealtimeEvent, factoryOptions?.subscribeOptions?.());
|
|
384
425
|
isSubscribed = true;
|
|
385
426
|
logger.debug("Subscription started", { collectionName });
|
|
386
427
|
if (subscriptionResolve) {
|
|
@@ -465,5 +506,5 @@ function newRecordId() {
|
|
|
465
506
|
}
|
|
466
507
|
|
|
467
508
|
export { createCollection, newRecordId, resetLogger, setLogger };
|
|
468
|
-
//# sourceMappingURL=chunk-
|
|
469
|
-
//# sourceMappingURL=chunk-
|
|
509
|
+
//# sourceMappingURL=chunk-7DHFWXB3.js.map
|
|
510
|
+
//# sourceMappingURL=chunk-7DHFWXB3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/logger.ts","../src/pocketbase-query-converter.ts","../src/collection.ts","../src/util.ts"],"names":["createTanStackCollection"],"mappings":";;;;;;;AAsCA,IAAM,aAAA,GAAwB;AAAA,EAC1B,KAAA,EAAO,CAAC,GAAA,EAAa,OAAA,KAAqB;AACtC,IAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa,aAAA,EAAe;AACxC,MAAA,OAAA,CAAQ,KAAA,CAAM,CAAA,SAAA,EAAY,GAAG,CAAA,CAAA,EAAI,WAAW,EAAE,CAAA;AAAA,IAClD;AAAA,EACJ,CAAA;AAAA,EACA,IAAA,EAAM,CAAC,GAAA,EAAa,OAAA,KAAqB;AACrC,IAAA,OAAA,CAAQ,IAAA,CAAK,CAAA,SAAA,EAAY,GAAG,CAAA,CAAA,EAAI,WAAW,EAAE,CAAA;AAAA,EACjD,CAAA;AAAA,EACA,IAAA,EAAM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AA,MACP,oBAAA;AAAA,MACA,CAAC,KAAA,KAAwE;AACrE,QAAA,MAAM,WAAW,KAAA,CAAM,eAAA;AACvB,QAAA,MAAM,gBAAgB,KAAA,CAAM,uBAAA;AAE5B,QAAA,IAAI,QAAA,GAAW,CAAA,IAAK,aAAA,KAAkB,CAAA,EAAG;AAErC,UAAA,iBAAA,EAAkB,CAAE,MAAM,MAAM;AAAA,UAAC,CAAC,CAAA;AAAA,QACtC,CAAA,MAAA,IAAW,QAAA,KAAa,CAAA,IAAK,aAAA,GAAgB,CAAA,EAAG;AAE5C,UAAA,gBAAA,EAAiB,CAAE,MAAM,MAAM;AAAA,UAAC,CAAC,CAAA;AAAA,QACrC;AAAA,MACJ;AAAA,KACJ;AAGA,IAAA,MAAA,CAAO,OAAO,UAAA,EAAY;AAAA,MACtB,cAAA;AAAA,MACA,mBAAA;AAAA,MACA,cAAc,MAAM;AAAA,KACvB,CAAA;AAED,IAAA,OAAO,UAAA;AAAA,EACX,CAAA;AACJ;;;ACvtBO,SAAS,WAAA,GAAsB;AAClC,EAAA,MAAM,KAAA,GAAQ,sCAAA;AACd,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,EAAA,EAAI,CAAA,EAAA,EAAK;AACzB,IAAA,MAAA,IAAU,KAAA,CAAM,OAAO,IAAA,CAAK,KAAA,CAAM,KAAK,MAAA,EAAO,GAAI,KAAA,CAAM,MAAM,CAAC,CAAA;AAAA,EACnE;AACA,EAAA,OAAO,MAAA;AACX","file":"chunk-7DHFWXB3.js","sourcesContent":["/**\n * Logger interface for subscription events and internal operations.\n * Users can provide their own implementation to integrate with external logging services.\n */\nexport interface Logger {\n /**\n * Log debug-level messages (typically only shown in development).\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n debug: (msg: string, context?: object) => void\n\n /**\n * Log info-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n info: (msg: string, context?: object) => void\n\n /**\n * Log warning-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n warn: (msg: string, context?: object) => void\n\n /**\n * Log error-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n error: (msg: string, context?: object) => void\n}\n\n/**\n * Default console-based logger implementation.\n * Only logs debug messages in development mode.\n */\nconst defaultLogger: Logger = {\n debug: (msg: string, context?: object) => {\n if (process.env.NODE_ENV === 'development') {\n console.debug(`[pbtsdb] ${msg}`, context || '')\n }\n },\n info: (msg: string, context?: object) => {\n console.info(`[pbtsdb] ${msg}`, context || '')\n },\n warn: (msg: string, context?: object) => {\n console.warn(`[pbtsdb] ${msg}`, context || '')\n },\n error: (msg: string, context?: object) => {\n console.error(`[pbtsdb] ${msg}`, context || '')\n },\n}\n\n/**\n * Current logger instance (can be replaced by users).\n */\nlet currentLogger: Logger = defaultLogger\n\n/**\n * Internal logger instance used by the library.\n */\nexport const logger: Logger = {\n debug: (msg: string, context?: object) => currentLogger.debug(msg, context),\n info: (msg: string, context?: object) => currentLogger.info(msg, context),\n warn: (msg: string, context?: object) => currentLogger.warn(msg, context),\n error: (msg: string, context?: object) => currentLogger.error(msg, context),\n}\n\n/**\n * Set a custom logger implementation.\n * This allows users to integrate with their own logging services (e.g., Sentry, LogRocket, etc.).\n *\n * @param customLogger - The custom logger implementation\n *\n * @example\n * ```ts\n * import { setLogger } from 'pbtsdb';\n *\n * // Integration with a custom logging service\n * setLogger({\n * debug: (msg, context) => myLogger.debug(msg, context),\n * warn: (msg, context) => myLogger.warn(msg, context),\n * error: (msg, context) => {\n * myLogger.error(msg, context);\n * Sentry.captureMessage(msg, { level: 'error', extra: context });\n * },\n * });\n * ```\n *\n * @example\n * ```ts\n * // Disable all logging\n * setLogger({\n * debug: () => {},\n * warn: () => {},\n * error: () => {},\n * });\n * ```\n */\nexport function setLogger(customLogger: Logger): void {\n currentLogger = customLogger\n}\n\n/**\n * Reset the logger to the default implementation.\n */\nexport function resetLogger(): void {\n currentLogger = defaultLogger\n}\n","import type { IR } from '@tanstack/db'\nimport {\n type FieldPath,\n type ParsedOrderBy,\n parseOrderByExpression,\n parseWhereExpression,\n} from '@tanstack/db'\n\ntype BasicExpression<T = unknown> = IR.BasicExpression<T>\n\nfunction escapeValue(value: unknown): string {\n if (value === null) {\n return 'null'\n }\n if (typeof value === 'boolean') {\n return value ? 'true' : 'false'\n }\n if (typeof value === 'number') {\n return value.toString()\n }\n if (typeof value === 'string') {\n return `\"${value.replace(/\"/g, '\\\\\"')}\"`\n }\n if (value instanceof Date) {\n return `\"${value.toISOString()}\"`\n }\n if (Array.isArray(value)) {\n return `[${value.map(escapeValue).join(',')}]`\n }\n return `\"${String(value)}\"`\n}\n\nfunction fieldPathToString(path: FieldPath): string {\n return path.join('.')\n}\n\nexport function convertToPocketBaseFilter(\n where: BasicExpression<boolean> | undefined | null\n): string | undefined {\n if (!where) {\n return undefined\n }\n\n const result = parseWhereExpression(where, {\n handlers: {\n eq: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} = ${escapeValue(value)}`\n },\n gt: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} > ${escapeValue(value)}`\n },\n gte: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} >= ${escapeValue(value)}`\n },\n lt: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} < ${escapeValue(value)}`\n },\n lte: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} <= ${escapeValue(value)}`\n },\n and: (...conditions: string[]) => {\n if (conditions.length === 0) return ''\n if (conditions.length === 1) return conditions[0]\n return `(${conditions.join(' && ')})`\n },\n or: (...conditions: string[]) => {\n if (conditions.length === 0) return ''\n if (conditions.length === 1) return conditions[0]\n return `(${conditions.join(' || ')})`\n },\n not: (condition: string) => {\n return `!(${condition})`\n },\n in: (field: FieldPath, values: unknown) => {\n const valueArray = Array.isArray(values) ? values : [values]\n const uniqueValues = [...new Set(valueArray)]\n const fieldStr = fieldPathToString(field)\n const conditions = uniqueValues.map(v => `${fieldStr} = ${escapeValue(v)}`)\n return conditions.length > 1 ? `(${conditions.join(' || ')})` : conditions[0]\n },\n like: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} ~ ${escapeValue(value)}`\n },\n isNull: (field: FieldPath) => {\n return `${fieldPathToString(field)} = null`\n },\n isUndefined: (field: FieldPath) => {\n return `${fieldPathToString(field)} = null`\n },\n },\n onUnknownOperator: (operator: string, _args: unknown[]) => {\n throw new Error(\n `Unsupported operator '${operator}' for PocketBase filter conversion. ` +\n `Supported operators: eq, gt, gte, lt, lte, in, like, and, or, not, isNull, isUndefined`\n )\n },\n })\n\n return result || undefined\n}\n\nexport function convertToPocketBaseSort(\n orderBy: IR.OrderBy | undefined | null\n): string | undefined {\n if (!orderBy) {\n return undefined\n }\n\n const sorts = parseOrderByExpression(orderBy)\n\n if (sorts.length === 0) {\n return undefined\n }\n\n return sorts\n .map((sort: ParsedOrderBy) => {\n const field = fieldPathToString(sort.field)\n return sort.direction === 'desc' ? `-${field}` : field\n })\n .join(',')\n}\n","import {\n type Collection,\n createCollection as createTanStackCollection,\n type LoadSubsetOptions,\n} from '@tanstack/db'\nimport {\n DeleteOperationItemNotFoundError,\n type QueryCollectionUtils,\n queryCollectionOptions,\n} from '@tanstack/query-db-collection'\nimport type { QueryClient } from '@tanstack/react-query'\nimport type PocketBase from 'pocketbase'\nimport type { RecordSubscribeOptions, RecordSubscription } from 'pocketbase'\nimport { logger } from './logger'\nimport { convertToPocketBaseFilter, convertToPocketBaseSort } from './pocketbase-query-converter'\nimport type {\n CreateCollectionOptions,\n ExpandTargetCollection,\n ExtractRecordType,\n SchemaDeclaration,\n} from './types'\n\nexport type { BaseRecord, CreateCollectionOptions, SchemaDeclaration } from './types'\n\n/**\n * Options applied to every collection built by a {@link createCollection} factory.\n */\nexport interface CreateCollectionFactoryOptions {\n /**\n * Extra options passed to every real-time subscription this factory creates,\n * such as `headers`, `filter`, `expand` or `fields`.\n *\n * Invoked at subscribe time rather than read once, because a subscription is\n * re-established on reconnect and whenever the subscriber count rises from\n * zero — a value captured at build time would go stale exactly then.\n *\n * Returning `undefined` subscribes with no extra options.\n */\n subscribeOptions?: () => RecordSubscribeOptions | undefined\n}\n\n/**\n * Extended LoadSubsetOptions that includes PocketBase-specific expand parameter.\n * @internal\n */\ntype ExtendedLoadSubsetOptions = LoadSubsetOptions & {\n pbExpand?: string\n}\n\n/**\n * Compute the record type with expand property when expand option is configured.\n * @internal\n */\ntype WithExpandFromConfig<\n Schema extends SchemaDeclaration,\n C extends keyof Schema,\n Opts,\n> = Opts extends {\n expand: infer E\n}\n ? ExtractRecordType<Schema, C> & {\n expand?: {\n [K in keyof E]: K extends keyof import('./types').ExtractRelations<Schema, C>\n ? import('./types').ExtractRelations<Schema, C>[K] extends Array<infer U>\n ? U[]\n : import('./types').ExtractRelations<Schema, C>[K]\n : never\n }\n }\n : ExtractRecordType<Schema, C>\n\n/**\n * Subscription helpers added to collection instances.\n * @internal\n */\ninterface CollectionSubscriptionHelpers {\n /** The PocketBase collection name */\n collectionName: string\n /** Wait for subscription to be established (useful in tests) */\n waitForSubscription: (timeout?: number) => Promise<void>\n /** Check if collection has an active subscription */\n isSubscribed: () => boolean\n}\n\n/**\n * Inferred collection type from config options.\n * @internal\n */\ntype InferCollectionType<\n Schema extends SchemaDeclaration,\n C extends keyof Schema,\n Opts extends CreateCollectionOptions<Schema, C>,\n> = Collection<\n WithExpandFromConfig<Schema, C, Opts>,\n string | number,\n // TUtils - QueryCollectionUtils from TanStack Query DB Collection\n QueryCollectionUtils<\n WithExpandFromConfig<Schema, C, Opts>,\n string | number,\n WithExpandFromConfig<Schema, C, Opts>\n >,\n // TSchema - we don't use StandardSchema validation\n never,\n Opts extends {\n omitOnInsert: infer O extends readonly import('./types').OmittableFields<\n ExtractRecordType<Schema, C>\n >[]\n }\n ? import('./types').ComputeInsertType<ExtractRecordType<Schema, C>, O>\n : ExtractRecordType<Schema, C>\n> &\n CollectionSubscriptionHelpers\n\n/**\n * Creates a type-safe TanStack DB collection backed by PocketBase.\n * Use this when you need fine-grained control or need to create collections with dependencies.\n *\n * @param pb - PocketBase client instance\n * @param queryClient - TanStack Query client\n * @returns A curried function that takes collection name and options\n *\n * @example\n * Basic usage:\n * ```ts\n * const booksCollection = createCollection<Schema>(pb, queryClient)('books', {});\n *\n * // Use directly\n * const books = await booksCollection.getFullList();\n * ```\n *\n * @example\n * With auto-expand relations:\n * ```ts\n * const authorsCollection = createCollection<Schema>(pb, queryClient)('authors', {});\n * const booksCollection = createCollection<Schema>(pb, queryClient)('books', {\n * expand: {\n * author: authorsCollection // Always expand, auto-upsert into authorsCollection\n * }\n * });\n *\n * // Expand is automatic - no .expand() call needed\n * const { data } = useLiveQuery((q) => q.from({ books: booksCollection }));\n * // data[0].expand.author is typed and populated\n * ```\n */\nexport function createCollection<Schema extends SchemaDeclaration>(\n pb: PocketBase,\n queryClient: QueryClient,\n factoryOptions?: CreateCollectionFactoryOptions\n) {\n return <\n C extends keyof Schema & string,\n Opts extends CreateCollectionOptions<Schema, C> = CreateCollectionOptions<Schema, C>,\n >(\n collectionName: C,\n options?: Opts\n ): InferCollectionType<Schema, C, Opts> => {\n type RecordType = ExtractRecordType<Schema, C>\n const expandStores = options?.expand as Record<string, ExpandTargetCollection> | undefined\n const expandString = expandStores ? Object.keys(expandStores).sort().join(',') : undefined\n\n const ignoreAutoCancellation = options?.ignoreAutoCancellation ?? true\n const refetchOnMutation = options?.refetchOnMutation ?? false\n\n async function upsertExpandedRelation(\n key: string,\n value: object | object[],\n stores: Record<string, ExpandTargetCollection>\n ): Promise<void> {\n const targetStore = stores[key]\n if (!targetStore.utils) return\n if (!targetStore.isReady()) {\n if (targetStore.config?.syncMode === 'on-demand') {\n await targetStore._sync.startSync()\n } else {\n logger.warn(\n `not syncing ${key} on ${collectionName} because store is not yet ready`\n )\n return\n }\n }\n const values = Array.isArray(value) ? value : [value]\n targetStore.utils.writeUpsert(values)\n }\n\n async function upsertExpandedRelations(items: RecordType[]): Promise<void> {\n if (!expandStores) return\n for (const record of items) {\n const expandData = (\n record as RecordType & { expand?: Record<string, object | object[]> }\n ).expand\n if (!expandData) continue\n for (const [key, value] of Object.entries(expandData)) {\n await upsertExpandedRelation(key, value, expandStores)\n }\n }\n }\n\n async function fetchItems(loadOptions?: ExtendedLoadSubsetOptions): Promise<RecordType[]> {\n const filter = convertToPocketBaseFilter(loadOptions?.where)\n const sort = convertToPocketBaseSort(loadOptions?.orderBy)\n const limit = loadOptions?.limit\n\n if (limit) {\n // Use getList when limit is specified to avoid fetching all records\n const result = await pb.collection(collectionName).getList(1, limit, {\n filter,\n sort,\n skipTotal: true, // Optimize by skipping total count\n expand: expandString,\n })\n return result.items as unknown as RecordType[]\n }\n // Use getFullList to fetch all records with automatic pagination\n return (await pb.collection(collectionName).getFullList({\n filter,\n sort,\n expand: expandString,\n })) as unknown as RecordType[]\n }\n\n // Each in-flight fetch registers a set here before its request goes out;\n // authoritative local writes (mutation write-backs and realtime echoes)\n // add the confirmed record ids to every registered set. An id added\n // after a fetch was issued is newer than that fetch's view of the\n // server, so the result cannot speak to the id's absence. The merge\n // deliberately ignores the fetch's filter: the manual-write cache push\n // grants every active query ownership of every synced row, so even a\n // row outside this subset's filter must be shielded from its reconcile.\n //\n // Why this exists: @tanstack/query-db-collection's applySuccessfulResult\n // reconcile-DELETES every row a query owns that its result omits, and the\n // synced-write guard below deliberately exempts deletes. A subset read\n // issued before a row existed can therefore resolve late and delete the\n // just-confirmed row (rows a query owns include rows pushed into its\n // cache by the manual-write path that runs on every write-back). The fix\n // is applied to the RESULT rather than the delete: fetchRecords merges\n // such rows back in, which both prevents the delete and keeps the row\n // owned by the query — a dropped delete alone would still strip\n // ownership and leave the row to a later GC pass.\n const inFlightConfirmedIds = new Set<Set<string>>()\n\n function markConfirmedPresent(records: RecordType[]): void {\n if (inFlightConfirmedIds.size === 0) return\n for (const record of records) {\n const id = (record as { id?: unknown } | null | undefined)?.id\n if (typeof id !== 'string') continue\n for (const confirmed of inFlightConfirmedIds) confirmed.add(id)\n }\n }\n\n function withRowsConfirmedMidFlight(\n items: RecordType[],\n confirmedMidFlight: Set<string>\n ): RecordType[] {\n if (confirmedMidFlight.size === 0) return items\n const resultIds = new Set(\n items.map(item => (item as { id?: unknown } | null | undefined)?.id)\n )\n // Ids absent from the synced store (deleted mid-flight, or never\n // landed) have nothing to protect and drop out of the merge.\n const mergedIds = [...confirmedMidFlight].filter(\n id => !resultIds.has(id) && collection._state.syncedData.has(id)\n )\n if (mergedIds.length === 0) return items\n logger.debug('Merging rows confirmed while fetch was in flight', {\n collectionName,\n ids: mergedIds,\n })\n return [\n ...items,\n ...mergedIds.map(id => collection._state.syncedData.get(id) as RecordType),\n ]\n }\n\n async function fetchRecords(\n loadOptions?: ExtendedLoadSubsetOptions,\n queryKey?: readonly unknown[]\n ): Promise<RecordType[]> {\n const confirmedMidFlight = new Set<string>()\n inFlightConfirmedIds.add(confirmedMidFlight)\n try {\n let items: RecordType[]\n try {\n items = await fetchItems(loadOptions)\n } catch (error) {\n if (\n ignoreAutoCancellation &&\n error instanceof Error &&\n error.message.includes('autocancelled')\n ) {\n // PocketBase auto-cancelled this in-flight read because a newer\n // request superseded it. Resolve to THIS subset's own cached rows\n // (keyed by the full query key) so the reconcile is a no-op for the\n // subset. The base key ([collectionName]) holds the full-collection\n // snapshot — returning that here would let applySuccessfulResult\n // reconcile foreign rows into a filtered subset (re-introducing rows\n // the subset's filter excludes). Re-throwing instead would error the\n // subset and empty/retry it.\n return withRowsConfirmedMidFlight(\n queryClient.getQueryData<RecordType[]>(queryKey ?? [collectionName]) ??\n [],\n confirmedMidFlight\n )\n }\n throw error\n }\n\n await upsertExpandedRelations(items)\n\n return withRowsConfirmedMidFlight(items, confirmedMidFlight)\n } finally {\n inFlightConfirmedIds.delete(confirmedMidFlight)\n }\n }\n\n const collectionOptions = queryCollectionOptions({\n ...options?.collectionOptions,\n queryClient,\n queryKey: [collectionName],\n syncMode: options?.syncMode ?? 'eager',\n queryFn: async (ctx): Promise<RecordType[]> => {\n return fetchRecords(\n ctx.meta?.loadSubsetOptions as ExtendedLoadSubsetOptions | undefined,\n ctx.queryKey\n )\n },\n getKey: (item: RecordType) => {\n const record = item as unknown as Record<string, unknown>\n if (!record || typeof record !== 'object' || !('id' in record)) {\n throw new Error(\n `Record in collection '${collectionName}' is missing required 'id' field. Received: ${JSON.stringify(item)}`\n )\n }\n return record.id as string\n },\n onInsert:\n options?.onInsert === false\n ? undefined\n : (options?.onInsert ??\n (async ({ transaction }) => {\n const created = await Promise.all(\n transaction.mutations.map(async mutation => {\n const {\n created: _created,\n updated: _updated,\n collectionId: _collectionId,\n collectionName: _collectionName,\n ...data\n } = mutation.modified as unknown as Record<string, unknown>\n return pb.collection(collectionName).create(data)\n })\n )\n writeServerRecords(created)\n return { refetch: refetchOnMutation }\n })),\n onUpdate:\n options?.onUpdate === false\n ? undefined\n : (options?.onUpdate ??\n (async ({ transaction }) => {\n const updated = await Promise.all(\n transaction.mutations.map(async mutation => {\n const recordWithId = mutation.original as { id: string }\n return pb\n .collection(collectionName)\n .update(recordWithId.id, mutation.changes)\n })\n )\n writeServerRecords(updated)\n return { refetch: refetchOnMutation }\n })),\n onDelete:\n options?.onDelete === false\n ? undefined\n : (options?.onDelete ??\n (async ({ transaction }) => {\n await Promise.all(\n transaction.mutations.map(async mutation => {\n const recordWithId = mutation.original as { id: string }\n await pb.collection(collectionName).delete(recordWithId.id)\n })\n )\n return { refetch: refetchOnMutation }\n })),\n })\n\n // Set while pbtsdb performs its own authoritative writes (mutation-response\n // write-backs and realtime echoes) through collection.utils.*. Those writes\n // share the same sync `write` primitive as the query-result reconcile path\n // (see the sync.sync wrapper below), so the guard uses this flag to tell them\n // apart: pbtsdb's own writes are exempt from the optimistic-pending arm of the\n // guard (the mutation-response write-back intentionally lands the confirmed\n // value while that very mutation's optimistic overlay is still in flight).\n let applyingOwnWrite = false\n function writeOwn(fn: () => void): void {\n applyingOwnWrite = true\n try {\n fn()\n } finally {\n applyingOwnWrite = false\n }\n }\n\n // The record id a synced insert/update targets, or null when the op is a delete\n // (terminal — never guarded) or carries no usable key.\n function syncedWriteKey(op: {\n type: string\n value?: unknown\n key?: unknown\n }): string | null {\n if (op.type !== 'insert' && op.type !== 'update') return null\n if (typeof op.key === 'string') return op.key\n const id = (op.value as { id?: unknown } | undefined)?.id\n return typeof id === 'string' ? id : null\n }\n\n // Guard the synced write path that pbtsdb does not otherwise control:\n // @tanstack/query-db-collection's applySuccessfulResult reconciles every query\n // result into the synced store via this same `write`, with no recency or\n // optimistic check. Under on-demand contention a single-row/subset read can\n // resolve with a pre-mutation row and land here after the row already moved on,\n // reverting it. We drop such a synced insert/update when either it targets a key\n // with a pending optimistic mutation (see the arm below) or it is no newer than\n // the synced row (out-of-order or post-settle read — equal `updated` included,\n // since the read can only carry a same-or-older value than the move's write-back\n // already in synced). pbtsdb's own writes (applyingOwnWrite) skip the optimistic\n // arm; they are still staleness-filtered upstream by writeServerRecords/isStaleEcho\n // (which keep the strict `<` so a confirmed same-second value can re-land).\n function shouldDropSyncedWrite(op: {\n type: string\n value?: unknown\n key?: unknown\n }): boolean {\n const key = syncedWriteKey(op)\n if (key === null) return false\n // Optimistic arm: only guard a key already present in the synced store. A\n // write to a key the synced store doesn't yet hold is populating it (e.g. the\n // initial fetch landing a row the user just optimistically inserted) and must\n // pass — dropping it would leave the row absent once the overlay clears. A\n // write to a key already synced, while an optimistic mutation is pending, is a\n // racing read that would revert the in-flight value, so drop it.\n if (\n !applyingOwnWrite &&\n hasPendingOptimisticMutation(key) &&\n collection._state.syncedData.has(key)\n ) {\n logger.debug('Dropping synced write for optimistically-pending row', {\n collectionName,\n id: key,\n })\n return true\n }\n if (isStaleServerRecord(op.value, !applyingOwnWrite)) {\n logger.debug('Dropping stale synced write', { collectionName, id: key })\n return true\n }\n return false\n }\n\n // Wrap the sync factory so every synced `write` flows through the guard above.\n // @tanstack/db invokes sync.sync with the write primitives; we hand back the\n // same params with a filtered `write`. begin/commit/markReady/etc. pass through.\n const innerSync = collectionOptions.sync.sync\n collectionOptions.sync = {\n ...collectionOptions.sync,\n sync: (params: Parameters<typeof innerSync>[0]) => {\n const guardedWrite: typeof params.write = message => {\n if (\n shouldDropSyncedWrite(\n message as { type: string; value?: unknown; key?: unknown }\n )\n ) {\n return\n }\n return params.write(message)\n }\n return innerSync({ ...params, write: guardedWrite })\n },\n }\n\n const collection = createTanStackCollection(collectionOptions)\n\n // True when the key has an in-flight (not yet settled) optimistic mutation.\n // While pending, the optimistic overlay — not syncedData — is the visible value,\n // so any synced write would only take effect once the overlay collapses, and an\n // older/racing read landing here is exactly what reverts a just-applied move.\n function hasPendingOptimisticMutation(key: string): boolean {\n const state = collection._state as unknown as {\n optimisticUpserts: { has: (k: string) => boolean }\n optimisticDeletes: { has: (k: string) => boolean }\n }\n return state.optimisticUpserts.has(key) || state.optimisticDeletes.has(key)\n }\n\n // Read the PocketBase `updated` autodate from a record, if present.\n // Collections without an `updated` field opt out of staleness checks.\n function recordUpdatedAt(record: unknown): string | undefined {\n const updated = (record as { updated?: unknown } | null | undefined)?.updated\n return typeof updated === 'string' && updated !== '' ? updated : undefined\n }\n\n // A server record is stale relative to the synced store when an entry for\n // the same key already holds a newer `updated` timestamp. PocketBase can\n // redeliver or reorder realtime echoes (and a slow mutation response can\n // resolve after a newer echo), so applying an older write would revert the\n // row. ISO 8601 timestamps sort lexicographically, so string comparison is\n // chronological. When either side lacks a comparable timestamp we cannot\n // tell, so we treat the write as fresh and let it through.\n //\n // `treatEqualAsStale` controls the equal-timestamp case. PocketBase bumps\n // `updated` on every mutation, so a record carrying the SAME `updated` second\n // as the synced row holds the same content — it cannot be newer. The default\n // (strict `<`) lets an own write-back/realtime echo re-land that confirmed\n // value harmlessly. The query-result path passes `true` (`<=`): a server read\n // resolving with the same-second value is the post-settle revert race — once\n // an optimistic move settles, the only thing that put a *fresher* value in\n // synced is that move's own write-back, and a same-second read would overwrite\n // it back to the pre-move row. There is nothing newer for an equal-timestamp\n // query result to legitimately deliver, so dropping it is safe.\n function isStaleServerRecord(record: unknown, treatEqualAsStale = false): boolean {\n const id = (record as { id?: unknown } | null | undefined)?.id\n if (typeof id !== 'string') return false\n const incoming = recordUpdatedAt(record)\n if (!incoming) return false\n const current = recordUpdatedAt(\n collection._state.syncedData.get(id) as RecordType | undefined\n )\n if (current === undefined) return false\n return treatEqualAsStale ? incoming <= current : incoming < current\n }\n\n // Write authoritative server records (mutation responses or realtime echoes)\n // into the synced store, dropping any that the store already supersedes.\n // No-op until the collection is ready: writing into the synced store before\n // sync has initialized throws, and with no live query there is nothing to\n // keep in sync — the next query fetches the already-persisted state.\n function writeServerRecords(records: RecordType[]): void {\n // Even a record the staleness filter below drops proves the server\n // holds the row — presence is what the mid-flight merge guards.\n markConfirmedPresent(records)\n if (!collection.utils || !collection.isReady()) return\n const fresh = records.filter(record => !isStaleServerRecord(record))\n if (fresh.length === 0) return\n writeOwn(() => collection.utils.writeUpsert(fresh))\n }\n\n // Decide whether a realtime echo should be dropped as stale. Under realtime\n // contention PocketBase can redeliver or reorder events, so an echo carrying\n // a pre-mutation value can arrive after the row already moved on (e.g. the\n // local mutation that just wrote the fresh value back). Applying a\n // strictly-older create/update echo would revert the row, so it is ignored.\n // Deletes are terminal and not timestamp-guarded.\n function isStaleEcho(event: RecordSubscription<RecordType>): boolean {\n if (event.action !== 'create' && event.action !== 'update') return false\n if (!isStaleServerRecord(event.record)) return false\n logger.debug('Ignoring stale realtime echo', {\n collectionName,\n id: (event.record as { id?: string } | undefined)?.id,\n })\n return true\n }\n\n // Real-time subscription state\n let unsubscribeFn: (() => Promise<void>) | null = null\n let isSubscribed = false\n let subscriptionPromise: Promise<void> | null = null\n let subscriptionResolve: (() => void) | null = null\n\n // Handle real-time events from PocketBase.\n //\n // The write primitives differ in how they treat a key that is absent from\n // the *synced* store (collection._state.syncedData, which is what they\n // validate against — not the optimistic view exposed by collection.has()):\n // - writeInsert / writeUpsert: idempotent, never throw on an absent key.\n // - writeDelete: throws DeleteOperationItemNotFoundError on an absent key.\n // So only the delete branch can throw, and we make it idempotent below.\n // Runs BEFORE the stale-echo filter: even a stale create/update echo\n // proves the server holds the row. Delete echoes need no marking — the\n // merge checks the synced store, which the delete's writeDelete empties.\n function markEchoPresence(event: RecordSubscription<RecordType>): void {\n if (event.action !== 'delete') markConfirmedPresent([event.record])\n }\n\n const handleRealtimeEvent = (event: RecordSubscription<RecordType>) => {\n if (!collection.utils) return\n markEchoPresence(event)\n if (isStaleEcho(event)) return\n\n try {\n writeOwn(() =>\n collection.utils.writeBatch(() => {\n switch (event.action) {\n case 'create':\n collection.utils.writeInsert(event.record)\n break\n case 'update':\n collection.utils.writeUpsert(event.record)\n break\n case 'delete':\n if (event.record && 'id' in event.record) {\n // Throws DeleteOperationItemNotFoundError if the key\n // is no longer in the synced store (see catch below).\n collection.utils.writeDelete(\n (event.record as { id: string }).id\n )\n }\n break\n }\n })\n )\n } catch (error) {\n // How a delete echo throws: writeDelete fails when its key is already\n // gone from the synced store. That happens when something removed it\n // before the echo arrived:\n // 1. on-demand sync — each useLiveQuery refetches with a server\n // filter, and query-db-collection prunes rows no longer owned by\n // any active query out of the synced store. If that prune (or a\n // concurrent query's reconcile) runs before this client's own\n // delete echo lands, the key is already gone -> throw. This is\n // the on-demand-only race; eager collections have no such second\n // writer to the synced store, so they cannot hit it.\n // 2. a re-delivered SSE delete (e.g. after a reconnect) for a key\n // that was already deleted -> throw on the second echo.\n // In both cases the record is already in its intended end state\n // (gone), so the echo is a no-op and the error is safe to ignore.\n // Anything that is NOT a missing-key delete is a real error: rethrow.\n if (error instanceof DeleteOperationItemNotFoundError) {\n logger.debug('Ignoring delete echo for already-removed record', {\n collectionName,\n id: (event.record as { id?: string } | undefined)?.id,\n })\n } else {\n throw error\n }\n }\n }\n\n // Start PocketBase real-time subscription\n const startSubscription = async () => {\n if (isSubscribed) return\n\n // Create promise before starting so waiters can await it\n if (!subscriptionPromise) {\n subscriptionPromise = new Promise<void>(resolve => {\n subscriptionResolve = resolve\n })\n }\n\n try {\n unsubscribeFn = await pb\n .collection(collectionName)\n .subscribe('*', handleRealtimeEvent, factoryOptions?.subscribeOptions?.())\n isSubscribed = true\n logger.debug('Subscription started', { collectionName })\n // Resolve the promise to notify waiters\n if (subscriptionResolve) {\n subscriptionResolve()\n }\n } catch (error) {\n logger.error('Failed to start subscription', { collectionName, error })\n }\n }\n\n // Stop PocketBase real-time subscription\n const stopSubscription = async () => {\n if (!isSubscribed || !unsubscribeFn) return\n\n try {\n await unsubscribeFn()\n unsubscribeFn = null\n isSubscribed = false\n // Reset promise for next subscription cycle\n subscriptionPromise = null\n subscriptionResolve = null\n logger.debug('Subscription stopped', { collectionName })\n } catch (error) {\n logger.debug('Unsubscribe failed (expected if connection closed)', {\n collectionName,\n error,\n })\n }\n }\n\n // Wait for subscription to be established (for testing)\n const waitForSubscription = async (timeout = 5000): Promise<void> => {\n if (isSubscribed) return\n\n if (!subscriptionPromise) {\n // No subscription in progress, wait for one to start\n await new Promise<void>(resolve => {\n const checkInterval = setInterval(() => {\n if (subscriptionPromise) {\n clearInterval(checkInterval)\n resolve()\n }\n }, 10)\n setTimeout(() => {\n clearInterval(checkInterval)\n resolve()\n }, timeout)\n })\n }\n\n if (subscriptionPromise) {\n await Promise.race([\n subscriptionPromise,\n new Promise<void>((_, reject) =>\n setTimeout(() => reject(new Error('Subscription timeout')), timeout)\n ),\n ])\n }\n }\n\n // Manage subscription based on collection subscriber count\n collection.on(\n 'subscribers:change',\n (event: { subscriberCount: number; previousSubscriberCount: number }) => {\n const newCount = event.subscriberCount\n const previousCount = event.previousSubscriberCount\n\n if (newCount > 0 && previousCount === 0) {\n // First subscriber - start real-time subscription\n startSubscription().catch(() => {})\n } else if (newCount === 0 && previousCount > 0) {\n // Last subscriber removed - stop real-time subscription\n stopSubscription().catch(() => {})\n }\n }\n )\n\n // Add collectionName and subscription helpers\n Object.assign(collection, {\n collectionName,\n waitForSubscription,\n isSubscribed: () => isSubscribed,\n })\n\n return collection as unknown as InferCollectionType<Schema, C, Opts>\n }\n}\n","/**\n * Generates a new PocketBase-compatible record ID.\n * Returns a 15-character alphanumeric string (lowercase letters and numbers).\n *\n * PocketBase uses 15-character IDs for records, formatted as lowercase alphanumeric.\n *\n * @returns A 15-character alphanumeric string suitable for use as a PocketBase record ID\n *\n * @example\n * ```ts\n * const id = newRecordId(); // \"a1b2c3d4e5f6g7h\"\n * ```\n */\nexport function newRecordId(): string {\n const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'\n let result = ''\n for (let i = 0; i < 15; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length))\n }\n return result\n}\n"]}
|
package/dist/core.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Collection, InsertMutationFn, UpdateMutationFn, DeleteMutationFn, BaseCollectionConfig } from '@tanstack/db';
|
|
2
2
|
export { BTreeIndex, BasicIndex, DeltaEvent, DeltaType, EffectConfig, EffectContext, IndexConstructor, ReverseIndex, createEffect, toArray } from '@tanstack/db';
|
|
3
|
+
import PocketBase, { RecordSubscribeOptions } from 'pocketbase';
|
|
4
|
+
export { RecordSubscribeOptions } from 'pocketbase';
|
|
3
5
|
import { QueryCollectionUtils } from '@tanstack/query-db-collection';
|
|
4
6
|
import { QueryClient } from '@tanstack/react-query';
|
|
5
|
-
import PocketBase from 'pocketbase';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Base record type required by PocketBase collections.
|
|
@@ -378,6 +379,22 @@ interface CreateCollectionOptions<Schema extends SchemaDeclaration, CollectionNa
|
|
|
378
379
|
collectionOptions?: Omit<Partial<BaseCollectionConfig<ExtractRecordType<Schema, CollectionName>, string | number>>, 'getKey' | 'syncMode' | 'onInsert' | 'onUpdate' | 'onDelete' | 'schema'>;
|
|
379
380
|
}
|
|
380
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Options applied to every collection built by a {@link createCollection} factory.
|
|
384
|
+
*/
|
|
385
|
+
interface CreateCollectionFactoryOptions {
|
|
386
|
+
/**
|
|
387
|
+
* Extra options passed to every real-time subscription this factory creates,
|
|
388
|
+
* such as `headers`, `filter`, `expand` or `fields`.
|
|
389
|
+
*
|
|
390
|
+
* Invoked at subscribe time rather than read once, because a subscription is
|
|
391
|
+
* re-established on reconnect and whenever the subscriber count rises from
|
|
392
|
+
* zero — a value captured at build time would go stale exactly then.
|
|
393
|
+
*
|
|
394
|
+
* Returning `undefined` subscribes with no extra options.
|
|
395
|
+
*/
|
|
396
|
+
subscribeOptions?: () => RecordSubscribeOptions | undefined;
|
|
397
|
+
}
|
|
381
398
|
/**
|
|
382
399
|
* Compute the record type with expand property when expand option is configured.
|
|
383
400
|
* @internal
|
|
@@ -440,7 +457,7 @@ type InferCollectionType<Schema extends SchemaDeclaration, C extends keyof Schem
|
|
|
440
457
|
* // data[0].expand.author is typed and populated
|
|
441
458
|
* ```
|
|
442
459
|
*/
|
|
443
|
-
declare function createCollection<Schema extends SchemaDeclaration>(pb: PocketBase, queryClient: QueryClient): <C extends keyof Schema & string, Opts extends CreateCollectionOptions<Schema, C> = CreateCollectionOptions<Schema, C>>(collectionName: C, options?: Opts) => InferCollectionType<Schema, C, Opts>;
|
|
460
|
+
declare function createCollection<Schema extends SchemaDeclaration>(pb: PocketBase, queryClient: QueryClient, factoryOptions?: CreateCollectionFactoryOptions): <C extends keyof Schema & string, Opts extends CreateCollectionOptions<Schema, C> = CreateCollectionOptions<Schema, C>>(collectionName: C, options?: Opts) => InferCollectionType<Schema, C, Opts>;
|
|
444
461
|
|
|
445
462
|
/**
|
|
446
463
|
* Logger interface for subscription events and internal operations.
|
|
@@ -524,4 +541,4 @@ declare function resetLogger(): void;
|
|
|
524
541
|
*/
|
|
525
542
|
declare function newRecordId(): string;
|
|
526
543
|
|
|
527
|
-
export { type CreateCollectionOptions, type ExcludeUndefined, type ExtractRecordType, type ExtractRelations, type Logger, type OmittableFields, type ParseExpandFields, type RelationAsCollection, type SchemaDeclaration, type WithExpand, createCollection, newRecordId, resetLogger, setLogger };
|
|
544
|
+
export { type CreateCollectionFactoryOptions, type CreateCollectionOptions, type ExcludeUndefined, type ExtractRecordType, type ExtractRelations, type Logger, type OmittableFields, type ParseExpandFields, type RelationAsCollection, type SchemaDeclaration, type WithExpand, createCollection, newRecordId, resetLogger, setLogger };
|
package/dist/core.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { BTreeIndex, BasicIndex, ReverseIndex, createCollection, createEffect, newRecordId, resetLogger, setLogger, toArray } from './chunk-
|
|
1
|
+
export { BTreeIndex, BasicIndex, ReverseIndex, createCollection, createEffect, newRecordId, resetLogger, setLogger, toArray } from './chunk-7DHFWXB3.js';
|
|
2
2
|
//# sourceMappingURL=core.js.map
|
|
3
3
|
//# sourceMappingURL=core.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { BTreeIndex, BasicIndex, DeltaEvent, DeltaType, EffectConfig, EffectContext, IndexConstructor, ReverseIndex, createEffect, toArray } from '@tanstack/db';
|
|
2
|
-
export {
|
|
2
|
+
export { RecordSubscribeOptions } from 'pocketbase';
|
|
3
|
+
export { CreateCollectionFactoryOptions, CreateCollectionOptions, ExcludeUndefined, ExtractRecordType, ExtractRelations, Logger, OmittableFields, ParseExpandFields, RelationAsCollection, SchemaDeclaration, WithExpand, createCollection, newRecordId, resetLogger, setLogger } from './core.js';
|
|
3
4
|
import React, { ReactNode } from 'react';
|
|
4
5
|
import '@tanstack/query-db-collection';
|
|
5
6
|
import '@tanstack/react-query';
|
|
6
|
-
import 'pocketbase';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* UseStore hook type for variadic collection access.
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { BTreeIndex, BasicIndex, ReverseIndex, createCollection, createEffect, newRecordId, resetLogger, setLogger, toArray } from './chunk-
|
|
1
|
+
export { BTreeIndex, BasicIndex, ReverseIndex, createCollection, createEffect, newRecordId, resetLogger, setLogger, toArray } from './chunk-7DHFWXB3.js';
|
|
2
2
|
import { createContext, useContext } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
package/llms.txt
CHANGED
|
@@ -72,7 +72,7 @@ const [myBooks] = useStore('myBooks');
|
|
|
72
72
|
|
|
73
73
|
## Key Concepts
|
|
74
74
|
|
|
75
|
-
- **createCollection**: Curried function `createCollection<Schema>(pb, queryClient)` returns `(collectionName, options) => Collection`
|
|
75
|
+
- **createCollection**: Curried function `createCollection<Schema>(pb, queryClient, factoryOptions?)` returns `(collectionName, options) => Collection`
|
|
76
76
|
- **createReactProvider**: Wraps collections for React, returns `{ Provider, useStore }`
|
|
77
77
|
- **Collections are lazy**: No network activity until first query
|
|
78
78
|
- **Subscriptions are automatic**: Start when component mounts, stop 5s after unmount
|
|
@@ -93,6 +93,33 @@ const booksCollection = c('books', {});
|
|
|
93
93
|
// Use with useLiveQuery or direct access
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
+
## Real-time Subscription Options
|
|
97
|
+
|
|
98
|
+
The third `createCollection` argument applies to every collection the factory
|
|
99
|
+
builds. `subscribeOptions` is a getter invoked at subscribe time, so the value
|
|
100
|
+
may change across reconnects:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const c = createCollection<Schema>(pb, queryClient, {
|
|
104
|
+
subscribeOptions: () => {
|
|
105
|
+
const token = getShareToken();
|
|
106
|
+
return token ? { headers: { 'X-Share-Token': token } } : undefined;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
- Returns `RecordSubscribeOptions` (re-exported from `pbtsdb`), so `headers`,
|
|
112
|
+
`filter`, `expand` and `fields` are all available.
|
|
113
|
+
- Returning `undefined` subscribes with no extra options — identical to omitting
|
|
114
|
+
the option entirely.
|
|
115
|
+
- Use a getter, not a captured object. Subscriptions restart on reconnect and
|
|
116
|
+
whenever the subscriber count rises from zero, so a value read once goes stale
|
|
117
|
+
exactly when it matters (for example when a visitor authenticates mid-session).
|
|
118
|
+
- Headers are snakecased by PocketBase, matching the REST path: `X-Share-Token`
|
|
119
|
+
is readable in collection rules as `@request.headers.x_share_token`. One rule
|
|
120
|
+
can therefore serve both REST and real-time.
|
|
121
|
+
- Requires `pocketbase >= 0.22.0`.
|
|
122
|
+
|
|
96
123
|
## Testing
|
|
97
124
|
|
|
98
125
|
```bash
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pbtsdb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Type-safe PocketBase integration with TanStack Query and TanStack DB",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pocketbase",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"@tanstack/query-db-collection": ">=1.0.40",
|
|
62
62
|
"@tanstack/react-db": ">=0.1.86",
|
|
63
63
|
"@tanstack/react-query": ">=5.0.0",
|
|
64
|
-
"pocketbase": ">=0.
|
|
64
|
+
"pocketbase": ">=0.22.0",
|
|
65
65
|
"react": ">=18.0.0",
|
|
66
66
|
"react-dom": ">=18.0.0"
|
|
67
67
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/logger.ts","../src/pocketbase-query-converter.ts","../src/collection.ts","../src/util.ts"],"names":["createTanStackCollection"],"mappings":";;;;;;;AAsCA,IAAM,aAAA,GAAwB;AAAA,EAC1B,KAAA,EAAO,CAAC,GAAA,EAAa,OAAA,KAAqB;AACtC,IAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa,aAAA,EAAe;AACxC,MAAA,OAAA,CAAQ,KAAA,CAAM,CAAA,SAAA,EAAY,GAAG,CAAA,CAAA,EAAI,WAAW,EAAE,CAAA;AAAA,IAClD;AAAA,EACJ,CAAA;AAAA,EACA,IAAA,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AW,QAAA,KAAa,CAAA,IAAK,aAAA,GAAgB,CAAA,EAAG;AAE5C,UAAA,gBAAA,EAAiB,CAAE,MAAM,MAAM;AAAA,UAAC,CAAC,CAAA;AAAA,QACrC;AAAA,MACJ;AAAA,KACJ;AAGA,IAAA,MAAA,CAAO,OAAO,UAAA,EAAY;AAAA,MACtB,cAAA;AAAA,MACA,mBAAA;AAAA,MACA,cAAc,MAAM;AAAA,KACvB,CAAA;AAED,IAAA,OAAO,UAAA;AAAA,EACX,CAAA;AACJ;;;AC5nBO,SAAS,WAAA,GAAsB;AAClC,EAAA,MAAM,KAAA,GAAQ,sCAAA;AACd,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,EAAA,EAAI,CAAA,EAAA,EAAK;AACzB,IAAA,MAAA,IAAU,KAAA,CAAM,OAAO,IAAA,CAAK,KAAA,CAAM,KAAK,MAAA,EAAO,GAAI,KAAA,CAAM,MAAM,CAAC,CAAA;AAAA,EACnE;AACA,EAAA,OAAO,MAAA;AACX","file":"chunk-RZISAGM7.js","sourcesContent":["/**\n * Logger interface for subscription events and internal operations.\n * Users can provide their own implementation to integrate with external logging services.\n */\nexport interface Logger {\n /**\n * Log debug-level messages (typically only shown in development).\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n debug: (msg: string, context?: object) => void\n\n /**\n * Log info-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n info: (msg: string, context?: object) => void\n\n /**\n * Log warning-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n warn: (msg: string, context?: object) => void\n\n /**\n * Log error-level messages.\n * @param msg - The message to log\n * @param context - Optional context object with additional information\n */\n error: (msg: string, context?: object) => void\n}\n\n/**\n * Default console-based logger implementation.\n * Only logs debug messages in development mode.\n */\nconst defaultLogger: Logger = {\n debug: (msg: string, context?: object) => {\n if (process.env.NODE_ENV === 'development') {\n console.debug(`[pbtsdb] ${msg}`, context || '')\n }\n },\n info: (msg: string, context?: object) => {\n console.info(`[pbtsdb] ${msg}`, context || '')\n },\n warn: (msg: string, context?: object) => {\n console.warn(`[pbtsdb] ${msg}`, context || '')\n },\n error: (msg: string, context?: object) => {\n console.error(`[pbtsdb] ${msg}`, context || '')\n },\n}\n\n/**\n * Current logger instance (can be replaced by users).\n */\nlet currentLogger: Logger = defaultLogger\n\n/**\n * Internal logger instance used by the library.\n */\nexport const logger: Logger = {\n debug: (msg: string, context?: object) => currentLogger.debug(msg, context),\n info: (msg: string, context?: object) => currentLogger.info(msg, context),\n warn: (msg: string, context?: object) => currentLogger.warn(msg, context),\n error: (msg: string, context?: object) => currentLogger.error(msg, context),\n}\n\n/**\n * Set a custom logger implementation.\n * This allows users to integrate with their own logging services (e.g., Sentry, LogRocket, etc.).\n *\n * @param customLogger - The custom logger implementation\n *\n * @example\n * ```ts\n * import { setLogger } from 'pbtsdb';\n *\n * // Integration with a custom logging service\n * setLogger({\n * debug: (msg, context) => myLogger.debug(msg, context),\n * warn: (msg, context) => myLogger.warn(msg, context),\n * error: (msg, context) => {\n * myLogger.error(msg, context);\n * Sentry.captureMessage(msg, { level: 'error', extra: context });\n * },\n * });\n * ```\n *\n * @example\n * ```ts\n * // Disable all logging\n * setLogger({\n * debug: () => {},\n * warn: () => {},\n * error: () => {},\n * });\n * ```\n */\nexport function setLogger(customLogger: Logger): void {\n currentLogger = customLogger\n}\n\n/**\n * Reset the logger to the default implementation.\n */\nexport function resetLogger(): void {\n currentLogger = defaultLogger\n}\n","import type { IR } from '@tanstack/db'\nimport {\n type FieldPath,\n type ParsedOrderBy,\n parseOrderByExpression,\n parseWhereExpression,\n} from '@tanstack/db'\n\ntype BasicExpression<T = unknown> = IR.BasicExpression<T>\n\nfunction escapeValue(value: unknown): string {\n if (value === null) {\n return 'null'\n }\n if (typeof value === 'boolean') {\n return value ? 'true' : 'false'\n }\n if (typeof value === 'number') {\n return value.toString()\n }\n if (typeof value === 'string') {\n return `\"${value.replace(/\"/g, '\\\\\"')}\"`\n }\n if (value instanceof Date) {\n return `\"${value.toISOString()}\"`\n }\n if (Array.isArray(value)) {\n return `[${value.map(escapeValue).join(',')}]`\n }\n return `\"${String(value)}\"`\n}\n\nfunction fieldPathToString(path: FieldPath): string {\n return path.join('.')\n}\n\nexport function convertToPocketBaseFilter(\n where: BasicExpression<boolean> | undefined | null\n): string | undefined {\n if (!where) {\n return undefined\n }\n\n const result = parseWhereExpression(where, {\n handlers: {\n eq: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} = ${escapeValue(value)}`\n },\n gt: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} > ${escapeValue(value)}`\n },\n gte: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} >= ${escapeValue(value)}`\n },\n lt: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} < ${escapeValue(value)}`\n },\n lte: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} <= ${escapeValue(value)}`\n },\n and: (...conditions: string[]) => {\n if (conditions.length === 0) return ''\n if (conditions.length === 1) return conditions[0]\n return `(${conditions.join(' && ')})`\n },\n or: (...conditions: string[]) => {\n if (conditions.length === 0) return ''\n if (conditions.length === 1) return conditions[0]\n return `(${conditions.join(' || ')})`\n },\n not: (condition: string) => {\n return `!(${condition})`\n },\n in: (field: FieldPath, values: unknown) => {\n const valueArray = Array.isArray(values) ? values : [values]\n const uniqueValues = [...new Set(valueArray)]\n const fieldStr = fieldPathToString(field)\n const conditions = uniqueValues.map(v => `${fieldStr} = ${escapeValue(v)}`)\n return conditions.length > 1 ? `(${conditions.join(' || ')})` : conditions[0]\n },\n like: (field: FieldPath, value: unknown) => {\n return `${fieldPathToString(field)} ~ ${escapeValue(value)}`\n },\n isNull: (field: FieldPath) => {\n return `${fieldPathToString(field)} = null`\n },\n isUndefined: (field: FieldPath) => {\n return `${fieldPathToString(field)} = null`\n },\n },\n onUnknownOperator: (operator: string, _args: unknown[]) => {\n throw new Error(\n `Unsupported operator '${operator}' for PocketBase filter conversion. ` +\n `Supported operators: eq, gt, gte, lt, lte, in, like, and, or, not, isNull, isUndefined`\n )\n },\n })\n\n return result || undefined\n}\n\nexport function convertToPocketBaseSort(\n orderBy: IR.OrderBy | undefined | null\n): string | undefined {\n if (!orderBy) {\n return undefined\n }\n\n const sorts = parseOrderByExpression(orderBy)\n\n if (sorts.length === 0) {\n return undefined\n }\n\n return sorts\n .map((sort: ParsedOrderBy) => {\n const field = fieldPathToString(sort.field)\n return sort.direction === 'desc' ? `-${field}` : field\n })\n .join(',')\n}\n","import {\n type Collection,\n createCollection as createTanStackCollection,\n type LoadSubsetOptions,\n} from '@tanstack/db'\nimport {\n DeleteOperationItemNotFoundError,\n type QueryCollectionUtils,\n queryCollectionOptions,\n} from '@tanstack/query-db-collection'\nimport type { QueryClient } from '@tanstack/react-query'\nimport type PocketBase from 'pocketbase'\nimport type { RecordSubscription } from 'pocketbase'\nimport { logger } from './logger'\nimport { convertToPocketBaseFilter, convertToPocketBaseSort } from './pocketbase-query-converter'\nimport type {\n CreateCollectionOptions,\n ExpandTargetCollection,\n ExtractRecordType,\n SchemaDeclaration,\n} from './types'\n\nexport type { BaseRecord, CreateCollectionOptions, SchemaDeclaration } from './types'\n\n/**\n * Extended LoadSubsetOptions that includes PocketBase-specific expand parameter.\n * @internal\n */\ntype ExtendedLoadSubsetOptions = LoadSubsetOptions & {\n pbExpand?: string\n}\n\n/**\n * Compute the record type with expand property when expand option is configured.\n * @internal\n */\ntype WithExpandFromConfig<\n Schema extends SchemaDeclaration,\n C extends keyof Schema,\n Opts,\n> = Opts extends {\n expand: infer E\n}\n ? ExtractRecordType<Schema, C> & {\n expand?: {\n [K in keyof E]: K extends keyof import('./types').ExtractRelations<Schema, C>\n ? import('./types').ExtractRelations<Schema, C>[K] extends Array<infer U>\n ? U[]\n : import('./types').ExtractRelations<Schema, C>[K]\n : never\n }\n }\n : ExtractRecordType<Schema, C>\n\n/**\n * Subscription helpers added to collection instances.\n * @internal\n */\ninterface CollectionSubscriptionHelpers {\n /** The PocketBase collection name */\n collectionName: string\n /** Wait for subscription to be established (useful in tests) */\n waitForSubscription: (timeout?: number) => Promise<void>\n /** Check if collection has an active subscription */\n isSubscribed: () => boolean\n}\n\n/**\n * Inferred collection type from config options.\n * @internal\n */\ntype InferCollectionType<\n Schema extends SchemaDeclaration,\n C extends keyof Schema,\n Opts extends CreateCollectionOptions<Schema, C>,\n> = Collection<\n WithExpandFromConfig<Schema, C, Opts>,\n string | number,\n // TUtils - QueryCollectionUtils from TanStack Query DB Collection\n QueryCollectionUtils<\n WithExpandFromConfig<Schema, C, Opts>,\n string | number,\n WithExpandFromConfig<Schema, C, Opts>\n >,\n // TSchema - we don't use StandardSchema validation\n never,\n Opts extends {\n omitOnInsert: infer O extends readonly import('./types').OmittableFields<\n ExtractRecordType<Schema, C>\n >[]\n }\n ? import('./types').ComputeInsertType<ExtractRecordType<Schema, C>, O>\n : ExtractRecordType<Schema, C>\n> &\n CollectionSubscriptionHelpers\n\n/**\n * Creates a type-safe TanStack DB collection backed by PocketBase.\n * Use this when you need fine-grained control or need to create collections with dependencies.\n *\n * @param pb - PocketBase client instance\n * @param queryClient - TanStack Query client\n * @returns A curried function that takes collection name and options\n *\n * @example\n * Basic usage:\n * ```ts\n * const booksCollection = createCollection<Schema>(pb, queryClient)('books', {});\n *\n * // Use directly\n * const books = await booksCollection.getFullList();\n * ```\n *\n * @example\n * With auto-expand relations:\n * ```ts\n * const authorsCollection = createCollection<Schema>(pb, queryClient)('authors', {});\n * const booksCollection = createCollection<Schema>(pb, queryClient)('books', {\n * expand: {\n * author: authorsCollection // Always expand, auto-upsert into authorsCollection\n * }\n * });\n *\n * // Expand is automatic - no .expand() call needed\n * const { data } = useLiveQuery((q) => q.from({ books: booksCollection }));\n * // data[0].expand.author is typed and populated\n * ```\n */\nexport function createCollection<Schema extends SchemaDeclaration>(\n pb: PocketBase,\n queryClient: QueryClient\n) {\n return <\n C extends keyof Schema & string,\n Opts extends CreateCollectionOptions<Schema, C> = CreateCollectionOptions<Schema, C>,\n >(\n collectionName: C,\n options?: Opts\n ): InferCollectionType<Schema, C, Opts> => {\n type RecordType = ExtractRecordType<Schema, C>\n const expandStores = options?.expand as Record<string, ExpandTargetCollection> | undefined\n const expandString = expandStores ? Object.keys(expandStores).sort().join(',') : undefined\n\n const ignoreAutoCancellation = options?.ignoreAutoCancellation ?? true\n const refetchOnMutation = options?.refetchOnMutation ?? false\n\n async function upsertExpandedRelation(\n key: string,\n value: object | object[],\n stores: Record<string, ExpandTargetCollection>\n ): Promise<void> {\n const targetStore = stores[key]\n if (!targetStore.utils) return\n if (!targetStore.isReady()) {\n if (targetStore.config?.syncMode === 'on-demand') {\n await targetStore._sync.startSync()\n } else {\n logger.warn(\n `not syncing ${key} on ${collectionName} because store is not yet ready`\n )\n return\n }\n }\n const values = Array.isArray(value) ? value : [value]\n targetStore.utils.writeUpsert(values)\n }\n\n async function upsertExpandedRelations(items: RecordType[]): Promise<void> {\n if (!expandStores) return\n for (const record of items) {\n const expandData = (\n record as RecordType & { expand?: Record<string, object | object[]> }\n ).expand\n if (!expandData) continue\n for (const [key, value] of Object.entries(expandData)) {\n await upsertExpandedRelation(key, value, expandStores)\n }\n }\n }\n\n async function fetchItems(loadOptions?: ExtendedLoadSubsetOptions): Promise<RecordType[]> {\n const filter = convertToPocketBaseFilter(loadOptions?.where)\n const sort = convertToPocketBaseSort(loadOptions?.orderBy)\n const limit = loadOptions?.limit\n\n if (limit) {\n // Use getList when limit is specified to avoid fetching all records\n const result = await pb.collection(collectionName).getList(1, limit, {\n filter,\n sort,\n skipTotal: true, // Optimize by skipping total count\n expand: expandString,\n })\n return result.items as unknown as RecordType[]\n }\n // Use getFullList to fetch all records with automatic pagination\n return (await pb.collection(collectionName).getFullList({\n filter,\n sort,\n expand: expandString,\n })) as unknown as RecordType[]\n }\n\n async function fetchRecords(\n loadOptions?: ExtendedLoadSubsetOptions,\n queryKey?: readonly unknown[]\n ): Promise<RecordType[]> {\n let items: RecordType[]\n try {\n items = await fetchItems(loadOptions)\n } catch (error) {\n if (\n ignoreAutoCancellation &&\n error instanceof Error &&\n error.message.includes('autocancelled')\n ) {\n // PocketBase auto-cancelled this in-flight read because a newer\n // request superseded it. Resolve to THIS subset's own cached rows\n // (keyed by the full query key) so the reconcile is a no-op for the\n // subset. The base key ([collectionName]) holds the full-collection\n // snapshot — returning that here would let applySuccessfulResult\n // reconcile foreign rows into a filtered subset (re-introducing rows\n // the subset's filter excludes). Re-throwing instead would error the\n // subset and empty/retry it.\n return (\n queryClient.getQueryData<RecordType[]>(queryKey ?? [collectionName]) ?? []\n )\n }\n throw error\n }\n\n await upsertExpandedRelations(items)\n\n return items\n }\n\n const collectionOptions = queryCollectionOptions({\n ...options?.collectionOptions,\n queryClient,\n queryKey: [collectionName],\n syncMode: options?.syncMode ?? 'eager',\n queryFn: async (ctx): Promise<RecordType[]> => {\n return fetchRecords(\n ctx.meta?.loadSubsetOptions as ExtendedLoadSubsetOptions | undefined,\n ctx.queryKey\n )\n },\n getKey: (item: RecordType) => {\n const record = item as unknown as Record<string, unknown>\n if (!record || typeof record !== 'object' || !('id' in record)) {\n throw new Error(\n `Record in collection '${collectionName}' is missing required 'id' field. Received: ${JSON.stringify(item)}`\n )\n }\n return record.id as string\n },\n onInsert:\n options?.onInsert === false\n ? undefined\n : (options?.onInsert ??\n (async ({ transaction }) => {\n const created = await Promise.all(\n transaction.mutations.map(async mutation => {\n const {\n created: _created,\n updated: _updated,\n collectionId: _collectionId,\n collectionName: _collectionName,\n ...data\n } = mutation.modified as unknown as Record<string, unknown>\n return pb.collection(collectionName).create(data)\n })\n )\n writeServerRecords(created)\n return { refetch: refetchOnMutation }\n })),\n onUpdate:\n options?.onUpdate === false\n ? undefined\n : (options?.onUpdate ??\n (async ({ transaction }) => {\n const updated = await Promise.all(\n transaction.mutations.map(async mutation => {\n const recordWithId = mutation.original as { id: string }\n return pb\n .collection(collectionName)\n .update(recordWithId.id, mutation.changes)\n })\n )\n writeServerRecords(updated)\n return { refetch: refetchOnMutation }\n })),\n onDelete:\n options?.onDelete === false\n ? undefined\n : (options?.onDelete ??\n (async ({ transaction }) => {\n await Promise.all(\n transaction.mutations.map(async mutation => {\n const recordWithId = mutation.original as { id: string }\n await pb.collection(collectionName).delete(recordWithId.id)\n })\n )\n return { refetch: refetchOnMutation }\n })),\n })\n\n // Set while pbtsdb performs its own authoritative writes (mutation-response\n // write-backs and realtime echoes) through collection.utils.*. Those writes\n // share the same sync `write` primitive as the query-result reconcile path\n // (see the sync.sync wrapper below), so the guard uses this flag to tell them\n // apart: pbtsdb's own writes are exempt from the optimistic-pending arm of the\n // guard (the mutation-response write-back intentionally lands the confirmed\n // value while that very mutation's optimistic overlay is still in flight).\n let applyingOwnWrite = false\n function writeOwn(fn: () => void): void {\n applyingOwnWrite = true\n try {\n fn()\n } finally {\n applyingOwnWrite = false\n }\n }\n\n // The record id a synced insert/update targets, or null when the op is a delete\n // (terminal — never guarded) or carries no usable key.\n function syncedWriteKey(op: {\n type: string\n value?: unknown\n key?: unknown\n }): string | null {\n if (op.type !== 'insert' && op.type !== 'update') return null\n if (typeof op.key === 'string') return op.key\n const id = (op.value as { id?: unknown } | undefined)?.id\n return typeof id === 'string' ? id : null\n }\n\n // Guard the synced write path that pbtsdb does not otherwise control:\n // @tanstack/query-db-collection's applySuccessfulResult reconciles every query\n // result into the synced store via this same `write`, with no recency or\n // optimistic check. Under on-demand contention a single-row/subset read can\n // resolve with a pre-mutation row and land here after the row already moved on,\n // reverting it. We drop such a synced insert/update when either it targets a key\n // with a pending optimistic mutation (see the arm below) or it is no newer than\n // the synced row (out-of-order or post-settle read — equal `updated` included,\n // since the read can only carry a same-or-older value than the move's write-back\n // already in synced). pbtsdb's own writes (applyingOwnWrite) skip the optimistic\n // arm; they are still staleness-filtered upstream by writeServerRecords/isStaleEcho\n // (which keep the strict `<` so a confirmed same-second value can re-land).\n function shouldDropSyncedWrite(op: {\n type: string\n value?: unknown\n key?: unknown\n }): boolean {\n const key = syncedWriteKey(op)\n if (key === null) return false\n // Optimistic arm: only guard a key already present in the synced store. A\n // write to a key the synced store doesn't yet hold is populating it (e.g. the\n // initial fetch landing a row the user just optimistically inserted) and must\n // pass — dropping it would leave the row absent once the overlay clears. A\n // write to a key already synced, while an optimistic mutation is pending, is a\n // racing read that would revert the in-flight value, so drop it.\n if (\n !applyingOwnWrite &&\n hasPendingOptimisticMutation(key) &&\n collection._state.syncedData.has(key)\n ) {\n logger.debug('Dropping synced write for optimistically-pending row', {\n collectionName,\n id: key,\n })\n return true\n }\n if (isStaleServerRecord(op.value, !applyingOwnWrite)) {\n logger.debug('Dropping stale synced write', { collectionName, id: key })\n return true\n }\n return false\n }\n\n // Wrap the sync factory so every synced `write` flows through the guard above.\n // @tanstack/db invokes sync.sync with the write primitives; we hand back the\n // same params with a filtered `write`. begin/commit/markReady/etc. pass through.\n const innerSync = collectionOptions.sync.sync\n collectionOptions.sync = {\n ...collectionOptions.sync,\n sync: (params: Parameters<typeof innerSync>[0]) => {\n const guardedWrite: typeof params.write = message => {\n if (\n shouldDropSyncedWrite(\n message as { type: string; value?: unknown; key?: unknown }\n )\n ) {\n return\n }\n return params.write(message)\n }\n return innerSync({ ...params, write: guardedWrite })\n },\n }\n\n const collection = createTanStackCollection(collectionOptions)\n\n // True when the key has an in-flight (not yet settled) optimistic mutation.\n // While pending, the optimistic overlay — not syncedData — is the visible value,\n // so any synced write would only take effect once the overlay collapses, and an\n // older/racing read landing here is exactly what reverts a just-applied move.\n function hasPendingOptimisticMutation(key: string): boolean {\n const state = collection._state as unknown as {\n optimisticUpserts: { has: (k: string) => boolean }\n optimisticDeletes: { has: (k: string) => boolean }\n }\n return state.optimisticUpserts.has(key) || state.optimisticDeletes.has(key)\n }\n\n // Read the PocketBase `updated` autodate from a record, if present.\n // Collections without an `updated` field opt out of staleness checks.\n function recordUpdatedAt(record: unknown): string | undefined {\n const updated = (record as { updated?: unknown } | null | undefined)?.updated\n return typeof updated === 'string' && updated !== '' ? updated : undefined\n }\n\n // A server record is stale relative to the synced store when an entry for\n // the same key already holds a newer `updated` timestamp. PocketBase can\n // redeliver or reorder realtime echoes (and a slow mutation response can\n // resolve after a newer echo), so applying an older write would revert the\n // row. ISO 8601 timestamps sort lexicographically, so string comparison is\n // chronological. When either side lacks a comparable timestamp we cannot\n // tell, so we treat the write as fresh and let it through.\n //\n // `treatEqualAsStale` controls the equal-timestamp case. PocketBase bumps\n // `updated` on every mutation, so a record carrying the SAME `updated` second\n // as the synced row holds the same content — it cannot be newer. The default\n // (strict `<`) lets an own write-back/realtime echo re-land that confirmed\n // value harmlessly. The query-result path passes `true` (`<=`): a server read\n // resolving with the same-second value is the post-settle revert race — once\n // an optimistic move settles, the only thing that put a *fresher* value in\n // synced is that move's own write-back, and a same-second read would overwrite\n // it back to the pre-move row. There is nothing newer for an equal-timestamp\n // query result to legitimately deliver, so dropping it is safe.\n function isStaleServerRecord(record: unknown, treatEqualAsStale = false): boolean {\n const id = (record as { id?: unknown } | null | undefined)?.id\n if (typeof id !== 'string') return false\n const incoming = recordUpdatedAt(record)\n if (!incoming) return false\n const current = recordUpdatedAt(\n collection._state.syncedData.get(id) as RecordType | undefined\n )\n if (current === undefined) return false\n return treatEqualAsStale ? incoming <= current : incoming < current\n }\n\n // Write authoritative server records (mutation responses or realtime echoes)\n // into the synced store, dropping any that the store already supersedes.\n // No-op until the collection is ready: writing into the synced store before\n // sync has initialized throws, and with no live query there is nothing to\n // keep in sync — the next query fetches the already-persisted state.\n function writeServerRecords(records: RecordType[]): void {\n if (!collection.utils || !collection.isReady()) return\n const fresh = records.filter(record => !isStaleServerRecord(record))\n if (fresh.length === 0) return\n writeOwn(() => collection.utils.writeUpsert(fresh))\n }\n\n // Decide whether a realtime echo should be dropped as stale. Under realtime\n // contention PocketBase can redeliver or reorder events, so an echo carrying\n // a pre-mutation value can arrive after the row already moved on (e.g. the\n // local mutation that just wrote the fresh value back). Applying a\n // strictly-older create/update echo would revert the row, so it is ignored.\n // Deletes are terminal and not timestamp-guarded.\n function isStaleEcho(event: RecordSubscription<RecordType>): boolean {\n if (event.action !== 'create' && event.action !== 'update') return false\n if (!isStaleServerRecord(event.record)) return false\n logger.debug('Ignoring stale realtime echo', {\n collectionName,\n id: (event.record as { id?: string } | undefined)?.id,\n })\n return true\n }\n\n // Real-time subscription state\n let unsubscribeFn: (() => Promise<void>) | null = null\n let isSubscribed = false\n let subscriptionPromise: Promise<void> | null = null\n let subscriptionResolve: (() => void) | null = null\n\n // Handle real-time events from PocketBase.\n //\n // The write primitives differ in how they treat a key that is absent from\n // the *synced* store (collection._state.syncedData, which is what they\n // validate against — not the optimistic view exposed by collection.has()):\n // - writeInsert / writeUpsert: idempotent, never throw on an absent key.\n // - writeDelete: throws DeleteOperationItemNotFoundError on an absent key.\n // So only the delete branch can throw, and we make it idempotent below.\n const handleRealtimeEvent = (event: RecordSubscription<RecordType>) => {\n if (!collection.utils) return\n if (isStaleEcho(event)) return\n\n try {\n writeOwn(() =>\n collection.utils.writeBatch(() => {\n switch (event.action) {\n case 'create':\n collection.utils.writeInsert(event.record)\n break\n case 'update':\n collection.utils.writeUpsert(event.record)\n break\n case 'delete':\n if (event.record && 'id' in event.record) {\n // Throws DeleteOperationItemNotFoundError if the key\n // is no longer in the synced store (see catch below).\n collection.utils.writeDelete(\n (event.record as { id: string }).id\n )\n }\n break\n }\n })\n )\n } catch (error) {\n // How a delete echo throws: writeDelete fails when its key is already\n // gone from the synced store. That happens when something removed it\n // before the echo arrived:\n // 1. on-demand sync — each useLiveQuery refetches with a server\n // filter, and query-db-collection prunes rows no longer owned by\n // any active query out of the synced store. If that prune (or a\n // concurrent query's reconcile) runs before this client's own\n // delete echo lands, the key is already gone -> throw. This is\n // the on-demand-only race; eager collections have no such second\n // writer to the synced store, so they cannot hit it.\n // 2. a re-delivered SSE delete (e.g. after a reconnect) for a key\n // that was already deleted -> throw on the second echo.\n // In both cases the record is already in its intended end state\n // (gone), so the echo is a no-op and the error is safe to ignore.\n // Anything that is NOT a missing-key delete is a real error: rethrow.\n if (error instanceof DeleteOperationItemNotFoundError) {\n logger.debug('Ignoring delete echo for already-removed record', {\n collectionName,\n id: (event.record as { id?: string } | undefined)?.id,\n })\n } else {\n throw error\n }\n }\n }\n\n // Start PocketBase real-time subscription\n const startSubscription = async () => {\n if (isSubscribed) return\n\n // Create promise before starting so waiters can await it\n if (!subscriptionPromise) {\n subscriptionPromise = new Promise<void>(resolve => {\n subscriptionResolve = resolve\n })\n }\n\n try {\n unsubscribeFn = await pb\n .collection(collectionName)\n .subscribe('*', handleRealtimeEvent)\n isSubscribed = true\n logger.debug('Subscription started', { collectionName })\n // Resolve the promise to notify waiters\n if (subscriptionResolve) {\n subscriptionResolve()\n }\n } catch (error) {\n logger.error('Failed to start subscription', { collectionName, error })\n }\n }\n\n // Stop PocketBase real-time subscription\n const stopSubscription = async () => {\n if (!isSubscribed || !unsubscribeFn) return\n\n try {\n await unsubscribeFn()\n unsubscribeFn = null\n isSubscribed = false\n // Reset promise for next subscription cycle\n subscriptionPromise = null\n subscriptionResolve = null\n logger.debug('Subscription stopped', { collectionName })\n } catch (error) {\n logger.debug('Unsubscribe failed (expected if connection closed)', {\n collectionName,\n error,\n })\n }\n }\n\n // Wait for subscription to be established (for testing)\n const waitForSubscription = async (timeout = 5000): Promise<void> => {\n if (isSubscribed) return\n\n if (!subscriptionPromise) {\n // No subscription in progress, wait for one to start\n await new Promise<void>(resolve => {\n const checkInterval = setInterval(() => {\n if (subscriptionPromise) {\n clearInterval(checkInterval)\n resolve()\n }\n }, 10)\n setTimeout(() => {\n clearInterval(checkInterval)\n resolve()\n }, timeout)\n })\n }\n\n if (subscriptionPromise) {\n await Promise.race([\n subscriptionPromise,\n new Promise<void>((_, reject) =>\n setTimeout(() => reject(new Error('Subscription timeout')), timeout)\n ),\n ])\n }\n }\n\n // Manage subscription based on collection subscriber count\n collection.on(\n 'subscribers:change',\n (event: { subscriberCount: number; previousSubscriberCount: number }) => {\n const newCount = event.subscriberCount\n const previousCount = event.previousSubscriberCount\n\n if (newCount > 0 && previousCount === 0) {\n // First subscriber - start real-time subscription\n startSubscription().catch(() => {})\n } else if (newCount === 0 && previousCount > 0) {\n // Last subscriber removed - stop real-time subscription\n stopSubscription().catch(() => {})\n }\n }\n )\n\n // Add collectionName and subscription helpers\n Object.assign(collection, {\n collectionName,\n waitForSubscription,\n isSubscribed: () => isSubscribed,\n })\n\n return collection as unknown as InferCollectionType<Schema, C, Opts>\n }\n}\n","/**\n * Generates a new PocketBase-compatible record ID.\n * Returns a 15-character alphanumeric string (lowercase letters and numbers).\n *\n * PocketBase uses 15-character IDs for records, formatted as lowercase alphanumeric.\n *\n * @returns A 15-character alphanumeric string suitable for use as a PocketBase record ID\n *\n * @example\n * ```ts\n * const id = newRecordId(); // \"a1b2c3d4e5f6g7h\"\n * ```\n */\nexport function newRecordId(): string {\n const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'\n let result = ''\n for (let i = 0; i < 15; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length))\n }\n return result\n}\n"]}
|