@livestore/react 0.0.0-snapshot-a2ca318cc053b3d24df8cc4e8ee8d02736ac62ee → 0.0.0-snapshot-60f3756dd4d7a44aeb731db7db666dd8689c6000

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/src/useRow.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { QueryInfo, RowQuery } from '@livestore/common'
2
2
  import { SessionIdSymbol } from '@livestore/common'
3
+ import type { SqliteDsl } from '@livestore/common/schema'
3
4
  import { DbSchema } from '@livestore/common/schema'
4
- import type { SqliteDsl } from '@livestore/db-schema'
5
5
  import type { LiveQuery, LiveQueryDef, Store } from '@livestore/livestore'
6
6
  import { queryDb } from '@livestore/livestore'
7
7
  import { shouldNeverHappen } from '@livestore/utils'
@@ -24,6 +24,14 @@ export type UseRowResult<TTableDef extends DbSchema.TableDefBase> = [
24
24
  * - `setRow` is a function that can be used to update the row (values will be encoded according to the table schema)
25
25
  * - `query$` is a `LiveQuery` that e.g. can be used to subscribe to changes to the row
26
26
  *
27
+ * `useRow` only works with for tables with client-only derived mutations,
28
+ * i.e. requires the following table options to be set:
29
+ * ```ts
30
+ * const myTable = DbSchema.table('myTable', {
31
+ * // fields ...
32
+ * }, { deriveMutations: { clientOnly: true } })
33
+ * ```
34
+ *
27
35
  * If the table is a singleton table, `useRow` can be called without an `id` argument. Otherwise, the `id` argument is required.
28
36
  */
29
37
  export const useRow: {
@@ -31,7 +39,7 @@ export const useRow: {
31
39
  <
32
40
  TTableDef extends DbSchema.TableDef<
33
41
  DbSchema.DefaultSqliteTableDef,
34
- DbSchema.TableOptions & { isSingleton: true; deriveMutations: { enabled: true } }
42
+ DbSchema.TableOptions & { isSingleton: true; deriveMutations: { enabled: true; clientOnly: true } }
35
43
  >,
36
44
  >(
37
45
  table: TTableDef,
@@ -45,7 +53,7 @@ export const useRow: {
45
53
  DbSchema.TableOptions & {
46
54
  isSingleton: false
47
55
  requiredInsertColumnNames: 'id'
48
- deriveMutations: { enabled: true }
56
+ deriveMutations: { enabled: true; clientOnly: true }
49
57
  }
50
58
  >,
51
59
  >(
@@ -59,7 +67,7 @@ export const useRow: {
59
67
  <
60
68
  TTableDef extends DbSchema.TableDef<
61
69
  DbSchema.DefaultSqliteTableDef,
62
- DbSchema.TableOptions & { isSingleton: false; deriveMutations: { enabled: true } }
70
+ DbSchema.TableOptions & { isSingleton: false; deriveMutations: { enabled: true; clientOnly: true } }
63
71
  >,
64
72
  >(
65
73
  table: TTableDef,
@@ -70,7 +78,7 @@ export const useRow: {
70
78
  } = <
71
79
  TTableDef extends DbSchema.TableDef<
72
80
  DbSchema.DefaultSqliteTableDefConstrained,
73
- DbSchema.TableOptions & { deriveMutations: { enabled: true } }
81
+ DbSchema.TableOptions & { deriveMutations: { enabled: true; clientOnly: true } }
74
82
  >,
75
83
  >(
76
84
  table: TTableDef,
@@ -90,11 +98,9 @@ export const useRow: {
90
98
 
91
99
  type TComponentState = SqliteDsl.FromColumns.RowDecoded<TTableDef['sqliteDef']['columns']>
92
100
 
93
- const tableName = table.sqliteDef.name
101
+ React.useMemo(() => validateTableOptions(table), [table])
94
102
 
95
- if (DbSchema.tableHasDerivedMutations(table) === false) {
96
- shouldNeverHappen(`useRow called on table "${tableName}" which does not have 'deriveMutations: true' set`)
97
- }
103
+ const tableName = table.sqliteDef.name
98
104
 
99
105
  const { store } = useStore({ store: options?.store })
100
106
 
@@ -188,3 +194,17 @@ export type StateSetters<TTableDef extends DbSchema.TableDefBase> = TTableDef['o
188
194
  } & {
189
195
  setMany: Dispatch<SetStateAction<Partial<RowQuery.Result<TTableDef>>>>
190
196
  }
197
+
198
+ const validateTableOptions = (table: DbSchema.TableDef<any, any>) => {
199
+ if (table.options.deriveMutations.clientOnly === false) {
200
+ return shouldNeverHappen(
201
+ `useRow called on table "${table.sqliteDef.name}" which does not have 'deriveMutations: { clientOnly: true }' set`,
202
+ )
203
+ }
204
+
205
+ if (table.options.deriveMutations.enabled === false) {
206
+ return shouldNeverHappen(
207
+ `useRow called on table "${table.sqliteDef.name}" which does not have 'deriveMutations: { clientOnly: true }' set`,
208
+ )
209
+ }
210
+ }
package/tsconfig.json CHANGED
@@ -11,9 +11,8 @@
11
11
  },
12
12
  "include": ["./src"],
13
13
  "references": [
14
- { "path": "../db-schema" },
15
14
  { "path": "../common" },
16
- { "path": "../web" },
15
+ { "path": "../adapter-web" },
17
16
  { "path": "../livestore" },
18
17
  { "path": "../utils" }
19
18
  ]