@livestore/livestore 0.0.42 → 0.0.44

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 (40) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/__tests__/react/fixture.d.ts +3 -3
  3. package/dist/cud.d.ts.map +1 -1
  4. package/dist/cud.js +1 -1
  5. package/dist/cud.js.map +1 -1
  6. package/dist/effect/LiveStore.d.ts +3 -2
  7. package/dist/effect/LiveStore.d.ts.map +1 -1
  8. package/dist/effect/LiveStore.js.map +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/query-info.d.ts.map +1 -1
  14. package/dist/query-info.js +7 -2
  15. package/dist/query-info.js.map +1 -1
  16. package/dist/react/useAtom.d.ts.map +1 -1
  17. package/dist/react/useQuery.d.ts.map +1 -1
  18. package/dist/react/useTemporaryQuery.d.ts +2 -2
  19. package/dist/react/useTemporaryQuery.d.ts.map +1 -1
  20. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +1 -1
  21. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  22. package/dist/reactiveQueries/js.d.ts +3 -3
  23. package/dist/reactiveQueries/js.d.ts.map +1 -1
  24. package/dist/reactiveQueries/sql.d.ts +6 -6
  25. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  26. package/dist/reactiveQueries/sql.js +17 -3
  27. package/dist/reactiveQueries/sql.js.map +1 -1
  28. package/dist/store.d.ts +2 -2
  29. package/dist/store.d.ts.map +1 -1
  30. package/dist/store.js +10 -10
  31. package/dist/store.js.map +1 -1
  32. package/dist/utils/bounded-collections.d.ts.map +1 -1
  33. package/package.json +11 -11
  34. package/src/cud.ts +1 -1
  35. package/src/effect/LiveStore.ts +3 -2
  36. package/src/index.ts +1 -1
  37. package/src/query-info.ts +6 -2
  38. package/src/reactiveQueries/sql.ts +21 -3
  39. package/src/store.ts +14 -11
  40. package/vitest.config.js +1 -1
@@ -1,10 +1,11 @@
1
- import type { DatabaseFactory, MainDatabase } from '@livestore/common'
1
+ import type { DatabaseFactory } from '@livestore/common'
2
2
  import type { LiveStoreSchema } from '@livestore/common/schema'
3
3
  import type { Scope } from '@livestore/utils/effect'
4
4
  import { Context, Deferred, Duration, Effect, Layer, OtelTracer, pipe, Runtime } from '@livestore/utils/effect'
5
5
  import * as otel from '@opentelemetry/api'
6
6
  import type { GraphQLSchema } from 'graphql'
7
7
 
8
+ import type { MainDatabaseWrapper } from '../MainDatabaseWrapper.js'
8
9
  import type { LiveQuery } from '../reactiveQueries/base-class.js'
9
10
  import type { BaseGraphQLContext, BootDb, GraphQLOptions, Store } from '../store.js'
10
11
  import { createStore } from '../store.js'
