@livestore/livestore 0.0.25 → 0.0.28

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 (209) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/QueryCache.d.ts +1 -1
  3. package/dist/QueryCache.d.ts.map +1 -1
  4. package/dist/QueryCache.js +50 -60
  5. package/dist/QueryCache.js.map +1 -1
  6. package/dist/__tests__/react/fixture.d.ts +21 -6
  7. package/dist/__tests__/react/fixture.d.ts.map +1 -1
  8. package/dist/__tests__/react/fixture.js +13 -14
  9. package/dist/__tests__/react/fixture.js.map +1 -1
  10. package/dist/__tests__/react/useQuery.test.js +5 -5
  11. package/dist/__tests__/react/useQuery.test.js.map +1 -1
  12. package/dist/__tests__/react/useRow.test.d.ts +2 -0
  13. package/dist/__tests__/react/useRow.test.d.ts.map +1 -0
  14. package/dist/__tests__/react/{useComponentState.test.js → useRow.test.js} +22 -27
  15. package/dist/__tests__/react/useRow.test.js.map +1 -0
  16. package/dist/__tests__/react/utils/stack-info.test.js +32 -0
  17. package/dist/__tests__/react/utils/stack-info.test.js.map +1 -1
  18. package/dist/__tests__/reactive.test.js +1 -1
  19. package/dist/__tests__/reactive.test.js.map +1 -1
  20. package/dist/__tests__/reactiveQueries/sql.test.js +10 -10
  21. package/dist/__tests__/reactiveQueries/sql.test.js.map +1 -1
  22. package/dist/effect/LiveStore.d.ts +3 -3
  23. package/dist/effect/LiveStore.d.ts.map +1 -1
  24. package/dist/effect/LiveStore.js +1 -1
  25. package/dist/effect/LiveStore.js.map +1 -1
  26. package/dist/global-state.d.ts +19 -0
  27. package/dist/global-state.d.ts.map +1 -0
  28. package/dist/global-state.js +20 -0
  29. package/dist/global-state.js.map +1 -0
  30. package/dist/inMemoryDatabase.d.ts +3 -3
  31. package/dist/inMemoryDatabase.d.ts.map +1 -1
  32. package/dist/inMemoryDatabase.js +14 -7
  33. package/dist/inMemoryDatabase.js.map +1 -1
  34. package/dist/index.d.ts +5 -9
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +4 -5
  37. package/dist/index.js.map +1 -1
  38. package/dist/migrations.d.ts +4 -4
  39. package/dist/migrations.d.ts.map +1 -1
  40. package/dist/migrations.js +34 -28
  41. package/dist/migrations.js.map +1 -1
  42. package/dist/react/LiveStoreContext.js.map +1 -1
  43. package/dist/react/LiveStoreProvider.d.ts +2 -2
  44. package/dist/react/LiveStoreProvider.d.ts.map +1 -1
  45. package/dist/react/LiveStoreProvider.js.map +1 -1
  46. package/dist/react/index.d.ts +1 -2
  47. package/dist/react/index.d.ts.map +1 -1
  48. package/dist/react/index.js +1 -1
  49. package/dist/react/index.js.map +1 -1
  50. package/dist/react/useQuery.d.ts +3 -0
  51. package/dist/react/useQuery.d.ts.map +1 -1
  52. package/dist/react/useQuery.js +5 -4
  53. package/dist/react/useQuery.js.map +1 -1
  54. package/dist/react/useRow.d.ts +36 -0
  55. package/dist/react/useRow.d.ts.map +1 -0
  56. package/dist/react/useRow.js +143 -0
  57. package/dist/react/useRow.js.map +1 -0
  58. package/dist/react/useTemporaryQuery.d.ts +2 -0
  59. package/dist/react/useTemporaryQuery.d.ts.map +1 -1
  60. package/dist/react/useTemporaryQuery.js +28 -11
  61. package/dist/react/useTemporaryQuery.js.map +1 -1
  62. package/dist/react/utils/stack-info.d.ts.map +1 -1
  63. package/dist/react/utils/stack-info.js +3 -2
  64. package/dist/react/utils/stack-info.js.map +1 -1
  65. package/dist/react/utils/useStateRefWithReactiveInput.js.map +1 -1
  66. package/dist/reactive.d.ts +1 -1
  67. package/dist/reactive.d.ts.map +1 -1
  68. package/dist/reactive.js +47 -44
  69. package/dist/reactive.js.map +1 -1
  70. package/dist/reactiveQueries/base-class.d.ts +6 -2
  71. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  72. package/dist/reactiveQueries/base-class.js +10 -12
  73. package/dist/reactiveQueries/base-class.js.map +1 -1
  74. package/dist/reactiveQueries/graphql.d.ts +2 -2
  75. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  76. package/dist/reactiveQueries/graphql.js +56 -50
  77. package/dist/reactiveQueries/graphql.js.map +1 -1
  78. package/dist/reactiveQueries/js.d.ts +1 -2
  79. package/dist/reactiveQueries/js.d.ts.map +1 -1
  80. package/dist/reactiveQueries/js.js +25 -15
  81. package/dist/reactiveQueries/js.js.map +1 -1
  82. package/dist/reactiveQueries/sql.d.ts +3 -3
  83. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  84. package/dist/reactiveQueries/sql.js +39 -34
  85. package/dist/reactiveQueries/sql.js.map +1 -1
  86. package/dist/row-query.d.ts +23 -0
  87. package/dist/row-query.d.ts.map +1 -0
  88. package/dist/row-query.js +78 -0
  89. package/dist/row-query.js.map +1 -0
  90. package/dist/schema/action.d.ts +30 -0
  91. package/dist/schema/action.d.ts.map +1 -0
  92. package/dist/schema/action.js +3 -0
  93. package/dist/schema/action.js.map +1 -0
  94. package/dist/schema/index.d.ts +28 -0
  95. package/dist/schema/index.d.ts.map +1 -0
  96. package/dist/schema/index.js +26 -0
  97. package/dist/schema/index.js.map +1 -0
  98. package/dist/schema/system-tables.d.ts +24 -0
  99. package/dist/schema/system-tables.d.ts.map +1 -0
  100. package/dist/schema/system-tables.js +11 -0
  101. package/dist/schema/system-tables.js.map +1 -0
  102. package/dist/schema/table-def.d.ts +161 -0
  103. package/dist/schema/table-def.d.ts.map +1 -0
  104. package/dist/schema/table-def.js +50 -0
  105. package/dist/schema/table-def.js.map +1 -0
  106. package/dist/storage/in-memory/index.d.ts +1 -1
  107. package/dist/storage/in-memory/index.d.ts.map +1 -1
  108. package/dist/storage/in-memory/index.js +6 -7
  109. package/dist/storage/in-memory/index.js.map +1 -1
  110. package/dist/storage/index.d.ts +1 -1
  111. package/dist/storage/index.d.ts.map +1 -1
  112. package/dist/storage/tauri/index.d.ts +1 -1
  113. package/dist/storage/tauri/index.d.ts.map +1 -1
  114. package/dist/storage/tauri/index.js +25 -23
  115. package/dist/storage/tauri/index.js.map +1 -1
  116. package/dist/storage/utils/idb.js +3 -1
  117. package/dist/storage/utils/idb.js.map +1 -1
  118. package/dist/storage/web-worker/index.d.ts +1 -1
  119. package/dist/storage/web-worker/index.d.ts.map +1 -1
  120. package/dist/storage/web-worker/index.js +38 -34
  121. package/dist/storage/web-worker/index.js.map +1 -1
  122. package/dist/storage/web-worker/worker.d.ts +1 -1
  123. package/dist/storage/web-worker/worker.d.ts.map +1 -1
  124. package/dist/storage/web-worker/worker.js +1 -1
  125. package/dist/storage/web-worker/worker.js.map +1 -1
  126. package/dist/store.d.ts +10 -10
  127. package/dist/store.d.ts.map +1 -1
  128. package/dist/store.js +285 -265
  129. package/dist/store.js.map +1 -1
  130. package/dist/utils/bounded-collections.d.ts.map +1 -0
  131. package/dist/utils/bounded-collections.js +90 -0
  132. package/dist/utils/bounded-collections.js.map +1 -0
  133. package/dist/utils/otel.d.ts.map +1 -0
  134. package/dist/{otel.js → utils/otel.js} +1 -1
  135. package/dist/utils/otel.js.map +1 -0
  136. package/dist/utils/util.d.ts.map +1 -0
  137. package/dist/utils/util.js.map +1 -0
  138. package/package.json +15 -15
  139. package/src/QueryCache.ts +2 -2
  140. package/src/__tests__/react/fixture.tsx +15 -15
  141. package/src/__tests__/react/useQuery.test.tsx +5 -5
  142. package/src/__tests__/react/{useComponentState.test.tsx → useRow.test.tsx} +28 -29
  143. package/src/__tests__/react/utils/stack-info.test.ts +34 -0
  144. package/src/__tests__/reactive.test.ts +1 -1
  145. package/src/__tests__/reactiveQueries/sql.test.ts +10 -10
  146. package/src/effect/LiveStore.ts +6 -6
  147. package/src/global-state.ts +26 -0
  148. package/src/inMemoryDatabase.ts +9 -4
  149. package/src/index.ts +18 -17
  150. package/src/migrations.ts +41 -35
  151. package/src/react/LiveStoreProvider.tsx +2 -2
  152. package/src/react/index.ts +7 -9
  153. package/src/react/useQuery.ts +10 -4
  154. package/src/react/useRow.ts +239 -0
  155. package/src/react/useTemporaryQuery.ts +43 -11
  156. package/src/react/utils/stack-info.ts +4 -2
  157. package/src/reactive.ts +1 -1
  158. package/src/reactiveQueries/base-class.ts +8 -2
  159. package/src/reactiveQueries/graphql.ts +4 -3
  160. package/src/reactiveQueries/js.ts +3 -4
  161. package/src/reactiveQueries/sql.ts +6 -6
  162. package/src/row-query.ts +148 -0
  163. package/src/schema/action.ts +41 -0
  164. package/src/schema/index.ts +63 -0
  165. package/src/schema/system-tables.ts +21 -0
  166. package/src/schema/table-def.ts +197 -0
  167. package/src/storage/in-memory/index.ts +1 -1
  168. package/src/storage/index.ts +2 -1
  169. package/src/storage/tauri/index.ts +2 -2
  170. package/src/storage/web-worker/index.ts +1 -1
  171. package/src/storage/web-worker/worker.ts +2 -2
  172. package/src/store.ts +43 -27
  173. package/dist/__tests__/react/useComponentState.test.d.ts +0 -2
  174. package/dist/__tests__/react/useComponentState.test.d.ts.map +0 -1
  175. package/dist/__tests__/react/useComponentState.test.js.map +0 -1
  176. package/dist/bounded-collections.d.ts.map +0 -1
  177. package/dist/bounded-collections.js +0 -103
  178. package/dist/bounded-collections.js.map +0 -1
  179. package/dist/componentKey.d.ts +0 -20
  180. package/dist/componentKey.d.ts.map +0 -1
  181. package/dist/componentKey.js +0 -3
  182. package/dist/componentKey.js.map +0 -1
  183. package/dist/otel.d.ts.map +0 -1
  184. package/dist/otel.js.map +0 -1
  185. package/dist/react/useComponentState.d.ts +0 -50
  186. package/dist/react/useComponentState.d.ts.map +0 -1
  187. package/dist/react/useComponentState.js +0 -226
  188. package/dist/react/useComponentState.js.map +0 -1
  189. package/dist/reactiveQueries/graph.d.ts +0 -10
  190. package/dist/reactiveQueries/graph.d.ts.map +0 -1
  191. package/dist/reactiveQueries/graph.js +0 -6
  192. package/dist/reactiveQueries/graph.js.map +0 -1
  193. package/dist/schema.d.ts +0 -81
  194. package/dist/schema.d.ts.map +0 -1
  195. package/dist/schema.js +0 -46
  196. package/dist/schema.js.map +0 -1
  197. package/dist/util.d.ts.map +0 -1
  198. package/dist/util.js.map +0 -1
  199. package/src/componentKey.ts +0 -9
  200. package/src/react/useComponentState.ts +0 -385
  201. package/src/reactiveQueries/graph.ts +0 -15
  202. package/src/schema.ts +0 -143
  203. /package/dist/{bounded-collections.d.ts → utils/bounded-collections.d.ts} +0 -0
  204. /package/dist/{otel.d.ts → utils/otel.d.ts} +0 -0
  205. /package/dist/{util.d.ts → utils/util.d.ts} +0 -0
  206. /package/dist/{util.js → utils/util.js} +0 -0
  207. /package/src/{bounded-collections.ts → utils/bounded-collections.ts} +0 -0
  208. /package/src/{otel.ts → utils/otel.ts} +0 -0
  209. /package/src/{util.ts → utils/util.ts} +0 -0
