@livestore/livestore 0.0.58-dev.0 → 0.0.58-dev.10
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/README.md +1 -117
- package/dist/.tsbuildinfo +1 -1
- package/dist/effect/LiveStore.d.ts +3 -3
- package/dist/effect/LiveStore.d.ts.map +1 -1
- package/dist/effect/LiveStore.js +1 -1
- package/dist/effect/LiveStore.js.map +1 -1
- package/dist/global-state.d.ts.map +1 -1
- package/dist/global-state.js +2 -1
- package/dist/global-state.js.map +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/reactiveQueries/base-class.d.ts +1 -1
- package/dist/reactiveQueries/base-class.d.ts.map +1 -1
- package/dist/reactiveQueries/base-class.js.map +1 -1
- package/dist/reactiveQueries/sql.d.ts +1 -1
- package/dist/reactiveQueries/sql.d.ts.map +1 -1
- package/dist/reactiveQueries/sql.js +4 -4
- package/dist/reactiveQueries/sql.js.map +1 -1
- package/dist/reactiveQueries/sql.test.js +2 -2
- package/dist/reactiveQueries/sql.test.js.map +1 -1
- package/dist/row-query.d.ts +3 -2
- package/dist/row-query.d.ts.map +1 -1
- package/dist/row-query.js +18 -10
- package/dist/row-query.js.map +1 -1
- package/dist/store-devtools.d.ts +2 -2
- package/dist/store-devtools.d.ts.map +1 -1
- package/dist/store-devtools.js +3 -3
- package/dist/store-devtools.js.map +1 -1
- package/dist/store.d.ts +23 -19
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +90 -59
- package/dist/store.js.map +1 -1
- package/dist/utils/dev.d.ts.map +1 -1
- package/dist/utils/dev.js +1 -0
- package/dist/utils/dev.js.map +1 -1
- package/dist/{react/utils → utils}/stack-info.d.ts +1 -2
- package/dist/utils/stack-info.d.ts.map +1 -0
- package/dist/{react/utils → utils}/stack-info.js +1 -9
- package/dist/utils/stack-info.js.map +1 -0
- package/dist/utils/stack-info.test.d.ts.map +1 -0
- package/dist/{__tests__/react/utils → utils}/stack-info.test.js +1 -1
- package/dist/utils/stack-info.test.js.map +1 -0
- package/dist/utils/tests/fixture.d.ts +259 -0
- package/dist/utils/tests/fixture.d.ts.map +1 -0
- package/dist/utils/tests/fixture.js +33 -0
- package/dist/utils/tests/fixture.js.map +1 -0
- package/dist/utils/tests/mod.d.ts +3 -0
- package/dist/utils/tests/mod.d.ts.map +1 -0
- package/dist/utils/tests/mod.js +3 -0
- package/dist/utils/tests/mod.js.map +1 -0
- package/dist/utils/tests/otel.d.ts.map +1 -0
- package/dist/utils/tests/otel.js.map +1 -0
- package/package.json +17 -24
- package/src/ambient.d.ts +3 -0
- package/src/effect/LiveStore.ts +4 -4
- package/src/global-state.ts +5 -1
- package/src/index.ts +17 -4
- package/src/reactiveQueries/base-class.ts +1 -1
- package/src/reactiveQueries/sql.test.ts +2 -2
- package/src/reactiveQueries/sql.ts +5 -5
- package/src/row-query.ts +36 -16
- package/src/store-devtools.ts +5 -5
- package/src/store.ts +146 -78
- package/src/utils/dev.ts +1 -0
- package/src/{__tests__/react/utils → utils}/stack-info.test.ts +1 -1
- package/src/{react/utils → utils}/stack-info.ts +2 -12
- package/src/utils/tests/fixture.ts +77 -0
- package/src/utils/tests/mod.ts +2 -0
- package/tsconfig.json +1 -2
- package/vitest.config.js +0 -8
- package/dist/__tests__/react/fixture.d.ts +0 -461
- package/dist/__tests__/react/fixture.d.ts.map +0 -1
- package/dist/__tests__/react/fixture.js +0 -68
- package/dist/__tests__/react/fixture.js.map +0 -1
- package/dist/__tests__/react/utils/otel.d.ts.map +0 -1
- package/dist/__tests__/react/utils/otel.js.map +0 -1
- package/dist/__tests__/react/utils/stack-info.test.d.ts.map +0 -1
- package/dist/__tests__/react/utils/stack-info.test.js.map +0 -1
- package/dist/react/LiveStoreContext.d.ts +0 -7
- package/dist/react/LiveStoreContext.d.ts.map +0 -1
- package/dist/react/LiveStoreContext.js +0 -13
- package/dist/react/LiveStoreContext.js.map +0 -1
- package/dist/react/LiveStoreProvider.d.ts +0 -47
- package/dist/react/LiveStoreProvider.d.ts.map +0 -1
- package/dist/react/LiveStoreProvider.js +0 -169
- package/dist/react/LiveStoreProvider.js.map +0 -1
- package/dist/react/LiveStoreProvider.test.d.ts +0 -2
- package/dist/react/LiveStoreProvider.test.d.ts.map +0 -1
- package/dist/react/LiveStoreProvider.test.js +0 -62
- package/dist/react/LiveStoreProvider.test.js.map +0 -1
- package/dist/react/components/LiveList.d.ts +0 -21
- package/dist/react/components/LiveList.d.ts.map +0 -1
- package/dist/react/components/LiveList.js +0 -31
- package/dist/react/components/LiveList.js.map +0 -1
- package/dist/react/index.d.ts +0 -11
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -10
- package/dist/react/index.js.map +0 -1
- package/dist/react/useAtom.d.ts +0 -10
- package/dist/react/useAtom.d.ts.map +0 -1
- package/dist/react/useAtom.js +0 -37
- package/dist/react/useAtom.js.map +0 -1
- package/dist/react/useLocalId.d.ts +0 -10
- package/dist/react/useLocalId.d.ts.map +0 -1
- package/dist/react/useLocalId.js +0 -21
- package/dist/react/useLocalId.js.map +0 -1
- package/dist/react/useQuery.d.ts +0 -9
- package/dist/react/useQuery.d.ts.map +0 -1
- package/dist/react/useQuery.js +0 -69
- package/dist/react/useQuery.js.map +0 -1
- package/dist/react/useQuery.test.d.ts +0 -2
- package/dist/react/useQuery.test.d.ts.map +0 -1
- package/dist/react/useQuery.test.js +0 -51
- package/dist/react/useQuery.test.js.map +0 -1
- package/dist/react/useRow.d.ts +0 -46
- package/dist/react/useRow.d.ts.map +0 -1
- package/dist/react/useRow.js +0 -94
- package/dist/react/useRow.js.map +0 -1
- package/dist/react/useRow.test.d.ts +0 -2
- package/dist/react/useRow.test.d.ts.map +0 -1
- package/dist/react/useRow.test.js +0 -562
- package/dist/react/useRow.test.js.map +0 -1
- package/dist/react/useTemporaryQuery.d.ts +0 -22
- package/dist/react/useTemporaryQuery.d.ts.map +0 -1
- package/dist/react/useTemporaryQuery.js +0 -70
- package/dist/react/useTemporaryQuery.js.map +0 -1
- package/dist/react/useTemporaryQuery.test.d.ts +0 -2
- package/dist/react/useTemporaryQuery.test.d.ts.map +0 -1
- package/dist/react/useTemporaryQuery.test.js +0 -37
- package/dist/react/useTemporaryQuery.test.js.map +0 -1
- package/dist/react/utils/stack-info.d.ts.map +0 -1
- package/dist/react/utils/stack-info.js.map +0 -1
- package/dist/react/utils/useStateRefWithReactiveInput.d.ts +0 -13
- package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +0 -1
- package/dist/react/utils/useStateRefWithReactiveInput.js +0 -38
- package/dist/react/utils/useStateRefWithReactiveInput.js.map +0 -1
- package/src/__tests__/react/fixture.tsx +0 -126
- package/src/react/LiveStoreContext.ts +0 -20
- package/src/react/LiveStoreProvider.test.tsx +0 -109
- package/src/react/LiveStoreProvider.tsx +0 -289
- package/src/react/components/LiveList.tsx +0 -84
- package/src/react/index.ts +0 -19
- package/src/react/useAtom.ts +0 -55
- package/src/react/useLocalId.ts +0 -33
- package/src/react/useQuery.test.tsx +0 -82
- package/src/react/useQuery.ts +0 -105
- package/src/react/useRow.test.tsx +0 -699
- package/src/react/useRow.ts +0 -180
- package/src/react/useTemporaryQuery.test.tsx +0 -56
- package/src/react/useTemporaryQuery.ts +0 -121
- package/src/react/utils/useStateRefWithReactiveInput.ts +0 -51
- /package/dist/{__tests__/react/utils → utils}/stack-info.test.d.ts +0 -0
- /package/dist/{__tests__/react/utils → utils/tests}/otel.d.ts +0 -0
- /package/dist/{__tests__/react/utils → utils/tests}/otel.js +0 -0
- /package/src/{__tests__/react/utils → utils/tests}/otel.ts +0 -0
package/src/row-query.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import type { QueryInfoCol, QueryInfoNone, QueryInfoRow } from '@livestore/common'
|
|
2
|
-
import { sql } from '@livestore/common'
|
|
2
|
+
import { SessionIdSymbol, sql } from '@livestore/common'
|
|
3
3
|
import { DbSchema } from '@livestore/common/schema'
|
|
4
|
+
import type { SqliteDsl } from '@livestore/db-schema'
|
|
4
5
|
import type { GetValForKey } from '@livestore/utils'
|
|
5
6
|
import { shouldNeverHappen } from '@livestore/utils'
|
|
6
7
|
import { Schema } from '@livestore/utils/effect'
|
|
7
8
|
import type * as otel from '@opentelemetry/api'
|
|
8
|
-
import type { SqliteDsl } from 'effect-db-schema'
|
|
9
9
|
|
|
10
|
-
import type {
|
|
10
|
+
import type {
|
|
11
|
+
GetAtomResult,
|
|
12
|
+
LiveQuery,
|
|
13
|
+
LiveQueryAny,
|
|
14
|
+
QueryContext,
|
|
15
|
+
ReactivityGraph,
|
|
16
|
+
} from './reactiveQueries/base-class.js'
|
|
11
17
|
import { computed } from './reactiveQueries/js.js'
|
|
12
18
|
import { LiveStoreSQLQuery } from './reactiveQueries/sql.js'
|
|
13
19
|
import type { Store } from './store.js'
|
|
@@ -46,7 +52,7 @@ export type MakeRowQuery = {
|
|
|
46
52
|
>(
|
|
47
53
|
table: TTableDef,
|
|
48
54
|
// TODO adjust so it works with arbitrary primary keys or unique constraints
|
|
49
|
-
id: string,
|
|
55
|
+
id: string | SessionIdSymbol,
|
|
50
56
|
options?: RowQueryOptions<TTableDef, TResult> & RowQueryOptionsDefaulValues<TTableDef>,
|
|
51
57
|
): LiveQuery<TResult, QueryInfoRow<TTableDef>>
|
|
52
58
|
}
|
|
@@ -54,15 +60,15 @@ export type MakeRowQuery = {
|
|
|
54
60
|
// TODO also allow other where clauses and multiple rows
|
|
55
61
|
export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
56
62
|
table: TTableDef,
|
|
57
|
-
idOrOptions?: string | RowQueryOptions<TTableDef, any>,
|
|
63
|
+
idOrOptions?: string | SessionIdSymbol | RowQueryOptions<TTableDef, any>,
|
|
58
64
|
options_?: RowQueryOptions<TTableDef, any> & RowQueryOptionsDefaulValues<TTableDef>,
|
|
59
65
|
) => {
|
|
60
|
-
const id = typeof idOrOptions === 'string' ? idOrOptions : undefined
|
|
61
|
-
const options = typeof idOrOptions === 'string' ? options_ : idOrOptions
|
|
66
|
+
const id = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? idOrOptions : undefined
|
|
67
|
+
const options = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? options_ : idOrOptions
|
|
62
68
|
const defaultValues: Partial<RowResult<TTableDef>> | undefined = (options as any)?.defaultValues ?? {}
|
|
63
69
|
|
|
64
70
|
// Validate query args
|
|
65
|
-
if (table.options.isSingleton === true && id !== undefined) {
|
|
71
|
+
if (table.options.isSingleton === true && id !== undefined && id !== SessionIdSymbol) {
|
|
66
72
|
shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} with id "${id}" as it is a singleton`)
|
|
67
73
|
} else if (table.options.isSingleton !== true && id === undefined) {
|
|
68
74
|
shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} without id`)
|
|
@@ -71,14 +77,21 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
|
71
77
|
const tableSchema = table.sqliteDef
|
|
72
78
|
const tableName = tableSchema.name
|
|
73
79
|
|
|
74
|
-
const
|
|
75
|
-
|
|
80
|
+
const makeQueryString = (id: string | undefined) =>
|
|
81
|
+
sql`select * from ${tableName} ${id === undefined ? '' : `where id = '${id}'`} limit 1`
|
|
82
|
+
|
|
83
|
+
const genQueryString =
|
|
84
|
+
id === SessionIdSymbol
|
|
85
|
+
? (_: GetAtomResult, ctx: QueryContext) => makeQueryString(ctx.store.sessionId)
|
|
86
|
+
: makeQueryString(id)
|
|
76
87
|
|
|
77
88
|
const rowSchema = table.isSingleColumn === true ? table.schema.pipe(Schema.pluck('value' as any)) : table.schema
|
|
78
89
|
|
|
79
90
|
return new LiveStoreSQLQuery({
|
|
80
|
-
label:
|
|
81
|
-
|
|
91
|
+
label:
|
|
92
|
+
options?.label ??
|
|
93
|
+
`rowQuery:query:${tableSchema.name}${id === undefined ? '' : id === SessionIdSymbol ? `:sessionId` : `:${id}`}`,
|
|
94
|
+
genQueryString,
|
|
82
95
|
queriedTables: new Set([tableName]),
|
|
83
96
|
reactivityGraph: options?.reactivityGraph,
|
|
84
97
|
// While this code-path is not needed for singleton tables, it's still needed for `useRow` with non-existing rows for a given ID
|
|
@@ -91,7 +104,11 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
|
|
|
91
104
|
}),
|
|
92
105
|
schema: rowSchema.pipe(Schema.Array, Schema.headOrElse()),
|
|
93
106
|
map: options?.map,
|
|
94
|
-
queryInfo: {
|
|
107
|
+
queryInfo: {
|
|
108
|
+
_tag: 'Row',
|
|
109
|
+
table,
|
|
110
|
+
id: id === SessionIdSymbol ? 'sessionId' : (id ?? 'singleton'),
|
|
111
|
+
},
|
|
95
112
|
})
|
|
96
113
|
}
|
|
97
114
|
|
|
@@ -130,7 +147,7 @@ const makeExecBeforeFirstRun =
|
|
|
130
147
|
otelContext: otelContext_,
|
|
131
148
|
table,
|
|
132
149
|
}: {
|
|
133
|
-
id?: string
|
|
150
|
+
id?: string | SessionIdSymbol
|
|
134
151
|
defaultValues?: any
|
|
135
152
|
skipInsertDefaultRow?: boolean
|
|
136
153
|
otelContext?: otel.Context
|
|
@@ -158,12 +175,15 @@ const insertRowWithDefaultValuesOrIgnore = ({
|
|
|
158
175
|
explicitDefaultValues,
|
|
159
176
|
}: {
|
|
160
177
|
store: Store
|
|
161
|
-
id: string
|
|
178
|
+
id: string | SessionIdSymbol
|
|
162
179
|
table: DbSchema.TableDef
|
|
163
180
|
otelContext: otel.Context
|
|
164
181
|
explicitDefaultValues: Partial<RowResult<DbSchema.TableDef>> | undefined
|
|
165
182
|
}) => {
|
|
166
|
-
const
|
|
183
|
+
const idStr = id === SessionIdSymbol ? store.sessionId : id
|
|
184
|
+
const rowExists =
|
|
185
|
+
store.syncDbWrapper.select(`select 1 from ${table.sqliteDef.name} where id = '${idStr}'`).length === 1
|
|
186
|
+
|
|
167
187
|
if (rowExists) return
|
|
168
188
|
|
|
169
189
|
// const mutationDef = deriveCreateMutationDef(table)
|
package/src/store-devtools.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ClientSession, DebugInfo } from '@livestore/common'
|
|
2
2
|
import { Devtools, liveStoreVersion, UnexpectedError } from '@livestore/common'
|
|
3
3
|
import { throttle } from '@livestore/utils'
|
|
4
4
|
import type { WebChannel } from '@livestore/utils/effect'
|
|
@@ -11,7 +11,7 @@ import { emptyDebugInfo as makeEmptyDebugInfo } from './SynchronousDatabaseWrapp
|
|
|
11
11
|
import type { ReferenceCountedSet } from './utils/data-structures.js'
|
|
12
12
|
|
|
13
13
|
type IStore = {
|
|
14
|
-
|
|
14
|
+
clientSession: ClientSession
|
|
15
15
|
reactivityGraph: ReactivityGraph
|
|
16
16
|
syncDbWrapper: SynchronousDatabaseWrapper
|
|
17
17
|
activeQueries: ReferenceCountedSet<LiveQuery<any>>
|
|
@@ -29,7 +29,7 @@ export const connectDevtoolsToStore = ({
|
|
|
29
29
|
store: IStore
|
|
30
30
|
}) =>
|
|
31
31
|
Effect.gen(function* () {
|
|
32
|
-
const appHostId = store.
|
|
32
|
+
const appHostId = store.clientSession.coordinator.devtools.appHostId
|
|
33
33
|
|
|
34
34
|
const reactivityGraphSubcriptions: SubMap = new Map()
|
|
35
35
|
const liveQueriesSubscriptions: SubMap = new Map()
|
|
@@ -49,14 +49,14 @@ export const connectDevtoolsToStore = ({
|
|
|
49
49
|
const onMessage = (decodedMessage: typeof Devtools.MessageToAppHostStore.Type) => {
|
|
50
50
|
// console.log('storeMessagePort message', decodedMessage)
|
|
51
51
|
|
|
52
|
-
if (decodedMessage.appHostId !== store.
|
|
52
|
+
if (decodedMessage.appHostId !== store.clientSession.coordinator.devtools.appHostId) {
|
|
53
53
|
// console.log(`Unknown message`, event)
|
|
54
54
|
return
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
const requestId = decodedMessage.requestId
|
|
58
58
|
|
|
59
|
-
const requestIdleCallback =
|
|
59
|
+
const requestIdleCallback = globalThis.requestIdleCallback ?? ((cb: () => void) => cb())
|
|
60
60
|
|
|
61
61
|
switch (decodedMessage._tag) {
|
|
62
62
|
case 'LSD.ReactivityGraphSubscribe': {
|