@livestore/livestore 0.0.12 → 0.0.15

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 (173) hide show
  1. package/README.md +7 -7
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/QueryCache.d.ts +20 -0
  4. package/dist/QueryCache.d.ts.map +1 -0
  5. package/dist/QueryCache.js +71 -0
  6. package/dist/QueryCache.js.map +1 -0
  7. package/dist/__tests__/react/fixture.d.ts +25 -0
  8. package/dist/__tests__/react/fixture.d.ts.map +1 -0
  9. package/dist/__tests__/react/fixture.js +60 -0
  10. package/dist/__tests__/react/fixture.js.map +1 -0
  11. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts +2 -0
  12. package/dist/__tests__/react/useLiveStoreComponent.test.d.ts.map +1 -0
  13. package/dist/__tests__/react/useLiveStoreComponent.test.js +78 -0
  14. package/dist/__tests__/react/useLiveStoreComponent.test.js.map +1 -0
  15. package/dist/__tests__/reactive.test.d.ts +2 -0
  16. package/dist/__tests__/reactive.test.d.ts.map +1 -0
  17. package/dist/__tests__/reactive.test.js +197 -0
  18. package/dist/__tests__/reactive.test.js.map +1 -0
  19. package/dist/bounded-collections.d.ts +34 -0
  20. package/dist/bounded-collections.d.ts.map +1 -0
  21. package/dist/bounded-collections.js +103 -0
  22. package/dist/bounded-collections.js.map +1 -0
  23. package/dist/componentKey.d.ts +20 -0
  24. package/dist/componentKey.d.ts.map +1 -0
  25. package/dist/componentKey.js +3 -0
  26. package/dist/componentKey.js.map +1 -0
  27. package/dist/effect/LiveStore.d.ts +36 -0
  28. package/dist/effect/LiveStore.d.ts.map +1 -0
  29. package/dist/effect/LiveStore.js +41 -0
  30. package/dist/effect/LiveStore.js.map +1 -0
  31. package/dist/effect/index.d.ts +2 -0
  32. package/dist/effect/index.d.ts.map +1 -0
  33. package/dist/effect/index.js +2 -0
  34. package/dist/effect/index.js.map +1 -0
  35. package/dist/events.d.ts +7 -0
  36. package/dist/events.d.ts.map +1 -0
  37. package/dist/events.js +2 -0
  38. package/dist/events.js.map +1 -0
  39. package/dist/inMemoryDatabase.d.ts +56 -0
  40. package/dist/inMemoryDatabase.d.ts.map +1 -0
  41. package/dist/inMemoryDatabase.js +223 -0
  42. package/dist/inMemoryDatabase.js.map +1 -0
  43. package/dist/index.d.ts +20 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +9 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/migrations.d.ts +16 -0
  48. package/dist/migrations.d.ts.map +1 -0
  49. package/dist/migrations.js +67 -0
  50. package/dist/migrations.js.map +1 -0
  51. package/dist/otel.d.ts +4 -0
  52. package/dist/otel.d.ts.map +1 -0
  53. package/dist/otel.js +6 -0
  54. package/dist/otel.js.map +1 -0
  55. package/dist/react/LiveStoreContext.d.ts +11 -0
  56. package/dist/react/LiveStoreContext.d.ts.map +1 -0
  57. package/dist/react/LiveStoreContext.js +10 -0
  58. package/dist/react/LiveStoreContext.js.map +1 -0
  59. package/dist/react/LiveStoreProvider.d.ts +20 -0
  60. package/dist/react/LiveStoreProvider.d.ts.map +1 -0
  61. package/dist/react/LiveStoreProvider.js +52 -0
  62. package/dist/react/LiveStoreProvider.js.map +1 -0
  63. package/dist/react/index.d.ts +8 -0
  64. package/dist/react/index.d.ts.map +1 -0
  65. package/dist/react/index.js +6 -0
  66. package/dist/react/index.js.map +1 -0
  67. package/dist/react/useGraphQL.d.ts +13 -0
  68. package/dist/react/useGraphQL.d.ts.map +1 -0
  69. package/dist/react/useGraphQL.js +85 -0
  70. package/dist/react/useGraphQL.js.map +1 -0
  71. package/dist/react/useLiveStoreComponent.d.ts +75 -0
  72. package/dist/react/useLiveStoreComponent.d.ts.map +1 -0
  73. package/dist/react/useLiveStoreComponent.js +317 -0
  74. package/dist/react/useLiveStoreComponent.js.map +1 -0
  75. package/dist/react/useQuery.d.ts +3 -0
  76. package/dist/react/useQuery.d.ts.map +1 -0
  77. package/dist/react/useQuery.js +38 -0
  78. package/dist/react/useQuery.js.map +1 -0
  79. package/dist/react/utils/useStateRefWithReactiveInput.d.ts +13 -0
  80. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +1 -0
  81. package/dist/react/utils/useStateRefWithReactiveInput.js +38 -0
  82. package/dist/react/utils/useStateRefWithReactiveInput.js.map +1 -0
  83. package/dist/reactive.d.ts +140 -0
  84. package/dist/reactive.d.ts.map +1 -0
  85. package/dist/reactive.js +302 -0
  86. package/dist/reactive.js.map +1 -0
  87. package/dist/reactiveQueries/base-class.d.ts +27 -0
  88. package/dist/reactiveQueries/base-class.d.ts.map +1 -0
  89. package/dist/reactiveQueries/base-class.js +23 -0
  90. package/dist/reactiveQueries/base-class.js.map +1 -0
  91. package/dist/reactiveQueries/graphql.d.ts +25 -0
  92. package/dist/reactiveQueries/graphql.d.ts.map +1 -0
  93. package/dist/reactiveQueries/graphql.js +18 -0
  94. package/dist/reactiveQueries/graphql.js.map +1 -0
  95. package/dist/reactiveQueries/js.d.ts +19 -0
  96. package/dist/reactiveQueries/js.d.ts.map +1 -0
  97. package/dist/reactiveQueries/js.js +13 -0
  98. package/dist/reactiveQueries/js.js.map +1 -0
  99. package/dist/reactiveQueries/sql.d.ts +31 -0
  100. package/dist/reactiveQueries/sql.d.ts.map +1 -0
  101. package/dist/reactiveQueries/sql.js +32 -0
  102. package/dist/reactiveQueries/sql.js.map +1 -0
  103. package/dist/schema.d.ts +81 -0
  104. package/dist/schema.d.ts.map +1 -0
  105. package/dist/schema.js +46 -0
  106. package/dist/schema.js.map +1 -0
  107. package/dist/storage/in-memory/index.d.ts +15 -0
  108. package/dist/storage/in-memory/index.d.ts.map +1 -0
  109. package/dist/storage/in-memory/index.js +14 -0
  110. package/dist/storage/in-memory/index.js.map +1 -0
  111. package/dist/storage/index.d.ts +14 -0
  112. package/dist/storage/index.d.ts.map +1 -0
  113. package/dist/storage/index.js +9 -0
  114. package/dist/storage/index.js.map +1 -0
  115. package/dist/storage/tauri/index.d.ts +19 -0
  116. package/dist/storage/tauri/index.d.ts.map +1 -0
  117. package/dist/storage/tauri/index.js +38 -0
  118. package/dist/storage/tauri/index.js.map +1 -0
  119. package/dist/storage/utils/idb.d.ts +10 -0
  120. package/dist/storage/utils/idb.d.ts.map +1 -0
  121. package/dist/storage/utils/idb.js +58 -0
  122. package/dist/storage/utils/idb.js.map +1 -0
  123. package/dist/storage/web-worker/index.d.ts +27 -0
  124. package/dist/storage/web-worker/index.d.ts.map +1 -0
  125. package/dist/storage/web-worker/index.js +74 -0
  126. package/dist/storage/web-worker/index.js.map +1 -0
  127. package/dist/storage/web-worker/worker.d.ts +13 -0
  128. package/dist/storage/web-worker/worker.d.ts.map +1 -0
  129. package/dist/storage/web-worker/worker.js +110 -0
  130. package/dist/storage/web-worker/worker.js.map +1 -0
  131. package/dist/store.d.ts +199 -0
  132. package/dist/store.d.ts.map +1 -0
  133. package/dist/store.js +603 -0
  134. package/dist/store.js.map +1 -0
  135. package/dist/util.d.ts +28 -0
  136. package/dist/util.d.ts.map +1 -0
  137. package/dist/util.js +55 -0
  138. package/dist/util.js.map +1 -0
  139. package/package.json +46 -19
  140. package/src/__tests__/react/fixture.tsx +23 -32
  141. package/src/__tests__/reactive.test.ts +3 -4
  142. package/src/effect/LiveStore.ts +22 -31
  143. package/src/events.ts +1 -1
  144. package/src/inMemoryDatabase.ts +115 -140
  145. package/src/index.ts +20 -20
  146. package/src/migrations.ts +119 -0
  147. package/src/otel.ts +0 -11
  148. package/src/react/LiveStoreProvider.tsx +24 -23
  149. package/src/react/index.ts +10 -1
  150. package/src/react/useGraphQL.ts +28 -2
  151. package/src/react/useLiveStoreComponent.ts +134 -50
  152. package/src/react/useQuery.ts +56 -0
  153. package/src/reactive.ts +6 -4
  154. package/src/reactiveQueries/base-class.ts +9 -3
  155. package/src/reactiveQueries/graphql.ts +4 -4
  156. package/src/reactiveQueries/js.ts +2 -2
  157. package/src/reactiveQueries/sql.ts +6 -6
  158. package/src/schema.ts +69 -145
  159. package/src/storage/in-memory/index.ts +21 -0
  160. package/src/storage/index.ts +27 -0
  161. package/src/{backends/tauri.ts → storage/tauri/index.ts} +14 -28
  162. package/src/storage/web-worker/index.ts +116 -0
  163. package/src/{backends/web-worker.ts → storage/web-worker/worker.ts} +17 -52
  164. package/src/store.ts +171 -98
  165. package/src/util.ts +13 -3
  166. package/tsconfig.json +1 -3
  167. package/src/backends/base.ts +0 -67
  168. package/src/backends/index.ts +0 -98
  169. package/src/backends/noop.ts +0 -32
  170. package/src/backends/web-in-memory.ts +0 -65
  171. package/src/backends/web.ts +0 -97
  172. package/src/react/useGlobalQuery.ts +0 -40
  173. /package/src/{backends → storage}/utils/idb.ts +0 -0
