@livestore/common 0.0.0-snapshot-6c61bcbb96b4666b347d3a9845d9f909cf96ae6d → 0.0.0-snapshot-2a3ba4e86ce747d9ef59d22aabc4de9e0528846b
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 +5 -9
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js.map +1 -1
- package/dist/devtools/devtools-messages-leader.d.ts +1 -1
- package/dist/devtools/devtools-messages-leader.js +1 -1
- package/dist/devtools/devtools-messages-leader.js.map +1 -1
- package/dist/leader-thread/eventlog.d.ts.map +1 -1
- package/dist/leader-thread/eventlog.js +9 -13
- package/dist/leader-thread/eventlog.js.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.js +1 -1
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +1 -8
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +2 -10
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/types.d.ts +1 -1
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/schema/schema.d.ts +10 -12
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +4 -27
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/state/sqlite/mod.d.ts +5 -0
- package/dist/schema/state/sqlite/mod.d.ts.map +1 -1
- package/dist/schema/state/sqlite/mod.js +8 -3
- package/dist/schema/state/sqlite/mod.js.map +1 -1
- package/dist/schema/state/sqlite/system-tables.d.ts +124 -3
- package/dist/schema/state/sqlite/system-tables.d.ts.map +1 -1
- package/dist/schema/state/sqlite/system-tables.js +3 -2
- package/dist/schema/state/sqlite/system-tables.js.map +1 -1
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +13 -11
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/{validate-mutation-defs.d.ts → validate-schema.d.ts} +1 -1
- package/dist/schema-management/validate-schema.d.ts.map +1 -0
- package/dist/schema-management/{validate-mutation-defs.js → validate-schema.js} +1 -1
- package/dist/schema-management/validate-schema.js.map +1 -0
- package/dist/sync/syncstate.d.ts +2 -2
- package/package.json +2 -2
- package/src/adapter-types.ts +4 -10
- package/src/devtools/devtools-messages-leader.ts +1 -1
- package/src/leader-thread/eventlog.ts +9 -14
- package/src/leader-thread/leader-worker-devtools.ts +1 -1
- package/src/leader-thread/make-leader-thread-layer.ts +2 -8
- package/src/leader-thread/recreate-db.ts +2 -14
- package/src/leader-thread/types.ts +1 -1
- package/src/schema/schema.ts +15 -50
- package/src/schema/state/sqlite/mod.ts +14 -3
- package/src/schema/state/sqlite/system-tables.ts +5 -4
- package/src/schema-management/migrations.ts +14 -12
- package/dist/schema-management/validate-mutation-defs.d.ts.map +0 -1
- package/dist/schema-management/validate-mutation-defs.js.map +0 -1
- /package/src/schema-management/{validate-mutation-defs.ts → validate-schema.ts} +0 -0
package/src/schema/schema.ts
CHANGED
|
@@ -5,8 +5,7 @@ import type { EventDef, EventDefRecord, Materializer, RawSqlEvent } from './Even
|
|
|
5
5
|
import { rawSqlEvent } from './EventDef.js'
|
|
6
6
|
import { tableIsClientDocumentTable } from './state/sqlite/client-document-def.js'
|
|
7
7
|
import type { SqliteDsl } from './state/sqlite/db-schema/mod.js'
|
|
8
|
-
import {
|
|
9
|
-
import { systemTables } from './state/sqlite/system-tables.js'
|
|
8
|
+
import { stateSystemTables } from './state/sqlite/system-tables.js'
|
|
10
9
|
import type { TableDef } from './state/sqlite/table-def.js'
|
|
11
10
|
|
|
12
11
|
export const LiveStoreSchemaSymbol = Symbol.for('livestore.LiveStoreSchema')
|
|
@@ -16,28 +15,24 @@ export type LiveStoreSchema<
|
|
|
16
15
|
TDbSchema extends SqliteDsl.DbSchema = SqliteDsl.DbSchema,
|
|
17
16
|
TEventsDefRecord extends EventDefRecord = EventDefRecord,
|
|
18
17
|
> = {
|
|
19
|
-
readonly
|
|
18
|
+
readonly LiveStoreSchemaSymbol: LiveStoreSchemaSymbol
|
|
20
19
|
/** Only used on type-level */
|
|
21
20
|
readonly _DbSchemaType: TDbSchema
|
|
22
21
|
/** Only used on type-level */
|
|
23
22
|
readonly _EventDefMapType: TEventsDefRecord
|
|
24
23
|
|
|
25
|
-
// TODO remove in favour of `state`
|
|
26
|
-
readonly tables: Map<string, TableDef>
|
|
27
|
-
/** Compound hash of all table defs etc */
|
|
28
|
-
readonly hash: number
|
|
29
24
|
readonly state: State
|
|
30
|
-
|
|
31
25
|
readonly eventsDefsMap: Map<string, EventDef.AnyWithoutFn>
|
|
32
|
-
|
|
33
|
-
// readonly materializers: Map<string, Materializer>
|
|
34
|
-
|
|
35
|
-
migrationOptions: MigrationOptions
|
|
36
26
|
}
|
|
37
27
|
|
|
28
|
+
// TODO abstract this further away from sqlite/tables
|
|
38
29
|
export type State = {
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
readonly sqlite: {
|
|
31
|
+
readonly tables: Map<string, TableDef.Any>
|
|
32
|
+
readonly migrations: MigrationOptions
|
|
33
|
+
/** Compound hash of all table defs etc */
|
|
34
|
+
readonly hash: number
|
|
35
|
+
}
|
|
41
36
|
readonly materializers: Map<string, Materializer>
|
|
42
37
|
}
|
|
43
38
|
|
|
@@ -48,32 +43,12 @@ export type InputSchema = {
|
|
|
48
43
|
|
|
49
44
|
export const makeSchema = <TInputSchema extends InputSchema>(
|
|
50
45
|
/** Note when using the object-notation for tables/events, the object keys are ignored and not used as table/mutation names */
|
|
51
|
-
inputSchema: TInputSchema
|
|
52
|
-
/** "hard-reset" is currently the default strategy */
|
|
53
|
-
migrations?: MigrationOptions
|
|
54
|
-
},
|
|
46
|
+
inputSchema: TInputSchema,
|
|
55
47
|
): FromInputSchema.DeriveSchema<TInputSchema> => {
|
|
56
|
-
// const inputTables: ReadonlyArray<TableDef> = Array.isArray(inputSchema.tables)
|
|
57
|
-
// ? inputSchema.tables
|
|
58
|
-
// : Object.values(inputSchema.tables)
|
|
59
|
-
|
|
60
|
-
// const inputTables = []
|
|
61
|
-
|
|
62
|
-
// const tables = new Map<string, TableDef>()
|
|
63
|
-
|
|
64
|
-
// for (const tableDef of inputTables) {
|
|
65
|
-
// // TODO validate tables (e.g. index names are unique)
|
|
66
|
-
// if (tables.has(tableDef.sqliteDef.ast.name)) {
|
|
67
|
-
// shouldNeverHappen(`Duplicate table name: ${tableDef.sqliteDef.ast.name}. Please use unique names for tables.`)
|
|
68
|
-
// }
|
|
69
|
-
// tables.set(tableDef.sqliteDef.ast.name, tableDef)
|
|
70
|
-
// }
|
|
71
|
-
|
|
72
48
|
const state = inputSchema.state
|
|
73
|
-
const tables = inputSchema.state.tables
|
|
49
|
+
const tables = inputSchema.state.sqlite.tables
|
|
74
50
|
|
|
75
|
-
for (const tableDef of
|
|
76
|
-
// // @ts-expect-error TODO fix type level issue
|
|
51
|
+
for (const tableDef of stateSystemTables) {
|
|
77
52
|
tables.set(tableDef.sqliteDef.name, tableDef)
|
|
78
53
|
}
|
|
79
54
|
|
|
@@ -100,22 +75,12 @@ export const makeSchema = <TInputSchema extends InputSchema>(
|
|
|
100
75
|
}
|
|
101
76
|
}
|
|
102
77
|
|
|
103
|
-
const hash = SqliteAst.hash({
|
|
104
|
-
_tag: 'dbSchema',
|
|
105
|
-
tables: [...tables.values()].map((_) => _.sqliteDef.ast),
|
|
106
|
-
})
|
|
107
|
-
|
|
108
78
|
return {
|
|
109
|
-
|
|
79
|
+
LiveStoreSchemaSymbol,
|
|
110
80
|
_DbSchemaType: Symbol.for('livestore.DbSchemaType') as any,
|
|
111
81
|
_EventDefMapType: Symbol.for('livestore.EventDefMapType') as any,
|
|
112
|
-
// tables,
|
|
113
|
-
// events,
|
|
114
82
|
state,
|
|
115
|
-
tables: state.tables,
|
|
116
83
|
eventsDefsMap,
|
|
117
|
-
migrationOptions: inputSchema.migrations ?? { strategy: 'from-eventlog' },
|
|
118
|
-
hash,
|
|
119
84
|
} satisfies LiveStoreSchema
|
|
120
85
|
}
|
|
121
86
|
|
|
@@ -139,7 +104,7 @@ export const getEventDef = <TSchema extends LiveStoreSchema>(
|
|
|
139
104
|
|
|
140
105
|
export namespace FromInputSchema {
|
|
141
106
|
export type DeriveSchema<TInputSchema extends InputSchema> = LiveStoreSchema<
|
|
142
|
-
DbSchemaFromInputSchemaTables<TInputSchema['state']['tables']>,
|
|
107
|
+
DbSchemaFromInputSchemaTables<TInputSchema['state']['sqlite']['tables']>,
|
|
143
108
|
EventDefRecordFromInputSchemaEvents<TInputSchema['events']>
|
|
144
109
|
>
|
|
145
110
|
|
|
@@ -148,7 +113,7 @@ export namespace FromInputSchema {
|
|
|
148
113
|
* - array: we use the table name of each array item (= table definition) as the object key
|
|
149
114
|
* - object: we discard the keys of the input object and use the table name of each object value (= table definition) as the new object key
|
|
150
115
|
*/
|
|
151
|
-
type DbSchemaFromInputSchemaTables<TTables extends InputSchema['state']['tables']> =
|
|
116
|
+
type DbSchemaFromInputSchemaTables<TTables extends InputSchema['state']['sqlite']['tables']> =
|
|
152
117
|
TTables extends ReadonlyArray<TableDef>
|
|
153
118
|
? { [K in TTables[number] as K['sqliteDef']['name']]: K['sqliteDef'] }
|
|
154
119
|
: TTables extends Record<string, TableDef>
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { shouldNeverHappen } from '@livestore/utils'
|
|
2
2
|
|
|
3
|
+
import type { MigrationOptions } from '../../../adapter-types.js'
|
|
3
4
|
import { type Materializer, rawSqlEvent, rawSqlMaterializer } from '../../EventDef.js'
|
|
4
5
|
import type { State } from '../../schema.js'
|
|
5
6
|
import { ClientDocumentTableDefSymbol, tableIsClientDocumentTable } from './client-document-def.js'
|
|
6
|
-
import {
|
|
7
|
+
import { SqliteAst } from './db-schema/mod.js'
|
|
8
|
+
import { stateSystemTables } from './system-tables.js'
|
|
7
9
|
import { type TableDef, type TableDefBase } from './table-def.js'
|
|
8
10
|
|
|
9
11
|
export * from './table-def.js'
|
|
@@ -32,7 +34,7 @@ export const makeState = <TStateInput extends InputState>(inputSchema: TStateInp
|
|
|
32
34
|
tables.set(sqliteDef.ast.name, tableDef)
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
for (const tableDef of
|
|
37
|
+
for (const tableDef of stateSystemTables) {
|
|
36
38
|
tables.set(tableDef.sqliteDef.name, tableDef)
|
|
37
39
|
}
|
|
38
40
|
|
|
@@ -53,10 +55,19 @@ export const makeState = <TStateInput extends InputState>(inputSchema: TStateInp
|
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
const hash = SqliteAst.hash({
|
|
59
|
+
_tag: 'dbSchema',
|
|
60
|
+
tables: [...tables.values()].map((_) => _.sqliteDef.ast),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
return { sqlite: { tables, migrations: inputSchema.migrations ?? { strategy: 'auto' }, hash }, materializers }
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
export type InputState = {
|
|
60
67
|
readonly tables: Record<string, TableDefBase> | ReadonlyArray<TableDefBase>
|
|
61
68
|
readonly materializers: Record<string, Materializer<any>>
|
|
69
|
+
/**
|
|
70
|
+
* @default { strategy: 'auto' }
|
|
71
|
+
*/
|
|
72
|
+
readonly migrations?: MigrationOptions
|
|
62
73
|
}
|
|
@@ -66,17 +66,16 @@ export const leaderMergeCounterTable = table({
|
|
|
66
66
|
|
|
67
67
|
export type LeaderMergeCounterRow = typeof leaderMergeCounterTable.Type
|
|
68
68
|
|
|
69
|
-
export const
|
|
69
|
+
export const stateSystemTables = [
|
|
70
70
|
schemaMetaTable,
|
|
71
71
|
schemaEventDefsMetaTable,
|
|
72
72
|
sessionChangesetMetaTable,
|
|
73
73
|
leaderMergeCounterTable,
|
|
74
74
|
]
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
export const isStateSystemTable = (tableName: string) => stateSystemTables.some((_) => _.sqliteDef.name === tableName)
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
export type SyncStatus = typeof SyncStatus.Type
|
|
78
|
+
/// Eventlog DB
|
|
80
79
|
|
|
81
80
|
export const EVENTLOG_META_TABLE = 'eventlog'
|
|
82
81
|
|
|
@@ -113,3 +112,5 @@ export const syncStatusTable = table({
|
|
|
113
112
|
})
|
|
114
113
|
|
|
115
114
|
export type SyncStatusRow = typeof syncStatusTable.Type
|
|
115
|
+
|
|
116
|
+
export const eventlogSystemTables = [eventlogMetaTable, syncStatusTable]
|
|
@@ -6,16 +6,16 @@ import type { LiveStoreSchema } from '../schema/mod.js'
|
|
|
6
6
|
import { SqliteAst, SqliteDsl } from '../schema/state/sqlite/db-schema/mod.js'
|
|
7
7
|
import type { SchemaEventDefsMetaRow, SchemaMetaRow } from '../schema/state/sqlite/system-tables.js'
|
|
8
8
|
import {
|
|
9
|
+
isStateSystemTable,
|
|
9
10
|
SCHEMA_EVENT_DEFS_META_TABLE,
|
|
10
11
|
SCHEMA_META_TABLE,
|
|
11
12
|
schemaEventDefsMetaTable,
|
|
12
|
-
|
|
13
|
-
systemTables,
|
|
13
|
+
stateSystemTables,
|
|
14
14
|
} from '../schema/state/sqlite/system-tables.js'
|
|
15
15
|
import { sql } from '../util.js'
|
|
16
16
|
import type { SchemaManager } from './common.js'
|
|
17
17
|
import { dbExecute, dbSelect } from './common.js'
|
|
18
|
-
import { validateSchema } from './validate-
|
|
18
|
+
import { validateSchema } from './validate-schema.js'
|
|
19
19
|
|
|
20
20
|
const getMemoizedTimestamp = memoizeByStringifyArgs(() => new Date().toISOString())
|
|
21
21
|
|
|
@@ -55,11 +55,13 @@ export const migrateDb = ({
|
|
|
55
55
|
onProgress?: (opts: { done: number; total: number }) => Effect.Effect<void>
|
|
56
56
|
}): Effect.Effect<MigrationsReport, UnexpectedError> =>
|
|
57
57
|
Effect.gen(function* () {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
for (const tableDef of stateSystemTables) {
|
|
59
|
+
yield* migrateTable({
|
|
60
|
+
db,
|
|
61
|
+
tableAst: tableDef.sqliteDef.ast,
|
|
62
|
+
behaviour: 'create-if-not-exists',
|
|
63
|
+
})
|
|
64
|
+
}
|
|
63
65
|
|
|
64
66
|
// TODO enforce that migrating tables isn't allowed once the store is running
|
|
65
67
|
|
|
@@ -72,11 +74,11 @@ export const migrateDb = ({
|
|
|
72
74
|
schemaMetaRows.map(({ tableName, schemaHash }) => [tableName, schemaHash]),
|
|
73
75
|
)
|
|
74
76
|
|
|
75
|
-
const tableDefs =
|
|
77
|
+
const tableDefs = [
|
|
76
78
|
// NOTE it's important the `SCHEMA_META_TABLE` comes first since we're writing to it below
|
|
77
|
-
...
|
|
78
|
-
...Array.from(schema.tables.values()).filter((_) => _.sqliteDef.name
|
|
79
|
-
]
|
|
79
|
+
...stateSystemTables,
|
|
80
|
+
...Array.from(schema.state.sqlite.tables.values()).filter((_) => !isStateSystemTable(_.sqliteDef.name)),
|
|
81
|
+
]
|
|
80
82
|
|
|
81
83
|
const tablesToMigrate = new Set<{ tableAst: SqliteAst.Table; schemaHash: number }>()
|
|
82
84
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validate-mutation-defs.d.ts","sourceRoot":"","sources":["../../src/schema-management/validate-mutation-defs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAU,MAAM,yBAAyB,CAAA;AAExD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE9D,eAAO,MAAM,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