@tanstack/db 0.0.1

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 (154) hide show
  1. package/README.md +37 -0
  2. package/dist/cjs/SortedMap.cjs +140 -0
  3. package/dist/cjs/SortedMap.cjs.map +1 -0
  4. package/dist/cjs/SortedMap.d.cts +91 -0
  5. package/dist/cjs/collection.cjs +597 -0
  6. package/dist/cjs/collection.cjs.map +1 -0
  7. package/dist/cjs/collection.d.cts +176 -0
  8. package/dist/cjs/deferred.cjs +25 -0
  9. package/dist/cjs/deferred.cjs.map +1 -0
  10. package/dist/cjs/deferred.d.cts +20 -0
  11. package/dist/cjs/errors.cjs +10 -0
  12. package/dist/cjs/errors.cjs.map +1 -0
  13. package/dist/cjs/errors.d.cts +3 -0
  14. package/dist/cjs/index.cjs +33 -0
  15. package/dist/cjs/index.cjs.map +1 -0
  16. package/dist/cjs/index.d.cts +9 -0
  17. package/dist/cjs/proxy.cjs +654 -0
  18. package/dist/cjs/proxy.cjs.map +1 -0
  19. package/dist/cjs/proxy.d.cts +59 -0
  20. package/dist/cjs/query/compiled-query.cjs +162 -0
  21. package/dist/cjs/query/compiled-query.cjs.map +1 -0
  22. package/dist/cjs/query/compiled-query.d.cts +22 -0
  23. package/dist/cjs/query/evaluators.cjs +146 -0
  24. package/dist/cjs/query/evaluators.cjs.map +1 -0
  25. package/dist/cjs/query/evaluators.d.cts +9 -0
  26. package/dist/cjs/query/extractors.cjs +122 -0
  27. package/dist/cjs/query/extractors.cjs.map +1 -0
  28. package/dist/cjs/query/extractors.d.cts +22 -0
  29. package/dist/cjs/query/functions.cjs +152 -0
  30. package/dist/cjs/query/functions.cjs.map +1 -0
  31. package/dist/cjs/query/functions.d.cts +21 -0
  32. package/dist/cjs/query/group-by.cjs +91 -0
  33. package/dist/cjs/query/group-by.cjs.map +1 -0
  34. package/dist/cjs/query/group-by.d.cts +40 -0
  35. package/dist/cjs/query/index.d.cts +5 -0
  36. package/dist/cjs/query/joins.cjs +155 -0
  37. package/dist/cjs/query/joins.cjs.map +1 -0
  38. package/dist/cjs/query/joins.d.cts +14 -0
  39. package/dist/cjs/query/key-by.cjs +43 -0
  40. package/dist/cjs/query/key-by.cjs.map +1 -0
  41. package/dist/cjs/query/key-by.d.cts +3 -0
  42. package/dist/cjs/query/order-by.cjs +229 -0
  43. package/dist/cjs/query/order-by.cjs.map +1 -0
  44. package/dist/cjs/query/order-by.d.cts +3 -0
  45. package/dist/cjs/query/pipeline-compiler.cjs +94 -0
  46. package/dist/cjs/query/pipeline-compiler.cjs.map +1 -0
  47. package/dist/cjs/query/pipeline-compiler.d.cts +9 -0
  48. package/dist/cjs/query/query-builder.cjs +314 -0
  49. package/dist/cjs/query/query-builder.cjs.map +1 -0
  50. package/dist/cjs/query/query-builder.d.cts +219 -0
  51. package/dist/cjs/query/schema.d.cts +98 -0
  52. package/dist/cjs/query/select.cjs +107 -0
  53. package/dist/cjs/query/select.cjs.map +1 -0
  54. package/dist/cjs/query/select.d.cts +3 -0
  55. package/dist/cjs/query/types.d.cts +188 -0
  56. package/dist/cjs/query/utils.cjs +154 -0
  57. package/dist/cjs/query/utils.cjs.map +1 -0
  58. package/dist/cjs/query/utils.d.cts +37 -0
  59. package/dist/cjs/transactions.cjs +137 -0
  60. package/dist/cjs/transactions.cjs.map +1 -0
  61. package/dist/cjs/transactions.d.cts +27 -0
  62. package/dist/cjs/types.d.cts +94 -0
  63. package/dist/cjs/utils.cjs +17 -0
  64. package/dist/cjs/utils.cjs.map +1 -0
  65. package/dist/cjs/utils.d.cts +3 -0
  66. package/dist/esm/SortedMap.d.ts +91 -0
  67. package/dist/esm/SortedMap.js +140 -0
  68. package/dist/esm/SortedMap.js.map +1 -0
  69. package/dist/esm/collection.d.ts +176 -0
  70. package/dist/esm/collection.js +597 -0
  71. package/dist/esm/collection.js.map +1 -0
  72. package/dist/esm/deferred.d.ts +20 -0
  73. package/dist/esm/deferred.js +25 -0
  74. package/dist/esm/deferred.js.map +1 -0
  75. package/dist/esm/errors.d.ts +3 -0
  76. package/dist/esm/errors.js +10 -0
  77. package/dist/esm/errors.js.map +1 -0
  78. package/dist/esm/index.d.ts +9 -0
  79. package/dist/esm/index.js +33 -0
  80. package/dist/esm/index.js.map +1 -0
  81. package/dist/esm/proxy.d.ts +59 -0
  82. package/dist/esm/proxy.js +654 -0
  83. package/dist/esm/proxy.js.map +1 -0
  84. package/dist/esm/query/compiled-query.d.ts +22 -0
  85. package/dist/esm/query/compiled-query.js +162 -0
  86. package/dist/esm/query/compiled-query.js.map +1 -0
  87. package/dist/esm/query/evaluators.d.ts +9 -0
  88. package/dist/esm/query/evaluators.js +146 -0
  89. package/dist/esm/query/evaluators.js.map +1 -0
  90. package/dist/esm/query/extractors.d.ts +22 -0
  91. package/dist/esm/query/extractors.js +122 -0
  92. package/dist/esm/query/extractors.js.map +1 -0
  93. package/dist/esm/query/functions.d.ts +21 -0
  94. package/dist/esm/query/functions.js +152 -0
  95. package/dist/esm/query/functions.js.map +1 -0
  96. package/dist/esm/query/group-by.d.ts +40 -0
  97. package/dist/esm/query/group-by.js +91 -0
  98. package/dist/esm/query/group-by.js.map +1 -0
  99. package/dist/esm/query/index.d.ts +5 -0
  100. package/dist/esm/query/joins.d.ts +14 -0
  101. package/dist/esm/query/joins.js +155 -0
  102. package/dist/esm/query/joins.js.map +1 -0
  103. package/dist/esm/query/key-by.d.ts +3 -0
  104. package/dist/esm/query/key-by.js +43 -0
  105. package/dist/esm/query/key-by.js.map +1 -0
  106. package/dist/esm/query/order-by.d.ts +3 -0
  107. package/dist/esm/query/order-by.js +229 -0
  108. package/dist/esm/query/order-by.js.map +1 -0
  109. package/dist/esm/query/pipeline-compiler.d.ts +9 -0
  110. package/dist/esm/query/pipeline-compiler.js +94 -0
  111. package/dist/esm/query/pipeline-compiler.js.map +1 -0
  112. package/dist/esm/query/query-builder.d.ts +219 -0
  113. package/dist/esm/query/query-builder.js +314 -0
  114. package/dist/esm/query/query-builder.js.map +1 -0
  115. package/dist/esm/query/schema.d.ts +98 -0
  116. package/dist/esm/query/select.d.ts +3 -0
  117. package/dist/esm/query/select.js +107 -0
  118. package/dist/esm/query/select.js.map +1 -0
  119. package/dist/esm/query/types.d.ts +188 -0
  120. package/dist/esm/query/utils.d.ts +37 -0
  121. package/dist/esm/query/utils.js +154 -0
  122. package/dist/esm/query/utils.js.map +1 -0
  123. package/dist/esm/transactions.d.ts +27 -0
  124. package/dist/esm/transactions.js +137 -0
  125. package/dist/esm/transactions.js.map +1 -0
  126. package/dist/esm/types.d.ts +94 -0
  127. package/dist/esm/utils.d.ts +3 -0
  128. package/dist/esm/utils.js +17 -0
  129. package/dist/esm/utils.js.map +1 -0
  130. package/package.json +57 -0
  131. package/src/SortedMap.ts +163 -0
  132. package/src/collection.ts +919 -0
  133. package/src/deferred.ts +47 -0
  134. package/src/errors.ts +6 -0
  135. package/src/index.ts +12 -0
  136. package/src/proxy.ts +1104 -0
  137. package/src/query/compiled-query.ts +193 -0
  138. package/src/query/evaluators.ts +222 -0
  139. package/src/query/extractors.ts +211 -0
  140. package/src/query/functions.ts +297 -0
  141. package/src/query/group-by.ts +137 -0
  142. package/src/query/index.ts +5 -0
  143. package/src/query/joins.ts +247 -0
  144. package/src/query/key-by.ts +61 -0
  145. package/src/query/order-by.ts +312 -0
  146. package/src/query/pipeline-compiler.ts +152 -0
  147. package/src/query/query-builder.ts +898 -0
  148. package/src/query/schema.ts +255 -0
  149. package/src/query/select.ts +173 -0
  150. package/src/query/types.ts +417 -0
  151. package/src/query/utils.ts +245 -0
  152. package/src/transactions.ts +198 -0
  153. package/src/types.ts +125 -0
  154. package/src/utils.ts +15 -0
