@taladb/react 0.9.3 → 0.9.4
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/dist/index.d.mts +175 -41
- package/dist/index.d.ts +175 -41
- package/dist/index.js +383 -146
- package/dist/index.mjs +393 -149
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { CollectionOptions, Document, TalaDB, OpenDBOptions, Collection, Filter } from 'taladb';
|
|
3
|
+
import { CollectionOptions, Document, TalaDB, OpenDBOptions, Collection, Filter, AggregatePipeline, RestSourceOptions, ReplicationSource, CoverageState } from 'taladb';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Per-collection options (`schema`, `syncSchema`, `migrateDocument`, …), keyed by
|
|
@@ -163,6 +163,40 @@ interface FindOneResult<T> {
|
|
|
163
163
|
*/
|
|
164
164
|
declare function useFindOne<T extends Document>(collection: Collection<T>, filter: Filter<T>): FindOneResult<T>;
|
|
165
165
|
|
|
166
|
+
interface AggregateResult<R> {
|
|
167
|
+
/** The current pipeline results. Empty while loading. */
|
|
168
|
+
data: R[];
|
|
169
|
+
/** True until the first snapshot has been delivered. */
|
|
170
|
+
loading: boolean;
|
|
171
|
+
/** Most recent subscription error, cleared by the next successful snapshot. */
|
|
172
|
+
error: unknown | null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Subscribe to a **live aggregation**. Re-renders whenever the results change.
|
|
176
|
+
*
|
|
177
|
+
* This is the paging primitive. `find()` has no sort/skip/limit — only `aggregate`
|
|
178
|
+
* does — but `aggregate` on its own returns a *dead snapshot*: call it in an effect
|
|
179
|
+
* and the page sits frozen while a background hydration fills the collection
|
|
180
|
+
* underneath it. The user watches a spinner finish and the rows never arrive.
|
|
181
|
+
*
|
|
182
|
+
* So anything that pages locally subscribes here rather than calling `aggregate`
|
|
183
|
+
* directly.
|
|
184
|
+
*
|
|
185
|
+
* @param collection A `Collection<T>` (memoize it, or take it from `useCollection`).
|
|
186
|
+
* @param pipeline Inline arrays are safe — the pipeline is serialised for
|
|
187
|
+
* subscription identity, so a fresh array each render does not
|
|
188
|
+
* re-subscribe.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const page = useAggregate<Product, Product>(products, [
|
|
192
|
+
* { $match: { category: 'kitchen' } },
|
|
193
|
+
* { $sort: { price: 1 } },
|
|
194
|
+
* { $skip: 100 },
|
|
195
|
+
* { $limit: 100 },
|
|
196
|
+
* ])
|
|
197
|
+
*/
|
|
198
|
+
declare function useAggregate<T extends Document, R extends Document = T>(collection: Collection<T>, pipeline: AggregatePipeline<T>): AggregateResult<R>;
|
|
199
|
+
|
|
166
200
|
/** Resolved network configuration for one replicated slice. */
|
|
167
201
|
interface ResolvedReplicationConfig {
|
|
168
202
|
/** Base URL; `/push` and `/pull` are appended by {@link HttpSyncAdapter}. */
|
|
@@ -183,6 +217,37 @@ interface ResolvedReplicationConfig {
|
|
|
183
217
|
};
|
|
184
218
|
}
|
|
185
219
|
|
|
220
|
+
/** When the background hydration walk is allowed to start. */
|
|
221
|
+
type HydrateMode =
|
|
222
|
+
/** Immediately on mount. */
|
|
223
|
+
'eager'
|
|
224
|
+
/** When the browser is idle (default). Keeps first paint responsive. */
|
|
225
|
+
| 'idle'
|
|
226
|
+
/** Never automatically — the app calls `hydrate()` itself. */
|
|
227
|
+
| 'manual';
|
|
228
|
+
interface ReplicateScope<RemoteRow = any, T extends Document = Document> extends Omit<RestSourceOptions<RemoteRow, T>, 'collection'> {
|
|
229
|
+
/**
|
|
230
|
+
* Provide a fully custom source instead of the REST defaults. When present,
|
|
231
|
+
* every other field here is ignored.
|
|
232
|
+
*/
|
|
233
|
+
source?: ReplicationSource<RemoteRow, T>;
|
|
234
|
+
hydrate?: HydrateMode;
|
|
235
|
+
/** Rows per bootstrap page. Default 500. */
|
|
236
|
+
pageSize?: number;
|
|
237
|
+
/** Re-check the origin for changes on this interval. `0` disables. */
|
|
238
|
+
refreshMs?: number;
|
|
239
|
+
/**
|
|
240
|
+
* Fetch the current query directly when coverage isn't ready yet, so a cold
|
|
241
|
+
* start paints immediately. Default `true`.
|
|
242
|
+
*
|
|
243
|
+
* Mandatory in practice for a Vite SPA or React Native, which have no server
|
|
244
|
+
* render to paint behind while the replica fills.
|
|
245
|
+
*/
|
|
246
|
+
bridge?: boolean;
|
|
247
|
+
}
|
|
248
|
+
/** One entry per local collection. */
|
|
249
|
+
type ReplicateRegistry = Record<string, ReplicateScope<any, any>>;
|
|
250
|
+
|
|
186
251
|
/** A slice to warm on first run — a collection, optionally on a specific endpoint. */
|
|
187
252
|
type PrefetchSlice = {
|
|
188
253
|
collection: string;
|
|
@@ -236,7 +301,15 @@ interface ReplicationConfig {
|
|
|
236
301
|
/** Max concurrent prefetch pulls — keeps the active page from starving. Default `2`. */
|
|
237
302
|
prefetchConcurrency?: number;
|
|
238
303
|
}
|
|
239
|
-
interface ReplicationProviderProps extends ReplicationConfig {
|
|
304
|
+
interface ReplicationProviderProps extends Partial<ReplicationConfig> {
|
|
305
|
+
/**
|
|
306
|
+
* Collections to replicate from a remote origin, keyed by local collection name.
|
|
307
|
+
*
|
|
308
|
+
* This is what makes `useQuery` a local read: once a collection is fully
|
|
309
|
+
* hydrated for its scope, filtering, sorting and paging never touch the network.
|
|
310
|
+
* See {@link ReplicateRegistry}.
|
|
311
|
+
*/
|
|
312
|
+
replicate?: ReplicateRegistry;
|
|
240
313
|
children: ReactNode;
|
|
241
314
|
}
|
|
242
315
|
/**
|
|
@@ -257,7 +330,7 @@ interface ReplicationProviderProps extends ReplicationConfig {
|
|
|
257
330
|
* </TalaDBProvider>
|
|
258
331
|
* ```
|
|
259
332
|
*/
|
|
260
|
-
declare function ReplicationProvider({ children, ...config }: ReplicationProviderProps): react_jsx_runtime.JSX.Element;
|
|
333
|
+
declare function ReplicationProvider({ children, replicate, ...config }: ReplicationProviderProps): react_jsx_runtime.JSX.Element;
|
|
261
334
|
/**
|
|
262
335
|
* Read the nearest replication config, merged with per-hook overrides.
|
|
263
336
|
* Non-throwing: `config` is `null` when no endpoint is resolvable (valid for
|
|
@@ -268,73 +341,134 @@ declare function useReplicationConfig(overrides?: Partial<ReplicationConfig>): {
|
|
|
268
341
|
pollMs: number;
|
|
269
342
|
};
|
|
270
343
|
|
|
344
|
+
interface Coverage {
|
|
345
|
+
/** The raw state machine value. */
|
|
346
|
+
status: CoverageState['status'];
|
|
347
|
+
/**
|
|
348
|
+
* Whether a purely local read is authorized.
|
|
349
|
+
*
|
|
350
|
+
* True **only** for `complete`. Notably *not* for `best-effort`, which means we
|
|
351
|
+
* applied every row the origin gave us but the origin could not pin a snapshot —
|
|
352
|
+
* so a row that shifted between pages during the walk may never have been seen,
|
|
353
|
+
* and we cannot prove the replica is whole. Serving that as authoritative would
|
|
354
|
+
* silently return incomplete results, which is worse than going to the network.
|
|
355
|
+
*/
|
|
356
|
+
ready: boolean;
|
|
357
|
+
/** Rows hydrated so far. */
|
|
358
|
+
rows: number;
|
|
359
|
+
/** Total rows in scope when supplied by the origin. */
|
|
360
|
+
total?: number;
|
|
361
|
+
/** 0–1 when the origin reported a total; otherwise undefined. */
|
|
362
|
+
progress?: number;
|
|
363
|
+
/** Present on `error`, `stale` and `best-effort`. */
|
|
364
|
+
reason?: string;
|
|
365
|
+
}
|
|
271
366
|
/**
|
|
272
|
-
* How a
|
|
273
|
-
*
|
|
274
|
-
*
|
|
367
|
+
* How much of a collection is local, and whether it can be trusted for a
|
|
368
|
+
* network-free read.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* const { ready, progress } = useCoverage('products')
|
|
372
|
+
* if (!ready) return <ProgressBar value={progress} />
|
|
275
373
|
*/
|
|
374
|
+
declare function useCoverage(collection: string): Coverage;
|
|
375
|
+
/** Semantic alias for progress-oriented UIs. */
|
|
376
|
+
declare const useHydrationProgress: typeof useCoverage;
|
|
377
|
+
|
|
276
378
|
type ReadSource = 'local-first' | 'remote-first' | 'local-only';
|
|
277
379
|
interface UseQueryOptions<T extends Document> extends Partial<Pick<ReplicationConfig, 'endpoint' | 'getAuth' | 'fetch' | 'paths' | 'pollMs'>> {
|
|
278
|
-
/**
|
|
380
|
+
/** The local collection to read. */
|
|
279
381
|
collection: string;
|
|
280
|
-
/**
|
|
382
|
+
/** Mongo-style filter, applied **locally**. */
|
|
281
383
|
filter?: Filter<T>;
|
|
282
|
-
/**
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
384
|
+
/** Sort, applied locally. `1` ascending, `-1` descending. */
|
|
385
|
+
sort?: Partial<Record<keyof T & string, 1 | -1>>;
|
|
386
|
+
/** 1-based page number. Requires `limit`. */
|
|
387
|
+
page?: number;
|
|
388
|
+
/** Rows per page. */
|
|
389
|
+
limit?: number;
|
|
390
|
+
/** Skip N rows. Ignored when `page` is set. */
|
|
391
|
+
skip?: number;
|
|
392
|
+
/** Skip the query entirely (e.g. while a route param is undefined). */
|
|
393
|
+
enabled?: boolean;
|
|
394
|
+
/** Legacy sync-contract read policy. Used when no full-replication scope exists. */
|
|
288
395
|
source?: ReadSource;
|
|
289
396
|
}
|
|
290
397
|
interface QueryResult<T> {
|
|
291
|
-
/**
|
|
398
|
+
/** The current page. Reactive: re-renders as rows land. */
|
|
292
399
|
data: T[];
|
|
293
|
-
/**
|
|
400
|
+
/** Total rows in the replicated scope when reported by the origin. */
|
|
401
|
+
total?: number;
|
|
402
|
+
/** True until the first local snapshot arrives. */
|
|
294
403
|
loading: boolean;
|
|
295
|
-
/** Most recent local
|
|
404
|
+
/** Most recent local read error. */
|
|
296
405
|
error: unknown | null;
|
|
297
|
-
/**
|
|
406
|
+
/** Most recent cold-start bridge error. */
|
|
407
|
+
fetchError: unknown | null;
|
|
408
|
+
/** How much of the collection is local, and whether it is trustworthy. */
|
|
409
|
+
coverage: Coverage;
|
|
410
|
+
/**
|
|
411
|
+
* A cold-start bridge fetch is in flight — we are serving the network because
|
|
412
|
+
* the replica isn't complete yet.
|
|
413
|
+
*/
|
|
414
|
+
fetching: boolean;
|
|
415
|
+
/** Legacy sync-contract pull in progress. */
|
|
298
416
|
syncing: boolean;
|
|
299
|
-
/**
|
|
417
|
+
/** Legacy sync-contract pull error. */
|
|
300
418
|
syncError: unknown | null;
|
|
301
|
-
/**
|
|
419
|
+
/** Force a delta refresh from the origin. */
|
|
302
420
|
refetch: () => Promise<void>;
|
|
303
421
|
}
|
|
304
422
|
/**
|
|
305
|
-
*
|
|
423
|
+
* Read a page of a collection.
|
|
424
|
+
*
|
|
425
|
+
* ## The point
|
|
426
|
+
*
|
|
427
|
+
* Once the collection is **covered** — fully replicated for this scope — this hook
|
|
428
|
+
* touches the network **zero times**. Filtering, sorting and paging are local
|
|
429
|
+
* queries against the on-device database. Page 1 → page 2 → a new filter → page 47
|
|
430
|
+
* → back to page 1: every one is a local read, instant and offline-capable.
|
|
431
|
+
* Pagination stops being a network concern at all.
|
|
432
|
+
*
|
|
433
|
+
* That is the whole reason to put a real database on the device. A cache of API
|
|
434
|
+
* pages could only ever answer the queries you already asked; a *covered replica*
|
|
435
|
+
* answers queries nobody has asked yet.
|
|
436
|
+
*
|
|
437
|
+
* ## Before coverage lands
|
|
306
438
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
439
|
+
* On a cold start the replica is empty, and a SPA or React Native app has no server
|
|
440
|
+
* render to paint behind. So the hook **bridges**: it fetches exactly the rows this
|
|
441
|
+
* query needs and writes them into the same collection, under the same derived ids
|
|
442
|
+
* the background walk will use. Those rows are not a cache entry to be reconciled
|
|
443
|
+
* later — they are the replica, arriving early. When the walk reaches them it
|
|
444
|
+
* overwrites them in place.
|
|
311
445
|
*
|
|
312
446
|
* @example
|
|
313
|
-
* const { data,
|
|
447
|
+
* const { data, coverage } = useQuery<Product>({
|
|
314
448
|
* collection: 'products',
|
|
315
|
-
* filter: { category: 'kitchen' },
|
|
316
|
-
*
|
|
449
|
+
* filter: { category: 'kitchen', price: { $lt: 500 } },
|
|
450
|
+
* sort: { price: 1 },
|
|
451
|
+
* page: 2,
|
|
452
|
+
* limit: 100,
|
|
317
453
|
* })
|
|
318
454
|
*/
|
|
319
455
|
declare function useQuery<T extends Document>(options: UseQueryOptions<T>): QueryResult<T>;
|
|
320
456
|
|
|
321
457
|
/**
|
|
322
|
-
*
|
|
323
|
-
* in parallel. The result array is index-aligned with `queries`.
|
|
458
|
+
* Several pages at once, from several collections. Index-aligned with `queries`.
|
|
324
459
|
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* no cross-collection joins (TalaDB is a document store); compose in the
|
|
329
|
-
* component.
|
|
460
|
+
* Hooks can't be called in a variable-length loop, so this manages its own
|
|
461
|
+
* subscriptions rather than calling `useQuery` N times. Behaviour is otherwise
|
|
462
|
+
* identical: once a collection is covered, its read is purely local.
|
|
330
463
|
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
464
|
+
* TalaDB is a document store with no cross-collection joins, so a page needing
|
|
465
|
+
* several collections composes them in the component (or denormalises at the
|
|
466
|
+
* origin). This is the hook for that.
|
|
333
467
|
*
|
|
334
468
|
* @example
|
|
335
|
-
* const [
|
|
336
|
-
* { collection: '
|
|
337
|
-
* { collection: '
|
|
469
|
+
* const [products, categories] = useQueries([
|
|
470
|
+
* { collection: 'products', filter: { category }, sort: { price: 1 }, page, limit: 50 },
|
|
471
|
+
* { collection: 'categories' },
|
|
338
472
|
* ])
|
|
339
473
|
*/
|
|
340
474
|
declare function useQueries(queries: UseQueryOptions<Document>[]): QueryResult<Document>[];
|
|
@@ -392,4 +526,4 @@ interface MutationResult<T extends Document> {
|
|
|
392
526
|
*/
|
|
393
527
|
declare function useMutation<T extends Document>(options: UseMutationOptions): MutationResult<T>;
|
|
394
528
|
|
|
395
|
-
export { type CollectionRegistry, type CollectionResolver, type FindOneResult, type FindResult, type MutationResult, type PrefetchEntry, type PrefetchMode, type PrefetchSlice, type QueryResult, type
|
|
529
|
+
export { type AggregateResult, type CollectionRegistry, type CollectionResolver, type Coverage, type FindOneResult, type FindResult, type HydrateMode, type MutationResult, type PrefetchEntry, type PrefetchMode, type PrefetchSlice, type QueryResult, type ReplicateRegistry, type ReplicateScope, type ReplicationConfig, ReplicationProvider, type ReplicationProviderProps, TalaDBProvider, type TalaDBProviderProps, type UseMutationOptions, type UseQueryOptions, type WriteOp, useAggregate, useCollection, useCollectionOptions, useCoverage, useFind, useFindOne, useHydrationProgress, useMutation, useQueries, useQuery, useReplicationConfig, useTalaDB };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { CollectionOptions, Document, TalaDB, OpenDBOptions, Collection, Filter } from 'taladb';
|
|
3
|
+
import { CollectionOptions, Document, TalaDB, OpenDBOptions, Collection, Filter, AggregatePipeline, RestSourceOptions, ReplicationSource, CoverageState } from 'taladb';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Per-collection options (`schema`, `syncSchema`, `migrateDocument`, …), keyed by
|
|
@@ -163,6 +163,40 @@ interface FindOneResult<T> {
|
|
|
163
163
|
*/
|
|
164
164
|
declare function useFindOne<T extends Document>(collection: Collection<T>, filter: Filter<T>): FindOneResult<T>;
|
|
165
165
|
|
|
166
|
+
interface AggregateResult<R> {
|
|
167
|
+
/** The current pipeline results. Empty while loading. */
|
|
168
|
+
data: R[];
|
|
169
|
+
/** True until the first snapshot has been delivered. */
|
|
170
|
+
loading: boolean;
|
|
171
|
+
/** Most recent subscription error, cleared by the next successful snapshot. */
|
|
172
|
+
error: unknown | null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Subscribe to a **live aggregation**. Re-renders whenever the results change.
|
|
176
|
+
*
|
|
177
|
+
* This is the paging primitive. `find()` has no sort/skip/limit — only `aggregate`
|
|
178
|
+
* does — but `aggregate` on its own returns a *dead snapshot*: call it in an effect
|
|
179
|
+
* and the page sits frozen while a background hydration fills the collection
|
|
180
|
+
* underneath it. The user watches a spinner finish and the rows never arrive.
|
|
181
|
+
*
|
|
182
|
+
* So anything that pages locally subscribes here rather than calling `aggregate`
|
|
183
|
+
* directly.
|
|
184
|
+
*
|
|
185
|
+
* @param collection A `Collection<T>` (memoize it, or take it from `useCollection`).
|
|
186
|
+
* @param pipeline Inline arrays are safe — the pipeline is serialised for
|
|
187
|
+
* subscription identity, so a fresh array each render does not
|
|
188
|
+
* re-subscribe.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const page = useAggregate<Product, Product>(products, [
|
|
192
|
+
* { $match: { category: 'kitchen' } },
|
|
193
|
+
* { $sort: { price: 1 } },
|
|
194
|
+
* { $skip: 100 },
|
|
195
|
+
* { $limit: 100 },
|
|
196
|
+
* ])
|
|
197
|
+
*/
|
|
198
|
+
declare function useAggregate<T extends Document, R extends Document = T>(collection: Collection<T>, pipeline: AggregatePipeline<T>): AggregateResult<R>;
|
|
199
|
+
|
|
166
200
|
/** Resolved network configuration for one replicated slice. */
|
|
167
201
|
interface ResolvedReplicationConfig {
|
|
168
202
|
/** Base URL; `/push` and `/pull` are appended by {@link HttpSyncAdapter}. */
|
|
@@ -183,6 +217,37 @@ interface ResolvedReplicationConfig {
|
|
|
183
217
|
};
|
|
184
218
|
}
|
|
185
219
|
|
|
220
|
+
/** When the background hydration walk is allowed to start. */
|
|
221
|
+
type HydrateMode =
|
|
222
|
+
/** Immediately on mount. */
|
|
223
|
+
'eager'
|
|
224
|
+
/** When the browser is idle (default). Keeps first paint responsive. */
|
|
225
|
+
| 'idle'
|
|
226
|
+
/** Never automatically — the app calls `hydrate()` itself. */
|
|
227
|
+
| 'manual';
|
|
228
|
+
interface ReplicateScope<RemoteRow = any, T extends Document = Document> extends Omit<RestSourceOptions<RemoteRow, T>, 'collection'> {
|
|
229
|
+
/**
|
|
230
|
+
* Provide a fully custom source instead of the REST defaults. When present,
|
|
231
|
+
* every other field here is ignored.
|
|
232
|
+
*/
|
|
233
|
+
source?: ReplicationSource<RemoteRow, T>;
|
|
234
|
+
hydrate?: HydrateMode;
|
|
235
|
+
/** Rows per bootstrap page. Default 500. */
|
|
236
|
+
pageSize?: number;
|
|
237
|
+
/** Re-check the origin for changes on this interval. `0` disables. */
|
|
238
|
+
refreshMs?: number;
|
|
239
|
+
/**
|
|
240
|
+
* Fetch the current query directly when coverage isn't ready yet, so a cold
|
|
241
|
+
* start paints immediately. Default `true`.
|
|
242
|
+
*
|
|
243
|
+
* Mandatory in practice for a Vite SPA or React Native, which have no server
|
|
244
|
+
* render to paint behind while the replica fills.
|
|
245
|
+
*/
|
|
246
|
+
bridge?: boolean;
|
|
247
|
+
}
|
|
248
|
+
/** One entry per local collection. */
|
|
249
|
+
type ReplicateRegistry = Record<string, ReplicateScope<any, any>>;
|
|
250
|
+
|
|
186
251
|
/** A slice to warm on first run — a collection, optionally on a specific endpoint. */
|
|
187
252
|
type PrefetchSlice = {
|
|
188
253
|
collection: string;
|
|
@@ -236,7 +301,15 @@ interface ReplicationConfig {
|
|
|
236
301
|
/** Max concurrent prefetch pulls — keeps the active page from starving. Default `2`. */
|
|
237
302
|
prefetchConcurrency?: number;
|
|
238
303
|
}
|
|
239
|
-
interface ReplicationProviderProps extends ReplicationConfig {
|
|
304
|
+
interface ReplicationProviderProps extends Partial<ReplicationConfig> {
|
|
305
|
+
/**
|
|
306
|
+
* Collections to replicate from a remote origin, keyed by local collection name.
|
|
307
|
+
*
|
|
308
|
+
* This is what makes `useQuery` a local read: once a collection is fully
|
|
309
|
+
* hydrated for its scope, filtering, sorting and paging never touch the network.
|
|
310
|
+
* See {@link ReplicateRegistry}.
|
|
311
|
+
*/
|
|
312
|
+
replicate?: ReplicateRegistry;
|
|
240
313
|
children: ReactNode;
|
|
241
314
|
}
|
|
242
315
|
/**
|
|
@@ -257,7 +330,7 @@ interface ReplicationProviderProps extends ReplicationConfig {
|
|
|
257
330
|
* </TalaDBProvider>
|
|
258
331
|
* ```
|
|
259
332
|
*/
|
|
260
|
-
declare function ReplicationProvider({ children, ...config }: ReplicationProviderProps): react_jsx_runtime.JSX.Element;
|
|
333
|
+
declare function ReplicationProvider({ children, replicate, ...config }: ReplicationProviderProps): react_jsx_runtime.JSX.Element;
|
|
261
334
|
/**
|
|
262
335
|
* Read the nearest replication config, merged with per-hook overrides.
|
|
263
336
|
* Non-throwing: `config` is `null` when no endpoint is resolvable (valid for
|
|
@@ -268,73 +341,134 @@ declare function useReplicationConfig(overrides?: Partial<ReplicationConfig>): {
|
|
|
268
341
|
pollMs: number;
|
|
269
342
|
};
|
|
270
343
|
|
|
344
|
+
interface Coverage {
|
|
345
|
+
/** The raw state machine value. */
|
|
346
|
+
status: CoverageState['status'];
|
|
347
|
+
/**
|
|
348
|
+
* Whether a purely local read is authorized.
|
|
349
|
+
*
|
|
350
|
+
* True **only** for `complete`. Notably *not* for `best-effort`, which means we
|
|
351
|
+
* applied every row the origin gave us but the origin could not pin a snapshot —
|
|
352
|
+
* so a row that shifted between pages during the walk may never have been seen,
|
|
353
|
+
* and we cannot prove the replica is whole. Serving that as authoritative would
|
|
354
|
+
* silently return incomplete results, which is worse than going to the network.
|
|
355
|
+
*/
|
|
356
|
+
ready: boolean;
|
|
357
|
+
/** Rows hydrated so far. */
|
|
358
|
+
rows: number;
|
|
359
|
+
/** Total rows in scope when supplied by the origin. */
|
|
360
|
+
total?: number;
|
|
361
|
+
/** 0–1 when the origin reported a total; otherwise undefined. */
|
|
362
|
+
progress?: number;
|
|
363
|
+
/** Present on `error`, `stale` and `best-effort`. */
|
|
364
|
+
reason?: string;
|
|
365
|
+
}
|
|
271
366
|
/**
|
|
272
|
-
* How a
|
|
273
|
-
*
|
|
274
|
-
*
|
|
367
|
+
* How much of a collection is local, and whether it can be trusted for a
|
|
368
|
+
* network-free read.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* const { ready, progress } = useCoverage('products')
|
|
372
|
+
* if (!ready) return <ProgressBar value={progress} />
|
|
275
373
|
*/
|
|
374
|
+
declare function useCoverage(collection: string): Coverage;
|
|
375
|
+
/** Semantic alias for progress-oriented UIs. */
|
|
376
|
+
declare const useHydrationProgress: typeof useCoverage;
|
|
377
|
+
|
|
276
378
|
type ReadSource = 'local-first' | 'remote-first' | 'local-only';
|
|
277
379
|
interface UseQueryOptions<T extends Document> extends Partial<Pick<ReplicationConfig, 'endpoint' | 'getAuth' | 'fetch' | 'paths' | 'pollMs'>> {
|
|
278
|
-
/**
|
|
380
|
+
/** The local collection to read. */
|
|
279
381
|
collection: string;
|
|
280
|
-
/**
|
|
382
|
+
/** Mongo-style filter, applied **locally**. */
|
|
281
383
|
filter?: Filter<T>;
|
|
282
|
-
/**
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
384
|
+
/** Sort, applied locally. `1` ascending, `-1` descending. */
|
|
385
|
+
sort?: Partial<Record<keyof T & string, 1 | -1>>;
|
|
386
|
+
/** 1-based page number. Requires `limit`. */
|
|
387
|
+
page?: number;
|
|
388
|
+
/** Rows per page. */
|
|
389
|
+
limit?: number;
|
|
390
|
+
/** Skip N rows. Ignored when `page` is set. */
|
|
391
|
+
skip?: number;
|
|
392
|
+
/** Skip the query entirely (e.g. while a route param is undefined). */
|
|
393
|
+
enabled?: boolean;
|
|
394
|
+
/** Legacy sync-contract read policy. Used when no full-replication scope exists. */
|
|
288
395
|
source?: ReadSource;
|
|
289
396
|
}
|
|
290
397
|
interface QueryResult<T> {
|
|
291
|
-
/**
|
|
398
|
+
/** The current page. Reactive: re-renders as rows land. */
|
|
292
399
|
data: T[];
|
|
293
|
-
/**
|
|
400
|
+
/** Total rows in the replicated scope when reported by the origin. */
|
|
401
|
+
total?: number;
|
|
402
|
+
/** True until the first local snapshot arrives. */
|
|
294
403
|
loading: boolean;
|
|
295
|
-
/** Most recent local
|
|
404
|
+
/** Most recent local read error. */
|
|
296
405
|
error: unknown | null;
|
|
297
|
-
/**
|
|
406
|
+
/** Most recent cold-start bridge error. */
|
|
407
|
+
fetchError: unknown | null;
|
|
408
|
+
/** How much of the collection is local, and whether it is trustworthy. */
|
|
409
|
+
coverage: Coverage;
|
|
410
|
+
/**
|
|
411
|
+
* A cold-start bridge fetch is in flight — we are serving the network because
|
|
412
|
+
* the replica isn't complete yet.
|
|
413
|
+
*/
|
|
414
|
+
fetching: boolean;
|
|
415
|
+
/** Legacy sync-contract pull in progress. */
|
|
298
416
|
syncing: boolean;
|
|
299
|
-
/**
|
|
417
|
+
/** Legacy sync-contract pull error. */
|
|
300
418
|
syncError: unknown | null;
|
|
301
|
-
/**
|
|
419
|
+
/** Force a delta refresh from the origin. */
|
|
302
420
|
refetch: () => Promise<void>;
|
|
303
421
|
}
|
|
304
422
|
/**
|
|
305
|
-
*
|
|
423
|
+
* Read a page of a collection.
|
|
424
|
+
*
|
|
425
|
+
* ## The point
|
|
426
|
+
*
|
|
427
|
+
* Once the collection is **covered** — fully replicated for this scope — this hook
|
|
428
|
+
* touches the network **zero times**. Filtering, sorting and paging are local
|
|
429
|
+
* queries against the on-device database. Page 1 → page 2 → a new filter → page 47
|
|
430
|
+
* → back to page 1: every one is a local read, instant and offline-capable.
|
|
431
|
+
* Pagination stops being a network concern at all.
|
|
432
|
+
*
|
|
433
|
+
* That is the whole reason to put a real database on the device. A cache of API
|
|
434
|
+
* pages could only ever answer the queries you already asked; a *covered replica*
|
|
435
|
+
* answers queries nobody has asked yet.
|
|
436
|
+
*
|
|
437
|
+
* ## Before coverage lands
|
|
306
438
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
439
|
+
* On a cold start the replica is empty, and a SPA or React Native app has no server
|
|
440
|
+
* render to paint behind. So the hook **bridges**: it fetches exactly the rows this
|
|
441
|
+
* query needs and writes them into the same collection, under the same derived ids
|
|
442
|
+
* the background walk will use. Those rows are not a cache entry to be reconciled
|
|
443
|
+
* later — they are the replica, arriving early. When the walk reaches them it
|
|
444
|
+
* overwrites them in place.
|
|
311
445
|
*
|
|
312
446
|
* @example
|
|
313
|
-
* const { data,
|
|
447
|
+
* const { data, coverage } = useQuery<Product>({
|
|
314
448
|
* collection: 'products',
|
|
315
|
-
* filter: { category: 'kitchen' },
|
|
316
|
-
*
|
|
449
|
+
* filter: { category: 'kitchen', price: { $lt: 500 } },
|
|
450
|
+
* sort: { price: 1 },
|
|
451
|
+
* page: 2,
|
|
452
|
+
* limit: 100,
|
|
317
453
|
* })
|
|
318
454
|
*/
|
|
319
455
|
declare function useQuery<T extends Document>(options: UseQueryOptions<T>): QueryResult<T>;
|
|
320
456
|
|
|
321
457
|
/**
|
|
322
|
-
*
|
|
323
|
-
* in parallel. The result array is index-aligned with `queries`.
|
|
458
|
+
* Several pages at once, from several collections. Index-aligned with `queries`.
|
|
324
459
|
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* no cross-collection joins (TalaDB is a document store); compose in the
|
|
329
|
-
* component.
|
|
460
|
+
* Hooks can't be called in a variable-length loop, so this manages its own
|
|
461
|
+
* subscriptions rather than calling `useQuery` N times. Behaviour is otherwise
|
|
462
|
+
* identical: once a collection is covered, its read is purely local.
|
|
330
463
|
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
464
|
+
* TalaDB is a document store with no cross-collection joins, so a page needing
|
|
465
|
+
* several collections composes them in the component (or denormalises at the
|
|
466
|
+
* origin). This is the hook for that.
|
|
333
467
|
*
|
|
334
468
|
* @example
|
|
335
|
-
* const [
|
|
336
|
-
* { collection: '
|
|
337
|
-
* { collection: '
|
|
469
|
+
* const [products, categories] = useQueries([
|
|
470
|
+
* { collection: 'products', filter: { category }, sort: { price: 1 }, page, limit: 50 },
|
|
471
|
+
* { collection: 'categories' },
|
|
338
472
|
* ])
|
|
339
473
|
*/
|
|
340
474
|
declare function useQueries(queries: UseQueryOptions<Document>[]): QueryResult<Document>[];
|
|
@@ -392,4 +526,4 @@ interface MutationResult<T extends Document> {
|
|
|
392
526
|
*/
|
|
393
527
|
declare function useMutation<T extends Document>(options: UseMutationOptions): MutationResult<T>;
|
|
394
528
|
|
|
395
|
-
export { type CollectionRegistry, type CollectionResolver, type FindOneResult, type FindResult, type MutationResult, type PrefetchEntry, type PrefetchMode, type PrefetchSlice, type QueryResult, type
|
|
529
|
+
export { type AggregateResult, type CollectionRegistry, type CollectionResolver, type Coverage, type FindOneResult, type FindResult, type HydrateMode, type MutationResult, type PrefetchEntry, type PrefetchMode, type PrefetchSlice, type QueryResult, type ReplicateRegistry, type ReplicateScope, type ReplicationConfig, ReplicationProvider, type ReplicationProviderProps, TalaDBProvider, type TalaDBProviderProps, type UseMutationOptions, type UseQueryOptions, type WriteOp, useAggregate, useCollection, useCollectionOptions, useCoverage, useFind, useFindOne, useHydrationProgress, useMutation, useQueries, useQuery, useReplicationConfig, useTalaDB };
|