package/dist/util.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /// <reference lib="es2022" />
2
+ import type { Brand } from '@livestore/utils/effect';
3
+ export type ParamsObject = Record<string, SqlValue>;
4
+ export type SqlValue = string | number | Uint8Array | null;
5
+ export type Bindable = SqlValue[] | ParamsObject;
6
+ export type PreparedBindValues = Brand.Branded<Bindable, 'PreparedBindValues'>;
7
+ /**
8
+ * This is a tag function for tagged literals.
9
+ * it lets us get syntax highlighting on SQL queries in VSCode, but
10
+ * doesn't do anything at runtime.
11
+ * Code copied from: https://esdiscuss.org/topic/string-identity-template-tag
12
+ */
13
+ export declare const sql: (template: TemplateStringsArray, ...args: unknown[]) => string;
14
+ /** Prepare bind values to send to SQLite
15
+ /* Add $ to the beginning of keys; which we use as our interpolation syntax
16
+ /* We also strip out any params that aren't used in the statement,
17
+ /* because rusqlite doesn't allow unused named params
18
+ /* TODO: Search for unused params via proper parsing, not string search
19
+ **/
20
+ export declare const prepareBindValues: (values: Bindable, statement: string) => PreparedBindValues;
21
+ /**
22
+ * Use this to make assertion at end of if-else chain that all members of a
23
+ * union have been accounted for.
24
+ */
25
+ export declare function casesHandled(x: never): never;
26
+ export declare const objectToString: (error: any) => string;
27
+ export declare const isPromise: (value: any) => value is Promise<unknown>;
28
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AAEpD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;AACnD,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,IAAI,CAAA;AAE1D,MAAM,MAAM,QAAQ,GAAG,QAAQ,EAAE,GAAG,YAAY,CAAA;AAEhD,MAAM,MAAM,kBAAkB,GAAG,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAA;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,GAAG,aAAc,oBAAoB,WAAW,OAAO,EAAE,KAAG,MAMxE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,WAAY,QAAQ,aAAa,MAAM,KAAG,kBAWvE,CAAA;AAED;;;GAGG;AAEH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,GAAG,KAAK,CAE5C;AAED,eAAO,MAAM,cAAc,UAAW,GAAG,KAAG,MAa3C,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,GAAG,8BAAiE,CAAA"}
package/dist/util.js ADDED
@@ -0,0 +1,55 @@
1
+ /// <reference lib="es2022" />
2
+ /**
3
+ * This is a tag function for tagged literals.
4
+ * it lets us get syntax highlighting on SQL queries in VSCode, but
5
+ * doesn't do anything at runtime.
6
+ * Code copied from: https://esdiscuss.org/topic/string-identity-template-tag
7
+ */
8
+ export const sql = (template, ...args) => {
9
+ let str = '';
10
+ for (const [i, arg] of args.entries()) {
11
+ str += template[i] + String(arg);
12
+ }
13
+ return str + template.at(-1);
14
+ };
15
+ /** Prepare bind values to send to SQLite
16
+ /* Add $ to the beginning of keys; which we use as our interpolation syntax
17
+ /* We also strip out any params that aren't used in the statement,
18
+ /* because rusqlite doesn't allow unused named params
19
+ /* TODO: Search for unused params via proper parsing, not string search
20
+ **/
21
+ export const prepareBindValues = (values, statement) => {
22
+ if (Array.isArray(values))
23
+ return values;
24
+ const result = {};
25
+ for (const [key, value] of Object.entries(values)) {
26
+ if (statement.includes(key)) {
27
+ result[`$${key}`] = value;
28
+ }
29
+ }
30
+ return result;
31
+ };
32
+ /**
33
+ * Use this to make assertion at end of if-else chain that all members of a
34
+ * union have been accounted for.
35
+ */
36
+ /* eslint-disable-next-line prefer-arrow/prefer-arrow-functions */
37
+ export function casesHandled(x) {
38
+ throw new Error(`A case was not handled for value: ${objectToString(x)}`);
39
+ }
40
+ export const objectToString = (error) => {
41
+ const stack = typeof process !== 'undefined' && process.env.CL_DEBUG ? error.stack : undefined;
42
+ const str = error.toString();
43
+ const stackStr = stack ? `\n${stack}` : '';
44
+ if (str !== '[object Object]')
45
+ return str + stackStr;
46
+ try {
47
+ return JSON.stringify({ ...error, stack }, null, 2);
48
+ }
49
+ catch (e) {
50
+ console.log(error);
51
+ return 'Error while printing error: ' + e;
52
+ }
53
+ };
54
+ export const isPromise = (value) => typeof value?.then === 'function';
55
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAW9B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,QAA8B,EAAE,GAAG,IAAe,EAAU,EAAE;IAChF,IAAI,GAAG,GAAG,EAAE,CAAA;IACZ,KAAK,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE;QACrC,GAAG,IAAI,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;KACjC;IACD,OAAO,GAAG,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;AAC9B,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,MAAgB,EAAE,SAAiB,EAAsB,EAAE;IAC3F,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,MAA4B,CAAA;IAE9D,MAAM,MAAM,GAAiB,EAAE,CAAA;IAC/B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjD,IAAI,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YAC3B,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC,GAAG,KAAK,CAAA;SAC1B;KACF;IAED,OAAO,MAA4B,CAAA;AACrC,CAAC,CAAA;AAED;;;GAGG;AACH,kEAAkE;AAClE,MAAM,UAAU,YAAY,CAAC,CAAQ;IACnC,MAAM,IAAI,KAAK,CAAC,qCAAqC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;AAC3E,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAU,EAAU,EAAE;IACnD,MAAM,KAAK,GAAG,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAC9F,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;IAC5B,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1C,IAAI,GAAG,KAAK,iBAAiB;QAAE,OAAO,GAAG,GAAG,QAAQ,CAAA;IAEpD,IAAI;QACF,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;KACpD;IAAC,OAAO,CAAM,EAAE;QACf,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAElB,OAAO,8BAA8B,GAAG,CAAC,CAAA;KAC1C;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAU,EAA6B,EAAE,CAAC,OAAO,KAAK,EAAE,IAAI,KAAK,UAAU,CAAA"}
package/package.json CHANGED
@@ -1,15 +1,36 @@
1
1
  {
2
2
  "name": "@livestore/livestore",
3
- "version": "0.0.12",
3
+ "version": "0.0.15",
4
4
  "type": "module",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
5
  "exports": {
9
- ".": "./dist/index.js",
10
- "./react": "./dist/react/index.js",
11
- "./effect": "./dist/effect/index.js",
12
- "./util": "./dist/util.js"
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "default": "./dist/index.js"
9
+ },
10
+ "./storage/web-worker": {
11
+ "types": "./dist/storage/web-worker/index.d.ts",
12
+ "default": "./dist/storage/web-worker/index.js"
13
+ },
14
+ "./storage/in-memory": {
15
+ "types": "./dist/storage/in-memory/index.d.ts",
16
+ "default": "./dist/storage/in-memory/index.js"
17
+ },
18
+ "./storage/tauri": {
19
+ "types": "./dist/storage/tauri/index.d.ts",
20
+ "default": "./dist/storage/tauri/index.js"
21
+ },
22
+ "./react": {
23
+ "types": "./dist/react/index.d.ts",
24
+ "default": "./dist/react/index.js"
25
+ },
26
+ "./effect": {
27
+ "types": "./dist/effect/index.d.ts",
28
+ "default": "./dist/effect/index.js"
29
+ },
30
+ "./util": {
31
+ "types": "./dist/util.d.ts",
32
+ "default": "./dist/util.js"
33
+ }
13
34
  },