@@ -0,0 +1,94 @@
1
+ import { Collection } from './collection.js';
2
+ import { StandardSchemaV1 } from '@standard-schema/spec';
3
+ import { Transaction } from './transactions.js';
4
+ export type TransactionState = `pending` | `persisting` | `completed` | `failed`;
5
+ /**
6
+ * Represents a pending mutation within a transaction
7
+ * Contains information about the original and modified data, as well as metadata
8
+ */
9
+ export interface PendingMutation<T extends object = Record<string, unknown>> {
10
+ mutationId: string;
11
+ original: Record<string, unknown>;
12
+ modified: Record<string, unknown>;
13
+ changes: Record<string, unknown>;
14
+ key: string;
15
+ type: OperationType;
16
+ metadata: unknown;
17
+ syncMetadata: Record<string, unknown>;
18
+ createdAt: Date;
19
+ updatedAt: Date;
20
+ collection: Collection<T>;
21
+ }
22
+ /**
23
+ * Configuration options for creating a new transaction
24
+ */
25
+ export type MutationFnParams = {
26
+ transaction: Transaction;
27
+ };
28
+ export type MutationFn = (params: MutationFnParams) => Promise<any>;
29
+ export interface TransactionConfig {
30
+ /** Unique identifier for the transaction */
31
+ id?: string;
32
+ autoCommit?: boolean;
33
+ mutationFn: MutationFn;
34
+ /** Custom metadata to associate with the transaction */
35
+ metadata?: Record<string, unknown>;
36
+ }
37
+ export type { Transaction };
38
+ type Value<TExtensions = never> = string | number | boolean | bigint | null | TExtensions | Array<Value<TExtensions>> | {
39
+ [key: string]: Value<TExtensions>;
40
+ };
41
+ export type Row<TExtensions = never> = Record<string, Value<TExtensions>>;
42
+ export type OperationType = `insert` | `update` | `delete`;
43
+ export interface SyncConfig<T extends object = Record<string, unknown>> {
44
+ sync: (params: {
45
+ collection: Collection<T>;
46
+ begin: () => void;
47
+ write: (message: ChangeMessage<T>) => void;
48
+ commit: () => void;
49
+ }) => void;
50
+ /**
51
+ * Get the sync metadata for insert operations
52
+ * @returns Record containing primaryKey and relation information
53
+ */
54
+ getSyncMetadata?: () => Record<string, unknown>;
55
+ }
56
+ export interface ChangeMessage<T extends object = Record<string, unknown>> {
57
+ key: string;
58
+ value: T;
59
+ previousValue?: T;
60
+ type: OperationType;
61
+ metadata?: Record<string, unknown>;
62
+ }
63
+ export interface OptimisticChangeMessage<T extends object = Record<string, unknown>> extends ChangeMessage<T> {
64
+ isActive?: boolean;
65
+ }
66
+ /**
67
+ * The Standard Schema interface.
68
+ * This follows the standard-schema specification: https://github.com/standard-schema/standard-schema
69
+ */
70
+ export type StandardSchema<T> = StandardSchemaV1 & {
71
+ "~standard": {
72
+ types?: {
73
+ input: T;
74
+ output: T;
75
+ };
76
+ };
77
+ };
78
+ /**
79
+ * Type alias for StandardSchema
80
+ */
81
+ export type StandardSchemaAlias<T = unknown> = StandardSchema<T>;
82
+ export interface OperationConfig {
83
+ metadata?: Record<string, unknown>;
84
+ }
85
+ export interface InsertConfig {
86
+ key?: string | Array<string | undefined>;
87
+ metadata?: Record<string, unknown>;
88
+ }
89
+ export interface CollectionConfig<T extends object = Record<string, unknown>> {
90
+ id: string;
91
+ sync: SyncConfig<T>;
92
+ schema?: StandardSchema<T>;
93
+ }
94
+ export type ChangesPayload<T extends object = Record<string, unknown>> = Array<ChangeMessage<T>>;
@@ -0,0 +1,3 @@
1
+ export declare function getLockedObjects(): Set<string>;
2
+ export declare function getGlobalVersion(): number;
3
+ export declare function advanceGlobalVersion(): number;
@@ -0,0 +1,17 @@
1
+ function getLockedObjects() {
2
+ return /* @__PURE__ */ new Set();
3
+ }
4
+ let globalVersion = 0;
5
+ function getGlobalVersion() {
6
+ return globalVersion;
7
+ }
8
+ function advanceGlobalVersion() {
9
+ console.log(`==== advancing global version`, globalVersion + 1);
10
+ return globalVersion++;
11
+ }
12
+ export {
13
+ advanceGlobalVersion,
14
+ getGlobalVersion,
15
+ getLockedObjects
16
+ };
17
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["export function getLockedObjects(): Set<string> {\n // Stub implementation that returns an empty Set\n return new Set()\n}\n\nlet globalVersion = 0\n\nexport function getGlobalVersion(): number {\n return globalVersion\n}\n\nexport function advanceGlobalVersion(): number {\n console.log(`==== advancing global version`, globalVersion + 1)\n return globalVersion++\n}\n"],"names":[],"mappings":"AAAO,SAAS,mBAAgC;AAE9C,6BAAW,IAAI;AACjB;AAEA,IAAI,gBAAgB;AAEb,SAAS,mBAA2B;AAClC,SAAA;AACT;AAEO,SAAS,uBAA+B;AACrC,UAAA,IAAI,iCAAiC,gBAAgB,CAAC;AACvD,SAAA;AACT;"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@tanstack/db",
3
+ "description": "A reactive client store for building super fast apps on sync",
4
+ "version": "0.0.1",
5
+ "dependencies": {
6
+ "@electric-sql/d2ts": "^0.1.5",
7
+ "@standard-schema/spec": "^1.0.0",
8
+ "@tanstack/store": "^0.7.0"
9
+ },
10
+ "devDependencies": {
11
+ "@vitest/coverage-istanbul": "^3.0.9"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "import": {
16
+ "types": "./dist/esm/index.d.ts",
17
+ "default": "./dist/esm/index.js"
18
+ },
19
+ "require": {
20
+ "types": "./dist/cjs/index.d.cts",
21
+ "default": "./dist/cjs/index.cjs"
22
+ }
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "src"
29
+ ],
30
+ "main": "dist/cjs/index.cjs",
31
+ "module": "dist/esm/index.js",
32
+ "packageManager": "pnpm@10.5.2",
33
+ "peerDependencies": {
34
+ "typescript": ">=4.7"
35
+ },
36
+ "author": "Kyle Mathews",
37
+ "license": "MIT",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/TanStack/db.git",
41
+ "directory": "packages/db"
42
+ },
43
+ "homepage": "https://tanstack.com/optimistic",
44
+ "keywords": [
45
+ "optimistic",
46
+ "typescript"
47
+ ],
48
+ "scripts": {
49
+ "build": "vite build",
50
+ "dev": "vite build --watch",
51
+ "lint": "eslint . --fix",
52
+ "test": "npx vitest --run"
53
+ },
54
+ "sideEffects": false,
55
+ "type": "module",
56
+ "types": "dist/esm/index.d.ts"
57
+ }
@@ -0,0 +1,163 @@
1
+ /**
2
+ * A Map implementation that keeps its entries sorted based on a comparator function
3
+ * @template TKey - The type of keys in the map
4
+ * @template TValue - The type of values in the map
5
+ */
6
+ export class SortedMap<TKey, TValue> {
7
+ private map: Map<TKey, TValue>
8
+ private sortedKeys: Array<TKey>
9
+ private comparator: (a: TValue, b: TValue) => number
10
+
11
+ /**
12
+ * Creates a new SortedMap instance
13
+ *
14
+ * @param comparator - Optional function to compare values for sorting
15
+ */
16
+ constructor(comparator?: (a: TValue, b: TValue) => number) {
17
+ this.map = new Map<TKey, TValue>()
18
+ this.sortedKeys = []
19
+ this.comparator = comparator || this.defaultComparator
20
+ }
21
+
22
+ /**
23
+ * Default comparator function used when none is provided
24
+ *
25
+ * @param a - First value to compare
26
+ * @param b - Second value to compare
27
+ * @returns -1 if a < b, 1 if a > b, 0 if equal
28
+ */
29
+ private defaultComparator(a: TValue, b: TValue): number {
30
+ if (a < b) return -1
31
+ if (a > b) return 1
32
+ return 0
33
+ }
34
+
35
+ /**
36
+ * Sets a key-value pair in the map and maintains sort order
37
+ *
38
+ * @param key - The key to set
39
+ * @param value - The value to associate with the key
40
+ * @returns This SortedMap instance for chaining
41
+ */
42
+ set(key: TKey, value: TValue): this {
43
+ this.map.set(key, value)
44
+
45
+ if (!this.sortedKeys.includes(key)) {
46
+ this.sortedKeys.push(key)
47
+ }
48
+
49
+ // Re-sort keys based on values
50
+ this.sortedKeys.sort((a, b) => {
51
+ const valueA = this.map.get(a)!
52
+ const valueB = this.map.get(b)!
53
+ return this.comparator(valueA, valueB)
54
+ })
55
+
56
+ return this
57
+ }
58
+
59
+ /**
60
+ * Gets a value by its key
61
+ *
62
+ * @param key - The key to look up
63
+ * @returns The value associated with the key, or undefined if not found
64
+ */
65
+ get(key: TKey): TValue | undefined {
66
+ return this.map.get(key)
67
+ }
68
+
69
+ /**
70
+ * Removes a key-value pair from the map
71
+ *
72
+ * @param key - The key to remove
73
+ * @returns True if the key was found and removed, false otherwise
74
+ */
75
+ delete(key: TKey): boolean {
76
+ if (this.map.delete(key)) {
77
+ const index = this.sortedKeys.indexOf(key)
78
+ this.sortedKeys.splice(index, 1)
79
+ return true
80
+ }
81
+ return false
82
+ }
83
+
84
+ /**
85
+ * Checks if a key exists in the map
86
+ *
87
+ * @param key - The key to check
88
+ * @returns True if the key exists, false otherwise
89
+ */
90
+ has(key: TKey): boolean {
91
+ return this.map.has(key)
92
+ }
93
+
94
+ /**
95
+ * Removes all key-value pairs from the map
96
+ */
97
+ clear(): void {
98
+ this.map.clear()
99
+ this.sortedKeys = []
100
+ }
101
+
102
+ /**
103
+ * Gets the number of key-value pairs in the map
104
+ */
105
+ get size(): number {
106
+ return this.map.size
107
+ }
108
+
109
+ /**
110
+ * Default iterator that returns entries in sorted order
111
+ *
112
+ * @returns An iterator for the map's entries
113
+ */
114
+ *[Symbol.iterator](): IterableIterator<[TKey, TValue]> {
115
+ for (const key of this.sortedKeys) {
116
+ yield [key, this.map.get(key)!] as [TKey, TValue]
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Returns an iterator for the map's entries in sorted order
122
+ *
123
+ * @returns An iterator for the map's entries
124
+ */
125
+ entries(): IterableIterator<[TKey, TValue]> {
126
+ return this[Symbol.iterator]()
127
+ }
128
+
129
+ /**
130
+ * Returns an iterator for the map's keys in sorted order
131
+ *
132
+ * @returns An iterator for the map's keys
133
+ */
134
+ keys(): IterableIterator<TKey> {
135
+ return this.sortedKeys[Symbol.iterator]()
136
+ }
137
+
138
+ /**
139
+ * Returns an iterator for the map's values in sorted order
140
+ *
141
+ * @returns An iterator for the map's values
142
+ */
143
+ values(): IterableIterator<TValue> {
144
+ return function* (this: SortedMap<TKey, TValue>) {
145
+ for (const key of this.sortedKeys) {
146
+ yield this.map.get(key)!
147
+ }
148
+ }.call(this)
149
+ }
150
+
151
+ /**
152
+ * Executes a callback function for each key-value pair in the map in sorted order
153
+ *
154
+ * @param callbackfn - Function to execute for each entry
155
+ */
156
+ forEach(
157
+ callbackfn: (value: TValue, key: TKey, map: Map<TKey, TValue>) => void
158
+ ): void {
159
+ for (const key of this.sortedKeys) {
160
+ callbackfn(this.map.get(key)!, key, this.map)
161
+ }
162
+ }
163
+ }