@livestore/common 0.0.56-dev.2 → 0.0.56-dev.3

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.
@@ -1,12 +1,7 @@
1
1
  import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
2
2
  import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
3
3
 
4
- import {
5
- type MigrationOptionsFromMutationLog,
6
- SqliteError,
7
- type SynchronousDatabase,
8
- UnexpectedError,
9
- } from './adapter-types.js'
4
+ import { type MigrationOptionsFromMutationLog, type SynchronousDatabase, UnexpectedError } from './adapter-types.js'
10
5
  import { getExecArgsFromMutation } from './mutation.js'
11
6
  import type { LiveStoreSchema, MutationDef, MutationLogMetaRow } from './schema/index.js'
12
7
  import { MUTATION_LOG_META_TABLE } from './schema/index.js'
@@ -73,26 +68,22 @@ This likely means the schema has changed in an incompatible way.
73
68
 
74
69
  const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
75
70
 
76
- for (const { statementSql, bindValues } of execArgsArr) {
77
- try {
78
- // TODO cache prepared statements for mutations
79
- const getRowsChanged = db.execute(statementSql, bindValues)
80
- if (
81
- import.meta.env.DEV &&
82
- getRowsChanged() === 0 &&
83
- migrationOptions.logging?.excludeAffectedRows?.(statementSql) !== true
84
- ) {
71
+ const makeExecuteOptions = (statementSql: string, bindValues: any) => ({
72
+ onRowsChanged: (rowsChanged: number) => {
73
+ if (rowsChanged === 0 && migrationOptions.logging?.excludeAffectedRows?.(statementSql) !== true) {
85
74
  console.warn(`Mutation "${mutationDef.name}" did not affect any rows:`, statementSql, bindValues)
86
75
  }
87
- // console.log(`Re-executed mutation ${mutationSql}`, bindValues)
88
- } catch (e) {
89
- yield* new SqliteError({
90
- sql: statementSql,
91
- bindValues,
92
- code: (e as any).resultCode,
93
- cause: e,
94
- })
95
- }
76
+ },
77
+ })
78
+
79
+ for (const { statementSql, bindValues } of execArgsArr) {
80
+ // TODO cache prepared statements for mutations
81
+ db.execute(
82
+ statementSql,
83
+ bindValues,
84
+ import.meta.env.DEV ? makeExecuteOptions(statementSql, bindValues) : undefined,
85
+ )
86
+ // console.log(`Re-executed mutation ${mutationSql}`, bindValues)
96
87
  }
97
88
  }).pipe(Effect.withSpan(`@livestore/common:rehydrateFromMutationLog:processMutation`))
98
89
 
@@ -39,6 +39,9 @@ export type LiveStoreSchema<
39
39
 
40
40
  migrationOptions: MigrationOptions
41
41
 
42
+ /**
43
+ * @default 'default'
44
+ */
42
45
  key: string
43
46
  }
44
47
 
@@ -47,9 +50,6 @@ export type InputSchema = {
47
50
  readonly mutations?: ReadonlyArray<MutationDef.Any> | Record<string, MutationDef.Any>
48
51
  /**
49
52
  * Can be used to isolate multiple LiveStore apps running in the same origin
50
- *
51
- * Make sure you also use this key in the `storage` options (e.g. directory, prefix etc) to make sure
52
- * different instances of LiveStore aren't overlapping on the storage level.
53
53
  */
54
54
  readonly key?: string
55
55
  }
@@ -118,7 +118,7 @@ export const makeSchema = <TInputSchema extends InputSchema>(
118
118
  mutations,
119
119
  migrationOptions: inputSchema.migrations ?? { strategy: 'hard-reset' },
120
120
  hash,
121
- key: inputSchema.key ?? '',
121
+ key: inputSchema.key ?? 'default',
122
122
  } satisfies LiveStoreSchema
123
123
  }
124
124
 
@@ -16,6 +16,8 @@ export const dbExecute = (db: SynchronousDatabase, queryStr: string, bindValues?
16
16
  const preparedBindValues = bindValues ? prepareBindValues(bindValues, queryStr) : undefined
17
17
 
18
18
  stmt.execute(preparedBindValues)
19
+
20
+ stmt.finalize()
19
21
  }
20
22
 
21
23
  export const dbSelect = <T>(db: SynchronousDatabase, queryStr: string, bindValues?: ParamsObject) => {
@@ -25,7 +27,9 @@ export const dbSelect = <T>(db: SynchronousDatabase, queryStr: string, bindValue
25
27
  // cachedStmts.set(queryStr, stmt)
26
28
  // }
27
29
 
28
- return stmt.select<T>(bindValues ? prepareBindValues(bindValues, queryStr) : undefined)
30
+ const res = stmt.select<T>(bindValues ? prepareBindValues(bindValues, queryStr) : undefined)
31
+ stmt.finalize()
32
+ return res
29
33
  }
30
34
 
31
35
  export interface SchemaManager {
@@ -28,14 +28,9 @@ export const makeSchemaManager = (db: SynchronousDatabase): Effect.Effect<Schema
28
28
  })
29
29
 
30
30
  return {
31
- getMutationDefInfos: () => {
32
- const schemaMutationsMetaRows = dbSelect<SchemaMutationsMetaRow>(
33
- db,
34
- sql`SELECT * FROM ${SCHEMA_MUTATIONS_META_TABLE}`,
35
- )
31
+ getMutationDefInfos: () =>
32
+ dbSelect<SchemaMutationsMetaRow>(db, sql`SELECT * FROM ${SCHEMA_MUTATIONS_META_TABLE}`),
36
33
 
37
- return schemaMutationsMetaRows
38
- },
39
34
  setMutationDefInfo: (info) => {
40
35
  dbExecute(
41
36
  db,
@@ -128,7 +123,7 @@ export const migrateTable = ({
128
123
  skipMetaTable?: boolean
129
124
  }) =>
130
125
  Effect.gen(function* () {
131
- console.log(`Migrating table '${tableAst.name}'...`)
126
+ // console.log(`Migrating table '${tableAst.name}'...`)
132
127
  const tableName = tableAst.name
133
128
  const columnSpec = makeColumnSpec(tableAst)
134
129