@livestore/livestore 0.0.55-dev.3 → 0.0.56-dev.0
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/dist/.tsbuildinfo +1 -1
- package/dist/__tests__/react/fixture.d.ts +0 -5
- package/dist/__tests__/react/fixture.d.ts.map +1 -1
- package/dist/__tests__/react/fixture.js +1 -2
- package/dist/__tests__/react/fixture.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/react/LiveStoreProvider.d.ts +4 -2
- package/dist/react/LiveStoreProvider.d.ts.map +1 -1
- package/dist/react/LiveStoreProvider.js +20 -5
- package/dist/react/LiveStoreProvider.js.map +1 -1
- package/dist/react/LiveStoreProvider.test.js +3 -2
- package/dist/react/LiveStoreProvider.test.js.map +1 -1
- package/dist/react/useQuery.test.js +11 -4
- package/dist/react/useQuery.test.js.map +1 -1
- package/dist/react/useRow.test.js +13 -5
- package/dist/react/useRow.test.js.map +1 -1
- package/dist/react/useTemporaryQuery.test.js +5 -2
- package/dist/react/useTemporaryQuery.test.js.map +1 -1
- package/dist/reactiveQueries/graphql.d.ts.map +1 -1
- package/dist/reactiveQueries/graphql.js.map +1 -1
- package/dist/reactiveQueries/sql.d.ts +9 -12
- package/dist/reactiveQueries/sql.d.ts.map +1 -1
- package/dist/reactiveQueries/sql.js +39 -69
- package/dist/reactiveQueries/sql.js.map +1 -1
- package/dist/reactiveQueries/sql.test.js +9 -5
- package/dist/reactiveQueries/sql.test.js.map +1 -1
- package/dist/row-query.d.ts +6 -4
- package/dist/row-query.d.ts.map +1 -1
- package/dist/row-query.js +5 -12
- package/dist/row-query.js.map +1 -1
- package/dist/store-devtools.d.ts +5 -4
- package/dist/store-devtools.d.ts.map +1 -1
- package/dist/store-devtools.js +4 -9
- package/dist/store-devtools.js.map +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +2 -2
- package/dist/store.js.map +1 -1
- package/package.json +5 -9
- package/src/__tests__/react/fixture.tsx +1 -3
- package/src/index.ts +1 -1
- package/src/react/LiveStoreProvider.test.tsx +3 -2
- package/src/react/LiveStoreProvider.tsx +28 -7
- package/src/react/useQuery.test.tsx +11 -4
- package/src/react/useRow.test.tsx +13 -9
- package/src/react/useTemporaryQuery.test.tsx +5 -2
- package/src/reactiveQueries/graphql.ts +5 -1
- package/src/reactiveQueries/sql.test.ts +9 -5
- package/src/reactiveQueries/sql.ts +61 -86
- package/src/row-query.ts +17 -21
- package/src/store-devtools.ts +11 -10
- package/src/store.ts +3 -2
package/src/row-query.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { sql } from '@livestore/common'
|
|
|
3
3
|
import { DbSchema } from '@livestore/common/schema'
|
|
4
4
|
import type { GetValForKey } from '@livestore/utils'
|
|
5
5
|
import { shouldNeverHappen } from '@livestore/utils'
|
|
6
|
-
import { Schema
|
|
6
|
+
import { Schema } from '@livestore/utils/effect'
|
|
7
7
|
import type * as otel from '@opentelemetry/api'
|
|
8
8
|
import type { SqliteDsl } from 'effect-db-schema'
|
|
9
9
|
|
|
@@ -12,14 +12,16 @@ import { computed } from './reactiveQueries/js.js'
|
|
|
12
12
|
import { LiveStoreSQLQuery } from './reactiveQueries/sql.js'
|
|
13
13
|
import type { Store } from './store.js'
|
|
14
14
|
|
|
15
|
-
export type RowQueryOptions = {
|
|
15
|
+
export type RowQueryOptions<TTableDef extends DbSchema.TableDef, TResult = RowResult<TTableDef>> = {
|
|
16
16
|
otelContext?: otel.Context
|
|
17
17
|
skipInsertDefaultRow?: boolean
|
|
18
18
|
reactivityGraph?: ReactivityGraph
|
|
19
|
+
map?: (result: RowResult<TTableDef>) => TResult
|
|
20
|
+
label?: string
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
export type RowQueryOptionsDefaulValues<TTableDef extends DbSchema.TableDef> = {
|
|
22
|
-
defaultValues
|
|
24
|
+
defaultValues?: Partial<RowResult<TTableDef>>
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export type MakeRowQuery = {
|
|
@@ -29,9 +31,10 @@ export type MakeRowQuery = {
|
|
|
29
31
|
boolean,
|
|
30
32
|
DbSchema.TableOptions & { isSingleton: true }
|
|
31
33
|
>,
|
|
34
|
+
TResult = RowResult<TTableDef>,
|
|
32
35
|
>(
|
|
33
36
|
table: TTableDef,
|
|
34
|
-
options?: RowQueryOptions,
|
|
37
|
+
options?: RowQueryOptions<TTableDef, TResult>,
|
|
35
38
|
): LiveQuery<RowResult<TTableDef>, QueryInfoRow<TTableDef>>
|
|
36
39
|
<
|
|
37
40
|
TTableDef extends DbSchema.TableDef<
|
|
@@ -39,19 +42,20 @@ export type MakeRowQuery = {
|
|
|
39
42
|
boolean,
|
|
40
43
|
DbSchema.TableOptions & { isSingleton: false }
|
|
41
44
|
>,
|
|
45
|
+
TResult = RowResult<TTableDef>,
|
|
42
46
|
>(
|
|
43
47
|
table: TTableDef,
|
|
44
48
|
// TODO adjust so it works with arbitrary primary keys or unique constraints
|
|
45
49
|
id: string,
|
|
46
|
-
options?: RowQueryOptions & RowQueryOptionsDefaulValues<TTableDef>,
|
|
47
|
-
): LiveQuery<
|
|
50
|
+
options?: RowQueryOptions<TTableDef, TResult> & RowQueryOptionsDefaulValues<TTableDef>,
|
|
51
|
+
): LiveQuery<TResult, QueryInfoRow<TTableDef>>
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
// TODO also allow other where clauses and multiple rows
|
|
51
55
|
export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
52
56
|
table: TTableDef,
|
|
53
|
-
idOrOptions?: string | RowQueryOptions,
|
|
54
|
-
options_?: RowQueryOptions & RowQueryOptionsDefaulValues<TTableDef>,
|
|
57
|
+
idOrOptions?: string | RowQueryOptions<TTableDef, any>,
|
|
58
|
+
options_?: RowQueryOptions<TTableDef, any> & RowQueryOptionsDefaulValues<TTableDef>,
|
|
55
59
|
) => {
|
|
56
60
|
const id = typeof idOrOptions === 'string' ? idOrOptions : undefined
|
|
57
61
|
const options = typeof idOrOptions === 'string' ? options_ : idOrOptions
|
|
@@ -70,8 +74,10 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
|
70
74
|
const whereClause = id === undefined ? '' : `where id = '${id}'`
|
|
71
75
|
const queryStr = sql`select * from ${tableName} ${whereClause} limit 1`
|
|
72
76
|
|
|
77
|
+
const rowSchema = table.isSingleColumn === true ? table.schema.pipe(Schema.pluck('value' as any)) : table.schema
|
|
78
|
+
|
|
73
79
|
return new LiveStoreSQLQuery({
|
|
74
|
-
label: `rowQuery:query:${tableSchema.name}${id === undefined ? '' : `:${id}`}`,
|
|
80
|
+
label: options?.label ?? `rowQuery:query:${tableSchema.name}${id === undefined ? '' : `:${id}`}`,
|
|
75
81
|
genQueryString: queryStr,
|
|
76
82
|
queriedTables: new Set([tableName]),
|
|
77
83
|
reactivityGraph: options?.reactivityGraph,
|
|
@@ -83,18 +89,8 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
|
83
89
|
id,
|
|
84
90
|
skipInsertDefaultRow: options?.skipInsertDefaultRow,
|
|
85
91
|
}),
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const parseResult = Schema.decodeEither(table.schema)(results[0]!)
|
|
90
|
-
|
|
91
|
-
if (parseResult._tag === 'Left') {
|
|
92
|
-
console.error('decode error', TreeFormatter.formatErrorSync(parseResult.left), 'results', results)
|
|
93
|
-
return shouldNeverHappen(`Error decoding query result for ${queryStr}`)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return table.isSingleColumn === true ? parseResult.right.value : parseResult.right
|
|
97
|
-
},
|
|
92
|
+
schema: rowSchema.pipe(Schema.Array, Schema.headOrElse()),
|
|
93
|
+
map: options?.map,
|
|
98
94
|
queryInfo: { _tag: 'Row', table, id: id ?? 'singleton' },
|
|
99
95
|
})
|
|
100
96
|
}
|
package/src/store-devtools.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { DebugInfo, StoreAdapter } from '@livestore/common'
|
|
2
2
|
import { Devtools, liveStoreVersion, UnexpectedError } from '@livestore/common'
|
|
3
3
|
import { throttle } from '@livestore/utils'
|
|
4
|
-
import { BrowserChannel
|
|
4
|
+
import type { BrowserChannel } from '@livestore/utils/effect'
|
|
5
|
+
import { Effect, Stream } from '@livestore/utils/effect'
|
|
5
6
|
|
|
6
7
|
import type { MainDatabaseWrapper } from './MainDatabaseWrapper.js'
|
|
7
8
|
import { emptyDebugInfo as makeEmptyDebugInfo } from './MainDatabaseWrapper.js'
|
|
@@ -20,7 +21,13 @@ type Unsub = () => void
|
|
|
20
21
|
type RequestId = string
|
|
21
22
|
type SubMap = Map<RequestId, Unsub>
|
|
22
23
|
|
|
23
|
-
export const connectDevtoolsToStore = ({
|
|
24
|
+
export const connectDevtoolsToStore = ({
|
|
25
|
+
storeDevtoolsChannel,
|
|
26
|
+
store,
|
|
27
|
+
}: {
|
|
28
|
+
storeDevtoolsChannel: BrowserChannel.BrowserChannel<Devtools.MessageToAppHostStore, Devtools.MessageFromAppHostStore>
|
|
29
|
+
store: IStore
|
|
30
|
+
}) =>
|
|
24
31
|
Effect.gen(function* () {
|
|
25
32
|
const channelId = store.adapter.coordinator.devtools.channelId
|
|
26
33
|
|
|
@@ -28,14 +35,8 @@ export const connectDevtoolsToStore = ({ storeMessagePort, store }: { storeMessa
|
|
|
28
35
|
const liveQueriesSubscriptions: SubMap = new Map()
|
|
29
36
|
const debugInfoHistorySubscriptions: SubMap = new Map()
|
|
30
37
|
|
|
31
|
-
const storePortChannel = yield* BrowserChannel.messagePortChannel({
|
|
32
|
-
port: storeMessagePort,
|
|
33
|
-
listenSchema: Devtools.MessageToAppHostStore,
|
|
34
|
-
sendSchema: Devtools.MessageFromAppHostStore,
|
|
35
|
-
})
|
|
36
|
-
|
|
37
38
|
const sendToDevtools = (message: Devtools.MessageFromAppHostStore) =>
|
|
38
|
-
|
|
39
|
+
storeDevtoolsChannel.send(message).pipe(Effect.tapCauseLogPretty, Effect.runSync)
|
|
39
40
|
|
|
40
41
|
const onMessage = (decodedMessage: typeof Devtools.MessageToAppHostStore.Type) => {
|
|
41
42
|
// console.log('storeMessagePort message', decodedMessage)
|
|
@@ -199,7 +200,7 @@ export const connectDevtoolsToStore = ({ storeMessagePort, store }: { storeMessa
|
|
|
199
200
|
}
|
|
200
201
|
}
|
|
201
202
|
|
|
202
|
-
yield*
|
|
203
|
+
yield* storeDevtoolsChannel.listen.pipe(
|
|
203
204
|
Stream.flatten(),
|
|
204
205
|
Stream.tapSync((message) => onMessage(message)),
|
|
205
206
|
Stream.runDrain,
|
package/src/store.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ResetMode,
|
|
7
7
|
StoreAdapter,
|
|
8
8
|
StoreAdapterFactory,
|
|
9
|
+
StoreDevtoolsChannel,
|
|
9
10
|
} from '@livestore/common'
|
|
10
11
|
import { getExecArgsFromMutation, prepareBindValues, UnexpectedError } from '@livestore/common'
|
|
11
12
|
import type { LiveStoreSchema, MutationEvent } from '@livestore/common/schema'
|
|
@@ -688,10 +689,10 @@ export const createStore = <
|
|
|
688
689
|
|
|
689
690
|
const storeDeferred = yield* Deferred.make<Store>()
|
|
690
691
|
|
|
691
|
-
const connectDevtoolsToStore_ = (
|
|
692
|
+
const connectDevtoolsToStore_ = (storeDevtoolsChannel: StoreDevtoolsChannel) =>
|
|
692
693
|
Effect.gen(function* () {
|
|
693
694
|
const store = yield* Deferred.await(storeDeferred)
|
|
694
|
-
yield* connectDevtoolsToStore({
|
|
695
|
+
yield* connectDevtoolsToStore({ storeDevtoolsChannel, store })
|
|
695
696
|
})
|
|
696
697
|
|
|
697
698
|
// TODO close parent scope? (Needs refactor with Mike A)
|