@livestore/common 0.3.0-dev.11 → 0.3.0-dev.3
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/adapter-types.d.ts +35 -47
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js.map +1 -1
- package/dist/derived-mutations.d.ts +4 -4
- package/dist/derived-mutations.d.ts.map +1 -1
- package/dist/derived-mutations.test.js.map +1 -1
- package/dist/devtools/devtools-bridge.d.ts +1 -2
- package/dist/devtools/devtools-bridge.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.d.ts +592 -3
- package/dist/devtools/devtools-messages.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.js +171 -3
- package/dist/devtools/devtools-messages.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/init-singleton-tables.d.ts +2 -2
- package/dist/init-singleton-tables.d.ts.map +1 -1
- package/dist/init-singleton-tables.js.map +1 -1
- package/dist/leader-thread/apply-mutation.d.ts +2 -5
- package/dist/leader-thread/apply-mutation.d.ts.map +1 -1
- package/dist/leader-thread/apply-mutation.js +29 -41
- package/dist/leader-thread/apply-mutation.js.map +1 -1
- package/dist/leader-thread/connection.d.ts +4 -4
- package/dist/leader-thread/connection.d.ts.map +1 -1
- package/dist/leader-thread/connection.js +5 -5
- package/dist/leader-thread/connection.js.map +1 -1
- package/dist/leader-thread/leader-sync-processor.d.ts +2 -2
- package/dist/leader-thread/leader-sync-processor.d.ts.map +1 -1
- package/dist/leader-thread/leader-sync-processor.js +12 -20
- package/dist/leader-thread/leader-sync-processor.js.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.d.ts +1 -1
- package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.js +81 -37
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +11 -12
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +14 -33
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mutationlog.d.ts +19 -6
- package/dist/leader-thread/mutationlog.d.ts.map +1 -1
- package/dist/leader-thread/mutationlog.js +6 -7
- 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/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +18 -24
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/types.d.ts +16 -36
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/leader-thread/types.js.map +1 -1
- package/dist/mutation.d.ts +2 -9
- package/dist/mutation.d.ts.map +1 -1
- package/dist/mutation.js +5 -5
- package/dist/mutation.js.map +1 -1
- package/dist/query-builder/impl.d.ts +1 -1
- package/dist/rehydrate-from-mutationlog.d.ts +5 -5
- package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
- package/dist/rehydrate-from-mutationlog.js +19 -13
- package/dist/rehydrate-from-mutationlog.js.map +1 -1
- package/dist/schema/EventId.d.ts +14 -16
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +7 -15
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/MutationEvent.d.ts +80 -49
- package/dist/schema/MutationEvent.d.ts.map +1 -1
- package/dist/schema/MutationEvent.js +15 -32
- package/dist/schema/MutationEvent.js.map +1 -1
- package/dist/schema/system-tables.d.ts +26 -26
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +11 -19
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema-management/common.d.ts +3 -3
- package/dist/schema-management/common.d.ts.map +1 -1
- package/dist/schema-management/common.js.map +1 -1
- package/dist/schema-management/migrations.d.ts +4 -4
- 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/sync/client-session-sync-processor.d.ts +4 -4
- package/dist/sync/client-session-sync-processor.d.ts.map +1 -1
- package/dist/sync/index.d.ts +1 -1
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +1 -1
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +4 -1
- 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/rebase-events.d.ts +3 -3
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +2 -3
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.d.ts +7 -7
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.js +9 -3
- package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
- package/dist/sync/sync.d.ts +11 -21
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/syncstate.d.ts +23 -45
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +12 -56
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +69 -125
- 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 +2 -2
- package/dist/sync/validate-push-payload.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +5 -6
- package/src/adapter-types.ts +40 -39
- package/src/derived-mutations.test.ts +1 -1
- package/src/derived-mutations.ts +5 -9
- package/src/devtools/devtools-bridge.ts +1 -2
- package/src/devtools/devtools-messages.ts +243 -3
- package/src/index.ts +6 -0
- package/src/init-singleton-tables.ts +2 -2
- package/src/leader-thread/apply-mutation.ts +35 -53
- package/src/leader-thread/connection.ts +7 -7
- package/src/leader-thread/{LeaderSyncProcessor.ts → leader-sync-processor.ts} +268 -306
- package/src/leader-thread/leader-worker-devtools.ts +124 -52
- package/src/leader-thread/make-leader-thread-layer.ts +30 -62
- package/src/leader-thread/mutationlog.ts +10 -14
- package/src/leader-thread/recreate-db.ts +20 -24
- package/src/leader-thread/types.ts +20 -41
- package/src/mutation.ts +7 -17
- package/src/rehydrate-from-mutationlog.ts +26 -18
- package/src/schema/EventId.ts +9 -23
- package/src/schema/MutationEvent.ts +24 -46
- package/src/schema/system-tables.ts +11 -19
- package/src/schema-management/common.ts +3 -3
- package/src/schema-management/migrations.ts +10 -10
- package/src/sync/{ClientSessionSyncProcessor.ts → client-session-sync-processor.ts} +19 -26
- package/src/sync/index.ts +1 -1
- package/src/sync/next/history-dag-common.ts +1 -1
- package/src/sync/next/rebase-events.ts +7 -7
- package/src/sync/next/test/mutation-fixtures.ts +10 -3
- package/src/sync/sync.ts +6 -19
- package/src/sync/syncstate.test.ts +67 -127
- package/src/sync/syncstate.ts +19 -21
- package/src/sync/validate-push-payload.ts +4 -7
- package/src/version.ts +1 -1
- package/dist/devtools/devtool-message-leader.d.ts +0 -2
- package/dist/devtools/devtool-message-leader.d.ts.map +0 -1
- package/dist/devtools/devtool-message-leader.js +0 -2
- package/dist/devtools/devtool-message-leader.js.map +0 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +0 -297
- package/dist/devtools/devtools-messages-client-session.d.ts.map +0 -1
- package/dist/devtools/devtools-messages-client-session.js +0 -61
- package/dist/devtools/devtools-messages-client-session.js.map +0 -1
- package/dist/devtools/devtools-messages-common.d.ts +0 -65
- package/dist/devtools/devtools-messages-common.d.ts.map +0 -1
- package/dist/devtools/devtools-messages-common.js +0 -35
- package/dist/devtools/devtools-messages-common.js.map +0 -1
- package/dist/devtools/devtools-messages-leader.d.ts +0 -261
- package/dist/devtools/devtools-messages-leader.d.ts.map +0 -1
- package/dist/devtools/devtools-messages-leader.js +0 -85
- package/dist/devtools/devtools-messages-leader.js.map +0 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts +0 -37
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +0 -1
- package/dist/leader-thread/LeaderSyncProcessor.js +0 -432
- package/dist/leader-thread/LeaderSyncProcessor.js.map +0 -1
- package/dist/schema/EventId.test.d.ts +0 -2
- package/dist/schema/EventId.test.d.ts.map +0 -1
- package/dist/schema/EventId.test.js +0 -11
- package/dist/schema/EventId.test.js.map +0 -1
- package/dist/schema/MutationEvent.test.d.ts +0 -2
- package/dist/schema/MutationEvent.test.d.ts.map +0 -1
- package/dist/schema/MutationEvent.test.js +0 -2
- package/dist/schema/MutationEvent.test.js.map +0 -1
- package/dist/sync/ClientSessionSyncProcessor.d.ts +0 -43
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +0 -1
- package/dist/sync/ClientSessionSyncProcessor.js +0 -141
- package/dist/sync/ClientSessionSyncProcessor.js.map +0 -1
- package/src/devtools/devtools-messages-client-session.ts +0 -109
- package/src/devtools/devtools-messages-common.ts +0 -52
- package/src/devtools/devtools-messages-leader.ts +0 -115
- package/src/schema/EventId.test.ts +0 -12
@@ -1,9 +1,9 @@
|
|
1
|
-
import type {
|
1
|
+
import type { SynchronousDatabase } from './adapter-types.js'
|
2
2
|
import type { LiveStoreSchema } from './schema/mod.js'
|
3
3
|
import { getDefaultValuesEncoded } from './schema/schema-helpers.js'
|
4
4
|
import { prepareBindValues, sql } from './util.js'
|
5
5
|
|
6
|
-
export const initializeSingletonTables = (schema: LiveStoreSchema, db:
|
6
|
+
export const initializeSingletonTables = (schema: LiveStoreSchema, db: SynchronousDatabase) => {
|
7
7
|
for (const [, tableDef] of schema.tables) {
|
8
8
|
if (tableDef.options.isSingleton) {
|
9
9
|
const defaultValues = getDefaultValuesEncoded(tableDef, undefined)
|
@@ -2,26 +2,21 @@ import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
|
|
2
2
|
import type { Scope } from '@livestore/utils/effect'
|
3
3
|
import { Effect, Option, Schema } from '@livestore/utils/effect'
|
4
4
|
|
5
|
-
import type {
|
6
|
-
import { getExecArgsFromMutation } from '../mutation.js'
|
5
|
+
import type { SqliteError, SynchronousDatabase, UnexpectedError } from '../index.js'
|
7
6
|
import {
|
8
|
-
|
7
|
+
getExecArgsFromMutation,
|
9
8
|
MUTATION_LOG_META_TABLE,
|
10
|
-
type MutationEvent,
|
11
9
|
mutationLogMetaTable,
|
12
10
|
SESSION_CHANGESET_META_TABLE,
|
13
11
|
sessionChangesetMetaTable,
|
14
|
-
} from '../
|
12
|
+
} from '../index.js'
|
13
|
+
import type { LiveStoreSchema, MutationEvent } from '../schema/mod.js'
|
15
14
|
import { insertRow } from '../sql-queries/index.js'
|
16
15
|
import { execSql, execSqlPrepared } from './connection.js'
|
17
16
|
import { LeaderThreadCtx } from './types.js'
|
18
17
|
|
19
18
|
export type ApplyMutation = (
|
20
19
|
mutationEventEncoded: MutationEvent.AnyEncoded,
|
21
|
-
options?: {
|
22
|
-
/** Needed for rehydrateFromMutationLog */
|
23
|
-
skipMutationLog?: boolean
|
24
|
-
},
|
25
20
|
) => Effect.Effect<void, SqliteError | UnexpectedError>
|
26
21
|
|
27
22
|
export const makeApplyMutation: Effect.Effect<ApplyMutation, never, Scope.Scope | LeaderThreadCtx> = Effect.gen(
|
@@ -36,27 +31,15 @@ export const makeApplyMutation: Effect.Effect<ApplyMutation, never, Scope.Scope
|
|
36
31
|
[...leaderThreadCtx.schema.mutations.entries()].map(([k, v]) => [k, Schema.hash(v.schema)] as const),
|
37
32
|
)
|
38
33
|
|
39
|
-
return (mutationEventEncoded
|
34
|
+
return (mutationEventEncoded) =>
|
40
35
|
Effect.gen(function* () {
|
41
|
-
const { schema,
|
42
|
-
const
|
36
|
+
const { mutationEventSchema, schema, db, dbLog } = leaderThreadCtx
|
37
|
+
const mutationEventDecoded = Schema.decodeUnknownSync(mutationEventSchema)(mutationEventEncoded)
|
43
38
|
|
44
|
-
const mutationName =
|
39
|
+
const mutationName = mutationEventDecoded.mutation
|
45
40
|
const mutationDef = schema.mutations.get(mutationName) ?? shouldNeverHappen(`Unknown mutation: ${mutationName}`)
|
46
41
|
|
47
|
-
const execArgsArr = getExecArgsFromMutation({
|
48
|
-
mutationDef,
|
49
|
-
mutationEvent: { decoded: undefined, encoded: mutationEventEncoded },
|
50
|
-
})
|
51
|
-
|
52
|
-
// NOTE we might want to bring this back if we want to debug no-op mutations
|
53
|
-
// const makeExecuteOptions = (statementSql: string, bindValues: any) => ({
|
54
|
-
// onRowsChanged: (rowsChanged: number) => {
|
55
|
-
// if (rowsChanged === 0) {
|
56
|
-
// console.warn(`Mutation "${mutationDef.name}" did not affect any rows:`, statementSql, bindValues)
|
57
|
-
// }
|
58
|
-
// },
|
59
|
-
// })
|
42
|
+
const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
|
60
43
|
|
61
44
|
// console.group('[@livestore/common:leader-thread:applyMutation]', { mutationName })
|
62
45
|
|
@@ -70,29 +53,31 @@ export const makeApplyMutation: Effect.Effect<ApplyMutation, never, Scope.Scope
|
|
70
53
|
|
71
54
|
const changeset = session.changeset()
|
72
55
|
session.finish()
|
73
|
-
|
74
|
-
// TODO
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
56
|
+
// NOTE for no-op mutations (e.g. if the state didn't change) the changeset will be empty
|
57
|
+
// TODO possibly write a null value instead of omitting the row
|
58
|
+
if (changeset !== undefined && changeset.length > 0) {
|
59
|
+
// TODO use prepared statements
|
60
|
+
yield* execSql(
|
61
|
+
db,
|
62
|
+
...insertRow({
|
63
|
+
tableName: SESSION_CHANGESET_META_TABLE,
|
64
|
+
columns: sessionChangesetMetaTable.sqliteDef.columns,
|
65
|
+
values: {
|
66
|
+
idGlobal: mutationEventEncoded.id.global,
|
67
|
+
idLocal: mutationEventEncoded.id.local,
|
68
|
+
changeset,
|
69
|
+
debug: execArgsArr,
|
70
|
+
},
|
71
|
+
}),
|
72
|
+
)
|
73
|
+
}
|
89
74
|
|
90
75
|
// console.groupEnd()
|
91
76
|
|
92
77
|
// write to mutation_log
|
93
|
-
const excludeFromMutationLog = shouldExcludeMutationFromLog(mutationName,
|
94
|
-
if (
|
95
|
-
yield* insertIntoMutationLog(mutationEventEncoded,
|
78
|
+
const excludeFromMutationLog = shouldExcludeMutationFromLog(mutationName, mutationEventDecoded)
|
79
|
+
if (excludeFromMutationLog === false) {
|
80
|
+
yield* insertIntoMutationLog(mutationEventEncoded, dbLog, mutationDefSchemaHashMap)
|
96
81
|
} else {
|
97
82
|
// console.debug('[@livestore/common:leader-thread] skipping mutation log write', mutation, statementSql, bindValues)
|
98
83
|
}
|
@@ -101,7 +86,7 @@ export const makeApplyMutation: Effect.Effect<ApplyMutation, never, Scope.Scope
|
|
101
86
|
attributes: {
|
102
87
|
mutationName: mutationEventEncoded.mutation,
|
103
88
|
mutationId: mutationEventEncoded.id,
|
104
|
-
'span.label':
|
89
|
+
'span.label': mutationEventEncoded.mutation,
|
105
90
|
},
|
106
91
|
}),
|
107
92
|
// Effect.logDuration('@livestore/common:leader-thread:applyMutation'),
|
@@ -111,7 +96,7 @@ export const makeApplyMutation: Effect.Effect<ApplyMutation, never, Scope.Scope
|
|
111
96
|
|
112
97
|
const insertIntoMutationLog = (
|
113
98
|
mutationEventEncoded: MutationEvent.AnyEncoded,
|
114
|
-
|
99
|
+
dbLog: SynchronousDatabase,
|
115
100
|
mutationDefSchemaHashMap: Map<string, number>,
|
116
101
|
) =>
|
117
102
|
Effect.gen(function* () {
|
@@ -121,7 +106,7 @@ const insertIntoMutationLog = (
|
|
121
106
|
|
122
107
|
// TODO use prepared statements
|
123
108
|
yield* execSql(
|
124
|
-
|
109
|
+
dbLog,
|
125
110
|
...insertRow({
|
126
111
|
tableName: MUTATION_LOG_META_TABLE,
|
127
112
|
columns: mutationLogMetaTable.sqliteDef.columns,
|
@@ -147,14 +132,11 @@ const makeShouldExcludeMutationFromLog = memoizeByRef((schema: LiveStoreSchema)
|
|
147
132
|
? (migrationOptions.excludeMutations ?? new Set(['livestore.RawSql']))
|
148
133
|
: new Set(['livestore.RawSql'])
|
149
134
|
|
150
|
-
return (mutationName: string,
|
135
|
+
return (mutationName: string, mutationEventDecoded: MutationEvent.Any): boolean => {
|
151
136
|
if (mutationLogExclude.has(mutationName)) return true
|
152
137
|
|
153
138
|
const mutationDef = schema.mutations.get(mutationName) ?? shouldNeverHappen(`Unknown mutation: ${mutationName}`)
|
154
|
-
const execArgsArr = getExecArgsFromMutation({
|
155
|
-
mutationDef,
|
156
|
-
mutationEvent: { decoded: undefined, encoded: mutationEventEncoded },
|
157
|
-
})
|
139
|
+
const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
|
158
140
|
|
159
141
|
return execArgsArr.some((_) => _.statementSql.includes('__livestore'))
|
160
142
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
// import type { WaSqlite } from '@livestore/sqlite-wasm'
|
2
2
|
import { Effect } from '@livestore/utils/effect'
|
3
3
|
|
4
|
-
import type {
|
4
|
+
import type { SynchronousDatabase } from '../adapter-types.js'
|
5
5
|
import { SqliteError } from '../adapter-types.js'
|
6
6
|
import type { BindValues } from '../sql-queries/index.js'
|
7
7
|
import type { PreparedBindValues } from '../util.js'
|
@@ -12,9 +12,9 @@ namespace WaSqlite {
|
|
12
12
|
export type SQLiteError = any
|
13
13
|
}
|
14
14
|
|
15
|
-
export const configureConnection = (
|
15
|
+
export const configureConnection = (syncDb: SynchronousDatabase, { fkEnabled }: { fkEnabled: boolean }) =>
|
16
16
|
execSql(
|
17
|
-
|
17
|
+
syncDb,
|
18
18
|
sql`
|
19
19
|
PRAGMA page_size=8192;
|
20
20
|
PRAGMA journal_mode=MEMORY;
|
@@ -23,10 +23,10 @@ export const configureConnection = (sqliteDb: SqliteDb, { fkEnabled }: { fkEnabl
|
|
23
23
|
{},
|
24
24
|
)
|
25
25
|
|
26
|
-
export const execSql = (
|
26
|
+
export const execSql = (syncDb: SynchronousDatabase, sql: string, bind: BindValues) => {
|
27
27
|
const bindValues = prepareBindValues(bind, sql)
|
28
28
|
return Effect.try({
|
29
|
-
try: () =>
|
29
|
+
try: () => syncDb.execute(sql, bindValues),
|
30
30
|
catch: (cause) =>
|
31
31
|
new SqliteError({ cause, query: { bindValues, sql }, code: (cause as WaSqlite.SQLiteError).code }),
|
32
32
|
}).pipe(
|
@@ -48,9 +48,9 @@ export const execSql = (sqliteDb: SqliteDb, sql: string, bind: BindValues) => {
|
|
48
48
|
// }
|
49
49
|
|
50
50
|
// TODO actually use prepared statements
|
51
|
-
export const execSqlPrepared = (
|
51
|
+
export const execSqlPrepared = (syncDb: SynchronousDatabase, sql: string, bindValues: PreparedBindValues) => {
|
52
52
|
return Effect.try({
|
53
|
-
try: () =>
|
53
|
+
try: () => syncDb.execute(sql, bindValues),
|
54
54
|
catch: (cause) =>
|
55
55
|
new SqliteError({ cause, query: { bindValues, sql }, code: (cause as WaSqlite.SQLiteError).code }),
|
56
56
|
}).pipe(
|