@@ -39,7 +40,7 @@ export type LiveStoreContextProps<GraphQLContext extends BaseGraphQLContext> = {
39
40
  schema: LiveStoreSchema
40
41
  graphQLOptions?: {
41
42
  schema: Effect.Effect<GraphQLSchema, never, otel.Tracer>
42
- makeContext: (db: MainDatabase) => GraphQLContext
43
+ makeContext: (db: MainDatabaseWrapper) => GraphQLContext
43
44
  }
44
45
  boot?: (db: BootDb) => Effect.Effect<void>
45
46
  makeDb: DatabaseFactory
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ export type { BaseGraphQLContext, QueryDebugInfo, RefreshReason, BootDb } from '
3
3
 
4
4
  export type { QueryDefinition, LiveStoreCreateStoreOptions, LiveStoreContext } from './effect/LiveStore.js'
5
5
 
6
- export { MainDatabaseWrapper as InMemoryDatabase, type DebugInfo, emptyDebugInfo } from './MainDatabaseWrapper.js'
6
+ export { MainDatabaseWrapper, type DebugInfo, emptyDebugInfo } from './MainDatabaseWrapper.js'
7
7
 
8
8
  export type { GetAtom, AtomDebugInfo, RefreshDebugInfo, SerializedAtom, Atom, Node, Ref, Effect } from './reactive.js'
9
9
  export { LiveStoreJSQuery, computed } from './reactiveQueries/js.js'
package/src/query-info.ts CHANGED
@@ -77,8 +77,12 @@ export const mutationForQueryInfo = <const TPath extends QueryInfo>(
77
77
  const partialStructSchema = updatePath.table.schema.pipe(Schema.pick(...columnNames))
78
78
 
79
79
  // const columnNames = Object.keys(value)
80
- const bindValues = Schema.encodeSync(partialStructSchema)(value)
81
- return { columnNames, bindValues }
80
+ const encodedBindValues = Schema.encodeEither(partialStructSchema)(value)
81
+ if (encodedBindValues._tag === 'Left') {
82
+ return shouldNeverHappen(encodedBindValues.left.toString())
83
+ } else {
84
+ return { columnNames, bindValues: encodedBindValues.right }
85
+ }
82
86
  } else if (updatePath._tag === 'Col') {
83
87
  const columnName = updatePath.column
84
88
  const columnSchema =
@@ -95,10 +95,28 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
95
95
  map === undefined
96
96
  ? (rows: any) => rows as TResult
97
97
  : Schema.isSchema(map)
98
- ? (rows: any) => {
98
+ ? (rows: any, opts: { sqlString: string }) => {
99
99
  const parseResult = Schema.decodeEither(map as Schema.Schema<TResult, ReadonlyArray<any>>)(rows)
100
100
  if (parseResult._tag === 'Left') {
101
- console.error(`Error parsing SQL query result: ${TreeFormatter.formatError(parseResult.left)}`)
101
+ const parseErrorStr = TreeFormatter.formatError(parseResult.left)
102
+ const expectedSchemaStr = String(map.ast)
103
+ const bindValuesStr = bindValues === undefined ? '' : `\nBind values: ${JSON.stringify(bindValues)}`
104
+
105
+ console.error(
106
+ `\
107
+ Error parsing SQL query result.
108
+
109
+ Query: ${opts.sqlString}\
110
+ ${bindValuesStr}
111
+
112
+ Expected schema: ${expectedSchemaStr}
113
+
114
+ Error: ${parseErrorStr}
115
+
116
+ Result:`,
117
+ rows,
118
+ )
119
+ // console.error(`Error parsing SQL query result: ${TreeFormatter.formatError(parseResult.left)}`)
102
120
  return shouldNeverHappen(`Error parsing SQL query result: ${parseResult.left}`)
103
121
  } else {
104
122
  return parseResult.right as TResult
@@ -167,7 +185,7 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
167
185
 
168
186
  span.setAttribute('sql.rowsCount', rawResults.length)
169
187
 
170
- const result = this.mapRows(rawResults)
188
+ const result = this.mapRows(rawResults, { sqlString })
171
189
 
172
190
  span.end()
173
191
 
package/src/store.ts CHANGED
@@ -1,11 +1,11 @@
1
- import type { DatabaseFactory, DatabaseImpl, MainDatabase, PreparedBindValues } from '@livestore/common'
1
+ import type { DatabaseFactory, DatabaseImpl, PreparedBindValues } from '@livestore/common'
2
2
  import { type LiveStoreSchema, makeMutationEventSchema, type MutationEvent } from '@livestore/common/schema'
3
3
  import { assertNever, isPromise, makeNoopTracer, shouldNeverHappen } from '@livestore/utils'
4
4
  import { Schema } from '@livestore/utils/effect'
5
5
  import * as otel from '@opentelemetry/api'
6
6
  import type { GraphQLSchema } from 'graphql'
7
7
 
8
- import { dynamicallyRegisteredTables, globalDbGraph } from './global-state.js'
8
+ import { globalDbGraph } from './global-state.js'
9
9
  import { MainDatabaseWrapper } from './MainDatabaseWrapper.js'
10
10
  import { migrateDb } from './migrations.js'
11
11
  import type { StackInfo } from './react/utils/stack-info.js'
@@ -24,7 +24,7 @@ export type BaseGraphQLContext = {
24
24
 
25
25
  export type GraphQLOptions<TContext> = {
26
26
  schema: GraphQLSchema
27
- makeContext: (db: MainDatabase, tracer: otel.Tracer) => TContext
27
+ makeContext: (db: MainDatabaseWrapper, tracer: otel.Tracer) => TContext
28
28
  }
29
29
 
30
30
  export type StoreOptions<
@@ -145,11 +145,11 @@ export class Store<
145
145
  }
146
146
 
147
147
  // Need a set here since `schema.tables` might contain duplicates and some componentStateTables
148
- const allTableNames = new Set([
149
- ...this.schema.tables.keys(),
148
+ const allTableNames = new Set(
149
+ this.schema.tables.keys(),
150
150
  // TODO activate dynamic tables
151
- ...Array.from(dynamicallyRegisteredTables.values()).map((_) => _.sqliteDef.name),
152
- ])
151
+ // ...Array.from(dynamicallyRegisteredTables.values()).map((_) => _.sqliteDef.name),
152
+ )
153
153
  const existingTableRefs = new Map(
154
154
  Array.from(this.graph.atoms.values())
155
155
  .filter((_): _ is Ref<any, any, any> => _._tag === 'ref' && _.label?.startsWith('tableRef:') === true)
@@ -161,7 +161,7 @@ export class Store<
161
161
 
162
162
  if (graphQLOptions) {
163
163
  this.graphQLSchema = graphQLOptions.schema
164
- this.graphQLContext = graphQLOptions.makeContext(db.mainDb, this.otel.tracer)
164
+ this.graphQLContext = graphQLOptions.makeContext(this.mainDbWrapper, this.otel.tracer)
165
165
  }
166
166
  }
167
167
 
@@ -512,7 +512,7 @@ export const createStore = async <
512
512
 
513
513
  if (boot !== undefined) {
514
514
  let isInTxn = false
515
- const txnExecuteStmnts: [string, PreparedBindValues | undefined][] = []
515
+ let txnExecuteStmnts: [string, PreparedBindValues | undefined][] = []
516
516
 
517
517
  const bootDbImpl: BootDb = {
518
518
  execute: (queryStr, bindValues) => {
@@ -521,9 +521,9 @@ export const createStore = async <
521
521
  stmt.execute(preparedBindValues)
522
522
 
523
523
  if (isInTxn === true) {
524
- void db.storageDb.execute(queryStr, preparedBindValues, undefined)
525
- } else {
526
524
  txnExecuteStmnts.push([queryStr, preparedBindValues])
525
+ } else {
526
+ void db.storageDb.execute(queryStr, preparedBindValues, undefined)
527
527
  }
528
528
  },
529
529
  select: (queryStr, bindValues) => {
@@ -548,6 +548,9 @@ export const createStore = async <
548
548
  } catch (e: any) {
549
549
  db.mainDb.execute('ROLLBACK', undefined)
550
550
  throw e
551
+ } finally {
552
+ isInTxn = false
553
+ txnExecuteStmnts = []
551
554
  }
552
555
  },
553
556
  }
package/vitest.config.js CHANGED
@@ -11,7 +11,7 @@ export default defineConfig({
11
11
  },
12
12
  resolve: {
13
13
  alias: {
14
- 'sqlite-esm': 'sqlite-esm/node',
14
+ '@livestore/sqlite-wasm': '@livestore/sqlite-wasm/node',
15
15
  },
16
16
  },
17
17
  })