@hyper-fetch/db 0.0.0-stage → 10.2.0

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.
Files changed (141) hide show
  1. package/README.md +180 -2
  2. package/__tests__/e2e/db.e2e.spec.ts +269 -0
  3. package/__tests__/features/connection/connection.spec.ts +23 -0
  4. package/__tests__/features/model/live-query.model.spec.ts +320 -0
  5. package/__tests__/features/operators/operators.spec.ts +113 -0
  6. package/__tests__/features/runtime/runtime.spec.ts +185 -0
  7. package/__tests__/features/serializer/serializer.spec.ts +73 -0
  8. package/__tests__/features/store/store.spec.ts +147 -0
  9. package/__tests__/features/types/db.types.spec.ts +337 -0
  10. package/__tests__/integration/audit.spec.ts +639 -0
  11. package/__tests__/integration/competitors.spec.ts +393 -0
  12. package/__tests__/integration/convergence.model.spec.ts +162 -0
  13. package/__tests__/integration/edge-cases.spec.ts +465 -0
  14. package/__tests__/integration/errors.spec.ts +178 -0
  15. package/__tests__/integration/infinite.model.spec.ts +142 -0
  16. package/__tests__/integration/infinite.spec.ts +290 -0
  17. package/__tests__/integration/inspect.spec.ts +103 -0
  18. package/__tests__/integration/joins-many.spec.ts +177 -0
  19. package/__tests__/integration/joins.spec.ts +179 -0
  20. package/__tests__/integration/key-loaders.spec.ts +196 -0
  21. package/__tests__/integration/links.spec.ts +105 -0
  22. package/__tests__/integration/mutations.spec.ts +337 -0
  23. package/__tests__/integration/normalize.spec.ts +80 -0
  24. package/__tests__/integration/offline.spec.ts +479 -0
  25. package/__tests__/integration/queries.spec.ts +513 -0
  26. package/__tests__/integration/races.spec.ts +127 -0
  27. package/__tests__/integration/server-operators.spec.ts +123 -0
  28. package/__tests__/integration/ssr.spec.ts +111 -0
  29. package/__tests__/integration/syncs.spec.ts +564 -0
  30. package/__tests__/integration/transactions.spec.ts +344 -0
  31. package/__tests__/integration/windows.spec.ts +192 -0
  32. package/__tests__/tsconfig.json +11 -0
  33. package/__tests__/type-budget/budget.fixture.ts +2476 -0
  34. package/__tests__/type-budget/tsconfig.json +4 -0
  35. package/__tests__/utils/fixtures.ts +225 -0
  36. package/__tests__/utils/random.ts +19 -0
  37. package/__tests__/vitest.setup.ts +11 -0
  38. package/dist/collection/collection.d.ts +111 -0
  39. package/dist/collection/collection.d.ts.map +1 -0
  40. package/dist/collection/collection.types.d.ts +316 -0
  41. package/dist/collection/collection.types.d.ts.map +1 -0
  42. package/dist/collection/resource.types.d.ts +77 -0
  43. package/dist/collection/resource.types.d.ts.map +1 -0
  44. package/dist/db/collection.runtime.d.ts +98 -0
  45. package/dist/db/collection.runtime.d.ts.map +1 -0
  46. package/dist/db/db.d.ts +154 -0
  47. package/dist/db/db.d.ts.map +1 -0
  48. package/dist/db/db.runtime.d.ts +114 -0
  49. package/dist/db/db.runtime.d.ts.map +1 -0
  50. package/dist/db/db.types.d.ts +162 -0
  51. package/dist/db/db.types.d.ts.map +1 -0
  52. package/dist/graphql/connection.d.ts +25 -0
  53. package/dist/graphql/connection.d.ts.map +1 -0
  54. package/dist/index.d.ts +17 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.mjs +4220 -0
  57. package/dist/index.mjs.map +1 -0
  58. package/dist/mutation/mutation.d.ts +136 -0
  59. package/dist/mutation/mutation.d.ts.map +1 -0
  60. package/dist/mutation/mutation.errors.d.ts +6 -0
  61. package/dist/mutation/mutation.errors.d.ts.map +1 -0
  62. package/dist/mutation/mutation.utils.d.ts +35 -0
  63. package/dist/mutation/mutation.utils.d.ts.map +1 -0
  64. package/dist/operators/operators.d.ts +33 -0
  65. package/dist/operators/operators.d.ts.map +1 -0
  66. package/dist/operators/operators.types.d.ts +68 -0
  67. package/dist/operators/operators.types.d.ts.map +1 -0
  68. package/dist/operators/operators.utils.d.ts +19 -0
  69. package/dist/operators/operators.utils.d.ts.map +1 -0
  70. package/dist/persistence/persistence.d.ts +42 -0
  71. package/dist/persistence/persistence.d.ts.map +1 -0
  72. package/dist/persistence/serializer.d.ts +23 -0
  73. package/dist/persistence/serializer.d.ts.map +1 -0
  74. package/dist/persistence/snapshot.d.ts +29 -0
  75. package/dist/persistence/snapshot.d.ts.map +1 -0
  76. package/dist/persistence/tabs.d.ts +53 -0
  77. package/dist/persistence/tabs.d.ts.map +1 -0
  78. package/dist/query/infinite-query.d.ts +59 -0
  79. package/dist/query/infinite-query.d.ts.map +1 -0
  80. package/dist/query/live-query.d.ts +85 -0
  81. package/dist/query/live-query.d.ts.map +1 -0
  82. package/dist/query/planner.d.ts +57 -0
  83. package/dist/query/planner.d.ts.map +1 -0
  84. package/dist/query/query.d.ts +155 -0
  85. package/dist/query/query.d.ts.map +1 -0
  86. package/dist/query/query.types.d.ts +71 -0
  87. package/dist/query/query.types.d.ts.map +1 -0
  88. package/dist/schema/schema.types.d.ts +33 -0
  89. package/dist/schema/schema.types.d.ts.map +1 -0
  90. package/dist/store/store.d.ts +76 -0
  91. package/dist/store/store.d.ts.map +1 -0
  92. package/dist/store/store.types.d.ts +27 -0
  93. package/dist/store/store.types.d.ts.map +1 -0
  94. package/dist/subset/base-subset.d.ts +66 -0
  95. package/dist/subset/base-subset.d.ts.map +1 -0
  96. package/dist/subset/key-subset.d.ts +23 -0
  97. package/dist/subset/key-subset.d.ts.map +1 -0
  98. package/dist/subset/list-subset.d.ts +63 -0
  99. package/dist/subset/list-subset.d.ts.map +1 -0
  100. package/dist/sync/sync.d.ts +42 -0
  101. package/dist/sync/sync.d.ts.map +1 -0
  102. package/dist/utils/request.utils.d.ts +11 -0
  103. package/dist/utils/request.utils.d.ts.map +1 -0
  104. package/package.json +71 -4
  105. package/project.json +4 -0
  106. package/scripts/docs-typescript.mjs +24 -0
  107. package/scripts/type-budget.mjs +37 -0
  108. package/src/collection/collection.ts +330 -0
  109. package/src/collection/collection.types.ts +383 -0
  110. package/src/collection/resource.types.ts +114 -0
  111. package/src/db/collection.runtime.ts +572 -0
  112. package/src/db/db.runtime.ts +537 -0
  113. package/src/db/db.ts +451 -0
  114. package/src/db/db.types.ts +157 -0
  115. package/src/graphql/connection.ts +23 -0
  116. package/src/index.ts +16 -0
  117. package/src/mutation/mutation.errors.ts +7 -0
  118. package/src/mutation/mutation.ts +776 -0
  119. package/src/mutation/mutation.utils.ts +58 -0
  120. package/src/operators/operators.ts +90 -0
  121. package/src/operators/operators.types.ts +67 -0
  122. package/src/operators/operators.utils.ts +169 -0
  123. package/src/persistence/persistence.ts +168 -0
  124. package/src/persistence/serializer.ts +123 -0
  125. package/src/persistence/snapshot.ts +76 -0
  126. package/src/persistence/tabs.ts +86 -0
  127. package/src/query/infinite-query.ts +206 -0
  128. package/src/query/live-query.ts +552 -0
  129. package/src/query/planner.ts +199 -0
  130. package/src/query/query.ts +319 -0
  131. package/src/query/query.types.ts +90 -0
  132. package/src/schema/schema.types.ts +29 -0
  133. package/src/store/store.ts +300 -0
  134. package/src/store/store.types.ts +17 -0
  135. package/src/subset/base-subset.ts +169 -0
  136. package/src/subset/key-subset.ts +44 -0
  137. package/src/subset/list-subset.ts +328 -0
  138. package/src/sync/sync.ts +99 -0
  139. package/src/utils/request.utils.ts +67 -0
  140. package/tsconfig.json +12 -0
  141. package/vite.config.ts +71 -0