14
35
  "types": "./dist/index.d.ts",
15
36
  "typesVersions": {
@@ -34,11 +55,26 @@
34
55
  "@livestore/utils": "workspace:*",
35
56
  "@opentelemetry/api": "^1.6.0",
36
57
  "comlink": "^4.4.1",
58
+ "effect-db-schema": "workspace:*",
37
59
  "graphql": "^16.8.1",
38
60
  "lodash-es": "^4.17.21",
39
61
  "sqlite-esm": "3.42.0-build6",
40
62
  "uuid": "^9.0.1"
41
63
  },
64
+ "devDependencies": {
65
+ "@tauri-apps/api": "^1.5.0",
66
+ "@testing-library/react": "^14.0.0",
67
+ "@types/lodash-es": "^4.17.9",
68
+ "@types/react": "^18.2.28",
69
+ "@types/react-dom": "^18.2.13",
70
+ "@types/uuid": "^9.0.5",
71
+ "jsdom": "^22.1.0",
72
+ "react": "^18.2.0",
73
+ "react-dom": "^18.2.0",
74
+ "typescript": "5.2.2",
75
+ "vite": "4.4.11",
76
+ "vitest": "^0.34.6"
77
+ },
42
78
  "peerDependencies": {
43
79
  "@tauri-apps/api": "^1.4.0",
44
80
  "react": "^18",
@@ -49,16 +85,7 @@
49
85
  "optional": true
50
86
  }
