@tanstack/db 0.0.7 → 0.0.8
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/cjs/collection.cjs +441 -284
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/cjs/collection.d.cts +103 -30
- package/dist/cjs/proxy.cjs +2 -2
- package/dist/cjs/proxy.cjs.map +1 -1
- package/dist/cjs/query/compiled-query.cjs +23 -37
- package/dist/cjs/query/compiled-query.cjs.map +1 -1
- package/dist/cjs/query/compiled-query.d.cts +2 -2
- package/dist/cjs/query/order-by.cjs +41 -38
- package/dist/cjs/query/order-by.cjs.map +1 -1
- package/dist/cjs/query/schema.d.cts +3 -3
- package/dist/cjs/transactions.cjs +7 -6
- package/dist/cjs/transactions.cjs.map +1 -1
- package/dist/cjs/transactions.d.cts +9 -9
- package/dist/cjs/types.d.cts +28 -22
- package/dist/esm/collection.d.ts +103 -30
- package/dist/esm/collection.js +442 -285
- package/dist/esm/collection.js.map +1 -1
- package/dist/esm/proxy.js +2 -2
- package/dist/esm/proxy.js.map +1 -1
- package/dist/esm/query/compiled-query.d.ts +2 -2
- package/dist/esm/query/compiled-query.js +23 -37
- package/dist/esm/query/compiled-query.js.map +1 -1
- package/dist/esm/query/order-by.js +41 -38
- package/dist/esm/query/order-by.js.map +1 -1
- package/dist/esm/query/schema.d.ts +3 -3
- package/dist/esm/transactions.d.ts +9 -9
- package/dist/esm/transactions.js +7 -6
- package/dist/esm/transactions.js.map +1 -1
- package/dist/esm/types.d.ts +28 -22
- package/package.json +2 -2
- package/src/collection.ts +624 -372
- package/src/proxy.ts +2 -2
- package/src/query/compiled-query.ts +26 -37
- package/src/query/order-by.ts +69 -67
- package/src/query/schema.ts +3 -3
- package/src/transactions.ts +24 -22
- package/src/types.ts +44 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactions.cjs","sources":["../../src/transactions.ts"],"sourcesContent":["import { createDeferred } from \"./deferred\"\nimport type { Deferred } from \"./deferred\"\nimport type {\n PendingMutation,\n TransactionConfig,\n TransactionState,\n TransactionWithMutations,\n} from \"./types\"\n\nfunction generateUUID() {\n // Check if crypto.randomUUID is available (modern browsers and Node.js 15+)\n if (\n typeof crypto !== `undefined` &&\n typeof crypto.randomUUID === `function`\n ) {\n return crypto.randomUUID()\n }\n\n // Fallback implementation for older environments\n return `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, function (c) {\n const r = (Math.random() * 16) | 0\n const v = c === `x` ? r : (r & 0x3) | 0x8\n return v.toString(16)\n })\n}\n\nconst transactions: Array<Transaction
|
|
1
|
+
{"version":3,"file":"transactions.cjs","sources":["../../src/transactions.ts"],"sourcesContent":["import { createDeferred } from \"./deferred\"\nimport type { Deferred } from \"./deferred\"\nimport type {\n MutationFn,\n PendingMutation,\n TransactionConfig,\n TransactionState,\n TransactionWithMutations,\n} from \"./types\"\n\nfunction generateUUID() {\n // Check if crypto.randomUUID is available (modern browsers and Node.js 15+)\n if (\n typeof crypto !== `undefined` &&\n typeof crypto.randomUUID === `function`\n ) {\n return crypto.randomUUID()\n }\n\n // Fallback implementation for older environments\n return `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, function (c) {\n const r = (Math.random() * 16) | 0\n const v = c === `x` ? r : (r & 0x3) | 0x8\n return v.toString(16)\n })\n}\n\nconst transactions: Array<Transaction<any>> = []\nlet transactionStack: Array<Transaction<any>> = []\n\nexport function createTransaction(config: TransactionConfig): Transaction {\n if (typeof config.mutationFn === `undefined`) {\n throw `mutationFn is required when creating a transaction`\n }\n\n let transactionId = config.id\n if (!transactionId) {\n transactionId = generateUUID()\n }\n const newTransaction = new Transaction({ ...config, id: transactionId })\n\n transactions.push(newTransaction)\n\n return newTransaction\n}\n\nexport function getActiveTransaction(): Transaction | undefined {\n if (transactionStack.length > 0) {\n return transactionStack.slice(-1)[0]\n } else {\n return undefined\n }\n}\n\nfunction registerTransaction(tx: Transaction<any>) {\n transactionStack.push(tx)\n}\n\nfunction unregisterTransaction(tx: Transaction<any>) {\n transactionStack = transactionStack.filter((t) => t.id !== tx.id)\n}\n\nfunction removeFromPendingList(tx: Transaction<any>) {\n const index = transactions.findIndex((t) => t.id === tx.id)\n if (index !== -1) {\n transactions.splice(index, 1)\n }\n}\n\nexport class Transaction<T extends object = Record<string, unknown>> {\n public id: string\n public state: TransactionState\n public mutationFn: MutationFn<T>\n public mutations: Array<PendingMutation<T>>\n public isPersisted: Deferred<Transaction<T>>\n public autoCommit: boolean\n public createdAt: Date\n public metadata: Record<string, unknown>\n public error?: {\n message: string\n error: Error\n }\n\n constructor(config: TransactionConfig<T>) {\n this.id = config.id!\n this.mutationFn = config.mutationFn\n this.state = `pending`\n this.mutations = []\n this.isPersisted = createDeferred<Transaction<T>>()\n this.autoCommit = config.autoCommit ?? true\n this.createdAt = new Date()\n this.metadata = config.metadata ?? {}\n }\n\n setState(newState: TransactionState) {\n this.state = newState\n\n if (newState === `completed` || newState === `failed`) {\n removeFromPendingList(this)\n }\n }\n\n mutate(callback: () => void): Transaction<T> {\n if (this.state !== `pending`) {\n throw `You can no longer call .mutate() as the transaction is no longer pending`\n }\n\n registerTransaction(this)\n try {\n callback()\n } finally {\n unregisterTransaction(this)\n }\n\n if (this.autoCommit) {\n this.commit()\n }\n\n return this\n }\n\n applyMutations(mutations: Array<PendingMutation<any>>): void {\n for (const newMutation of mutations) {\n const existingIndex = this.mutations.findIndex(\n (m) => m.globalKey === newMutation.globalKey\n )\n\n if (existingIndex >= 0) {\n // Replace existing mutation\n this.mutations[existingIndex] = newMutation\n } else {\n // Insert new mutation\n this.mutations.push(newMutation)\n }\n }\n }\n\n rollback(config?: { isSecondaryRollback?: boolean }): Transaction<T> {\n const isSecondaryRollback = config?.isSecondaryRollback ?? false\n if (this.state === `completed`) {\n throw `You can no longer call .rollback() as the transaction is already completed`\n }\n\n this.setState(`failed`)\n\n // See if there's any other transactions w/ mutations on the same ids\n // and roll them back as well.\n if (!isSecondaryRollback) {\n const mutationIds = new Set()\n this.mutations.forEach((m) => mutationIds.add(m.globalKey))\n for (const t of transactions) {\n t.state === `pending` &&\n t.mutations.some((m) => mutationIds.has(m.globalKey)) &&\n t.rollback({ isSecondaryRollback: true })\n }\n }\n\n // Reject the promise\n this.isPersisted.reject(this.error?.error)\n this.touchCollection()\n\n return this\n }\n\n // Tell collection that something has changed with the transaction\n touchCollection(): void {\n const hasCalled = new Set()\n for (const mutation of this.mutations) {\n if (!hasCalled.has(mutation.collection.id)) {\n mutation.collection.onTransactionStateChange()\n mutation.collection.commitPendingTransactions()\n hasCalled.add(mutation.collection.id)\n }\n }\n }\n\n async commit(): Promise<Transaction<T>> {\n if (this.state !== `pending`) {\n throw `You can no longer call .commit() as the transaction is no longer pending`\n }\n\n this.setState(`persisting`)\n\n if (this.mutations.length === 0) {\n this.setState(`completed`)\n\n return this\n }\n\n // Run mutationFn\n try {\n // At this point we know there's at least one mutation\n // We've already verified mutations is non-empty, so this cast is safe\n // Use a direct type assertion instead of object spreading to preserve the original type\n await this.mutationFn({\n transaction: this as unknown as TransactionWithMutations<T>,\n })\n\n this.setState(`completed`)\n this.touchCollection()\n\n this.isPersisted.resolve(this)\n } catch (error) {\n // Update transaction with error information\n this.error = {\n message: error instanceof Error ? error.message : String(error),\n error: error instanceof Error ? error : new Error(String(error)),\n }\n\n // rollback the transaction\n return this.rollback()\n }\n\n return this\n }\n}\n"],"names":["createDeferred"],"mappings":";;;AAUA,SAAS,eAAe;AAEtB,MACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,YAC7B;AACA,WAAO,OAAO,WAAW;AAAA,EAAA;AAI3B,SAAO,uCAAuC,QAAQ,SAAS,SAAU,GAAG;AAC1E,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AAC/B,WAAA,EAAE,SAAS,EAAE;AAAA,EAAA,CACrB;AACH;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B;AACpC,kBAAA,IAAI,SAAS,WAAW,EAAE;AAAA,MAAA;AAAA,IACtC;AAAA,EACF;AAAA,EAGF,MAAM,SAAkC;AAClC,QAAA,KAAK,UAAU,WAAW;AACtB,YAAA;AAAA,IAAA;AAGR,SAAK,SAAS,YAAY;AAEtB,QAAA,KAAK,UAAU,WAAW,GAAG;AAC/B,WAAK,SAAS,WAAW;AAElB,aAAA;AAAA,IAAA;AAIL,QAAA;AAIF,YAAM,KAAK,WAAW;AAAA,QACpB,aAAa;AAAA,MAAA,CACd;AAED,WAAK,SAAS,WAAW;AACzB,WAAK,gBAAgB;AAEhB,WAAA,YAAY,QAAQ,IAAI;AAAA,aACtB,OAAO;AAEd,WAAK,QAAQ;AAAA,QACX,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,QAC9D,OAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;AAAA,MACjE;AAGA,aAAO,KAAK,SAAS;AAAA,IAAA;AAGhB,WAAA;AAAA,EAAA;AAEX;;;;"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Deferred } from './deferred.cjs';
|
|
2
|
-
import { PendingMutation, TransactionConfig, TransactionState } from './types.cjs';
|
|
2
|
+
import { MutationFn, PendingMutation, TransactionConfig, TransactionState } from './types.cjs';
|
|
3
3
|
export declare function createTransaction(config: TransactionConfig): Transaction;
|
|
4
4
|
export declare function getActiveTransaction(): Transaction | undefined;
|
|
5
|
-
export declare class Transaction {
|
|
5
|
+
export declare class Transaction<T extends object = Record<string, unknown>> {
|
|
6
6
|
id: string;
|
|
7
7
|
state: TransactionState;
|
|
8
|
-
mutationFn:
|
|
9
|
-
mutations: Array<PendingMutation<
|
|
10
|
-
isPersisted: Deferred<Transaction
|
|
8
|
+
mutationFn: MutationFn<T>;
|
|
9
|
+
mutations: Array<PendingMutation<T>>;
|
|
10
|
+
isPersisted: Deferred<Transaction<T>>;
|
|
11
11
|
autoCommit: boolean;
|
|
12
12
|
createdAt: Date;
|
|
13
13
|
metadata: Record<string, unknown>;
|
|
@@ -15,13 +15,13 @@ export declare class Transaction {
|
|
|
15
15
|
message: string;
|
|
16
16
|
error: Error;
|
|
17
17
|
};
|
|
18
|
-
constructor(config: TransactionConfig);
|
|
18
|
+
constructor(config: TransactionConfig<T>);
|
|
19
19
|
setState(newState: TransactionState): void;
|
|
20
|
-
mutate(callback: () => void): Transaction
|
|
20
|
+
mutate(callback: () => void): Transaction<T>;
|
|
21
21
|
applyMutations(mutations: Array<PendingMutation<any>>): void;
|
|
22
22
|
rollback(config?: {
|
|
23
23
|
isSecondaryRollback?: boolean;
|
|
24
|
-
}): Transaction
|
|
24
|
+
}): Transaction<T>;
|
|
25
25
|
touchCollection(): void;
|
|
26
|
-
commit(): Promise<Transaction
|
|
26
|
+
commit(): Promise<Transaction<T>>;
|
|
27
27
|
}
|
package/dist/cjs/types.d.cts
CHANGED
|
@@ -17,36 +17,41 @@ export type UtilsRecord = Record<string, Fn>;
|
|
|
17
17
|
*/
|
|
18
18
|
export interface PendingMutation<T extends object = Record<string, unknown>> {
|
|
19
19
|
mutationId: string;
|
|
20
|
-
original:
|
|
21
|
-
modified:
|
|
22
|
-
changes:
|
|
20
|
+
original: Partial<T>;
|
|
21
|
+
modified: T;
|
|
22
|
+
changes: Partial<T>;
|
|
23
|
+
globalKey: string;
|
|
23
24
|
key: any;
|
|
24
25
|
type: OperationType;
|
|
25
26
|
metadata: unknown;
|
|
26
27
|
syncMetadata: Record<string, unknown>;
|
|
27
28
|
createdAt: Date;
|
|
28
29
|
updatedAt: Date;
|
|
29
|
-
collection: Collection<T>;
|
|
30
|
+
collection: Collection<T, any>;
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Configuration options for creating a new transaction
|
|
33
34
|
*/
|
|
34
|
-
export type MutationFnParams = {
|
|
35
|
-
transaction:
|
|
35
|
+
export type MutationFnParams<T extends object = Record<string, unknown>> = {
|
|
36
|
+
transaction: TransactionWithMutations<T>;
|
|
36
37
|
};
|
|
37
|
-
export type MutationFn = (params: MutationFnParams) => Promise<any>;
|
|
38
|
+
export type MutationFn<T extends object = Record<string, unknown>> = (params: MutationFnParams<T>) => Promise<any>;
|
|
39
|
+
/**
|
|
40
|
+
* Represents a non-empty array (at least one element)
|
|
41
|
+
*/
|
|
42
|
+
export type NonEmptyArray<T> = [T, ...Array<T>];
|
|
38
43
|
/**
|
|
39
44
|
* Utility type for a Transaction with at least one mutation
|
|
40
45
|
* This is used internally by the Transaction.commit method
|
|
41
46
|
*/
|
|
42
|
-
export type TransactionWithMutations<T extends object = Record<string, unknown>> = Transaction & {
|
|
43
|
-
mutations:
|
|
47
|
+
export type TransactionWithMutations<T extends object = Record<string, unknown>> = Transaction<T> & {
|
|
48
|
+
mutations: NonEmptyArray<PendingMutation<T>>;
|
|
44
49
|
};
|
|
45
|
-
export interface TransactionConfig {
|
|
50
|
+
export interface TransactionConfig<T extends object = Record<string, unknown>> {
|
|
46
51
|
/** Unique identifier for the transaction */
|
|
47
52
|
id?: string;
|
|
48
53
|
autoCommit?: boolean;
|
|
49
|
-
mutationFn: MutationFn
|
|
54
|
+
mutationFn: MutationFn<T>;
|
|
50
55
|
/** Custom metadata to associate with the transaction */
|
|
51
56
|
metadata?: Record<string, unknown>;
|
|
52
57
|
}
|
|
@@ -56,9 +61,9 @@ type Value<TExtensions = never> = string | number | boolean | bigint | null | TE
|
|
|
56
61
|
};
|
|
57
62
|
export type Row<TExtensions = never> = Record<string, Value<TExtensions>>;
|
|
58
63
|
export type OperationType = `insert` | `update` | `delete`;
|
|
59
|
-
export interface SyncConfig<T extends object = Record<string, unknown
|
|
64
|
+
export interface SyncConfig<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
|
|
60
65
|
sync: (params: {
|
|
61
|
-
collection: Collection<T>;
|
|
66
|
+
collection: Collection<T, TKey>;
|
|
62
67
|
begin: () => void;
|
|
63
68
|
write: (message: Omit<ChangeMessage<T>, `key`>) => void;
|
|
64
69
|
commit: () => void;
|
|
@@ -69,8 +74,8 @@ export interface SyncConfig<T extends object = Record<string, unknown>> {
|
|
|
69
74
|
*/
|
|
70
75
|
getSyncMetadata?: () => Record<string, unknown>;
|
|
71
76
|
}
|
|
72
|
-
export interface ChangeMessage<T extends object = Record<string, unknown
|
|
73
|
-
key:
|
|
77
|
+
export interface ChangeMessage<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
|
|
78
|
+
key: TKey;
|
|
74
79
|
value: T;
|
|
75
80
|
previousValue?: T;
|
|
76
81
|
type: OperationType;
|
|
@@ -101,9 +106,9 @@ export interface OperationConfig {
|
|
|
101
106
|
export interface InsertConfig {
|
|
102
107
|
metadata?: Record<string, unknown>;
|
|
103
108
|
}
|
|
104
|
-
export interface CollectionConfig<T extends object = Record<string, unknown
|
|
109
|
+
export interface CollectionConfig<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
|
|
105
110
|
id?: string;
|
|
106
|
-
sync: SyncConfig<T>;
|
|
111
|
+
sync: SyncConfig<T, TKey>;
|
|
107
112
|
schema?: StandardSchema<T>;
|
|
108
113
|
/**
|
|
109
114
|
* Function to extract the ID from an object
|
|
@@ -112,27 +117,27 @@ export interface CollectionConfig<T extends object = Record<string, unknown>> {
|
|
|
112
117
|
* @returns The ID string for the item
|
|
113
118
|
* @example
|
|
114
119
|
* // For a collection with a 'uuid' field as the primary key
|
|
115
|
-
*
|
|
120
|
+
* getKey: (item) => item.uuid
|
|
116
121
|
*/
|
|
117
|
-
|
|
122
|
+
getKey: (item: T) => TKey;
|
|
118
123
|
/**
|
|
119
124
|
* Optional asynchronous handler function called before an insert operation
|
|
120
125
|
* @param params Object containing transaction and mutation information
|
|
121
126
|
* @returns Promise resolving to any value
|
|
122
127
|
*/
|
|
123
|
-
onInsert?: MutationFn
|
|
128
|
+
onInsert?: MutationFn<T>;
|
|
124
129
|
/**
|
|
125
130
|
* Optional asynchronous handler function called before an update operation
|
|
126
131
|
* @param params Object containing transaction and mutation information
|
|
127
132
|
* @returns Promise resolving to any value
|
|
128
133
|
*/
|
|
129
|
-
onUpdate?: MutationFn
|
|
134
|
+
onUpdate?: MutationFn<T>;
|
|
130
135
|
/**
|
|
131
136
|
* Optional asynchronous handler function called before a delete operation
|
|
132
137
|
* @param params Object containing transaction and mutation information
|
|
133
138
|
* @returns Promise resolving to any value
|
|
134
139
|
*/
|
|
135
|
-
onDelete?: MutationFn
|
|
140
|
+
onDelete?: MutationFn<T>;
|
|
136
141
|
}
|
|
137
142
|
export type ChangesPayload<T extends object = Record<string, unknown>> = Array<ChangeMessage<T>>;
|
|
138
143
|
/**
|
|
@@ -159,3 +164,4 @@ export type KeyedNamespacedRow = [unknown, NamespacedRow];
|
|
|
159
164
|
* a `select` clause.
|
|
160
165
|
*/
|
|
161
166
|
export type NamespacedAndKeyedStream = IStreamBuilder<KeyedNamespacedRow>;
|
|
167
|
+
export type ChangeListener<T extends object = Record<string, unknown>, TKey extends string | number = string | number> = (changes: Array<ChangeMessage<T, TKey>>) => void;
|
package/dist/esm/collection.d.ts
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Store } from '@tanstack/store';
|
|
2
2
|
import { Transaction } from './transactions.js';
|
|
3
3
|
import { SortedMap } from './SortedMap.js';
|
|
4
|
-
import { ChangeMessage, CollectionConfig, Fn, InsertConfig, OperationConfig,
|
|
5
|
-
export declare const collectionsStore:
|
|
4
|
+
import { ChangeListener, ChangeMessage, CollectionConfig, Fn, InsertConfig, OperationConfig, Transaction as TransactionType, UtilsRecord } from './types.js';
|
|
5
|
+
export declare const collectionsStore: Map<string, CollectionImpl<any, any>>;
|
|
6
6
|
/**
|
|
7
7
|
* Enhanced Collection interface that includes both data type T and utilities TUtils
|
|
8
8
|
* @template T - The type of items in the collection
|
|
9
9
|
* @template TUtils - The utilities record type
|
|
10
10
|
*/
|
|
11
|
-
export interface Collection<T extends object = Record<string, unknown>, TUtils extends UtilsRecord = {}> extends CollectionImpl<T> {
|
|
11
|
+
export interface Collection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}> extends CollectionImpl<T, TKey> {
|
|
12
12
|
readonly utils: TUtils;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* Creates a new Collection instance with the given configuration
|
|
16
16
|
*
|
|
17
17
|
* @template T - The type of items in the collection
|
|
18
|
+
* @template TKey - The type of the key for the collection
|
|
18
19
|
* @template TUtils - The utilities record type
|
|
19
20
|
* @param options - Collection options with optional utilities
|
|
20
21
|
* @returns A new Collection with utilities exposed both at top level and under .utils
|
|
21
22
|
*/
|
|
22
|
-
export declare function createCollection<T extends object = Record<string, unknown>, TUtils extends UtilsRecord = {}>(options: CollectionConfig<T> & {
|
|
23
|
+
export declare function createCollection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}>(options: CollectionConfig<T, TKey> & {
|
|
23
24
|
utils?: TUtils;
|
|
24
|
-
}): Collection<T, TUtils>;
|
|
25
|
+
}): Collection<T, TKey, TUtils>;
|
|
25
26
|
/**
|
|
26
27
|
* Preloads a collection with the given configuration
|
|
27
28
|
* Returns a promise that resolves once the sync tool has done its first commit (initial sync is finished)
|
|
@@ -48,7 +49,7 @@ export declare function createCollection<T extends object = Record<string, unkno
|
|
|
48
49
|
* @param config - Configuration for the collection, including id and sync
|
|
49
50
|
* @returns Promise that resolves when the initial sync is finished
|
|
50
51
|
*/
|
|
51
|
-
export declare function preloadCollection<T extends object = Record<string, unknown
|
|
52
|
+
export declare function preloadCollection<T extends object = Record<string, unknown>, TKey extends string | number = string | number>(config: CollectionConfig<T, TKey>): Promise<CollectionImpl<T, TKey>>;
|
|
52
53
|
/**
|
|
53
54
|
* Custom error class for schema validation errors
|
|
54
55
|
*/
|
|
@@ -63,22 +64,19 @@ export declare class SchemaValidationError extends Error {
|
|
|
63
64
|
path?: ReadonlyArray<string | number | symbol>;
|
|
64
65
|
}>, message?: string);
|
|
65
66
|
}
|
|
66
|
-
export declare class CollectionImpl<T extends object = Record<string, unknown
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
export declare class CollectionImpl<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
|
|
68
|
+
transactions: SortedMap<string, Transaction<any>>;
|
|
69
|
+
syncedData: Map<TKey, T>;
|
|
70
|
+
syncedMetadata: Map<TKey, unknown>;
|
|
71
|
+
derivedUpserts: Map<TKey, T>;
|
|
72
|
+
derivedDeletes: Set<TKey>;
|
|
73
|
+
private _size;
|
|
74
|
+
private changeListeners;
|
|
75
|
+
private changeKeyListeners;
|
|
71
76
|
utils: Record<string, Fn>;
|
|
72
|
-
transactions: Store<SortedMap<string, TransactionType>>;
|
|
73
|
-
optimisticOperations: Derived<Array<OptimisticChangeMessage<T>>>;
|
|
74
|
-
derivedState: Derived<Map<string, T>>;
|
|
75
|
-
derivedArray: Derived<Array<T>>;
|
|
76
|
-
derivedChanges: Derived<Array<ChangeMessage<T>>>;
|
|
77
|
-
syncedData: Store<Map<string, T>, (cb: Map<string, T>) => Map<string, T>>;
|
|
78
|
-
syncedMetadata: Store<Map<string, unknown>, (cb: Map<string, unknown>) => Map<string, unknown>>;
|
|
79
77
|
private pendingSyncedTransactions;
|
|
80
78
|
private syncedKeys;
|
|
81
|
-
config: CollectionConfig<T>;
|
|
79
|
+
config: CollectionConfig<T, TKey>;
|
|
82
80
|
private hasReceivedFirstCommit;
|
|
83
81
|
private onFirstCommitCallbacks;
|
|
84
82
|
/**
|
|
@@ -94,15 +92,59 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
|
|
|
94
92
|
* @param config - Configuration object for the collection
|
|
95
93
|
* @throws Error if sync config is missing
|
|
96
94
|
*/
|
|
97
|
-
constructor(config: CollectionConfig<T>);
|
|
95
|
+
constructor(config: CollectionConfig<T, TKey>);
|
|
96
|
+
/**
|
|
97
|
+
* Recompute optimistic state from active transactions
|
|
98
|
+
*/
|
|
99
|
+
private recomputeOptimisticState;
|
|
100
|
+
/**
|
|
101
|
+
* Calculate the current size based on synced data and optimistic changes
|
|
102
|
+
*/
|
|
103
|
+
private calculateSize;
|
|
104
|
+
/**
|
|
105
|
+
* Collect events for optimistic changes
|
|
106
|
+
*/
|
|
107
|
+
private collectOptimisticChanges;
|
|
108
|
+
/**
|
|
109
|
+
* Get the previous value for a key given previous optimistic state
|
|
110
|
+
*/
|
|
111
|
+
private getPreviousValue;
|
|
112
|
+
/**
|
|
113
|
+
* Emit multiple events at once to all listeners
|
|
114
|
+
*/
|
|
115
|
+
private emitEvents;
|
|
116
|
+
/**
|
|
117
|
+
* Get the current value for a key (virtual derived state)
|
|
118
|
+
*/
|
|
119
|
+
get(key: TKey): T | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Check if a key exists in the collection (virtual derived state)
|
|
122
|
+
*/
|
|
123
|
+
has(key: TKey): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Get the current size of the collection (cached)
|
|
126
|
+
*/
|
|
127
|
+
get size(): number;
|
|
128
|
+
/**
|
|
129
|
+
* Get all keys (virtual derived state)
|
|
130
|
+
*/
|
|
131
|
+
keys(): IterableIterator<TKey>;
|
|
132
|
+
/**
|
|
133
|
+
* Get all values (virtual derived state)
|
|
134
|
+
*/
|
|
135
|
+
values(): IterableIterator<T>;
|
|
136
|
+
/**
|
|
137
|
+
* Get all entries (virtual derived state)
|
|
138
|
+
*/
|
|
139
|
+
entries(): IterableIterator<[TKey, T]>;
|
|
98
140
|
/**
|
|
99
141
|
* Attempts to commit pending synced transactions if there are no active transactions
|
|
100
142
|
* This method processes operations from pending transactions and applies them to the synced data
|
|
101
143
|
*/
|
|
102
144
|
commitPendingTransactions: () => void;
|
|
103
145
|
private ensureStandardSchema;
|
|
104
|
-
|
|
105
|
-
|
|
146
|
+
getKeyFromItem(item: T): TKey;
|
|
147
|
+
generateGlobalKey(key: any, item: any): string;
|
|
106
148
|
private validateData;
|
|
107
149
|
/**
|
|
108
150
|
* Inserts one or more items into the collection
|
|
@@ -123,7 +165,7 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
|
|
|
123
165
|
* // Insert with custom key
|
|
124
166
|
* insert({ text: "Buy groceries" }, { key: "grocery-task" })
|
|
125
167
|
*/
|
|
126
|
-
insert: (data: T | Array<T>, config?: InsertConfig) => Transaction
|
|
168
|
+
insert: (data: T | Array<T>, config?: InsertConfig) => Transaction<Record<string, unknown>>;
|
|
127
169
|
/**
|
|
128
170
|
* Updates one or more items in the collection using a callback function
|
|
129
171
|
* @param items - Single item/key or array of items/keys to update
|
|
@@ -162,8 +204,10 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
|
|
|
162
204
|
* // Update with metadata
|
|
163
205
|
* update("todo-1", { metadata: { reason: "user update" } }, (draft) => { draft.text = "Updated text" })
|
|
164
206
|
*/
|
|
165
|
-
update<TItem extends object = T>(
|
|
166
|
-
update<TItem extends object = T>(
|
|
207
|
+
update<TItem extends object = T>(key: Array<TKey | unknown>, callback: (drafts: Array<TItem>) => void): TransactionType;
|
|
208
|
+
update<TItem extends object = T>(keys: Array<TKey | unknown>, config: OperationConfig, callback: (drafts: Array<TItem>) => void): TransactionType;
|
|
209
|
+
update<TItem extends object = T>(id: TKey | unknown, callback: (draft: TItem) => void): TransactionType;
|
|
210
|
+
update<TItem extends object = T>(id: TKey | unknown, config: OperationConfig, callback: (draft: TItem) => void): TransactionType;
|
|
167
211
|
/**
|
|
168
212
|
* Deletes one or more items from the collection
|
|
169
213
|
* @param ids - Single ID or array of IDs to delete
|
|
@@ -179,20 +223,20 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
|
|
|
179
223
|
* // Delete with metadata
|
|
180
224
|
* delete("todo-1", { metadata: { reason: "completed" } })
|
|
181
225
|
*/
|
|
182
|
-
delete: (
|
|
226
|
+
delete: (keys: Array<TKey> | TKey, config?: OperationConfig) => TransactionType<any>;
|
|
183
227
|
/**
|
|
184
228
|
* Gets the current state of the collection as a Map
|
|
185
229
|
*
|
|
186
230
|
* @returns A Map containing all items in the collection, with keys as identifiers
|
|
187
231
|
*/
|
|
188
|
-
get state(): Map<
|
|
232
|
+
get state(): Map<TKey, T>;
|
|
189
233
|
/**
|
|
190
234
|
* Gets the current state of the collection as a Map, but only resolves when data is available
|
|
191
235
|
* Waits for the first sync commit to complete before resolving
|
|
192
236
|
*
|
|
193
237
|
* @returns Promise that resolves to a Map containing all items in the collection
|
|
194
238
|
*/
|
|
195
|
-
stateWhenReady(): Promise<Map<
|
|
239
|
+
stateWhenReady(): Promise<Map<TKey, T>>;
|
|
196
240
|
/**
|
|
197
241
|
* Gets the current state of the collection as an Array
|
|
198
242
|
*
|
|
@@ -216,5 +260,34 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
|
|
|
216
260
|
* @param callback - A function that will be called with the changes in the collection
|
|
217
261
|
* @returns A function that can be called to unsubscribe from the changes
|
|
218
262
|
*/
|
|
219
|
-
subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void
|
|
263
|
+
subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void, { includeInitialState }?: {
|
|
264
|
+
includeInitialState?: boolean;
|
|
265
|
+
}): () => void;
|
|
266
|
+
/**
|
|
267
|
+
* Subscribe to changes for a specific key
|
|
268
|
+
*/
|
|
269
|
+
subscribeChangesKey(key: TKey, listener: ChangeListener<T, TKey>, { includeInitialState }?: {
|
|
270
|
+
includeInitialState?: boolean;
|
|
271
|
+
}): () => void;
|
|
272
|
+
/**
|
|
273
|
+
* Trigger a recomputation when transactions change
|
|
274
|
+
* This method should be called by the Transaction class when state changes
|
|
275
|
+
*/
|
|
276
|
+
onTransactionStateChange(): void;
|
|
277
|
+
private _storeMap;
|
|
278
|
+
/**
|
|
279
|
+
* Returns a Tanstack Store Map that is updated when the collection changes
|
|
280
|
+
* This is a temporary solution to enable the existing framework hooks to work
|
|
281
|
+
* with the new internals of Collection until they are rewritten.
|
|
282
|
+
* TODO: Remove this once the framework hooks are rewritten.
|
|
283
|
+
*/
|
|
284
|
+
asStoreMap(): Store<Map<TKey, T>>;
|
|
285
|
+
private _storeArray;
|
|
286
|
+
/**
|
|
287
|
+
* Returns a Tanstack Store Array that is updated when the collection changes
|
|
288
|
+
* This is a temporary solution to enable the existing framework hooks to work
|
|
289
|
+
* with the new internals of Collection until they are rewritten.
|
|
290
|
+
* TODO: Remove this once the framework hooks are rewritten.
|
|
291
|
+
*/
|
|
292
|
+
asStoreArray(): Store<Array<T>>;
|
|
220
293
|
}
|