@livestore/common 0.3.0-dev.28 → 0.3.0-dev.29
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__/fixture.d.ts +83 -221
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +33 -11
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/adapter-types.d.ts +22 -15
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +15 -2
- package/dist/adapter-types.js.map +1 -1
- package/dist/bounded-collections.d.ts +1 -1
- package/dist/bounded-collections.d.ts.map +1 -1
- package/dist/debug-info.d.ts.map +1 -1
- package/dist/debug-info.js +1 -0
- package/dist/debug-info.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +21 -21
- package/dist/devtools/devtools-messages-common.d.ts +6 -6
- package/dist/devtools/devtools-messages-leader.d.ts +45 -45
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
- package/dist/devtools/devtools-messages-leader.js +11 -11
- package/dist/devtools/devtools-messages-leader.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts +10 -10
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.js +63 -65
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
- package/dist/leader-thread/{apply-mutation.d.ts → apply-event.d.ts} +7 -7
- package/dist/leader-thread/apply-event.d.ts.map +1 -0
- package/dist/leader-thread/apply-event.js +103 -0
- package/dist/leader-thread/apply-event.js.map +1 -0
- package/dist/leader-thread/eventlog.d.ts +27 -0
- package/dist/leader-thread/eventlog.d.ts.map +1 -0
- package/dist/leader-thread/eventlog.js +123 -0
- package/dist/leader-thread/eventlog.js.map +1 -0
- package/dist/leader-thread/leader-worker-devtools.js +18 -18
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +2 -2
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +16 -16
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mod.d.ts +1 -1
- package/dist/leader-thread/mod.d.ts.map +1 -1
- package/dist/leader-thread/mod.js +1 -1
- package/dist/leader-thread/mod.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +6 -8
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/types.d.ts +11 -11
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/materializer-helper.d.ts +23 -0
- package/dist/materializer-helper.d.ts.map +1 -0
- package/dist/materializer-helper.js +70 -0
- package/dist/materializer-helper.js.map +1 -0
- package/dist/query-builder/api.d.ts +58 -53
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/query-builder/api.js +3 -5
- package/dist/query-builder/api.js.map +1 -1
- package/dist/query-builder/astToSql.d.ts.map +1 -1
- package/dist/query-builder/astToSql.js +59 -37
- package/dist/query-builder/astToSql.js.map +1 -1
- package/dist/query-builder/impl.d.ts +2 -3
- package/dist/query-builder/impl.d.ts.map +1 -1
- package/dist/query-builder/impl.js +48 -46
- package/dist/query-builder/impl.js.map +1 -1
- package/dist/query-builder/impl.test.d.ts +86 -1
- package/dist/query-builder/impl.test.d.ts.map +1 -1
- package/dist/query-builder/impl.test.js +244 -36
- package/dist/query-builder/impl.test.js.map +1 -1
- package/dist/rehydrate-from-eventlog.d.ts +14 -0
- package/dist/rehydrate-from-eventlog.d.ts.map +1 -0
- package/dist/{rehydrate-from-mutationlog.js → rehydrate-from-eventlog.js} +25 -26
- package/dist/rehydrate-from-eventlog.js.map +1 -0
- package/dist/schema/EventDef.d.ts +136 -0
- package/dist/schema/EventDef.d.ts.map +1 -0
- package/dist/schema/EventDef.js +58 -0
- package/dist/schema/EventDef.js.map +1 -0
- package/dist/schema/EventId.d.ts +2 -2
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +3 -2
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/{MutationEvent.d.ts → LiveStoreEvent.d.ts} +56 -56
- package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
- package/dist/schema/{MutationEvent.js → LiveStoreEvent.js} +24 -24
- package/dist/schema/LiveStoreEvent.js.map +1 -0
- package/dist/schema/client-document-def.d.ts +223 -0
- package/dist/schema/client-document-def.d.ts.map +1 -0
- package/dist/schema/client-document-def.js +170 -0
- package/dist/schema/client-document-def.js.map +1 -0
- package/dist/schema/client-document-def.test.d.ts +2 -0
- package/dist/schema/client-document-def.test.d.ts.map +1 -0
- package/dist/schema/client-document-def.test.js +201 -0
- package/dist/schema/client-document-def.test.js.map +1 -0
- package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -1
- package/dist/schema/events.d.ts +2 -0
- package/dist/schema/events.d.ts.map +1 -0
- package/dist/schema/events.js +2 -0
- package/dist/schema/events.js.map +1 -0
- package/dist/schema/mod.d.ts +4 -3
- package/dist/schema/mod.d.ts.map +1 -1
- package/dist/schema/mod.js +4 -3
- package/dist/schema/mod.js.map +1 -1
- package/dist/schema/schema.d.ts +27 -23
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +45 -43
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/sqlite-state.d.ts +12 -0
- package/dist/schema/sqlite-state.d.ts.map +1 -0
- package/dist/schema/sqlite-state.js +36 -0
- package/dist/schema/sqlite-state.js.map +1 -0
- package/dist/schema/system-tables.d.ts +67 -98
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +62 -48
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema/table-def.d.ts +26 -96
- package/dist/schema/table-def.d.ts.map +1 -1
- package/dist/schema/table-def.js +16 -64
- package/dist/schema/table-def.js.map +1 -1
- package/dist/schema/view.d.ts +3 -0
- package/dist/schema/view.d.ts.map +1 -0
- package/dist/schema/view.js +3 -0
- package/dist/schema/view.js.map +1 -0
- package/dist/schema-management/common.d.ts +4 -4
- package/dist/schema-management/common.d.ts.map +1 -1
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +6 -6
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts +3 -3
- package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
- package/dist/schema-management/validate-mutation-defs.js +17 -17
- package/dist/schema-management/validate-mutation-defs.js.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.d.ts +7 -7
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.js +31 -30
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
- package/dist/sync/next/facts.d.ts +19 -19
- package/dist/sync/next/facts.d.ts.map +1 -1
- package/dist/sync/next/facts.js +2 -2
- package/dist/sync/next/facts.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +3 -3
- package/dist/sync/next/history-dag-common.d.ts.map +1 -1
- package/dist/sync/next/history-dag-common.js +1 -1
- package/dist/sync/next/history-dag-common.js.map +1 -1
- package/dist/sync/next/history-dag.js +1 -1
- package/dist/sync/next/history-dag.js.map +1 -1
- package/dist/sync/next/rebase-events.d.ts +7 -7
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +5 -5
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.calculator.test.js +38 -33
- package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +71 -71
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +29 -29
- package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
- package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +60 -25
- package/dist/sync/next/test/event-fixtures.js.map +1 -0
- package/dist/sync/next/test/mod.d.ts +1 -1
- package/dist/sync/next/test/mod.d.ts.map +1 -1
- package/dist/sync/next/test/mod.js +1 -1
- package/dist/sync/next/test/mod.js.map +1 -1
- package/dist/sync/sync.d.ts +3 -3
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/syncstate.d.ts +32 -32
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +10 -10
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +5 -5
- package/dist/sync/syncstate.test.js.map +1 -1
- package/dist/sync/validate-push-payload.d.ts +2 -2
- package/dist/sync/validate-push-payload.d.ts.map +1 -1
- package/dist/sync/validate-push-payload.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
- package/src/__tests__/fixture.ts +36 -15
- package/src/adapter-types.ts +23 -16
- package/src/debug-info.ts +1 -0
- package/src/devtools/devtools-messages-leader.ts +13 -13
- package/src/index.ts +2 -5
- package/src/leader-thread/LeaderSyncProcessor.ts +81 -91
- package/src/leader-thread/{apply-mutation.ts → apply-event.ts} +50 -74
- package/src/leader-thread/eventlog.ts +199 -0
- package/src/leader-thread/leader-worker-devtools.ts +18 -18
- package/src/leader-thread/make-leader-thread-layer.ts +18 -18
- package/src/leader-thread/mod.ts +1 -1
- package/src/leader-thread/recreate-db.ts +6 -9
- package/src/leader-thread/types.ts +12 -12
- package/src/materializer-helper.ts +110 -0
- package/src/query-builder/api.ts +79 -105
- package/src/query-builder/astToSql.ts +68 -39
- package/src/query-builder/impl.test.ts +264 -42
- package/src/query-builder/impl.ts +72 -56
- package/src/{rehydrate-from-mutationlog.ts → rehydrate-from-eventlog.ts} +33 -40
- package/src/schema/EventDef.ts +216 -0
- package/src/schema/EventId.ts +5 -3
- package/src/schema/{MutationEvent.ts → LiveStoreEvent.ts} +67 -68
- package/src/schema/client-document-def.test.ts +239 -0
- package/src/schema/client-document-def.ts +444 -0
- package/src/schema/db-schema/dsl/mod.ts +0 -1
- package/src/schema/events.ts +1 -0
- package/src/schema/mod.ts +4 -3
- package/src/schema/schema.ts +79 -69
- package/src/schema/sqlite-state.ts +62 -0
- package/src/schema/system-tables.ts +42 -53
- package/src/schema/table-def.ts +53 -209
- package/src/schema/view.ts +2 -0
- package/src/schema-management/common.ts +4 -4
- package/src/schema-management/migrations.ts +8 -9
- package/src/schema-management/validate-mutation-defs.ts +22 -24
- package/src/sync/ClientSessionSyncProcessor.ts +37 -36
- package/src/sync/next/facts.ts +31 -32
- package/src/sync/next/history-dag-common.ts +4 -4
- package/src/sync/next/history-dag.ts +1 -1
- package/src/sync/next/rebase-events.ts +13 -13
- package/src/sync/next/test/compact-events.calculator.test.ts +45 -45
- package/src/sync/next/test/compact-events.test.ts +73 -73
- package/src/sync/next/test/event-fixtures.ts +219 -0
- package/src/sync/next/test/mod.ts +1 -1
- package/src/sync/sync.ts +3 -3
- package/src/sync/syncstate.test.ts +8 -8
- package/src/sync/syncstate.ts +19 -19
- package/src/sync/validate-push-payload.ts +2 -2
- package/src/version.ts +1 -1
- package/tmp/pack.tgz +0 -0
- package/tsconfig.json +1 -0
- package/dist/derived-mutations.d.ts +0 -109
- package/dist/derived-mutations.d.ts.map +0 -1
- package/dist/derived-mutations.js +0 -54
- package/dist/derived-mutations.js.map +0 -1
- package/dist/derived-mutations.test.d.ts +0 -2
- package/dist/derived-mutations.test.d.ts.map +0 -1
- package/dist/derived-mutations.test.js +0 -93
- package/dist/derived-mutations.test.js.map +0 -1
- package/dist/init-singleton-tables.d.ts +0 -4
- package/dist/init-singleton-tables.d.ts.map +0 -1
- package/dist/init-singleton-tables.js +0 -16
- package/dist/init-singleton-tables.js.map +0 -1
- package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
- package/dist/leader-thread/apply-mutation.js +0 -122
- package/dist/leader-thread/apply-mutation.js.map +0 -1
- package/dist/leader-thread/mutationlog.d.ts +0 -27
- package/dist/leader-thread/mutationlog.d.ts.map +0 -1
- package/dist/leader-thread/mutationlog.js +0 -124
- package/dist/leader-thread/mutationlog.js.map +0 -1
- package/dist/leader-thread/pull-queue-set.d.ts +0 -7
- package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
- package/dist/leader-thread/pull-queue-set.js +0 -38
- package/dist/leader-thread/pull-queue-set.js.map +0 -1
- package/dist/mutation.d.ts +0 -20
- package/dist/mutation.d.ts.map +0 -1
- package/dist/mutation.js +0 -68
- package/dist/mutation.js.map +0 -1
- package/dist/query-info.d.ts +0 -41
- package/dist/query-info.d.ts.map +0 -1
- package/dist/query-info.js +0 -7
- package/dist/query-info.js.map +0 -1
- package/dist/rehydrate-from-mutationlog.d.ts +0 -15
- package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
- package/dist/rehydrate-from-mutationlog.js.map +0 -1
- package/dist/schema/MutationEvent.d.ts.map +0 -1
- package/dist/schema/MutationEvent.js.map +0 -1
- package/dist/schema/mutations.d.ts +0 -115
- package/dist/schema/mutations.d.ts.map +0 -1
- package/dist/schema/mutations.js +0 -42
- package/dist/schema/mutations.js.map +0 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
- package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
- package/src/derived-mutations.test.ts +0 -101
- package/src/derived-mutations.ts +0 -170
- package/src/init-singleton-tables.ts +0 -24
- package/src/leader-thread/mutationlog.ts +0 -202
- package/src/mutation.ts +0 -108
- package/src/query-info.ts +0 -83
- package/src/schema/mutations.ts +0 -193
- package/src/sync/next/test/mutation-fixtures.ts +0 -228
package/src/schema/mutations.ts
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
import { Schema } from '@livestore/utils/effect'
|
2
|
-
|
3
|
-
import type { QueryBuilder } from '../query-builder/mod.js'
|
4
|
-
import type { BindValues } from '../sql-queries/sql-queries.js'
|
5
|
-
|
6
|
-
export type MutationDefMap = {
|
7
|
-
map: Map<string | 'livestore.RawSql', MutationDef.Any>
|
8
|
-
wasProvided: boolean
|
9
|
-
}
|
10
|
-
export type MutationDefRecord = {
|
11
|
-
'livestore.RawSql': RawSqlMutation
|
12
|
-
[name: string]: MutationDef.Any
|
13
|
-
}
|
14
|
-
|
15
|
-
export type InternalMutationSchema<TRecord extends MutationDefRecord = MutationDefRecord> = {
|
16
|
-
_DefRecord: TRecord
|
17
|
-
|
18
|
-
map: Map<keyof TRecord, TRecord[keyof TRecord]>
|
19
|
-
schemaHashMap: Map<keyof TRecord, number>
|
20
|
-
}
|
21
|
-
|
22
|
-
export type MutationDefSqlResult<TTo> =
|
23
|
-
| SingleOrReadonlyArray<string>
|
24
|
-
| ((
|
25
|
-
args: TTo,
|
26
|
-
context: { currentFacts: MutationEventFacts; clientOnly: boolean },
|
27
|
-
) => SingleOrReadonlyArray<
|
28
|
-
| string
|
29
|
-
| {
|
30
|
-
sql: string
|
31
|
-
/** Note args need to be manually encoded to `BindValues` when returning this argument */
|
32
|
-
bindValues: BindValues
|
33
|
-
writeTables?: ReadonlySet<string>
|
34
|
-
}
|
35
|
-
| QueryBuilder.Any
|
36
|
-
>)
|
37
|
-
|
38
|
-
export type MutationHandlerResult = {
|
39
|
-
sql: string
|
40
|
-
bindValues: BindValues
|
41
|
-
writeTables?: ReadonlySet<string>
|
42
|
-
}
|
43
|
-
|
44
|
-
export type SingleOrReadonlyArray<T> = T | ReadonlyArray<T>
|
45
|
-
|
46
|
-
export type MutationDef<TName extends string, TFrom, TTo> = {
|
47
|
-
name: TName
|
48
|
-
schema: Schema.Schema<TTo, TFrom>
|
49
|
-
sql: MutationDefSqlResult<NoInfer<TTo>>
|
50
|
-
options: {
|
51
|
-
/** Warning: This feature is not fully implemented yet */
|
52
|
-
historyId: string
|
53
|
-
/**
|
54
|
-
* When set to true, the mutation won't be synced across clients but
|
55
|
-
*/
|
56
|
-
clientOnly: boolean
|
57
|
-
/** Warning: This feature is not fully implemented yet */
|
58
|
-
facts: FactsCallback<TTo> | undefined
|
59
|
-
}
|
60
|
-
|
61
|
-
/** Helper function to construct a partial mutation event */
|
62
|
-
(args: TTo): {
|
63
|
-
mutation: TName
|
64
|
-
args: TTo
|
65
|
-
}
|
66
|
-
}
|
67
|
-
|
68
|
-
export type FactsCallback<TTo> = (
|
69
|
-
args: TTo,
|
70
|
-
currentFacts: MutationEventFacts,
|
71
|
-
) => {
|
72
|
-
modify: {
|
73
|
-
set: Iterable<MutationEventFactInput>
|
74
|
-
unset: Iterable<MutationEventFactInput>
|
75
|
-
}
|
76
|
-
require: Iterable<MutationEventFactInput>
|
77
|
-
}
|
78
|
-
|
79
|
-
export namespace MutationDef {
|
80
|
-
export type Any = MutationDef<string, any, any>
|
81
|
-
}
|
82
|
-
|
83
|
-
export type MutationEventKey = string
|
84
|
-
export type MutationEventFact = string
|
85
|
-
export type MutationEventFacts = ReadonlyMap<string, any>
|
86
|
-
|
87
|
-
export type MutationEventFactsGroup = {
|
88
|
-
modifySet: MutationEventFacts
|
89
|
-
modifyUnset: MutationEventFacts
|
90
|
-
|
91
|
-
/**
|
92
|
-
* Events on independent "dependency" branches are commutative which can facilitate more prioritized syncing
|
93
|
-
*/
|
94
|
-
depRequire: MutationEventFacts
|
95
|
-
depRead: MutationEventFacts
|
96
|
-
}
|
97
|
-
|
98
|
-
export type MutationEventFactsSnapshot = Map<string, any>
|
99
|
-
|
100
|
-
export type MutationEventFactInput = string | readonly [string, any]
|
101
|
-
|
102
|
-
export const defineFacts = <
|
103
|
-
TRecord extends Record<string, MutationEventFactInput | ((...args: any[]) => MutationEventFactInput)>,
|
104
|
-
>(
|
105
|
-
record: TRecord,
|
106
|
-
): TRecord => record
|
107
|
-
|
108
|
-
export type DefineMutationOptions<TTo> = {
|
109
|
-
// TODO actually implement this
|
110
|
-
onError?: (error: any) => void
|
111
|
-
historyId?: string
|
112
|
-
/** Warning: This feature is not fully implemented yet */
|
113
|
-
facts?: (
|
114
|
-
args: TTo,
|
115
|
-
currentFacts: MutationEventFacts,
|
116
|
-
) => {
|
117
|
-
modify?: {
|
118
|
-
set?: Iterable<MutationEventFactInput>
|
119
|
-
unset?: Iterable<MutationEventFactInput>
|
120
|
-
}
|
121
|
-
/**
|
122
|
-
* Two purposes: constrain history and constrain compaction
|
123
|
-
*/
|
124
|
-
require?: Iterable<MutationEventFactInput>
|
125
|
-
}
|
126
|
-
/**
|
127
|
-
* When set to true, the mutation won't be synced over the network
|
128
|
-
*/
|
129
|
-
clientOnly?: boolean
|
130
|
-
}
|
131
|
-
|
132
|
-
// TODO possibly also allow for mutation event subsumption behaviour
|
133
|
-
export const defineMutation = <TName extends string, TFrom, TTo>(
|
134
|
-
name: TName,
|
135
|
-
schema: Schema.Schema<TTo, TFrom>,
|
136
|
-
sql: MutationDefSqlResult<NoInfer<TTo>>,
|
137
|
-
options?: DefineMutationOptions<TTo>,
|
138
|
-
): MutationDef<TName, TFrom, TTo> => {
|
139
|
-
const makePartialEvent = (args: TTo) => ({ mutation: name, args })
|
140
|
-
|
141
|
-
Object.defineProperty(makePartialEvent, 'name', { value: name })
|
142
|
-
Object.defineProperty(makePartialEvent, 'schema', { value: schema })
|
143
|
-
Object.defineProperty(makePartialEvent, 'sql', { value: sql })
|
144
|
-
Object.defineProperty(makePartialEvent, 'options', {
|
145
|
-
value: {
|
146
|
-
historyId: options?.historyId ?? 'main',
|
147
|
-
clientOnly: options?.clientOnly ?? false,
|
148
|
-
facts: options?.facts
|
149
|
-
? (args, currentFacts) => {
|
150
|
-
const res = options.facts!(args, currentFacts)
|
151
|
-
return {
|
152
|
-
modify: {
|
153
|
-
set: res.modify?.set ? new Set(res.modify.set) : new Set(),
|
154
|
-
unset: res.modify?.unset ? new Set(res.modify.unset) : new Set(),
|
155
|
-
},
|
156
|
-
require: res.require ? new Set(res.require) : new Set(),
|
157
|
-
}
|
158
|
-
}
|
159
|
-
: undefined,
|
160
|
-
} satisfies MutationDef.Any['options'],
|
161
|
-
})
|
162
|
-
|
163
|
-
return makePartialEvent as MutationDef<TName, TFrom, TTo>
|
164
|
-
}
|
165
|
-
|
166
|
-
export const makeMutationDefRecord = <TInputRecord extends Record<string, MutationDef.Any>>(
|
167
|
-
inputRecord: TInputRecord,
|
168
|
-
): {
|
169
|
-
[K in TInputRecord[keyof TInputRecord]['name']]: Extract<TInputRecord[keyof TInputRecord], { name: K }>
|
170
|
-
} => {
|
171
|
-
const result: any = {}
|
172
|
-
|
173
|
-
for (const [name, def] of Object.entries(inputRecord)) {
|
174
|
-
result[name] = def
|
175
|
-
}
|
176
|
-
|
177
|
-
result['livestore.RawSql'] = rawSqlMutation
|
178
|
-
|
179
|
-
return result
|
180
|
-
}
|
181
|
-
|
182
|
-
export const rawSqlMutation = defineMutation(
|
183
|
-
'livestore.RawSql',
|
184
|
-
Schema.Struct({
|
185
|
-
sql: Schema.String,
|
186
|
-
bindValues: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
|
187
|
-
writeTables: Schema.optional(Schema.ReadonlySet(Schema.String)),
|
188
|
-
}),
|
189
|
-
({ sql, bindValues, writeTables }) => ({ sql, bindValues: bindValues ?? {}, writeTables }),
|
190
|
-
)
|
191
|
-
|
192
|
-
export type RawSqlMutation = typeof rawSqlMutation
|
193
|
-
export type RawSqlMutationEvent = ReturnType<typeof rawSqlMutation>
|
@@ -1,228 +0,0 @@
|
|
1
|
-
import { Schema } from '@livestore/utils/effect'
|
2
|
-
|
3
|
-
import * as EventId from '../../../schema/EventId.js'
|
4
|
-
import type { MutationDef } from '../../../schema/mutations.js'
|
5
|
-
import { defineFacts, defineMutation } from '../../../schema/mutations.js'
|
6
|
-
import { factsSnapshotForDag, getFactsGroupForMutationArgs } from '../facts.js'
|
7
|
-
import { historyDagFromNodes } from '../history-dag.js'
|
8
|
-
import type { HistoryDagNode } from '../history-dag-common.js'
|
9
|
-
import { rootEventNode } from '../history-dag-common.js'
|
10
|
-
|
11
|
-
/** Used for conflict detection and event history compaction */
|
12
|
-
export const facts = defineFacts({
|
13
|
-
todoExists: (id: string) => `todo-exists-${id}`,
|
14
|
-
todoIsWriteable: (id: string, writeable: boolean) => [`todo-is-writeable-${id}`, writeable],
|
15
|
-
todoCompleted: (id: string, completed: boolean) => [`todo-completed-${id}`, completed],
|
16
|
-
todoTextUpdated: (id: string) => `todo-text-updated-${id}`,
|
17
|
-
inputValue: (id: string) => `input-value-${id}`,
|
18
|
-
})
|
19
|
-
|
20
|
-
export const mutations = {
|
21
|
-
createTodo: defineMutation(
|
22
|
-
'createTodo',
|
23
|
-
Schema.Struct({ id: Schema.String, text: Schema.String }),
|
24
|
-
'INSERT INTO todos (id, text) VALUES ($id, $text)',
|
25
|
-
{
|
26
|
-
facts: ({ id }) => ({
|
27
|
-
modify: {
|
28
|
-
set: [facts.todoExists(id), facts.todoIsWriteable(id, true), facts.todoCompleted(id, false)],
|
29
|
-
},
|
30
|
-
}),
|
31
|
-
},
|
32
|
-
),
|
33
|
-
upsertTodo: defineMutation(
|
34
|
-
'upsertTodo',
|
35
|
-
Schema.Struct({ id: Schema.String, text: Schema.optional(Schema.String) }),
|
36
|
-
'INSERT INTO todos (id, text) VALUES ($id, $text) ON CONFLICT (id) DO UPDATE SET text = $text',
|
37
|
-
{
|
38
|
-
facts: ({ id }, currentFacts) =>
|
39
|
-
// TODO enable an API along the lines of `map.has(key, value)`
|
40
|
-
currentFacts.has(facts.todoExists(id)) && currentFacts.get(facts.todoIsWriteable(id, true)[0]) === false
|
41
|
-
? { require: [facts.todoExists(id), facts.todoIsWriteable(id, true)] }
|
42
|
-
: { modify: { set: [facts.todoExists(id), facts.todoIsWriteable(id, true), facts.todoTextUpdated(id)] } },
|
43
|
-
},
|
44
|
-
),
|
45
|
-
todoCompleted: defineMutation(
|
46
|
-
'todoCompleted',
|
47
|
-
Schema.Struct({ id: Schema.String }),
|
48
|
-
// consider `RETURNING` to validate before applying facts
|
49
|
-
'UPDATE todos SET completed = true WHERE id = $id',
|
50
|
-
{
|
51
|
-
// prewrite assertions from DB
|
52
|
-
// enables more concurrency
|
53
|
-
// turning database inside out
|
54
|
-
// similar to upsert semantics
|
55
|
-
facts: ({ id }) => ({
|
56
|
-
require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
|
57
|
-
modify: { set: [facts.todoCompleted(id, true)] },
|
58
|
-
}),
|
59
|
-
},
|
60
|
-
),
|
61
|
-
todoUncompleted: defineMutation(
|
62
|
-
'todoUncompleted',
|
63
|
-
Schema.Struct({ id: Schema.String }),
|
64
|
-
'UPDATE todos SET completed = false WHERE id = $id',
|
65
|
-
{
|
66
|
-
facts: ({ id }) => ({
|
67
|
-
require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
|
68
|
-
modify: { set: [facts.todoCompleted(id, false)] },
|
69
|
-
}),
|
70
|
-
},
|
71
|
-
),
|
72
|
-
todoCompleteds: defineMutation(
|
73
|
-
'todoCompleteds',
|
74
|
-
Schema.Struct({ ids: Schema.Array(Schema.String) }),
|
75
|
-
'UPDATE todos SET completed = true WHERE id IN ($ids:csv)',
|
76
|
-
{
|
77
|
-
facts: ({ ids }) => ({
|
78
|
-
require: ids.flatMap((id) => [facts.todoExists(id), facts.todoIsWriteable(id, true)]),
|
79
|
-
modify: { set: ids.map((id) => facts.todoCompleted(id, true)) },
|
80
|
-
}),
|
81
|
-
},
|
82
|
-
),
|
83
|
-
toggleTodo: defineMutation(
|
84
|
-
'toggleTodo',
|
85
|
-
Schema.Struct({ id: Schema.String }),
|
86
|
-
'UPDATE todos SET completed = NOT completed WHERE id = $id',
|
87
|
-
{
|
88
|
-
facts: ({ id }, currentFacts) => {
|
89
|
-
const currentIsCompleted = currentFacts.get(facts.todoCompleted(id, true)[0]) === true
|
90
|
-
return {
|
91
|
-
require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
|
92
|
-
modify: {
|
93
|
-
// remove: [facts.todoCompleted(id, currentIsCompleted)],
|
94
|
-
set: [facts.todoCompleted(id, !currentIsCompleted)],
|
95
|
-
},
|
96
|
-
}
|
97
|
-
},
|
98
|
-
},
|
99
|
-
),
|
100
|
-
setReadonlyTodo: defineMutation(
|
101
|
-
'setReadonlyTodo',
|
102
|
-
Schema.Struct({ id: Schema.String, readonly: Schema.Boolean }),
|
103
|
-
'UPDATE todos SET readonly = $readonly WHERE id = $id',
|
104
|
-
{
|
105
|
-
facts: ({ id, readonly }) => ({
|
106
|
-
require: [facts.todoExists(id)],
|
107
|
-
modify: { set: [facts.todoIsWriteable(id, !readonly)] },
|
108
|
-
}),
|
109
|
-
},
|
110
|
-
),
|
111
|
-
setTextTodo: defineMutation(
|
112
|
-
'setTextTodo',
|
113
|
-
Schema.Struct({ id: Schema.String, text: Schema.String }),
|
114
|
-
'UPDATE todos SET text = $text WHERE id = $id',
|
115
|
-
{
|
116
|
-
facts: ({ id }) => ({
|
117
|
-
require: [facts.todoExists(id), facts.todoIsWriteable(id, true)],
|
118
|
-
modify: { set: [facts.todoTextUpdated(id)] },
|
119
|
-
}),
|
120
|
-
},
|
121
|
-
),
|
122
|
-
setInputValue: defineMutation(
|
123
|
-
'setInputValue',
|
124
|
-
Schema.Struct({ id: Schema.String, text: Schema.String }),
|
125
|
-
'UPDATE todos SET text = $text WHERE id = $id',
|
126
|
-
{
|
127
|
-
clientOnly: true,
|
128
|
-
facts: ({ id }) => ({ modify: { set: [facts.inputValue(id)] } }),
|
129
|
-
},
|
130
|
-
),
|
131
|
-
}
|
132
|
-
|
133
|
-
export type PartialEvent = { mutation: string; args: any }
|
134
|
-
|
135
|
-
export const toEventNodes = (
|
136
|
-
partialEvents: PartialEvent[],
|
137
|
-
mutationDefs: Record<string, MutationDef.Any>,
|
138
|
-
clientId: string,
|
139
|
-
sessionId: string | undefined,
|
140
|
-
): HistoryDagNode[] => {
|
141
|
-
const nodesAcc: HistoryDagNode[] = [rootEventNode]
|
142
|
-
|
143
|
-
let currentEventId: EventId.EventId = EventId.ROOT
|
144
|
-
|
145
|
-
const eventNodes = partialEvents.map((partialEvent) => {
|
146
|
-
const mutationDef = mutationDefs[partialEvent.mutation]!
|
147
|
-
const eventId = EventId.nextPair(currentEventId, mutationDef.options.clientOnly).id
|
148
|
-
currentEventId = eventId
|
149
|
-
|
150
|
-
const factsSnapshot = factsSnapshotForDag(historyDagFromNodes(nodesAcc, { skipFactsCheck: true }), undefined)
|
151
|
-
// console.log('factsSnapshot', eventId, factsSnapshot)
|
152
|
-
// const depRead: MutationEventFactsSnapshot = new Map<string, any>()
|
153
|
-
// const factsSnapshotProxy = new Proxy(factsSnapshot, {
|
154
|
-
// get: (target, prop) => {
|
155
|
-
// if (prop === 'has') {
|
156
|
-
// return (key: string) => {
|
157
|
-
// depRead.set(key, EMPTY_FACT_VALUE)
|
158
|
-
// return target.has(key)
|
159
|
-
// }
|
160
|
-
// } else if (prop === 'get') {
|
161
|
-
// return (key: string) => {
|
162
|
-
// depRead.set(key, EMPTY_FACT_VALUE)
|
163
|
-
// return target.get(key)
|
164
|
-
// }
|
165
|
-
// }
|
166
|
-
|
167
|
-
// notYetImplemented(`toEventNodes: ${prop.toString()} is not yet implemented`)
|
168
|
-
// },
|
169
|
-
// })
|
170
|
-
|
171
|
-
// const factsRes = mutationDef.options.facts?.(partialEvent.args, factsSnapshotProxy)
|
172
|
-
// console.log('factsRes', factsRes?.modify, factsRes?.require)
|
173
|
-
// const iterableToMap = (iterable: Iterable<MutationEventFactInput>) => {
|
174
|
-
// const map = new Map()
|
175
|
-
// for (const item of iterable) {
|
176
|
-
// if (typeof item === 'string') {
|
177
|
-
// map.set(item, EMPTY_FACT_VALUE)
|
178
|
-
// } else {
|
179
|
-
// map.set(item[0], item[1])
|
180
|
-
// }
|
181
|
-
// }
|
182
|
-
// return map
|
183
|
-
// }
|
184
|
-
// const facts = {
|
185
|
-
// modifyAdd: factsRes?.modify.add ? iterableToMap(factsRes.modify.add) : new Map(),
|
186
|
-
// modifyRemove: factsRes?.modify.remove ? iterableToMap(factsRes.modify.remove) : new Map(),
|
187
|
-
// depRequire: factsRes?.require ? iterableToMap(factsRes.require) : new Map(),
|
188
|
-
// depRead,
|
189
|
-
// } satisfies MutationEventFactsGroup
|
190
|
-
|
191
|
-
// applyFactGroup(facts, factsSnapshot)
|
192
|
-
|
193
|
-
const facts = getFactsGroupForMutationArgs({
|
194
|
-
factsCallback: mutationDef.options.facts,
|
195
|
-
args: partialEvent.args,
|
196
|
-
currentFacts: factsSnapshot,
|
197
|
-
})
|
198
|
-
|
199
|
-
const node = {
|
200
|
-
id: eventId,
|
201
|
-
parentId: getParentId(eventId),
|
202
|
-
mutation: partialEvent.mutation,
|
203
|
-
args: partialEvent.args,
|
204
|
-
factsGroup: facts,
|
205
|
-
clientId,
|
206
|
-
sessionId,
|
207
|
-
} satisfies HistoryDagNode
|
208
|
-
nodesAcc.push(node)
|
209
|
-
return node
|
210
|
-
})
|
211
|
-
|
212
|
-
eventNodes.unshift(rootEventNode as never)
|
213
|
-
|
214
|
-
// console.log('eventNodes', eventNodes)
|
215
|
-
|
216
|
-
return eventNodes
|
217
|
-
}
|
218
|
-
|
219
|
-
const getParentId = (eventId: EventId.EventId): EventId.EventId => {
|
220
|
-
const globalParentId = eventId.global
|
221
|
-
const clientParentId = eventId.client - 1
|
222
|
-
|
223
|
-
if (clientParentId < 0) {
|
224
|
-
return EventId.make({ global: globalParentId - 1, client: EventId.clientDefault })
|
225
|
-
}
|
226
|
-
|
227
|
-
return EventId.make({ global: globalParentId, client: clientParentId })
|
228
|
-
}
|