51
87
  },
52
- "devDependencies": {
53
- "@tauri-apps/api": "^1.4.0",
54
- "@testing-library/react": "^14.0.0",
55
- "@types/lodash-es": "^4.17.9",
56
- "@types/uuid": "^9.0.4",
57
- "jsdom": "^22.1.0",
58
- "react": "^18.2.0",
59
- "react-dom": "^18.2.0",
60
- "typescript": "5.2.2",
61
- "vite": "4.4.9",
62
- "vitest": "^0.34.6"
88
+ "publishConfig": {
89
+ "access": "public"
63
90
  }
64
91
  }
@@ -1,9 +1,10 @@
1
- import { mapObjectValues } from '@livestore/utils'
2
1
  import React from 'react'
2
+ import initSqlite3Wasm from 'sqlite-esm'
3
3
 
4
4
  import * as LiveStore from '../../index.js'
5
5
  import { sql } from '../../index.js'
6
6
  import * as LiveStoreReact from '../../react/index.js'
7
+ import { InMemoryStorage } from '../../storage/in-memory/index.js'
7
8
 
8
9
  export type Todo = {
9
10
  id: string
@@ -25,24 +26,19 @@ export const globalQueryDefs = {
25
26
  appState,
26
27
  }
27
28
 
28
- export const schema = LiveStore.defineSchema({
29
+ export const schema = LiveStore.makeSchema({
29
30
  tables: {
30
- todos: {
31
- columns: {
32
- id: { type: 'text', primaryKey: true },
33
- text: { type: 'text', default: '', nullable: false },
34
- completed: { type: 'boolean', default: false, nullable: false },
35
- },
36
- },
37
- app: {
38
- columns: {
39
- id: { type: 'text', primaryKey: true },
40
- newTodoText: { type: 'text', default: '', nullable: true },
41
- filter: { type: 'text', default: 'all', nullable: false },
42
- },
43
- },
31
+ todos: LiveStore.DbSchema.table('todos', {
32
+ id: LiveStore.DbSchema.text({ primaryKey: true }),
33
+ text: LiveStore.DbSchema.text({ default: '', nullable: false }),
34
+ completed: LiveStore.DbSchema.boolean({ default: false, nullable: false }),
35
+ }),
36
+ app: LiveStore.DbSchema.table('app', {
37
+ id: LiveStore.DbSchema.text({ primaryKey: true }),
38
+ newTodoText: LiveStore.DbSchema.text({ default: '', nullable: true }),
39
+ filter: LiveStore.DbSchema.text({ default: 'all', nullable: false }),
40
+ }),
44
41
  },
45
- materializedViews: {},
46
42
  actions: {
47
43
  // TODO: fix these actions to make them have write annotatinos
48
44
  addTodo: {
@@ -63,28 +59,23 @@ export const schema = LiveStore.defineSchema({
63
59
  })
64
60
 
65
61
  export const makeTodoMvc = async () => {
66
- type UserInfoComponentState = { username: string }
62
+ const AppSchema = LiveStore.defineComponentStateSchema('UserInfo', {
63
+ username: LiveStore.DbSchema.text({ default: '' }),
64
+ })
67
65
 
68
- const AppSchema = LiveStore.defineComponentStateSchema<UserInfoComponentState>({
69
- componentType: 'UserInfo',
70
- columns: {
71
- username: { type: 'text', default: '' },
72
- },
66
+ const sqlite3 = await initSqlite3Wasm({
67
+ print: (message) => console.log(`[livestore sqlite] ${message}`),
68
+ printErr: (message) => console.error(`[livestore sqlite] ${message}`),
73
69
  })
74
70
 
75
71
  const store = await LiveStore.createStore({
76
72
  schema,
77
- backendOptions: { type: 'web-in-memory' },
78
- boot: async (backend) => {
79
- backend.execute(sql`INSERT INTO app (newTodoText, filter) VALUES ('', 'all');`)
80
- // NOTE we can't insert into components__UserInfo yet because the table doesn't exist yet
81
- // backend.execute(sql`INSERT INTO components__UserInfo (id, username) VALUES ('u1', 'username_u1');`)
82
- // backend.execute(sql`INSERT INTO components__UserInfo (id, username) VALUES ('u2', 'username_u2');`)
83
- },
73
+ loadStorage: () => InMemoryStorage.load(),
74
+ boot: (db) => db.execute(sql`INSERT OR IGNORE INTO app (id, newTodoText, filter) VALUES ('static', '', 'all');`),
75
+ sqlite3,
84
76
  })
85
77
 
86
- const globalQueries = mapObjectValues(globalQueryDefs, (_, queryDef) => queryDef(store))
87
- const storeContext: LiveStore.LiveStoreContext = { store, globalQueries }
78
+ const storeContext: LiveStore.LiveStoreContext = { store }
88
79
 
89
80
  const wrapper = ({ children }: any) => (
90
81
  <LiveStoreReact.LiveStoreContext.Provider value={storeContext}>{children}</LiveStoreReact.LiveStoreContext.Provider>
@@ -226,8 +226,7 @@ describe('a diamond shaped graph', () => {
226
226
  })
227
227
  })
228
228
 
229
- // TODO handle `undefined` in the graph
230
- describe.todo('a trivial graph with undefined', () => {
229
+ describe('a trivial graph with undefined', () => {
231
230
  const makeGraph = () => {
232
231
  const graph = new ReactiveGraph({ otelTracer: makeNoopTracer() })
233
232
  const a = graph.makeRef(undefined)
@@ -260,7 +259,7 @@ describe.todo('a trivial graph with undefined', () => {
260
259
 
261
260
  it('has the right initial values', () => {
262
261
  const { c, e } = makeGraph()
263
- expect(c.result).toBe(3)
264
- expect(e.result).toBe(6)
262
+ expect(c.result).toBe(2)
263
+ expect(e.result).toBe(5)
265
264
  })
266
265
  })
@@ -2,31 +2,35 @@ import type { Scope } from '@livestore/utils/effect'
2
2
  import { Context, Deferred, Duration, Effect, Layer, OtelTracer, pipe, Runtime } from '@livestore/utils/effect'
3
3
  import * as otel from '@opentelemetry/api'
4
4
  import type { GraphQLSchema } from 'graphql'
5
- import { mapValues } from 'lodash-es'
5
+ import initSqlite3Wasm from 'sqlite-esm'
6
6
 
7
- import type { Backend, BackendOptions } from '../backends/index.js'
8
7
  import type { InMemoryDatabase } from '../inMemoryDatabase.js'
9
8
  import type { Schema } from '../schema.js'
9
+ import type { StorageInit } from '../storage/index.js'
10
10
  import type { BaseGraphQLContext, GraphQLOptions, LiveStoreQuery, Store } from '../store.js'
11
11
  import { createStore } from '../store.js'
12
12
 
13
+ // NOTE we're starting to initialize the sqlite wasm binary here (already before calling `createStore`),
14
+ // so that it's ready when we need it
15
+ const sqlite3Promise = initSqlite3Wasm({
16
+ print: (message) => console.log(`[livestore sqlite] ${message}`),
17
+ printErr: (message) => console.error(`[livestore sqlite] ${message}`),
18
+ })
19
+
13
20
  // TODO get rid of `LiveStoreContext` wrapper and only expose the `Store` directly
14
21
  export type LiveStoreContext = {
15
- store: Store<any>
16
- globalQueries: LiveStoreQueryTypes
22
+ store: Store
17
23
  }
18
24
 
19
- export type QueryDefinition = (store: Store<any>) => LiveStoreQuery
20
- export type GlobalQueryDefs = { [key: string]: QueryDefinition }
25
+ export type QueryDefinition = (store: Store) => LiveStoreQuery
21
26
 
22
27
  export type LiveStoreCreateStoreOptions<GraphQLContext extends BaseGraphQLContext> = {
23
28
  schema: Schema
24
- globalQueryDefs: GlobalQueryDefs
25
- backendOptions: BackendOptions
29
+ loadStorage: () => StorageInit | Promise<StorageInit>
26
30
  graphQLOptions?: GraphQLOptions<GraphQLContext>
27
31
  otelTracer?: otel.Tracer
28
32
  otelRootSpanContext?: otel.Context
29
- boot?: (backend: Backend, parentSpan: otel.Span) => Promise<void>
33
+ boot?: (db: InMemoryDatabase, parentSpan: otel.Span) => unknown | Promise<unknown>
30
34
  }
31
35
 
32
36
  export const LiveStoreContext = Context.Tag<LiveStoreContext>('@livestore/livestore/LiveStoreContext')
@@ -40,13 +44,12 @@ export const DeferredStoreContext = Context.Tag<DeferredStoreContext>(
40
44
 
41
45
  export type LiveStoreContextProps<GraphQLContext extends BaseGraphQLContext> = {
42
46
  schema: Schema
43
- globalQueryDefs?: Effect.Effect<never, never, GlobalQueryDefs>
44
- backendOptions: Effect.Effect<never, never, BackendOptions>
47
+ loadStorage: () => StorageInit | Promise<StorageInit>
45
48
  graphQLOptions?: {
46
49
  schema: Effect.Effect<otel.Tracer, never, GraphQLSchema>
47
50
  makeContext: (db: InMemoryDatabase) => GraphQLContext
48
51
  }
49
- boot?: (backend: Backend) => Effect.Effect<never, never, void>
52
+ boot?: (db: InMemoryDatabase) => Effect.Effect<never, never, void>
50
53
  }
51
54
 
52
55
  export const LiveStoreContextLayer = <GraphQLContext extends BaseGraphQLContext>(
@@ -60,9 +63,8 @@ export const LiveStoreContextLayer = <GraphQLContext extends BaseGraphQLContext>
60
63
  export const LiveStoreContextDeferred = Layer.effect(DeferredStoreContext, Deferred.make<never, LiveStoreContext>())
61
64
 
62
65
  export const makeLiveStoreContext = <GraphQLContext extends BaseGraphQLContext>({
63
- globalQueryDefs,
64
66
  schema,
65
- backendOptions: backendOptions_,
67
+ loadStorage,
66
68
  graphQLOptions: graphQLOptions_,
67
69
  boot: boot_,
68
70
  }: LiveStoreContextProps<GraphQLContext>): Effect.Effect<
@@ -84,22 +86,23 @@ export const makeLiveStoreContext = <GraphQLContext extends BaseGraphQLContext>(
84
86
  : Effect.succeed(undefined),
85
87
  )
86
88
 
87
- const backendOptions = yield* $(backendOptions_)
88
-
89
89
  const boot = boot_
90
- ? (db: Backend) =>
90
+ ? (db: InMemoryDatabase) =>
91
91
  boot_(db).pipe(Effect.withSpan('boot'), Effect.tapCauseLogPretty, Runtime.runPromise(runtime))
92
92
  : undefined
93
93
 
94
+ const sqlite3 = yield* $(Effect.promise(() => sqlite3Promise))
95
+
94
96
  const store = yield* $(
95
97
  Effect.tryPromise(() =>
96
98
  createStore({
97
99
  schema,
98
- backendOptions,
100
+ loadStorage,
99
101
  graphQLOptions,
100
102
  otelTracer,
101
103
  otelRootSpanContext,
102
104
  boot,
105
+ sqlite3,
103
106
  }),
104
107
  ),
105
108
  Effect.acquireRelease((store) => Effect.sync(() => store.destroy())),
@@ -107,19 +110,7 @@ export const makeLiveStoreContext = <GraphQLContext extends BaseGraphQLContext>(
107
110
 
108
111
  window.__debugLiveStore = store
109
112
 
110
- // TODO remove global queries concept
111
- const globalQueries = yield* $(
112
- globalQueryDefs ?? Effect.succeed({} as GlobalQueryDefs),
113
- Effect.map((defs) => mapValues(defs, (queryDef) => queryDef(store))),
114
- Effect.withSpan('LiveStore:makeGlobalQueries', {
115
- parent: OtelTracer.makeExternalSpan(otel.trace.getSpanContext(store.otel.queriesSpanContext)!),
116
- }),
117
- )
118
-
119
- // NOTE give main thread a chance to render
120
- yield* $(Effect.yieldNow())
121
-
122
- return { store, globalQueries }
113
+ return { store }
123
114
  }),
124
115
  Effect.tap((storeCtx) => Effect.flatMap(DeferredStoreContext, (def) => Deferred.succeed(def, storeCtx))),
125
116
  Effect.timeoutFail({
package/src/events.ts CHANGED
@@ -5,4 +5,4 @@ export type LiveStoreEvent = {
5
5
  args?: any
6
6
  }
7
7
 
8
- export const EVENTS_TABLE_NAME = '__events'
8
+ export const EVENTS_TABLE_NAME = '__livestore_events'