@livestore/common 0.3.0-dev.12 → 0.3.0-dev.13
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 +21 -21
- package/dist/db-schema/ast/sqlite.d.ts +69 -0
- package/dist/db-schema/ast/sqlite.d.ts.map +1 -0
- package/dist/db-schema/ast/sqlite.js +71 -0
- package/dist/db-schema/ast/sqlite.js.map +1 -0
- package/dist/db-schema/ast/validate.d.ts +3 -0
- package/dist/db-schema/ast/validate.d.ts.map +1 -0
- package/dist/db-schema/ast/validate.js +12 -0
- package/dist/db-schema/ast/validate.js.map +1 -0
- package/dist/db-schema/dsl/field-defs.d.ts +90 -0
- package/dist/db-schema/dsl/field-defs.d.ts.map +1 -0
- package/dist/db-schema/dsl/field-defs.js +87 -0
- package/dist/db-schema/dsl/field-defs.js.map +1 -0
- package/dist/db-schema/dsl/field-defs.test.d.ts +2 -0
- package/dist/db-schema/dsl/field-defs.test.d.ts.map +1 -0
- package/dist/db-schema/dsl/field-defs.test.js +29 -0
- package/dist/db-schema/dsl/field-defs.test.js.map +1 -0
- package/dist/db-schema/dsl/index.d.ts +88 -0
- package/dist/db-schema/dsl/index.d.ts.map +1 -0
- package/dist/db-schema/dsl/index.js +35 -0
- package/dist/db-schema/dsl/index.js.map +1 -0
- package/dist/db-schema/dsl/mod.d.ts +90 -0
- package/dist/db-schema/dsl/mod.d.ts.map +1 -0
- package/dist/db-schema/dsl/mod.js +35 -0
- package/dist/db-schema/dsl/mod.js.map +1 -0
- package/dist/db-schema/dsl/sqlite/field-defs.d.ts +90 -0
- package/dist/db-schema/dsl/sqlite/field-defs.d.ts.map +1 -0
- package/dist/db-schema/dsl/sqlite/field-defs.js +86 -0
- package/dist/db-schema/dsl/sqlite/field-defs.js.map +1 -0
- package/dist/db-schema/dsl/sqlite/field-defs.test.d.ts +2 -0
- package/dist/db-schema/dsl/sqlite/field-defs.test.d.ts.map +1 -0
- package/dist/db-schema/dsl/sqlite/field-defs.test.js +29 -0
- package/dist/db-schema/dsl/sqlite/field-defs.test.js.map +1 -0
- package/dist/db-schema/dsl/sqlite/index.d.ts +88 -0
- package/dist/db-schema/dsl/sqlite/index.d.ts.map +1 -0
- package/dist/db-schema/dsl/sqlite/index.js +35 -0
- package/dist/db-schema/dsl/sqlite/index.js.map +1 -0
- package/dist/db-schema/hash.d.ts +2 -0
- package/dist/db-schema/hash.d.ts.map +1 -0
- package/dist/db-schema/hash.js +14 -0
- package/dist/db-schema/hash.js.map +1 -0
- package/dist/db-schema/index.d.ts +4 -0
- package/dist/db-schema/index.d.ts.map +1 -0
- package/dist/db-schema/index.js +6 -0
- package/dist/db-schema/index.js.map +1 -0
- package/dist/db-schema/mod.d.ts +3 -0
- package/dist/db-schema/mod.d.ts.map +1 -0
- package/dist/db-schema/mod.js +3 -0
- package/dist/db-schema/mod.js.map +1 -0
- package/dist/derived-mutations.d.ts +1 -1
- package/dist/derived-mutations.d.ts.map +1 -1
- package/dist/derived-mutations.js +3 -3
- package/dist/derived-mutations.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +23 -23
- package/dist/devtools/devtools-messages-common.d.ts +6 -6
- package/dist/devtools/devtools-messages-leader.d.ts +41 -53
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
- package/dist/devtools/devtools-messages-leader.js +5 -4
- package/dist/devtools/devtools-messages-leader.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.js +24 -22
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
- package/dist/leader-thread/apply-mutation.d.ts.map +1 -1
- package/dist/leader-thread/apply-mutation.js +8 -6
- package/dist/leader-thread/apply-mutation.js.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.js +6 -12
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +6 -1
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mutationlog.d.ts +1 -1
- package/dist/leader-thread/mutationlog.d.ts.map +1 -1
- package/dist/leader-thread/mutationlog.js +7 -5
- package/dist/leader-thread/mutationlog.js.map +1 -1
- package/dist/leader-thread/pull-queue-set.d.ts.map +1 -1
- package/dist/leader-thread/types.d.ts +7 -3
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/leader-thread/types.js +1 -1
- package/dist/leader-thread/types.js.map +1 -1
- package/dist/query-builder/api.d.ts +1 -1
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.js +8 -6
- package/dist/rehydrate-from-mutationlog.js.map +1 -1
- package/dist/schema/EventId.d.ts +9 -9
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +12 -12
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/EventId.test.js +3 -3
- package/dist/schema/EventId.test.js.map +1 -1
- package/dist/schema/MutationEvent.d.ts +37 -12
- package/dist/schema/MutationEvent.d.ts.map +1 -1
- package/dist/schema/MutationEvent.js +20 -4
- package/dist/schema/MutationEvent.js.map +1 -1
- package/dist/schema/db-schema/ast/sqlite.d.ts +69 -0
- package/dist/schema/db-schema/ast/sqlite.d.ts.map +1 -0
- package/dist/schema/db-schema/ast/sqlite.js +71 -0
- package/dist/schema/db-schema/ast/sqlite.js.map +1 -0
- package/dist/schema/db-schema/ast/validate.d.ts +3 -0
- package/dist/schema/db-schema/ast/validate.d.ts.map +1 -0
- package/dist/schema/db-schema/ast/validate.js +12 -0
- package/dist/schema/db-schema/ast/validate.js.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.d.ts +90 -0
- package/dist/schema/db-schema/dsl/field-defs.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.js +87 -0
- package/dist/schema/db-schema/dsl/field-defs.js.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.test.d.ts +2 -0
- package/dist/schema/db-schema/dsl/field-defs.test.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/field-defs.test.js +29 -0
- package/dist/schema/db-schema/dsl/field-defs.test.js.map +1 -0
- package/dist/schema/db-schema/dsl/mod.d.ts +88 -0
- package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -0
- package/dist/schema/db-schema/dsl/mod.js +35 -0
- package/dist/schema/db-schema/dsl/mod.js.map +1 -0
- package/dist/schema/db-schema/hash.d.ts +2 -0
- package/dist/schema/db-schema/hash.d.ts.map +1 -0
- package/dist/schema/db-schema/hash.js +14 -0
- package/dist/schema/db-schema/hash.js.map +1 -0
- package/dist/schema/db-schema/mod.d.ts +3 -0
- package/dist/schema/db-schema/mod.d.ts.map +1 -0
- package/dist/schema/db-schema/mod.js +3 -0
- package/dist/schema/db-schema/mod.js.map +1 -0
- package/dist/schema/mod.d.ts +1 -0
- package/dist/schema/mod.d.ts.map +1 -1
- package/dist/schema/mod.js +1 -0
- package/dist/schema/mod.js.map +1 -1
- package/dist/schema/mutations.d.ts +5 -9
- package/dist/schema/mutations.d.ts.map +1 -1
- package/dist/schema/mutations.js +2 -2
- package/dist/schema/mutations.js.map +1 -1
- package/dist/schema/schema-helpers.js +1 -1
- package/dist/schema/schema-helpers.js.map +1 -1
- package/dist/schema/schema.d.ts +1 -1
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +1 -1
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/system-tables.d.ts +47 -29
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +10 -7
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema/table-def.d.ts +18 -14
- package/dist/schema/table-def.d.ts.map +1 -1
- package/dist/schema/table-def.js +3 -4
- package/dist/schema/table-def.js.map +1 -1
- package/dist/schema-management/migrations.d.ts +1 -1
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +1 -1
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/sql-queries/sql-queries.d.ts +1 -1
- package/dist/sql-queries/sql-queries.d.ts.map +1 -1
- package/dist/sql-queries/sql-queries.js.map +1 -1
- package/dist/sql-queries/sql-query-builder.d.ts +1 -1
- package/dist/sql-queries/sql-query-builder.d.ts.map +1 -1
- package/dist/sql-queries/sql-query-builder.js.map +1 -1
- package/dist/sql-queries/types.d.ts +2 -1
- package/dist/sql-queries/types.d.ts.map +1 -1
- package/dist/sql-queries/types.js.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.js +18 -3
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
- package/dist/sync/next/facts.js +1 -1
- package/dist/sync/next/facts.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +2 -0
- package/dist/sync/next/history-dag-common.d.ts.map +1 -1
- package/dist/sync/next/history-dag-common.js +3 -1
- package/dist/sync/next/history-dag-common.js.map +1 -1
- package/dist/sync/next/history-dag.d.ts.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 +3 -1
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +5 -3
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.calculator.test.js +12 -12
- package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +43 -43
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts +11 -11
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.js +12 -10
- package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
- package/dist/sync/sync.d.ts +2 -2
- package/dist/sync/syncstate.d.ts +9 -9
- package/dist/sync/syncstate.js +6 -6
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +18 -16
- package/dist/sync/syncstate.test.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -3
- package/src/derived-mutations.ts +4 -4
- package/src/devtools/devtools-messages-leader.ts +5 -4
- package/src/index.ts +1 -1
- package/src/leader-thread/LeaderSyncProcessor.ts +28 -22
- package/src/leader-thread/apply-mutation.ts +15 -5
- package/src/leader-thread/leader-worker-devtools.ts +6 -13
- package/src/leader-thread/make-leader-thread-layer.ts +9 -1
- package/src/leader-thread/mutationlog.ts +8 -6
- package/src/leader-thread/types.ts +6 -2
- package/src/query-builder/api.ts +1 -1
- package/src/rehydrate-from-mutationlog.ts +8 -6
- package/src/schema/EventId.test.ts +3 -3
- package/src/schema/EventId.ts +17 -17
- package/src/schema/MutationEvent.ts +31 -6
- package/src/schema/db-schema/ast/sqlite.ts +142 -0
- package/src/schema/db-schema/ast/validate.ts +13 -0
- package/src/schema/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +206 -0
- package/src/schema/db-schema/dsl/field-defs.test.ts +35 -0
- package/src/schema/db-schema/dsl/field-defs.ts +242 -0
- package/src/schema/db-schema/dsl/mod.ts +195 -0
- package/src/schema/db-schema/hash.ts +14 -0
- package/src/schema/db-schema/mod.ts +2 -0
- package/src/schema/mod.ts +1 -0
- package/src/schema/mutations.ts +6 -19
- package/src/schema/schema-helpers.ts +1 -1
- package/src/schema/schema.ts +2 -2
- package/src/schema/system-tables.ts +10 -7
- package/src/schema/table-def.ts +17 -16
- package/src/schema-management/migrations.ts +1 -1
- package/src/sql-queries/sql-queries.ts +1 -1
- package/src/sql-queries/sql-query-builder.ts +1 -2
- package/src/sql-queries/types.ts +3 -1
- package/src/sync/ClientSessionSyncProcessor.ts +23 -4
- package/src/sync/next/facts.ts +1 -1
- package/src/sync/next/history-dag-common.ts +5 -1
- package/src/sync/next/history-dag.ts +1 -1
- package/src/sync/next/rebase-events.ts +8 -2
- package/src/sync/next/test/compact-events.calculator.test.ts +12 -12
- package/src/sync/next/test/compact-events.test.ts +43 -43
- package/src/sync/next/test/mutation-fixtures.ts +16 -12
- package/src/sync/syncstate.test.ts +19 -17
- package/src/sync/syncstate.ts +6 -6
- package/src/version.ts +1 -1
- package/tsconfig.json +1 -1
package/src/schema/mod.ts
CHANGED
package/src/schema/mutations.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import { Schema } from '@livestore/utils/effect'
|
2
2
|
|
3
3
|
import type { BindValues } from '../sql-queries/sql-queries.js'
|
4
|
-
import type * as EventId from './EventId.js'
|
5
4
|
|
6
5
|
export type MutationDefMap = Map<string | 'livestore.RawSql', MutationDef.Any>
|
7
6
|
export type MutationDefRecord = {
|
@@ -38,24 +37,17 @@ export type MutationDef<TName extends string, TFrom, TTo> = {
|
|
38
37
|
/** Warning: This feature is not fully implemented yet */
|
39
38
|
historyId: string
|
40
39
|
/**
|
41
|
-
* When set to true, the mutation won't be synced
|
40
|
+
* When set to true, the mutation won't be synced across clients but
|
42
41
|
*/
|
43
|
-
|
42
|
+
clientOnly: boolean
|
44
43
|
/** Warning: This feature is not fully implemented yet */
|
45
44
|
facts: FactsCallback<TTo> | undefined
|
46
45
|
}
|
47
46
|
|
48
47
|
/** Helper function to construct a partial mutation event */
|
49
|
-
(
|
50
|
-
args: TTo,
|
51
|
-
options?: {
|
52
|
-
id?: number
|
53
|
-
},
|
54
|
-
): {
|
48
|
+
(args: TTo): {
|
55
49
|
mutation: TName
|
56
50
|
args: TTo
|
57
|
-
// TODO remove/clean up after sync-next is fully implemented
|
58
|
-
id?: EventId.EventId
|
59
51
|
}
|
60
52
|
}
|
61
53
|
|
@@ -120,7 +112,7 @@ export type DefineMutationOptions<TTo> = {
|
|
120
112
|
/**
|
121
113
|
* When set to true, the mutation won't be synced over the network
|
122
114
|
*/
|
123
|
-
|
115
|
+
clientOnly?: boolean
|
124
116
|
}
|
125
117
|
|
126
118
|
// TODO possibly also allow for mutation event subsumption behaviour
|
@@ -130,12 +122,7 @@ export const defineMutation = <TName extends string, TFrom, TTo>(
|
|
130
122
|
sql: MutationDefSqlResult<NoInfer<TTo>>,
|
131
123
|
options?: DefineMutationOptions<TTo>,
|
132
124
|
): MutationDef<TName, TFrom, TTo> => {
|
133
|
-
const makePartialEvent = (
|
134
|
-
args: TTo,
|
135
|
-
options?: {
|
136
|
-
id?: EventId.EventId
|
137
|
-
},
|
138
|
-
) => ({ mutation: name, args, ...options })
|
125
|
+
const makePartialEvent = (args: TTo) => ({ mutation: name, args })
|
139
126
|
|
140
127
|
Object.defineProperty(makePartialEvent, 'name', { value: name })
|
141
128
|
Object.defineProperty(makePartialEvent, 'schema', { value: schema })
|
@@ -143,7 +130,7 @@ export const defineMutation = <TName extends string, TFrom, TTo>(
|
|
143
130
|
Object.defineProperty(makePartialEvent, 'options', {
|
144
131
|
value: {
|
145
132
|
historyId: options?.historyId ?? 'main',
|
146
|
-
|
133
|
+
clientOnly: options?.clientOnly ?? false,
|
147
134
|
facts: options?.facts
|
148
135
|
? (args, currentFacts) => {
|
149
136
|
const res = options.facts!(args, currentFacts)
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { SqliteDsl } from '@livestore/db-schema'
|
2
1
|
import { shouldNeverHappen } from '@livestore/utils'
|
3
2
|
import { pipe, ReadonlyRecord, Schema } from '@livestore/utils/effect'
|
4
3
|
|
4
|
+
import { SqliteDsl } from '../schema/db-schema/mod.js'
|
5
5
|
import type { TableDef, TableDefBase } from './table-def.js'
|
6
6
|
|
7
7
|
export const getDefaultValuesEncoded = <TTableDef extends TableDef>(
|
package/src/schema/schema.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import type { SqliteDsl } from '@livestore/db-schema'
|
2
|
-
import { SqliteAst } from '@livestore/db-schema'
|
3
1
|
import { isReadonlyArray, shouldNeverHappen } from '@livestore/utils'
|
4
2
|
|
5
3
|
import type { MigrationOptions } from '../adapter-types.js'
|
6
4
|
import { makeDerivedMutationDefsForTable } from '../derived-mutations.js'
|
5
|
+
import type { SqliteDsl } from './db-schema/mod.js'
|
6
|
+
import { SqliteAst } from './db-schema/mod.js'
|
7
7
|
import {
|
8
8
|
type MutationDef,
|
9
9
|
type MutationDefMap,
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { type SqliteAst as __SqliteAst, SqliteDsl } from '@livestore/db-schema'
|
2
1
|
import { Schema } from '@livestore/utils/effect'
|
3
2
|
|
3
|
+
import { SqliteDsl } from './db-schema/mod.js'
|
4
4
|
import * as EventId from './EventId.js'
|
5
5
|
import type { FromTable } from './table-def.js'
|
6
6
|
import { table } from './table-def.js'
|
@@ -48,13 +48,13 @@ export const sessionChangesetMetaTable = table(
|
|
48
48
|
{
|
49
49
|
// TODO bring back primary key
|
50
50
|
idGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
|
51
|
-
|
51
|
+
idClient: SqliteDsl.integer({ schema: EventId.ClientEventId }),
|
52
52
|
changeset: SqliteDsl.blob({ nullable: true }),
|
53
53
|
debug: SqliteDsl.json({ nullable: true }),
|
54
54
|
},
|
55
55
|
{
|
56
56
|
disableAutomaticIdColumn: true,
|
57
|
-
indexes: [{ columns: ['idGlobal', '
|
57
|
+
indexes: [{ columns: ['idGlobal', 'idClient'], name: 'idx_session_changeset_id' }],
|
58
58
|
},
|
59
59
|
)
|
60
60
|
|
@@ -64,7 +64,7 @@ export const systemTables = [schemaMetaTable, schemaMutationsMetaTable, sessionC
|
|
64
64
|
|
65
65
|
/// Mutation log DB
|
66
66
|
|
67
|
-
export const SyncStatus = Schema.Literal('synced', 'pending', 'error', '
|
67
|
+
export const SyncStatus = Schema.Literal('synced', 'pending', 'error', 'clientOnly')
|
68
68
|
export type SyncStatus = typeof SyncStatus.Type
|
69
69
|
|
70
70
|
export const MUTATION_LOG_META_TABLE = 'mutation_log'
|
@@ -73,11 +73,14 @@ export const mutationLogMetaTable = table(
|
|
73
73
|
MUTATION_LOG_META_TABLE,
|
74
74
|
{
|
75
75
|
idGlobal: SqliteDsl.integer({ primaryKey: true, schema: EventId.GlobalEventId }),
|
76
|
-
|
76
|
+
idClient: SqliteDsl.integer({ primaryKey: true, schema: EventId.ClientEventId }),
|
77
77
|
parentIdGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
|
78
|
-
|
78
|
+
parentIdClient: SqliteDsl.integer({ schema: EventId.ClientEventId }),
|
79
79
|
mutation: SqliteDsl.text({}),
|
80
80
|
argsJson: SqliteDsl.text({ schema: Schema.parseJson(Schema.Any) }),
|
81
|
+
clientId: SqliteDsl.text({}),
|
82
|
+
/** Only available for mutations which were executed in this client */
|
83
|
+
sessionId: SqliteDsl.text({ nullable: true }),
|
81
84
|
schemaHash: SqliteDsl.integer({}),
|
82
85
|
syncMetadataJson: SqliteDsl.text({ schema: Schema.parseJson(Schema.Option(Schema.JsonValue)) }),
|
83
86
|
},
|
@@ -85,7 +88,7 @@ export const mutationLogMetaTable = table(
|
|
85
88
|
disableAutomaticIdColumn: true,
|
86
89
|
indexes: [
|
87
90
|
{ columns: ['idGlobal'], name: 'idx_idGlobal' },
|
88
|
-
{ columns: ['idGlobal', '
|
91
|
+
{ columns: ['idGlobal', 'idClient'], name: 'idx_mutationlog_id' },
|
89
92
|
],
|
90
93
|
},
|
91
94
|
)
|
package/src/schema/table-def.ts
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
import type { Nullable
|
2
|
-
import { SqliteDsl } from '@livestore/db-schema'
|
1
|
+
import type { Nullable } from '@livestore/utils'
|
3
2
|
import { shouldNeverHappen } from '@livestore/utils'
|
3
|
+
import type { Types } from '@livestore/utils/effect'
|
4
4
|
import { ReadonlyRecord, Schema } from '@livestore/utils/effect'
|
5
5
|
|
6
6
|
import type { DerivedMutationHelperFns } from '../derived-mutations.js'
|
7
7
|
import { makeDerivedMutationDefsForTable } from '../derived-mutations.js'
|
8
8
|
import type { QueryBuilder } from '../query-builder/mod.js'
|
9
9
|
import { makeQueryBuilder } from '../query-builder/mod.js'
|
10
|
+
import { SqliteDsl } from './db-schema/mod.js'
|
10
11
|
|
11
12
|
export const { blob, boolean, column, datetime, integer, isColumnDefinition, json, real, text } = SqliteDsl
|
12
13
|
|
13
|
-
export { SqliteDsl } from '@livestore/db-schema'
|
14
|
-
|
15
14
|
export type StateType = 'singleton' | 'dynamic'
|
16
15
|
|
17
16
|
export type DefaultSqliteTableDef = SqliteDsl.TableDefinition<string, SqliteDsl.Columns>
|
@@ -61,7 +60,7 @@ export type TableOptionsInput = Partial<{
|
|
61
60
|
| boolean
|
62
61
|
| {
|
63
62
|
enabled: true
|
64
|
-
|
63
|
+
clientOnly?: boolean
|
65
64
|
}
|
66
65
|
}>
|
67
66
|
|
@@ -115,7 +114,7 @@ export type TableOptions = {
|
|
115
114
|
/**
|
116
115
|
* When set to true, the mutations won't be synced over the network
|
117
116
|
*/
|
118
|
-
|
117
|
+
clientOnly: boolean
|
119
118
|
}
|
120
119
|
|
121
120
|
/** Derived based on whether the table definition has one or more columns (besides the `id` column) */
|
@@ -153,12 +152,12 @@ export const table = <
|
|
153
152
|
disableAutomaticIdColumn: options?.disableAutomaticIdColumn ?? false,
|
154
153
|
deriveMutations:
|
155
154
|
options?.deriveMutations === true
|
156
|
-
? { enabled: true as const,
|
155
|
+
? { enabled: true as const, clientOnly: false }
|
157
156
|
: options?.deriveMutations === false
|
158
157
|
? { enabled: false as const }
|
159
158
|
: options?.deriveMutations === undefined
|
160
159
|
? { enabled: false as const }
|
161
|
-
: { enabled: true as const,
|
160
|
+
: { enabled: true as const, clientOnly: options.deriveMutations.clientOnly ?? false },
|
162
161
|
isSingleColumn: SqliteDsl.isColumnDefinition(columnOrColumns) === true,
|
163
162
|
requiredInsertColumnNames: 'type-level-only',
|
164
163
|
}
|
@@ -234,7 +233,7 @@ export const table = <
|
|
234
233
|
export const tableHasDerivedMutations = <TTableDef extends TableDefBase>(
|
235
234
|
tableDef: TTableDef,
|
236
235
|
): tableDef is TTableDef & {
|
237
|
-
options: { deriveMutations: { enabled: true;
|
236
|
+
options: { deriveMutations: { enabled: true; clientOnly: boolean } }
|
238
237
|
} & DerivedMutationHelperFns<TTableDef['sqliteDef']['columns'], TTableDef['options']> =>
|
239
238
|
tableDef.options.deriveMutations.enabled === true
|
240
239
|
|
@@ -242,6 +241,8 @@ export const tableIsSingleton = <TTableDef extends TableDefBase>(
|
|
242
241
|
tableDef: TTableDef,
|
243
242
|
): tableDef is TTableDef & { options: { isSingleton: true } } => tableDef.options.isSingleton === true
|
244
243
|
|
244
|
+
export type PrettifyFlat<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
|
245
|
+
|
245
246
|
type SqliteTableDefForInput<
|
246
247
|
TName extends string,
|
247
248
|
TColumns extends SqliteDsl.Columns | SqliteDsl.ColumnDefinition<any, any>,
|
@@ -268,13 +269,13 @@ type WithDefaults<
|
|
268
269
|
isSingleton: TOptionsInput['isSingleton'] extends true ? true : false
|
269
270
|
disableAutomaticIdColumn: TOptionsInput['disableAutomaticIdColumn'] extends true ? true : false
|
270
271
|
deriveMutations: TOptionsInput['deriveMutations'] extends true
|
271
|
-
? { enabled: true;
|
272
|
+
? { enabled: true; clientOnly: boolean }
|
272
273
|
: TOptionsInput['deriveMutations'] extends false
|
273
274
|
? { enabled: false }
|
274
|
-
: TOptionsInput['deriveMutations'] extends { enabled: true;
|
275
|
+
: TOptionsInput['deriveMutations'] extends { enabled: true; clientOnly?: boolean }
|
275
276
|
? {
|
276
277
|
enabled: true
|
277
|
-
|
278
|
+
clientOnly: TOptionsInput['deriveMutations']['clientOnly'] extends true ? true : false
|
278
279
|
}
|
279
280
|
: never
|
280
281
|
isSingleColumn: SqliteDsl.IsSingleColumn<TColumns>
|
@@ -283,7 +284,7 @@ type WithDefaults<
|
|
283
284
|
|
284
285
|
export namespace FromTable {
|
285
286
|
// TODO this sometimes doesn't preserve the order of columns
|
286
|
-
export type RowDecoded<TTableDef extends TableDefBase> =
|
287
|
+
export type RowDecoded<TTableDef extends TableDefBase> = Types.Simplify<
|
287
288
|
Nullable<Pick<RowDecodedAll<TTableDef>, NullableColumnNames<TTableDef>>> &
|
288
289
|
Omit<RowDecodedAll<TTableDef>, NullableColumnNames<TTableDef>>
|
289
290
|
>
|
@@ -302,7 +303,7 @@ export namespace FromTable {
|
|
302
303
|
>
|
303
304
|
}
|
304
305
|
|
305
|
-
export type RowEncoded<TTableDef extends TableDefBase> =
|
306
|
+
export type RowEncoded<TTableDef extends TableDefBase> = Types.Simplify<
|
306
307
|
Nullable<Pick<RowEncodeNonNullable<TTableDef>, NullableColumnNames<TTableDef>>> &
|
307
308
|
Omit<RowEncodeNonNullable<TTableDef>, NullableColumnNames<TTableDef>>
|
308
309
|
>
|
@@ -314,7 +315,7 @@ export namespace FromTable {
|
|
314
315
|
|
315
316
|
export namespace FromColumns {
|
316
317
|
// TODO this sometimes doesn't preserve the order of columns
|
317
|
-
export type RowDecoded<TColumns extends SqliteDsl.Columns> =
|
318
|
+
export type RowDecoded<TColumns extends SqliteDsl.Columns> = Types.Simplify<
|
318
319
|
Nullable<Pick<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>> &
|
319
320
|
Omit<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>
|
320
321
|
>
|
@@ -323,7 +324,7 @@ export namespace FromColumns {
|
|
323
324
|
[K in keyof TColumns]: Schema.Schema.Type<TColumns[K]['schema']>
|
324
325
|
}
|
325
326
|
|
326
|
-
export type RowEncoded<TColumns extends SqliteDsl.Columns> =
|
327
|
+
export type RowEncoded<TColumns extends SqliteDsl.Columns> = Types.Simplify<
|
327
328
|
Nullable<Pick<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>> &
|
328
329
|
Omit<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>
|
329
330
|
>
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import { SqliteAst, SqliteDsl } from '@livestore/db-schema'
|
2
1
|
import { memoizeByStringifyArgs } from '@livestore/utils'
|
3
2
|
import { Effect, Schema as EffectSchema } from '@livestore/utils/effect'
|
4
3
|
|
5
4
|
import type { MigrationsReport, MigrationsReportEntry, SqliteDb, UnexpectedError } from '../adapter-types.js'
|
5
|
+
import { SqliteAst, SqliteDsl } from '../schema/db-schema/mod.js'
|
6
6
|
import type { LiveStoreSchema } from '../schema/mod.js'
|
7
7
|
import type { SchemaMetaRow, SchemaMutationsMetaRow } from '../schema/system-tables.js'
|
8
8
|
import {
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import type { SqliteDsl } from '@livestore/db-schema'
|
2
1
|
import { shouldNeverHappen } from '@livestore/utils'
|
3
2
|
import { pipe, ReadonlyArray, Schema, TreeFormatter } from '@livestore/utils/effect'
|
4
3
|
|
4
|
+
import type { SqliteDsl } from '../schema/db-schema/mod.js'
|
5
5
|
import { sql } from '../util.js'
|
6
6
|
import { objectEntries } from './misc.js'
|
7
7
|
import * as ClientTypes from './types.js'
|
package/src/sql-queries/types.ts
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
import type { Prettify
|
1
|
+
import type { Prettify } from '@livestore/utils'
|
2
2
|
import type { Schema } from '@livestore/utils/effect'
|
3
3
|
|
4
|
+
import type { SqliteDsl } from '../schema/db-schema/mod.js'
|
5
|
+
|
4
6
|
export type DecodedValuesForTableAll<TSchema extends SqliteDsl.DbSchema, TTableName extends keyof TSchema> = {
|
5
7
|
[K in keyof GetColumns<TSchema, TTableName>]: Schema.Schema.Type<GetColumn<TSchema, TTableName, K>['schema']>
|
6
8
|
}
|
@@ -54,10 +54,9 @@ export const makeClientSessionSyncProcessor = ({
|
|
54
54
|
}
|
55
55
|
|
56
56
|
const syncStateUpdateQueue = Queue.unbounded<SyncState.SyncState>().pipe(Effect.runSync)
|
57
|
-
|
58
57
|
const isLocalEvent = (mutationEventEncoded: MutationEvent.EncodedWithMeta) => {
|
59
58
|
const mutationDef = schema.mutations.get(mutationEventEncoded.mutation)!
|
60
|
-
return mutationDef.options.
|
59
|
+
return mutationDef.options.clientOnly
|
61
60
|
}
|
62
61
|
|
63
62
|
const push: ClientSessionSyncProcessor['push'] = (batch, { otelContext }) => {
|
@@ -66,10 +65,15 @@ export const makeClientSessionSyncProcessor = ({
|
|
66
65
|
let baseEventId = syncStateRef.current.localHead
|
67
66
|
const encodedMutationEvents = batch.map((mutationEvent) => {
|
68
67
|
const mutationDef = schema.mutations.get(mutationEvent.mutation)!
|
69
|
-
const nextIdPair = EventId.nextPair(baseEventId, mutationDef.options.
|
68
|
+
const nextIdPair = EventId.nextPair(baseEventId, mutationDef.options.clientOnly)
|
70
69
|
baseEventId = nextIdPair.id
|
71
70
|
return new MutationEvent.EncodedWithMeta(
|
72
|
-
Schema.encodeUnknownSync(mutationEventSchema)({
|
71
|
+
Schema.encodeUnknownSync(mutationEventSchema)({
|
72
|
+
...mutationEvent,
|
73
|
+
...nextIdPair,
|
74
|
+
clientId: clientSession.clientId,
|
75
|
+
sessionId: clientSession.sessionId,
|
76
|
+
}),
|
73
77
|
)
|
74
78
|
})
|
75
79
|
|
@@ -114,6 +118,21 @@ export const makeClientSessionSyncProcessor = ({
|
|
114
118
|
const otelContext = otel.trace.setSpan(otel.context.active(), span)
|
115
119
|
|
116
120
|
const boot: ClientSessionSyncProcessor['boot'] = Effect.gen(function* () {
|
121
|
+
// eslint-disable-next-line unicorn/prefer-global-this
|
122
|
+
if (typeof window !== 'undefined') {
|
123
|
+
const onBeforeUnload = (event: BeforeUnloadEvent) => {
|
124
|
+
if (syncStateRef.current.pending.length > 0) {
|
125
|
+
// Trigger the default browser dialog
|
126
|
+
event.preventDefault()
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
yield* Effect.acquireRelease(
|
131
|
+
Effect.sync(() => window.addEventListener('beforeunload', onBeforeUnload)),
|
132
|
+
() => Effect.sync(() => window.removeEventListener('beforeunload', onBeforeUnload)),
|
133
|
+
)
|
134
|
+
}
|
135
|
+
|
117
136
|
yield* clientSession.leaderThread.mutations.pull.pipe(
|
118
137
|
Stream.tap(({ payload, remaining }) =>
|
119
138
|
Effect.gen(function* () {
|
package/src/sync/next/facts.ts
CHANGED
@@ -20,7 +20,7 @@ export const emptyHistoryDag = (): HistoryDag =>
|
|
20
20
|
})
|
21
21
|
|
22
22
|
// TODO consider making `ROOT_ID` parent to itself
|
23
|
-
export const rootParentId = EventId.make({ global: EventId.ROOT.global - 1,
|
23
|
+
export const rootParentId = EventId.make({ global: EventId.ROOT.global - 1, client: EventId.clientDefault })
|
24
24
|
|
25
25
|
export type HistoryDagNode = {
|
26
26
|
id: EventId.EventId
|
@@ -30,6 +30,8 @@ export type HistoryDagNode = {
|
|
30
30
|
/** Facts are being used for conflict detection and history compaction */
|
31
31
|
factsGroup: MutationEventFactsGroup
|
32
32
|
meta?: any
|
33
|
+
clientId: string
|
34
|
+
sessionId: string | undefined
|
33
35
|
}
|
34
36
|
|
35
37
|
export const rootEventNode: HistoryDagNode = {
|
@@ -39,6 +41,8 @@ export const rootEventNode: HistoryDagNode = {
|
|
39
41
|
mutation: '__Root__',
|
40
42
|
args: {},
|
41
43
|
factsGroup: { modifySet: new Map(), modifyUnset: new Map(), depRequire: new Map(), depRead: new Map() },
|
44
|
+
clientId: 'root',
|
45
|
+
sessionId: undefined,
|
42
46
|
}
|
43
47
|
|
44
48
|
export const EMPTY_FACT_VALUE = Symbol('EMPTY_FACT_VALUE')
|
@@ -3,7 +3,7 @@ import { factsToString, validateFacts } from './facts.js'
|
|
3
3
|
import { emptyHistoryDag, type HistoryDagNode, rootParentId } from './history-dag-common.js'
|
4
4
|
|
5
5
|
export const eventIdToString = (eventId: EventId.EventId) =>
|
6
|
-
eventId.
|
6
|
+
eventId.client === 0 ? eventId.global.toString() : `${eventId.global}.${eventId.client}`
|
7
7
|
|
8
8
|
export const historyDagFromNodes = (dagNodes: HistoryDagNode[], options?: { skipFactsCheck: boolean }) => {
|
9
9
|
if (options?.skipFactsCheck !== true) {
|
@@ -43,11 +43,15 @@ export const rebaseEvents = ({
|
|
43
43
|
pendingLocalEvents,
|
44
44
|
newRemoteEvents,
|
45
45
|
currentFactsSnapshot,
|
46
|
+
clientId,
|
47
|
+
sessionId,
|
46
48
|
}: {
|
47
49
|
pendingLocalEvents: HistoryDagNode[]
|
48
50
|
newRemoteEvents: HistoryDagNode[]
|
49
51
|
rebaseFn: RebaseFn
|
50
52
|
currentFactsSnapshot: MutationEventFactsSnapshot
|
53
|
+
clientId: string
|
54
|
+
sessionId: string | undefined
|
51
55
|
}): ReadonlyArray<MutationEvent.AnyDecoded> => {
|
52
56
|
const initialSnapshot = new Map(currentFactsSnapshot)
|
53
57
|
applyFactGroups(
|
@@ -89,10 +93,12 @@ export const rebaseEvents = ({
|
|
89
93
|
return rebasedLocalEvents.map(
|
90
94
|
(event, index) =>
|
91
95
|
({
|
92
|
-
id: EventId.make({ global: headGlobalId + index + 1,
|
93
|
-
parentId: EventId.make({ global: headGlobalId + index,
|
96
|
+
id: EventId.make({ global: headGlobalId + index + 1, client: EventId.clientDefault }),
|
97
|
+
parentId: EventId.make({ global: headGlobalId + index, client: EventId.clientDefault }),
|
94
98
|
mutation: event.mutation,
|
95
99
|
args: event.args,
|
100
|
+
clientId,
|
101
|
+
sessionId,
|
96
102
|
}) satisfies MutationEvent.AnyDecoded,
|
97
103
|
)
|
98
104
|
}
|
@@ -10,7 +10,7 @@ import { toEventNodes } from './mutation-fixtures.js'
|
|
10
10
|
expect.addSnapshotSerializer(customSerializer)
|
11
11
|
|
12
12
|
const compact = (events: any[]) => {
|
13
|
-
const dag = historyDagFromNodes(toEventNodes(events, mutations))
|
13
|
+
const dag = historyDagFromNodes(toEventNodes(events, mutations, 'client-id', 'session-id'))
|
14
14
|
const compacted = compactEvents(dag)
|
15
15
|
|
16
16
|
return Array.from(compacted.dag.nodeEntries())
|
@@ -50,8 +50,8 @@ describe('compactEvents calculator', () => {
|
|
50
50
|
|
51
51
|
expect(expected).toMatchInlineSnapshot(`
|
52
52
|
[
|
53
|
-
{ id: 0, parentId: -1, mutation: "add", args: { value: 1 }, facts: "" }
|
54
|
-
{ id: 1, parentId: 0, mutation: "add", args: { value: 1 }, facts: "" }
|
53
|
+
{ id: 0, parentId: -1, mutation: "add", args: { value: 1 }, clientId: "client-id", sessionId: "session-id", facts: "" }
|
54
|
+
{ id: 1, parentId: 0, mutation: "add", args: { value: 1 }, clientId: "client-id", sessionId: "session-id", facts: "" }
|
55
55
|
]
|
56
56
|
`)
|
57
57
|
})
|
@@ -64,8 +64,8 @@ describe('compactEvents calculator', () => {
|
|
64
64
|
|
65
65
|
expect(expected).toMatchInlineSnapshot(`
|
66
66
|
[
|
67
|
-
{ id: 0, parentId: -1, mutation: "multiply", args: { value: 2 }, facts: "?multiplyByZero -multiplyByZero" }
|
68
|
-
{ id: 1, parentId: 0, mutation: "multiply", args: { value: 2 }, facts: "?multiplyByZero -multiplyByZero" }
|
67
|
+
{ id: 0, parentId: -1, mutation: "multiply", args: { value: 2 }, clientId: "client-id", sessionId: "session-id", facts: "?multiplyByZero -multiplyByZero" }
|
68
|
+
{ id: 1, parentId: 0, mutation: "multiply", args: { value: 2 }, clientId: "client-id", sessionId: "session-id", facts: "?multiplyByZero -multiplyByZero" }
|
69
69
|
]
|
70
70
|
`)
|
71
71
|
})
|
@@ -79,7 +79,7 @@ describe('compactEvents calculator', () => {
|
|
79
79
|
|
80
80
|
expect(expected).toMatchInlineSnapshot(`
|
81
81
|
[
|
82
|
-
{ id: 2, parentId: -1, mutation: "multiply", args: { value: 0 }, facts: "+multiplyByZero" }
|
82
|
+
{ id: 2, parentId: -1, mutation: "multiply", args: { value: 0 }, clientId: "client-id", sessionId: "session-id", facts: "+multiplyByZero" }
|
83
83
|
]
|
84
84
|
`)
|
85
85
|
})
|
@@ -94,8 +94,8 @@ describe('compactEvents calculator', () => {
|
|
94
94
|
|
95
95
|
expect(expected).toMatchInlineSnapshot(`
|
96
96
|
[
|
97
|
-
{ id: 2, parentId: -1, mutation: "multiply", args: { value: 0 }, facts: "+multiplyByZero" }
|
98
|
-
{ id: 3, parentId: 2, mutation: "add", args: { value: 1 }, facts: "" }
|
97
|
+
{ id: 2, parentId: -1, mutation: "multiply", args: { value: 0 }, clientId: "client-id", sessionId: "session-id", facts: "+multiplyByZero" }
|
98
|
+
{ id: 3, parentId: 2, mutation: "add", args: { value: 1 }, clientId: "client-id", sessionId: "session-id", facts: "" }
|
99
99
|
]
|
100
100
|
`)
|
101
101
|
})
|
@@ -111,10 +111,10 @@ describe('compactEvents calculator', () => {
|
|
111
111
|
|
112
112
|
expect(expected).toMatchInlineSnapshot(`
|
113
113
|
[
|
114
|
-
{ id: 0, parentId: -1, mutation: "add", args: { value: 1 }, facts: "" }
|
115
|
-
{ id: 2, parentId: 0, mutation: "multiply", args: { value: 0 }, facts: "+multiplyByZero" }
|
116
|
-
{ id: 3, parentId: 2, mutation: "multiply", args: { value: 2 }, facts: "?multiplyByZero +multiplyByZero -multiplyByZero" }
|
117
|
-
{ id: 4, parentId: 3, mutation: "add", args: { value: 1 }, facts: "" }
|
114
|
+
{ id: 0, parentId: -1, mutation: "add", args: { value: 1 }, clientId: "client-id", sessionId: "session-id", facts: "" }
|
115
|
+
{ id: 2, parentId: 0, mutation: "multiply", args: { value: 0 }, clientId: "client-id", sessionId: "session-id", facts: "+multiplyByZero" }
|
116
|
+
{ id: 3, parentId: 2, mutation: "multiply", args: { value: 2 }, clientId: "client-id", sessionId: "session-id", facts: "?multiplyByZero +multiplyByZero -multiplyByZero" }
|
117
|
+
{ id: 4, parentId: 3, mutation: "add", args: { value: 1 }, clientId: "client-id", sessionId: "session-id", facts: "" }
|
118
118
|
]
|
119
119
|
`)
|
120
120
|
})
|