@@ -0,0 +1,85 @@
1
+ import { RowKey } from '../collection/collection.types';
2
+ import { CollectionRuntime } from '../db/collection.runtime';
3
+ import { SubsetObserver } from '../subset/base-subset';
4
+ import { QueryPlan } from './planner';
5
+ import { QueryDefinition, QueryExplanation, QueryState } from './query.types';
6
+ /**
7
+ * The running form of a query: it attaches to the loaded subset that serves it, keeps its result current as rows
8
+ * change and tells its subscribers. Queries with the same definition share one live query.
9
+ */
10
+ export declare class LiveQuery implements SubsetObserver {
11
+ readonly collection: CollectionRuntime;
12
+ readonly definition: QueryDefinition;
13
+ readonly key: string;
14
+ readonly plan: QueryPlan;
15
+ private listeners;
16
+ private source;
17
+ private keySubset;
18
+ private answeredLocally;
19
+ /** The subset a query that nothing subscribes to reads, see `getSource` */
20
+ private peeked;
21
+ private sorted;
22
+ private members;
23
+ private selections;
24
+ /** Key subsets that load the joined rows, by `collection:key` */
25
+ private joinSubsets;
26
+ /** Live queries that load the rows of one-to-many joins, by join name */
27
+ private joinQueries;
28
+ /** The rows of each one-to-many join by the value of its `on` field, built when first read */
29
+ private childIndex;
30
+ private state;
31
+ constructor(collection: CollectionRuntime, definition: QueryDefinition, key: string);
32
+ /** The current state. A query nobody subscribes to is computed from the rows the db holds right now. */
33
+ getState: () => QueryState<unknown>;
34
+ subscribe: (listener: () => void) => () => void;
35
+ /** Whether anything subscribes to the query */
36
+ isActive: () => boolean;
37
+ /** How many listeners subscribe to the query */
38
+ get subscriberCount(): number;
39
+ refetch: () => Promise<void>;
40
+ /** How the query's loader pages: by cursor, by a growing `limit`, or not at all */
41
+ getPaging: () => "cursor" | "limit" | "none";
42
+ fetchNextPage: () => Promise<void>;
43
+ /** Refetches the query's subset when it is stale, after focus or reconnect */
44
+ revalidate: () => void;
45
+ explain: () => QueryExplanation;
46
+ /** Rows changed: update membership and order of just those rows */
47
+ onRowsChanged: (keys: Iterable<RowKey>) => void;
48
+ /** The subset's rows or status changed */
49
+ onSubsetChange: () => void;
50
+ /** Recomputes the whole result */
51
+ rebuild: () => void;
52
+ private attach;
53
+ private detach;
54
+ private getComparator;
55
+ private insertSorted;
56
+ private isMember;
57
+ /** The joined value of a row: the row with that key, or every row whose `on` field holds the value */
58
+ private resolveJoin;
59
+ private getChildren;
60
+ private hasInnerJoins;
61
+ /** Whether a row has every row its inner joins need */
62
+ private passesInnerJoins;
63
+ private select;
64
+ /** Whether the query reads rows of a collection, its own or a joined one */
65
+ dependsOn: (collection: CollectionRuntime) => boolean;
66
+ /** Rows of a joined collection changed: results that point at them select again */
67
+ onJoinedRowsChanged: () => void;
68
+ /** Attaches the key subsets that load the joined rows of the current result and releases the others */
69
+ private syncJoins;
70
+ /**
71
+ * Loads the rows of one-to-many joins: one query of the joined collection for the values of the whole result, when
72
+ * one of its list loaders can send `inArray` on the joined field. Otherwise the join reads the rows the db holds.
73
+ */
74
+ private syncChildQueries;
75
+ private buildList;
76
+ private computeState;
77
+ /**
78
+ * The subset that serves a list query. Before anything subscribes, such as on a component's first render, it is the
79
+ * loaded subset that would serve it, read without attaching to it or loading it.
80
+ */
81
+ private getSource;
82
+ private peekSource;
83
+ private emit;
84
+ }
85
+ //# sourceMappingURL=live-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-query.d.ts","sourceRoot":"","sources":["../../src/query/live-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAG5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AA+BnF;;;GAGG;AACH,qBAAa,SAAU,YAAW,cAAc;IA2B5C,QAAQ,CAAC,UAAU,EAAE,iBAAiB;IACtC,QAAQ,CAAC,UAAU,EAAE,eAAe;IACpC,QAAQ,CAAC,GAAG,EAAE,MAAM;IA5BtB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,SAAS,CAAwB;IACzC,OAAO,CAAC,eAAe,CAAS;IAChC,2EAA2E;IAC3E,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,OAAO,CAAqB;IACpC,OAAO,CAAC,UAAU,CAAsE;IACxF,iEAAiE;IACjE,OAAO,CAAC,WAAW,CAAgC;IACnD,yEAAyE;IACzE,OAAO,CAAC,WAAW,CAA+D;IAClF,8FAA8F;IAC9F,OAAO,CAAC,UAAU,CAA0C;IAC5D,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F;;;OAGG;IACH,OAAO,CAAC,SAAS,CAAoC;IAErD,OAAO,CAAC,UAAU,CAMhB;IAEF,OAAO,CAAC,IAAI,CAcV;CACH"}
@@ -0,0 +1,57 @@
1
+ import { AnyRequestPatch, CollectionConfig, LoaderConfig, RowKey, SortDirection } from '../collection/collection.types';
2
+ import { DbNotation } from '../db/db.types';
3
+ import { ExpressionNode, SendableNode } from '../operators/operators.types';
4
+ import { QueryDefinition } from './query.types';
5
+ export type ListPlan = {
6
+ kind: "list";
7
+ loaderIndex: number;
8
+ /** Conditions sent to the backend */
9
+ sent: SendableNode[];
10
+ /** Conditions evaluated on the rows the db holds */
11
+ local: ExpressionNode[];
12
+ /** Whether the query's order is sent to the backend */
13
+ sendOrder: boolean;
14
+ /** Whether the query's limit is sent to the backend */
15
+ sendLimit: boolean;
16
+ paginated: boolean;
17
+ /** Whether the query filters or sorts by a field the backend compares differently, so it is never answered locally */
18
+ serverOnly: boolean;
19
+ subsetId: string;
20
+ };
21
+ export type OnePlan = {
22
+ kind: "one";
23
+ key: RowKey;
24
+ loaderIndex: number | undefined;
25
+ };
26
+ export type LocalPlan = {
27
+ kind: "local";
28
+ };
29
+ export type ErrorPlan = {
30
+ kind: "error";
31
+ error: Error;
32
+ };
33
+ export type QueryPlan = ListPlan | OnePlan | LocalPlan | ErrorPlan;
34
+ export declare const isPaginatedLoader: (loader: LoaderConfig) => boolean;
35
+ /**
36
+ * How a loader sends a comparison: its own mapping, the db-wide notation for one declared with `true`, or the loader's
37
+ * adapter notation for any field. `undefined` when it cannot send it.
38
+ */
39
+ export declare const getWhereMapping: (loader: LoaderConfig, node: SendableNode, notation: DbNotation | undefined) => ((value: unknown) => AnyRequestPatch) | undefined;
40
+ /** How a loader sends an order, resolved like `getWhereMapping` */
41
+ export declare const getOrderMapping: (loader: LoaderConfig, field: string, notation: DbNotation | undefined) => ((direction: SortDirection) => AnyRequestPatch) | undefined;
42
+ /** Whether a loader can send a condition. `and`, `or` and `not` run on the client. */
43
+ export declare const canSend: (loader: LoaderConfig, node: ExpressionNode, notation: DbNotation | undefined) => boolean;
44
+ /** Builds the id of the loaded subset that serves a plan, shared by every query with the same backend request */
45
+ export declare const getSubsetId: (collectionName: string, plan: Omit<ListPlan, "subsetId">, definition: QueryDefinition) => string;
46
+ /**
47
+ * Picks how the db serves a query. Among the list loaders whose required filters the query sets, it picks the one that
48
+ * can send the most of the query's top-level conditions; ties go to the loader declared first. A paginated loader is a
49
+ * candidate only when it can send every condition and order, because filtering one page on the client is wrong.
50
+ */
51
+ export declare const planQuery: ({ collectionName, config, definition, notation, }: {
52
+ collectionName: string;
53
+ config: CollectionConfig;
54
+ definition: QueryDefinition;
55
+ notation: DbNotation | undefined;
56
+ }) => QueryPlan;
57
+ //# sourceMappingURL=planner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"planner.d.ts","sourceRoot":"","sources":["../../src/query/planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,aAAa,EACd,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAkB,cAAc,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAEjG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,qCAAqC;IACrC,IAAI,EAAE,YAAY,EAAE,CAAC;IACrB,oDAAoD;IACpD,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,uDAAuD;IACvD,SAAS,EAAE,OAAO,CAAC;IACnB,uDAAuD;IACvD,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;IACnB,sHAAsH;IACtH,UAAU,EAAE,OAAO,CAAC;IACpB,QAAQ,EA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;IACzB,UAAU,EAAE,eAAe,CAAC;IAC5B,QAAQ,EAAE,UAAU,GAAG,SAAS,CAAC;CAClC,KAAG,SAsEH,CAAC"}
@@ -0,0 +1,155 @@
1
+ import { SortDirection } from '../collection/collection.types';
2
+ import { DbRuntime } from '../db/db.runtime';
3
+ import { Condition, ExtractConditionEq, ExtractConditionTokens, FieldRef, RowRefs } from '../operators/operators.types';
4
+ import { InfiniteQueryOptions, InfiniteQuery } from './infinite-query';
5
+ import { IsServable, QueryDefinition, QueryExplanation, QueryState } from './query.types';
6
+ /** What a query's `data` holds: a list of results, or one result for `findOne` */
7
+ export type QueryData<Result, Mode extends "list" | "one"> = Mode extends "one" ? Result : Result[];
8
+ /** A collection a query can join: a handle of the db, with its name, row and key field */
9
+ export type JoinTarget = {
10
+ readonly collectionName: string;
11
+ readonly __types?: {
12
+ row: unknown;
13
+ key: string;
14
+ };
15
+ };
16
+ /** The row of a joined collection */
17
+ export type JoinedRowOf<Target extends JoinTarget> = NonNullable<Target["__types"]>["row"];
18
+ /** The key type of a joined collection, which the foreign key must hold */
19
+ export type JoinedKeyOf<Target extends JoinTarget> = JoinedRowOf<Target>[NonNullable<Target["__types"]>["key"] & keyof JoinedRowOf<Target>];
20
+ /** What a join adds to each result: one row, one row or `undefined`, or an array of rows with `on` */
21
+ export type JoinedValue<JoinedRow, On, Type> = unknown extends On ? [Type] extends ["inner"] ? JoinedRow : JoinedRow | undefined : JoinedRow[];
22
+ /** The value the foreign key must hold: the joined collection's key, or its `on` field */
23
+ export type JoinedMatchOf<Target extends JoinTarget, On> = unknown extends On ? JoinedKeyOf<Target> : On extends keyof JoinedRowOf<Target> ? JoinedRowOf<Target>[On] : never;
24
+ /** The field that holds the joined key, alone or with join options */
25
+ export type JoinSpec<Row, Field extends keyof Row & string, Match, On, Type> = JoinBy<Row, Field, Match> | {
26
+ by: JoinBy<Row, Field, Match>;
27
+ on?: On;
28
+ type?: Type & ("left" | "inner");
29
+ };
30
+ /** Picks the field of a row that holds the joined key or value */
31
+ export type JoinBy<Row, Field extends keyof Row & string, Match> = (row: RowRefs<Row>) => FieldRef<Field, Row[Field] & (Match | null | undefined)>;
32
+ /** The result of a query with one more joined value */
33
+ type Joined<Result, Name extends string, Value> = {
34
+ [Field in keyof Result | Name]: Field extends Name ? Value : Field extends keyof Result ? Result[Field] : never;
35
+ };
36
+ type Selected<Selection> = {
37
+ [Output in keyof Selection]: Selection[Output] extends FieldRef<string, infer Value> ? Value : never;
38
+ };
39
+ /**
40
+ * An immutable description of a query over one collection. Each method returns a new query, and two queries with the
41
+ * same structure are the same query, so a component can rebuild its query on every render.
42
+ *
43
+ * The type parameters record what the query asks a backend for, so the types can check that a loader serves it.
44
+ */
45
+ export declare class Query<Row, Loaders, Result, Tokens extends string = never, Eq extends string = never, Order extends string = never, Mode extends "list" | "one" = "list", Local extends boolean = false> {
46
+ private readonly runtime;
47
+ readonly definition: QueryDefinition;
48
+ /** Type-only carrier of the query's generics, never set at runtime */
49
+ readonly __types?: {
50
+ row: Row;
51
+ loaders: Loaders;
52
+ result: Result;
53
+ tokens: Tokens;
54
+ eq: Eq;
55
+ order: Order;
56
+ mode: Mode;
57
+ local: Local;
58
+ };
59
+ /** Identifies the query by its structure */
60
+ readonly key: string;
61
+ constructor(runtime: DbRuntime, definition: QueryDefinition);
62
+ /**
63
+ * Filters rows. The db splits the condition at its top-level `and`: conditions the loader declares are sent to the
64
+ * backend, and the rest run on the rows the db holds. Calling it again adds conditions.
65
+ */
66
+ where<C extends Condition<string, string>>(this: Query<Row, Loaders, Result, Tokens, Eq, Order, "list", Local>, callback: (row: RowRefs<Row>) => C): Query<Row, Loaders, Result, Tokens | ExtractConditionTokens<C>, Eq | ExtractConditionEq<C>, Order, Mode, Local>;
67
+ /** Sorts rows by a field. Calling it again adds a sort that breaks ties of the previous ones. */
68
+ orderBy<Name extends keyof Row & string>(this: Query<Row, Loaders, Result, Tokens, Eq, Order, "list", Local>, callback: (row: RowRefs<Row>) => FieldRef<Name, Row[Name]>, direction?: SortDirection): Query<Row, Loaders, Result, Tokens, Eq, Order | Name, Mode, Local>;
69
+ /** Picks and renames the fields each result holds */
70
+ select<Selection extends Record<string, FieldRef<string, unknown>>>(callback: (row: RowRefs<Row>) => Selection): Query<Row, Loaders, Selected<Selection>, Tokens, Eq, Order, Mode, Local>;
71
+ /**
72
+ * Adds the row of another collection whose key is the given field, as a left join: each result holds it under `name`,
73
+ * or `undefined` while it is not loaded or when there is none. With `{ type: "inner" }`, results without a joined row
74
+ * are left out. With `{ on: "postId" }`, the join matches that field of the joined collection instead of its key and
75
+ * holds an array of every matching row, loaded through a list loader that can send `inArray` on it. Options go in an
76
+ * object with the field as `by`, as in `join("comments", db.comment, { by: (post) => post.id, on: "postId" })`. The db loads joined rows in batches through the joined
77
+ * collection's key loader, and a result updates whenever its joined row changes. Joined rows are loaded by key, so
78
+ * only syncs without `where` reach them.
79
+ */
80
+ join<Name extends string, Target extends JoinTarget, Field extends keyof Row & string, const On, const Type>(name: Name, collection: Target, by: JoinSpec<Row, Field, JoinedMatchOf<Target, On>, On, Type>): Query<Row, Loaders, Joined<Result, Name, JoinedValue<JoinedRowOf<Target>, On, Type>>, Tokens, Eq, Order, Mode, Local>;
81
+ /** Limits the number of results. A paginated loader that declares `limit` receives it as its page size. */
82
+ limit(this: Query<Row, Loaders, Result, Tokens, Eq, Order, "list", Local>, limit: number): Query<Row, Loaders, Result, Tokens, Eq, Order, Mode, Local>;
83
+ /** Reads only the rows the db holds and never sends a request */
84
+ local(): Query<Row, Loaders, Result, Tokens, Eq, Order, Mode, true>;
85
+ /**
86
+ * Subscribes to the query's state. The first subscriber attaches the query to the subset that serves it, which loads
87
+ * when it is stale. Returns a function that unsubscribes.
88
+ */
89
+ subscribe(this: Query<Row, Loaders, Result, Tokens, Eq, Order, Mode, Local> & ServableCheck<Loaders, Tokens, Eq, Order, Mode, Local>, listener: () => void): () => void;
90
+ /** The current state. It keeps its identity until something changes, as `useSyncExternalStore` expects. */
91
+ getSnapshot(): QueryState<QueryData<Result, Mode>>;
92
+ /** Reads the query once: resolves with the first state that is not loading */
93
+ get(this: Query<Row, Loaders, Result, Tokens, Eq, Order, Mode, Local> & ServableCheck<Loaders, Tokens, Eq, Order, Mode, Local>): Promise<QueryState<QueryData<Result, Mode>>>;
94
+ /**
95
+ * A window over the query's ordered result, widened one page at a time with `fetchNextPage`. Rows the db holds show
96
+ * at once, and the loader loads more only when the window runs past them. The query must have `orderBy`, so a page
97
+ * never shifts between renders.
98
+ *
99
+ * @example
100
+ * const feed = db.post.orderBy((post) => post.createdAt, "desc").infinite({ pageSize: 20 });
101
+ * feed.subscribe(() => render(feed.getSnapshot().pages));
102
+ */
103
+ infinite(this: Query<Row, Loaders, Result, Tokens, Eq, Order, Mode, Local> & ServableCheck<Loaders, Tokens, Eq, Order, Mode, Local> & InfiniteCheck<Order, Mode>, options: InfiniteQueryOptions): InfiniteQuery<Result>;
104
+ /** Loads the query's subset again */
105
+ refetch(): Promise<void>;
106
+ /** Loads the next page of a paginated query */
107
+ fetchNextPage(): Promise<void>;
108
+ /** How the db serves the query: the loader, what is sent, what runs locally and which syncs keep it live */
109
+ explain(): QueryExplanation;
110
+ }
111
+ /** Fails to compile when no loader can serve the query */
112
+ export type ServableCheck<Loaders, Tokens extends string, Eq extends string, Order extends string, Mode extends "list" | "one", Local extends boolean> = IsServable<Loaders, Tokens, Eq, Order, Mode, Local> extends true ? unknown : {
113
+ servable: never;
114
+ };
115
+ /** Fails to compile for a query without `orderBy` or one that reads one row */
116
+ export type InfiniteCheck<Order extends string, Mode extends "list" | "one"> = [Order] extends [never] ? {
117
+ orderBy: never;
118
+ } : Mode extends "one" ? {
119
+ list: never;
120
+ } : unknown;
121
+ /** The type arguments a query instance records, read directly so that `never` stays `never` */
122
+ type QueryTypes<Q> = Q extends {
123
+ __types?: infer Types;
124
+ } ? NonNullable<Types> : never;
125
+ /** Fails to compile when a query cannot be read as an infinite query */
126
+ export type InfiniteQueryCheck<Q> = QueryTypes<Q> extends {
127
+ loaders: infer Loaders;
128
+ tokens: infer Tokens extends string;
129
+ eq: infer Eq extends string;
130
+ order: infer Order extends string;
131
+ mode: infer Mode extends "list" | "one";
132
+ local: infer Local extends boolean;
133
+ } ? ServableCheck<Loaders, Tokens, Eq, Order, Mode, Local> & InfiniteCheck<Order, Mode> : never;
134
+ /** The item type of a query instance's results */
135
+ export type ExtractQueryResult<Q> = QueryTypes<Q> extends {
136
+ result: infer Result;
137
+ } ? Result : never;
138
+ /** Any query, whatever its collection and result */
139
+ export type QueryInstance = Query<any, any, any, any, any, any, any, any>;
140
+ /** Fails to compile when no loader can serve the query */
141
+ export type ServableQuery<Q> = QueryTypes<Q> extends {
142
+ loaders: infer Loaders;
143
+ tokens: infer Tokens extends string;
144
+ eq: infer Eq extends string;
145
+ order: infer Order extends string;
146
+ mode: infer Mode extends "list" | "one";
147
+ local: infer Local extends boolean;
148
+ } ? ServableCheck<Loaders, Tokens, Eq, Order, Mode, Local> : never;
149
+ /** The data of a query instance */
150
+ export type ExtractQueryData<Q> = QueryTypes<Q> extends {
151
+ result: infer Result;
152
+ mode: infer Mode extends "list" | "one";
153
+ } ? QueryData<Result, Mode> : never;
154
+ export {};
155
+ //# sourceMappingURL=query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../src/query/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EACV,SAAS,EACT,kBAAkB,EAClB,sBAAsB,EACtB,QAAQ,EACR,OAAO,EACR,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAA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EZ,mCAAmC;AACnC,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAC5B,UAAU,CAAC,CAAC,CAAC,SAAS;IAAE,MAAM,EAAE,MAAM,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,IAAI,SAAS,MAAM,GAAG,KAAK,CAAA;CAAE,GACnF,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,GACvB,KAAK,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { ListLoaderDescriptor, RowKey, SortDirection } from '../collection/collection.types';
2
+ import { ComparisonOperator, ExpressionNode, LocalToken, SendableNode } from '../operators/operators.types';
3
+ /** A query as data. Two queries with the same definition are the same query. */
4
+ export type QueryDefinition = {
5
+ collection: string;
6
+ where?: ExpressionNode;
7
+ orderBy: {
8
+ field: string;
9
+ direction: SortDirection;
10
+ }[];
11
+ /** Output name to row field */
12
+ select?: Record<string, string>;
13
+ limit?: number;
14
+ one?: {
15
+ key: RowKey;
16
+ };
17
+ local: boolean;
18
+ /** Left joins on another collection's key: each result carries the joined row under `name` */
19
+ joins?: {
20
+ name: string;
21
+ collection: string;
22
+ field: string;
23
+ type?: "inner";
24
+ on?: string;
25
+ }[];
26
+ };
27
+ export type QueryStatus = "loading" | "ready" | "error";
28
+ export type QueryState<Data> = {
29
+ /** `undefined` until the query has loaded, then the selected fields of every row */
30
+ data: Data | undefined;
31
+ /** `error` only when a first load failed and there is nothing to show */
32
+ status: QueryStatus;
33
+ /** The last failure, also when a refresh failed and the rows stay */
34
+ error: unknown;
35
+ /** Whether the rows may be out of date: their subset is stale or its syncs are disconnected */
36
+ stale: boolean;
37
+ /** Whether a request for the query is in flight */
38
+ isFetching: boolean;
39
+ /** Whether a paginated query has another page */
40
+ hasNextPage: boolean;
41
+ };
42
+ /** How the db serves a query, from `query.explain()` */
43
+ export type QueryExplanation = {
44
+ kind: "list" | "one" | "local" | "error";
45
+ /** Index of the loader in the collection's chain */
46
+ loader?: number;
47
+ /** Conditions sent to the backend */
48
+ sent: SendableNode[];
49
+ /** Conditions evaluated on the rows the db holds */
50
+ local: ExpressionNode[];
51
+ /** Whether rows the db already holds answer the query, with no request */
52
+ answeredLocally: boolean;
53
+ /** The syncs that keep the result live */
54
+ syncs: {
55
+ type: "insert" | "update" | "delete" | "changes";
56
+ topic: string;
57
+ }[];
58
+ error?: Error;
59
+ };
60
+ /**
61
+ * Whether some loader can serve a query: the query sets its required filters, and a paginated loader can send every
62
+ * condition and order.
63
+ */
64
+ export type IsServable<Loaders, Tokens extends string, Eq extends string, Order extends string, Mode extends "list" | "one", Local extends boolean> = Local extends true ? true : Mode extends "one" ? [Extract<Loaders, {
65
+ kind: "key";
66
+ }>] extends [never] ? false : true : true extends ServableBy<Loaders, Tokens, Eq, Order> ? true : false;
67
+ /** Tokens of server-only operators, which the loader must send because the client cannot evaluate them */
68
+ type ServerTokens<Tokens extends string> = Tokens extends `${string}:${ComparisonOperator}` | LocalToken ? never : Tokens;
69
+ type ServableBy<Loaders, Tokens extends string, Eq extends string, Order extends string> = Loaders extends ListLoaderDescriptor<infer Required, infer LoaderTokens, infer LoaderOrder, infer Paginated> ? [Required] extends [Eq] ? [ServerTokens<Tokens>] extends [LoaderTokens] ? Paginated extends true ? [Tokens] extends [LoaderTokens] ? [Order] extends [LoaderOrder] ? true : false : false : true : false : false : false;
70
+ export {};
71
+ //# sourceMappingURL=query.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.types.d.ts","sourceRoot":"","sources":["../../src/query/query.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAClG,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAEjH,gFAAgF;AAChF,MAAM,MAAM,eAAe,GAAG;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,cAAc,CAAC;IACv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AAY,CAAC,GAC7B,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,GAC3B,IAAI,GACJ,KAAK,GACP,KAAK,GACP,IAAI,GACN,KAAK,GACP,KAAK,GACP,KAAK,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The [Standard Schema](https://standardschema.dev) interface. Zod, Valibot, ArkType and other libraries implement it,
3
+ * so a collection accepts any of them without an adapter.
4
+ */
5
+ export type StandardSchemaV1<Input = unknown, Output = Input> = {
6
+ readonly "~standard": StandardSchemaV1Props<Input, Output>;
7
+ };
8
+ export type StandardSchemaV1Props<Input = unknown, Output = Input> = {
9
+ readonly version: 1;
10
+ readonly vendor: string;
11
+ readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>;
12
+ readonly types?: {
13
+ readonly input: Input;
14
+ readonly output: Output;
15
+ } | undefined;
16
+ };
17
+ export type StandardSchemaV1Result<Output> = {
18
+ readonly value: Output;
19
+ readonly issues?: undefined;
20
+ } | {
21
+ readonly issues: readonly StandardSchemaV1Issue[];
22
+ };
23
+ export type StandardSchemaV1Issue = {
24
+ readonly message: string;
25
+ readonly path?: readonly (PropertyKey | {
26
+ readonly key: PropertyKey;
27
+ })[] | undefined;
28
+ };
29
+ /** The type a schema accepts */
30
+ export type InferSchemaInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
31
+ /** The type a schema produces */
32
+ export type InferSchemaOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
33
+ //# sourceMappingURL=schema.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.types.d.ts","sourceRoot":"","sources":["../../src/schema/schema.types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,IAAI;IAC9D,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC,KAAK,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,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,76 @@
1
+ import { RowKey } from '../collection/collection.types';
2
+ import { Layer, Row, Stamp } from './store.types';
3
+ /**
4
+ * Holds one collection's rows. Server rows and optimistic layers are kept apart: queries read the server row with
5
+ * every pending layer applied on top, and dropping a layer reverts exactly what it changed.
6
+ *
7
+ * Writes are ordered per field. A field keeps its value when the write is older than the one that set it: an older
8
+ * `version`, or a response to a request sent before the field was written.
9
+ */
10
+ export declare class Store {
11
+ private readonly options;
12
+ private rows;
13
+ private meta;
14
+ private tombstones;
15
+ private layers;
16
+ private visible;
17
+ private arrival;
18
+ private arrivalCounter;
19
+ /** Stores of linked collections, which hold some of this store's fields for the same keys */
20
+ private links;
21
+ constructor(options: {
22
+ keyField: string;
23
+ versionField?: string;
24
+ onChange: (key: RowKey) => void;
25
+ });
26
+ /** Whether the server state holds the row */
27
+ hasServerRow: (key: RowKey) => boolean;
28
+ /** The pending optimistic layers, oldest first */
29
+ getLayers: () => readonly Layer[];
30
+ /** The server row, without optimistic layers */
31
+ getServerRow: (key: RowKey) => Row | undefined;
32
+ /** The newest `seq` that wrote any field of a server row, or 0 */
33
+ getLastSeq: (key: RowKey) => number;
34
+ /** Every key with a server row */
35
+ getServerKeys: () => RowKey[];
36
+ /** The row as queries see it: the server row with every pending layer applied */
37
+ get: (key: RowKey) => Row | undefined;
38
+ /** Every key queries can see */
39
+ keys: () => RowKey[];
40
+ /** The order in which the db first saw a row, used when a query has no `orderBy` */
41
+ getArrival: (key: RowKey) => number;
42
+ /** Whether a pending write touches the row */
43
+ hasLayer: (key: RowKey) => boolean;
44
+ /**
45
+ * Writes server fields. A complete write creates the row or replaces its fields, a partial write changes only the
46
+ * fields it carries and is ignored for a row the store does not hold. Returns whether the row changed.
47
+ */
48
+ /** Links another store: writes, deletes and layers on one change the fields the other has, for the same key */
49
+ link: (store: Store) => void;
50
+ write: (key: RowKey, written: Row, { complete, stamp, linked }: {
51
+ complete: boolean;
52
+ stamp: Stamp;
53
+ linked?: boolean;
54
+ }) => boolean;
55
+ /** Removes a row the server deleted, and remembers the key so an older response cannot bring it back */
56
+ remove: (key: RowKey, stamp: Stamp, linked?: boolean) => boolean;
57
+ /** Removes a row without remembering it, for rows no subset holds any more */
58
+ drop: (key: RowKey) => boolean;
59
+ /** Forgets deleted keys once every request sent before the delete has settled */
60
+ forgetTombstones: (oldestPendingSeq: number) => void;
61
+ addLayer: (layer: Layer, linked?: boolean) => void;
62
+ removeLayer: (id: string, linked?: boolean) => void;
63
+ /** Moves the pending layers of a temporary key to the key the server assigned */
64
+ rekeyLayers: (from: RowKey, to: RowKey) => void;
65
+ /** Removes every row, layer and tombstone */
66
+ clear: () => void;
67
+ /** The fields a linked store wrote, limited to the fields this store's row has. Rows it does not hold stay away. */
68
+ /** The fields of a row this store also has, by its server row, which a pending delete layer does not hide */
69
+ private pickLinked;
70
+ private mirrorWrite;
71
+ /** Mirrors an update or delete layer. An insert cannot make a complete row of another shape, so it stays away. */
72
+ private mirrorLayer;
73
+ private accepts;
74
+ private invalidate;
75
+ }
76
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAE7D,OAAO,KAAK,EAAa,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAElE;;;;;;GAMG;AACH,qBAAa,KAAK;IAYd,OAAO,CAAC,QAAQ,CAAC,OAAO;IAX1B,OAAO,CAAC,IAAI,CAA0B;IACtC,OAAO,CAAC,IAAI,CAA6C;IACzD,OAAO,CAAC,UAAU,CAAoD;IACtE,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,OAAO,CAAsC;IACrD,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,cAAc,CAAK;IAC3B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IAAI,MAAM,EAAE,gBAAa,UAUtC;IAEF,iFAAiF;IACjF,WAAW,GAAI,MAAM,MAAM,EAAE,IAAI,MAAM,UAarC;IAEF,6CAA6C;IAC7C,KAAK,aAOH;IAEF,oHAAoH;IACpH,6GAA6G;IAC7G,OAAO,CAAC,UAAU,CAMhB;IAEF,OAAO,CAAC,WAAW,CAKjB;IAEF,kHAAkH;IAClH,OAAO,CAAC,WAAW,CAMjB;IAEF,OAAO,CAAC,OAAO,CAcb;IAEF,OAAO,CAAC,UAAU,CAGhB;CACH"}
@@ -0,0 +1,27 @@
1
+ import { RowKey } from '../collection/collection.types';
2
+ export type Row = Record<string, unknown>;
3
+ /**
4
+ * When a write happened, for ordering:
5
+ * - `sent`: a response to a request; `seq` and `wall` are when it was sent, also for ingested requests sent elsewhere
6
+ * - `now`: a socket event or a mutation result that is newer than anything before it
7
+ *
8
+ * Every stamp's `seq` comes from one counter, so writes are ordered exactly, even within one millisecond.
9
+ */
10
+ export type Stamp = {
11
+ kind: "sent" | "now";
12
+ seq: number;
13
+ wall: number;
14
+ };
15
+ export type FieldMeta = {
16
+ seq: number;
17
+ wall: number;
18
+ version?: unknown;
19
+ };
20
+ /** One pending optimistic write */
21
+ export type Layer = {
22
+ id: string;
23
+ key: RowKey;
24
+ type: "insert" | "update" | "delete";
25
+ values: Row;
26
+ };
27
+ //# sourceMappingURL=store.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.types.d.ts","sourceRoot":"","sources":["../../src/store/store.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAE7D,MAAM,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAExE,MAAM,MAAM,SAAS,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEzE,mCAAmC;AACnC,MAAM,MAAM,KAAK,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;IAAC,MAAM,EAAE,GAAG,CAAA;CAAE,CAAC"}
@@ -0,0 +1,66 @@
1
+ import { LoaderConfig, RowKey } from '../collection/collection.types';
2
+ import { CollectionRuntime } from '../db/collection.runtime';
3
+ import { SyncSubscription } from '../sync/sync';
4
+ /** A query attached to a subset, told when the subset's status or rows change */
5
+ export type SubsetObserver = {
6
+ onSubsetChange: () => void;
7
+ };
8
+ /** What every loaded subset shares: who uses it, its load status, freshness and garbage collection */
9
+ export type LoadOptions = {
10
+ /**
11
+ * Whether the load waits for the collection's writes in flight, so its response is never older than them. A write's
12
+ * own `onSuccess: "refetch"` does not wait, because the writes queued behind it wait for that refetch.
13
+ */
14
+ waitForWrites?: boolean;
15
+ };
16
+ export declare abstract class BaseSubset {
17
+ readonly collection: CollectionRuntime;
18
+ readonly loader: LoaderConfig;
19
+ hasLoaded: boolean;
20
+ isLoading: boolean;
21
+ error: unknown;
22
+ loadedAt: number;
23
+ /** Set when events may have been missed, so only a load makes the subset fresh again */
24
+ invalidated: boolean;
25
+ subscriptions: SyncSubscription[];
26
+ protected observers: Set<SubsetObserver>;
27
+ protected pending: Promise<void> | undefined;
28
+ private gcTimer;
29
+ constructor(collection: CollectionRuntime, loader: LoaderConfig);
30
+ /** Whether a query is attached */
31
+ isUsed: () => boolean;
32
+ /** How many queries read the subset */
33
+ get observerCount(): number;
34
+ attach: (observer: SubsetObserver) => void;
35
+ detach: (observer: SubsetObserver) => void;
36
+ /** The values that pick a sync's subscription for the subset, by field */
37
+ abstract getEqValues: () => Record<string, unknown>;
38
+ /**
39
+ * Moves the subset to the sync subscriptions its values pick now, for example when the row a key subset holds moved
40
+ * to another team, and leaves the ones it no longer needs.
41
+ */
42
+ refreshSyncs: () => void;
43
+ /** Stops the garbage collection timer, for `destroy` */
44
+ cancelGc: () => void;
45
+ /** Releases the subset: stops its syncs and removes the rows nothing else holds */
46
+ dispose: () => void;
47
+ /** Whether syncs deliver every insert, update and delete for the subset */
48
+ isLive: () => boolean;
49
+ isFresh: () => boolean;
50
+ /** Marks the subset stale, for example after a reconnect that may have missed events */
51
+ invalidate: () => void;
52
+ notify: () => void;
53
+ /** Whether the load in flight loads the whole subset, rather than its next page */
54
+ protected pendingIsFull: boolean;
55
+ /** Loads the subset unless a load of the whole subset is in flight. One asked during a next page runs after it. */
56
+ load: (options?: LoadOptions) => Promise<void>;
57
+ /** Loads again after the load in flight, so the result is newer than anything sent before */
58
+ reload: (options?: LoadOptions) => Promise<void>;
59
+ /** Refetches when the subset is stale and no load is in flight */
60
+ revalidate: () => void;
61
+ abstract holds: (key: RowKey) => boolean;
62
+ protected abstract run: (options: LoadOptions) => Promise<void>;
63
+ protected beginLoad: () => void;
64
+ protected endLoad: () => void;
65
+ }
66
+ //# sourceMappingURL=base-subset.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-subset.d.ts","sourceRoot":"","sources":["../../src/subset/base-subset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,iFAAiF;AACjF,MAAM,MAAM,cAAc,GAAG;IAAE,cAAc,EAAE,MAAM,IAAI,CAAA;CAAE,CAAC;AAI5D,sGAAsG;AACtG,MAAM,MAAM,WAAW,GAAG;IACxB;;;OAGG;I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