@@ -1 +0,0 @@
1
- {"version":3,"file":"useComponentState.js","sourceRoot":"","sources":["../../src/react/useComponentState.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAChD,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAE9C,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAA;AAGjC,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAE/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,8BAA8B,EAAE,kBA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AAA;AAED,MAAM,kBAAkB,GAAG,CAAC,KAAuC,EAAE,EAAE;IACrE,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE;QAC5E,OAAO,KAAK,CAAA;KACb;SAAM;QACL,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;KACrB;AACH,CAAC,CAAA"}
@@ -1,10 +0,0 @@
1
- import type * as otel from '@opentelemetry/api';
2
- import { ReactiveGraph } from '../reactive.js';
3
- import type { QueryDebugInfo, RefreshReason, Store } from '../store.js';
4
- export type DbContext = {
5
- store: Store;
6
- otelTracer: otel.Tracer;
7
- rootOtelContext: otel.Context;
8
- };
9
- export declare const dbGraph: ReactiveGraph<RefreshReason, QueryDebugInfo, DbContext>;
10
- //# sourceMappingURL=graph.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAG/C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAEvE,MAAM,MAAM,SAAS,GAAG;IACtB,KAAK,EAAE,KAAK,CAAA;IACZ,UAAU,EAAE,IAAI,CAAC,MAAM,CAAA;IACvB,eAAe,EAAE,IAAI,CAAC,OAAO,CAAA;CAC9B,CAAA;AAED,eAAO,MAAM,OAAO,yDAElB,CAAA"}
@@ -1,6 +0,0 @@
1
- import ReactDOM from 'react-dom';
2
- import { ReactiveGraph } from '../reactive.js';
3
- export const dbGraph = new ReactiveGraph({
4
- effectsWrapper: (run) => ReactDOM.unstable_batchedUpdates(() => run()),
5
- });
6
- //# sourceMappingURL=graph.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/reactiveQueries/graph.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAS9C,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,aAAa,CAA2C;IACjF,cAAc,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,uBAAuB,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;CACvE,CAAC,CAAA"}
package/dist/schema.d.ts DELETED
@@ -1,81 +0,0 @@
1
- import type { PrettifyFlat } from '@livestore/utils';
2
- import type { Schema } from '@livestore/utils/effect';
3
- import type { SqliteAst } from 'effect-db-schema';
4
- import { SqliteDsl } from 'effect-db-schema';
5
- export type Index = {
6
- name: string;
7
- columns: string[];
8
- /** @default false */
9
- isUnique?: boolean;
10
- };
11
- export declare const componentStateTables: {
12
- [key: string]: SqliteAst.Table;
13
- };
14
- export type InputSchema = {
15
- tables: {
16
- [tableName: string]: SqliteDsl.TableDefinition<any, any>;
17
- };
18
- materializedViews?: MaterializedViewDefinitions;
19
- actions: ActionDefinitions<any>;
20
- };
21
- export declare const makeSchema: <TSchema extends InputSchema>(schema: TSchema) => Schema;
22
- export type ComponentStateSchema = SqliteDsl.TableDefinition<any, any> & {
23
- register: () => void;
24
- };
25
- export declare const defineComponentStateSchema: <TName extends string, TColumns extends SqliteDsl.Columns>(name: TName, columns: TColumns) => SqliteDsl.TableDefinition<`components__${TName}`, PrettifyFlat<TColumns & {
26
- id: SqliteDsl.ColumnDefinition<SqliteDsl.FieldType.FieldTypeText<string, string>, false>;
27
- }>>;
28
- export type SQLWriteStatement = {
29
- sql: string;
30
- /** Tables written by the statement */
31
- writeTables: string[];
32
- argsAlreadyBound?: boolean;
33
- };
34
- export type ActionDefinition<TArgs = any> = {
35
- statement: SQLWriteStatement | ((args: TArgs) => SQLWriteStatement);
36
- prepareBindValues?: (args: TArgs) => any;
37
- };
38
- export type Schema = {
39
- tables: TableDefinitions;
40
- materializedViews: MaterializedViewDefinitions;
41
- actions: ActionDefinitions<any>;
42
- };
43
- export type TableDefinitions = {
44
- [key: string]: SqliteAst.Table;
45
- };
46
- export type MaterializedViewDefinitions = {
47
- [key: string]: {};
48
- };
49
- export type ActionDefinitions<TArgsMap extends Record<string, any>> = {
50
- [key in keyof TArgsMap]: ActionDefinition<TArgsMap[key]>;
51
- };
52
- export declare const SCHEMA_META_TABLE = "__livestore_schema";
53
- declare const schemaMetaTable: SqliteDsl.TableDefinition<"__livestore_schema", {
54
- tableName: SqliteDsl.ColumnDefinition<SqliteDsl.FieldType.FieldTypeText<string, string>, false>;
55
- schemaHash: SqliteDsl.ColumnDefinition<SqliteDsl.FieldType.FieldTypeInteger, false>;
56
- /** ISO date format */
57
- updatedAt: SqliteDsl.ColumnDefinition<SqliteDsl.FieldType.FieldTypeText<string, string>, false>;
58
- }>;
59
- export type SchemaMetaRow = SqliteDsl.FromTable.RowDecoded<typeof schemaMetaTable>;
60
- export declare const systemTables: {
61
- __livestore_schema: SqliteAst.Table;
62
- };
63
- export declare const defineTables: <T extends TableDefinitions>(tables: T) => T;
64
- export declare const defineMaterializedViews: <M extends MaterializedViewDefinitions>(materializedViews: M) => M;
65
- export declare const defineActions: <A extends ActionDefinitions<any>>(actions: A) => A;
66
- export declare const defineAction: <TArgs extends Record<string, any>>(action: ActionDefinition<TArgs>) => ActionDefinition<TArgs>;
67
- export type GetApplyEventArgs<TActionDefinitionsMap> = RecordValues<{
68
- [eventType in keyof TActionDefinitionsMap]: {
69
- eventType: eventType;
70
- args: GetActionArgs<TActionDefinitionsMap[eventType]>;
71
- };
72
- }>;
73
- type RecordValues<T> = T extends Record<string, infer V> ? V : never;
74
- export type GetActionArgs<A> = A extends ActionDefinition<infer TArgs> ? TArgs : never;
75
- declare global {
76
- interface LiveStoreActionDefinitionsTypes {
77
- [key: string]: ActionDefinition;
78
- }
79
- }
80
- export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,MAAM,YAAY,iGAEW,CAAA;AAEpC,MAAM,MAAM,iBAAiB,CAAC,qBAAqB,IAAI,YAAY,CAAC;KACjE,SAAS,IAAI,MAAM,qBAAqB,GAAG;QAC1C,SAAS,EAAE,SAAS,CAAA;QACpB,IAAI,EAAE,aAAa,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,CAAA;KACtD;CACF,CAAC,CAAA;AAEF,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAEpE,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS,gBAAgB,CAAC,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,KAAK,CAAA;AAGtF,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,+BAA+B;QACvC,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,CAAA;KAChC;CACF"}
package/dist/schema.js DELETED
@@ -1,46 +0,0 @@
1
- import { mapObjectValues } from '@livestore/utils';
2
- import { SqliteDsl } from 'effect-db-schema';
3
- import { DbSchema } from './index.js';
4
- // A global variable representing component state tables we should create in the database
5
- export const componentStateTables = {};
6
- export const makeSchema = (schema) => ({
7
- tables: { ...mapObjectValues(schema.tables, (_tableName, table) => table.ast), ...systemTables },
8
- materializedViews: schema.materializedViews ?? {},
9
- actions: schema.actions,
10
- });
11
- // TODO get rid of "side effect" in this function (via explicit register fn)
12
- export const defineComponentStateSchema = (
13
- // TODO get rid of the `name` param here and use the `componentKey` name instead
14
- name, columns) => {
15
- const tablePath = `components__${name}`;
16
- if (Object.keys(componentStateTables).includes(tablePath)) {
17
- // throw new Error(`Can't register duplicate component: ${name}`)
18
- console.error(`Can't register duplicate component: ${tablePath}`);
19
- }
20
- const schemaWithId = columns;
21
- schemaWithId.id = DbSchema.text({ primaryKey: true });
22
- const tableDef = SqliteDsl.table(tablePath, schemaWithId, []);
23
- // TODO move into register fn
24
- componentStateTables[tablePath] = tableDef.ast;
25
- return tableDef;
26
- };
27
- export const SCHEMA_META_TABLE = '__livestore_schema';
28
- const schemaMetaTable = SqliteDsl.table(SCHEMA_META_TABLE, {
29
- tableName: SqliteDsl.text({ primaryKey: true }),
30
- schemaHash: SqliteDsl.integer({ nullable: false }),
31
- /** ISO date format */
32
- updatedAt: SqliteDsl.text({ nullable: false }),
33
- });
34
- export const systemTables = {
35
- // [EVENTS_TABLE_NAME]: SqliteDsl.table(EVENTS_TABLE_NAME, {
36
- // id: SqliteDsl.text({ primaryKey: true }),
37
- // type: SqliteDsl.text({ nullable: false }),
38
- // args: SqliteDsl.text({ nullable: false }),
39
- // }).ast,
40
- [SCHEMA_META_TABLE]: schemaMetaTable.ast,
41
- };
42
- export const defineTables = (tables) => tables;
43
- export const defineMaterializedViews = (materializedViews) => materializedViews;
44
- export const defineActions = (actions) => actions;
45
- export const defineAction = (action) => action;
46
- //# sourceMappingURL=schema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAGlD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AASrC,yFAAyF;AACzF,MAAM,CAAC,MAAM,oBAAoB,GAAuC,EAAE,CAAA;AAU1E,MAAM,CAAC,MAAM,UAAU,GAAG,CAA8B,MAAe,EAAU,EAAE,CACjF,CAAC;IACC,MAAM,EAAE,EAAE,GAAG,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,YAAY,EAAE;IAChG,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,IAAI,EAAE;IACjD,OAAO,EAAE,MAAM,CAAC,OAAO;CACxB,CAAkB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ACd,CAAA;AAE5B,MAAM,CAAC,MAAM,YAAY,GAAG,CAA6B,MAAS,EAAE,EAAE,CAAC,MAAM,CAAA;AAE7E,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAwC,iBAAoB,EAAE,EAAE,CACrG,iBAAiB,CAAA;AAEnB,MAAM,CAAC,MAAM,aAAa,GAAG,CAAmC,OAAU,EAAE,EAAE,CAAC,OAAO,CAAA;AACtF,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,MAA+B,EACN,EAAE,CAAC,MAAM,CAAA"}
@@ -1 +0,0 @@
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.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"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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"}
@@ -1,9 +0,0 @@
1
- type SingletonKey = { _tag: 'singleton'; componentName: string; id: 'singleton' }
2
- type EphemeralKey = { _tag: 'ephemeral'; componentName: string; id: string }
3
- type CustomKey = { _tag: 'custom'; componentName: string; id: string }
4
-
5
- export type ComponentKey = SingletonKey | EphemeralKey | CustomKey
6
-
7
- export const labelForKey = (key: ComponentKey): string => `${key.componentName}/${key.id}`
8
-
9
- export const tableNameForComponentKey = (componentKey: ComponentKey) => `components__${componentKey.componentName}`
@@ -1,385 +0,0 @@
1
- import type { LiteralUnion } from '@livestore/utils'
2
- import { omit, shouldNeverHappen } from '@livestore/utils'
3
- import { Schema } from '@livestore/utils/effect'
4
- import * as otel from '@opentelemetry/api'
5
- import { SqliteAst, SqliteDsl } from 'effect-db-schema'
6
- import { isEqual, mapValues } from 'lodash-es'
7
- import type { DependencyList } from 'react'
8
- import React from 'react'
9
- import { v4 as uuid } from 'uuid'
10
-
11
- import type { ComponentKey } from '../componentKey.js'
12
- import { labelForKey, tableNameForComponentKey } from '../componentKey.js'
13
- import { migrateTable } from '../migrations.js'
14
- import type { LiveStoreJSQuery } from '../reactiveQueries/js.js'
15
- import { LiveStoreSQLQuery } from '../reactiveQueries/sql.js'
16
- import { SCHEMA_META_TABLE } from '../schema.js'
17
- import type { BaseGraphQLContext, LiveStoreQuery, Store } from '../store.js'
18
- import { sql } from '../util.js'
19
- import { useStore } from './LiveStoreContext.js'
20
- import { extractStackInfoFromStackTrace, originalStackLimit } from './utils/stack-info.js'
21
- import { useStateRefWithReactiveInput } from './utils/useStateRefWithReactiveInput.js'
22
-
23
- export interface QueryDefinitions {
24
- [queryName: string]: LiveStoreQuery
25
- }
26
-
27
- export type UseComponentStateProps<TStateColumns extends ComponentColumns> = {
28
- schema: SqliteDsl.TableDefinition<string, TStateColumns>
29
- componentKey: ComponentKeyConfig
30
- reactDeps?: React.DependencyList
31
- }
32
-
33
- export type ComponentKeyConfig = {
34
- /**
35
- * Name of the Component
36
- *
37
- * TODO we should eventually derive this info automatically from the component (TBD how though...)
38
- */
39
- name: string
40
- id: LiteralUnion<'singleton' | '__ephemeral__', string>
41
- }
42
-
43
- // TODO enforce columns are non-nullable or have a default
44
- export interface ComponentColumns extends SqliteDsl.Columns {
45
- id: SqliteDsl.ColumnDefinition<SqliteDsl.FieldType.FieldTypeText<string, string>, false>
46
- }
47
-
48
- // type ComponentState = {
49
- // /** Equivalent to `componentKey.key` */
50
- // id: string
51
- // [key: string]: string | number | boolean | null
52
- // }
53
-
54
- /**
55
- * This is needed because the `React.useMemo` call below, can sometimes be called multiple times 🤷,
56
- * so we need to "cache" the fact that we've already started a span for this component.
57
- * The map entry is being removed again in the `React.useEffect` call below.
58
- */
59
- const spanAlreadyStartedCache = new Map<string, { span: otel.Span; otelContext: otel.Context }>()
60
-
61
- type UseLiveStoreJsonState<TState> = <TResult>(
62
- jsonStringKey: keyof TState,
63
- parse?: (_: unknown) => TResult,
64
- ) => [value: TResult, setValue: (newVal: TResult | ((prevVal: TResult) => TResult)) => void]
65
-
66
- export type GetStateType<TTableDef extends SqliteDsl.TableDefinition<any, any>> = SqliteDsl.FromColumns.RowDecoded<
67
- TTableDef['columns']
68
- >
69
-
70
- export type GetStateTypeEncoded<TTableDef extends SqliteDsl.TableDefinition<any, any>> =
71
- SqliteDsl.FromColumns.RowEncoded<TTableDef['columns']>
72
-
73
- /**
74
- * Create reactive queries within a component.
75
- * @param config.queries A function that returns a map of named reactive queries.
76
- * @param config.componentKey A function that returns a unique key for this component.
77
- * @param config.reactDeps A list of React-level dependencies that will refresh the queries.
78
- */
79
- export const useComponentState = <TStateColumns extends ComponentColumns>({
80
- schema: stateSchema_,
81
- componentKey: componentKeyConfig,
82
- reactDeps = [],
83
- }: UseComponentStateProps<TStateColumns>): {
84
- state$: LiveStoreJSQuery<SqliteDsl.FromColumns.RowDecoded<TStateColumns>>
85
- state: SqliteDsl.FromColumns.RowDecoded<TStateColumns>
86
- setState: Setters<SqliteDsl.FromColumns.RowDecoded<TStateColumns>>
87
- useLiveStoreJsonState: UseLiveStoreJsonState<SqliteDsl.FromColumns.RowDecoded<TStateColumns>>
88
- } => {
89
- type TComponentState = SqliteDsl.FromColumns.RowDecoded<TStateColumns>
90
-
91
- // TODO validate schema to make sure each column has a default value
92
- // TODO we should clean up the state schema handling to remove this special handling for the `id` column
93
- const stateSchema = React.useMemo(
94
- () => ({ ...stateSchema_, columns: omit(stateSchema_.columns, 'id' as any) }),
95
- [stateSchema_],
96
- )
97
-
98
- const componentKey = useComponentKey(componentKeyConfig, reactDeps)
99
- const { store } = useStore()
100
-
101
- const componentKeyLabel = React.useMemo(() => labelForKey(componentKey), [componentKey])
102
-
103
- const stackInfo = React.useMemo(() => {
104
- Error.stackTraceLimit = 10
105
- // eslint-disable-next-line unicorn/error-message
106
- const stack = new Error().stack!
107
- Error.stackTraceLimit = originalStackLimit
108
- return extractStackInfoFromStackTrace(stack)
109
- }, [])
110
-
111
- // The following `React.useMemo` and `React.useEffect` calls are used to start and end a span for the lifetime of this component.
112
- const { span, otelContext } = React.useMemo(() => {
113
- const existingSpan = spanAlreadyStartedCache.get(componentKeyLabel)
114
- if (existingSpan !== undefined) return existingSpan
115
-
116
- const span = store.otel.tracer.startSpan(
117
- `LiveStore:useComponentState:${componentKeyLabel}`,
118
- { attributes: { stackInfo: JSON.stringify(stackInfo) } },
119
- store.otel.queriesSpanContext,
120
- )
121
-
122
- const otelContext = otel.trace.setSpan(otel.context.active(), span)
123
-
124
- spanAlreadyStartedCache.set(componentKeyLabel, { span, otelContext })
125
-
126
- return { span, otelContext }
127
- }, [componentKeyLabel, stackInfo, store.otel.queriesSpanContext, store.otel.tracer])
128
-
129
- React.useEffect(
130
- () => () => {
131
- spanAlreadyStartedCache.delete(componentKeyLabel)
132
- span.end()
133
- },
134
- [componentKeyLabel, span],
135
- )
136
-
137
- const defaultComponentState = React.useMemo(() => {
138
- const defaultState = mapValues(stateSchema.columns, (c) => c.default) as TComponentState
139
-
140
- // @ts-expect-error TODO fix typing
141
- defaultState.id = componentKeyConfig.id
142
-
143
- return defaultState
144
- }, [componentKeyConfig.id, stateSchema])
145
-
146
- const componentStateEffectSchema = React.useMemo(() => SqliteDsl.structSchemaForTable(stateSchema), [stateSchema])
147
-
148
- // create state query
149
- const state$ = React.useMemo(() => {
150
- const componentTableName = tableNameForComponentKey(componentKey)
151
- const whereClause = componentKey._tag === 'singleton' ? '' : `where id = '${componentKey.id}'`
152
-
153
- // TODO find a better solution for this
154
- if (store.tableRefs[componentTableName] === undefined) {
155
- const schemaHash = SqliteAst.hash(stateSchema.ast)
156
- const res = store.inMemoryDB.select<{ schemaHash: number }>(
157
- sql`SELECT schemaHash FROM ${SCHEMA_META_TABLE} WHERE tableName = '${componentTableName}'`,
158
- )
159
- if (res.length === 0 || res[0]!.schemaHash !== schemaHash) {
160
- migrateTable({ db: store._proxyDb, tableDef: stateSchema.ast, otelContext, schemaHash })
161
- }
162
-
163
- store.tableRefs[componentTableName] = store.graph.makeRef(null, {
164
- equal: () => false,
165
- label: componentTableName,
166
- meta: { liveStoreRefType: 'table' },
167
- })
168
- }
169
-
170
- return (
171
- new LiveStoreSQLQuery({
172
- label: `localState:query:${componentKeyLabel}`,
173
- genQueryString: () => sql`select * from ${componentTableName} ${whereClause} limit 1`,
174
- queriedTables: new Set([componentTableName]),
175
- })
176
- // TODO consider to instead of just returning the default value, to write the default component state to the DB
177
- .pipe<TComponentState>((results) =>
178
- results.length === 1
179
- ? (Schema.parseSync(componentStateEffectSchema)(results[0]!) as TComponentState)
180
- : defaultComponentState,
181
- )
182
- )
183
- }, [
184
- componentKey,
185
- componentKeyLabel,
186
- componentStateEffectSchema,
187
- defaultComponentState,
188
- otelContext,
189
- stateSchema,
190
- store,
191
- ])
192
-
193
- // Step 1:
194
- // Synchronously create state and queries for initial render pass.
195
- const initialComponentState = React.useMemo(
196
- () => state$.run(otelContext, { _tag: 'react', api: 'useComponentState', label: state$.label, stackInfo }),
197
- [otelContext, stackInfo, state$],
198
- )
199
-
200
- // Now that we've computed the initial state synchronously,
201
- // we can set up our useState calls w/ a default value populated...
202
- const [componentStateRef, setComponentState_] = useStateRefWithReactiveInput<TComponentState>(initialComponentState)
203
-
204
- const setState = // TODO: do we have a better type for the values that can go in SQLite?
205
- mapValues(stateSchema.columns, (column, columnName) => (value: string | number) => {
206
- // Don't update the state if it's the same as the value already seen in the component
207
- // @ts-expect-error TODO fix typing
208
- if (componentStateRef.current[columnName] === value) return
209
-
210
- const encodedValue = Schema.encodeSync(column.type.codec)(value)
211
-
212
- if (['componentKey', 'columnNames'].includes(columnName)) {
213
- shouldNeverHappen(`Can't use reserved column name ${columnName}`)
214
- }
215
-
216
- return store.applyEvent('updateComponentState', {
217
- componentKey,
218
- columnNames: [columnName],
219
- [columnName]: encodedValue,
220
- })
221
- }) as Setters<TComponentState>
222
-
223
- setState.setMany = (columnValues: Partial<TComponentState>) => {
224
- // TODO use hashing instead
225
- // Don't update the state if it's the same as the value already seen in the component
226
- // @ts-expect-error TODO fix typing
227
- if (Object.entries(columnValues).every(([columnName, value]) => componentStateRef.current[columnName] === value)) {
228
- return
229
- }
230
-
231
- const columnNames = Object.keys(columnValues)
232
-
233
- return store.applyEvent('updateComponentState', { componentKey, columnNames, ...columnValues })
234
- }
235
-
236
- // OK, now all the synchronous work is done;
237
- // time to set up our long-running queries in an effect
238
- React.useEffect(() => {
239
- return store.otel.tracer.startActiveSpan(
240
- 'LiveStore:useComponentState:long-running',
241
- { attributes: {} },
242
- otelContext,
243
- (span) => {
244
- const unsubs: (() => void)[] = []
245
-
246
- const otelContext = otel.trace.setSpan(otel.context.active(), span)
247
- insertRowForComponentInstance({ store, componentKey, stateSchema, otelContext })
248
-
249
- state$.activeSubscriptions.add(stackInfo)
250
-
251
- unsubs.push(
252
- store.subscribe(
253
- state$,
254
- (results) => {
255
- if (isEqual(results, componentStateRef.current) === false) {
256
- setComponentState_(results as TComponentState)
257
- }
258
- },
259
- undefined,
260
- { label: `useComponentState:localState:subscribe:${state$.label}`, otelContext },
261
- ),
262
- () => state$.activeSubscriptions.delete(stackInfo),
263
- )
264
-
265
- return () => {
266
- for (const unsub of unsubs) {
267
- unsub()
268
- }
269
-
270
- span.end()
271
- }
272
- },
273
- )
274
- }, [
275
- store,
276
- stackInfo,
277
- stateSchema,
278
- defaultComponentState,
279
- otelContext,
280
- componentStateRef,
281
- state$,
282
- setComponentState_,
283
- componentKey,
284
- ])
285
-
286
- React.useEffect(() => () => state$.destroy(), [state$])
287
-
288
- const state = componentStateRef.current
289
-
290
- const useLiveStoreJsonState = <TResult>(
291
- jsonStringKey: keyof TComponentState,
292
- parse: (_: unknown) => TResult = (_) => _ as TResult,
293
- ): [value: TResult, setValue: (newVal: TResult | ((prevVal: TResult) => TResult)) => void] => {
294
- const value = React.useMemo<TResult>(() => {
295
- return parse(JSON.parse(state[jsonStringKey] as string))
296
- // eslint-disable-next-line react-hooks/exhaustive-deps
297
- }, [state[jsonStringKey], parse])
298
-
299
- const setValue = React.useCallback(
300
- (newValOrFn: TResult | ((prev: TResult) => TResult)) => {
301
- const newVal =
302
- typeof newValOrFn === 'function'
303
- ? // NOTE we're using the ref instead of the value because we want to be sure
304
- // we're using the latest value when the setter is called
305
- (newValOrFn as any)(parse(JSON.parse(componentStateRef.current[jsonStringKey] as string)))
306
- : newValOrFn
307
- setState[jsonStringKey](JSON.stringify(newVal) as any)
308
- },
309
- [parse, jsonStringKey],
310
- )
311
-
312
- return [value, setValue]
313
- }
314
-
315
- return {
316
- state$,
317
- state,
318
- setState,
319
- useLiveStoreJsonState,
320
- }
321
- }
322
-
323
- export type Setters<TComponentState> = {
324
- [k in keyof TComponentState]: (newValue: TComponentState[k]) => void
325
- } & {
326
- setMany: (newValues: Partial<TComponentState>) => void
327
- }
328
-
329
- export const useComponentKey = ({ name, id }: ComponentKeyConfig, deps: DependencyList = []) =>
330
- React.useMemo<ComponentKey>(() => {
331
- switch (id) {
332
- case 'singleton': {
333
- return { _tag: 'singleton', componentName: name, id: 'singleton' }
334
- }
335
- case '__ephemeral__': {
336
- return { _tag: 'ephemeral', componentName: name, id: uuid() }
337
- }
338
- default: {
339
- return { _tag: 'custom', componentName: name, id }
340
- }
341
- }
342
- // eslint-disable-next-line react-hooks/exhaustive-deps
343
- }, [...deps, id, name])
344
-
345
- /**
346
- * Create a row storing the state for a component instance, if none exists yet.
347
- * Initialized with default values, and keyed on the component key.
348
- */
349
- const insertRowForComponentInstance = ({
350
- store,
351
- componentKey,
352
- stateSchema,
353
- otelContext,
354
- }: {
355
- store: Store<BaseGraphQLContext>
356
- componentKey: ComponentKey
357
- stateSchema: SqliteDsl.TableDefinition<string, SqliteDsl.Columns>
358
- otelContext: otel.Context
359
- }) => {
360
- const columnNames = ['id', ...Object.keys(stateSchema.columns)]
361
- const columnValues = columnNames.map((name) => `$${name}`).join(', ')
362
-
363
- const tableName = tableNameForComponentKey(componentKey)
364
- const insertQuery = sql`insert into ${tableName} (${columnNames.join(
365
- ', ',
366
- )}) select ${columnValues} where not exists(select 1 from ${tableName} where id = '${componentKey.id}')`
367
-
368
- void store.execute(
369
- insertQuery,
370
- {
371
- ...mapValues(stateSchema.columns, (column) => prepareValueForSql(column.default ?? null)),
372
- id: componentKey.id,
373
- },
374
- [tableName],
375
- otelContext,
376
- )
377
- }
378
-
379
- const prepareValueForSql = (value: string | number | boolean | null) => {
380
- if (typeof value === 'string' || typeof value === 'number' || value === null) {
381
- return value
382
- } else {
383
- return value ? 1 : 0
384
- }
385
- }
@@ -1,15 +0,0 @@
1
- import type * as otel from '@opentelemetry/api'
2
- import ReactDOM from 'react-dom'
3
-
4
- import { ReactiveGraph } from '../reactive.js'
5
- import type { QueryDebugInfo, RefreshReason, Store } from '../store.js'
6
-
7
- export type DbContext = {
8
- store: Store
9
- otelTracer: otel.Tracer
10
- rootOtelContext: otel.Context
11
- }
12
-
13
- export const dbGraph = new ReactiveGraph<RefreshReason, QueryDebugInfo, DbContext>({
14
- effectsWrapper: (run) => ReactDOM.unstable_batchedUpdates(() => run()),
15
- })