@livestore/common 0.3.0-dev.2 → 0.3.0-dev.22
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/adapter-types.d.ts +97 -53
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +17 -0
- 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 +1 -1
- package/dist/debug-info.d.ts.map +1 -1
- package/dist/derived-mutations.d.ts +5 -5
- 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/derived-mutations.test.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +389 -0
- package/dist/devtools/devtools-messages-client-session.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-client-session.js +96 -0
- package/dist/devtools/devtools-messages-client-session.js.map +1 -0
- package/dist/devtools/devtools-messages-common.d.ts +61 -0
- package/dist/devtools/devtools-messages-common.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-common.js +54 -0
- package/dist/devtools/devtools-messages-common.js.map +1 -0
- package/dist/devtools/devtools-messages-leader.d.ts +393 -0
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-leader.js +148 -0
- package/dist/devtools/devtools-messages-leader.js.map +1 -0
- package/dist/devtools/devtools-messages.d.ts +3 -592
- package/dist/devtools/devtools-messages.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.js +3 -171
- package/dist/devtools/devtools-messages.js.map +1 -1
- package/dist/devtools/devtools-sessioninfo.d.ts +28 -0
- package/dist/devtools/devtools-sessioninfo.d.ts.map +1 -0
- package/dist/devtools/devtools-sessioninfo.js +34 -0
- package/dist/devtools/devtools-sessioninfo.js.map +1 -0
- package/dist/devtools/mod.d.ts +39 -0
- package/dist/devtools/mod.d.ts.map +1 -0
- package/dist/devtools/mod.js +27 -0
- package/dist/devtools/mod.js.map +1 -0
- package/dist/index.d.ts +2 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.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/LeaderSyncProcessor.d.ts +39 -0
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -0
- package/dist/leader-thread/LeaderSyncProcessor.js +527 -0
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -0
- package/dist/leader-thread/apply-mutation.d.ts +5 -2
- package/dist/leader-thread/apply-mutation.d.ts.map +1 -1
- package/dist/leader-thread/apply-mutation.js +55 -35
- package/dist/leader-thread/apply-mutation.js.map +1 -1
- package/dist/leader-thread/connection.d.ts +34 -6
- package/dist/leader-thread/connection.d.ts.map +1 -1
- package/dist/leader-thread/connection.js +22 -7
- package/dist/leader-thread/connection.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 +147 -124
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +12 -11
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +55 -18
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/mutationlog.d.ts +6 -19
- package/dist/leader-thread/mutationlog.d.ts.map +1 -1
- package/dist/leader-thread/mutationlog.js +12 -9
- package/dist/leader-thread/mutationlog.js.map +1 -1
- package/dist/leader-thread/pull-queue-set.d.ts +3 -3
- package/dist/leader-thread/pull-queue-set.d.ts.map +1 -1
- package/dist/leader-thread/pull-queue-set.js +9 -0
- package/dist/leader-thread/pull-queue-set.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts +4 -2
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +32 -21
- package/dist/leader-thread/recreate-db.js.map +1 -1
- package/dist/leader-thread/shutdown-channel.d.ts +2 -5
- package/dist/leader-thread/shutdown-channel.d.ts.map +1 -1
- package/dist/leader-thread/shutdown-channel.js +2 -4
- package/dist/leader-thread/shutdown-channel.js.map +1 -1
- package/dist/leader-thread/types.d.ts +58 -26
- package/dist/leader-thread/types.d.ts.map +1 -1
- package/dist/leader-thread/types.js +1 -3
- package/dist/leader-thread/types.js.map +1 -1
- package/dist/mutation.d.ts +9 -2
- package/dist/mutation.d.ts.map +1 -1
- package/dist/mutation.js +5 -5
- package/dist/mutation.js.map +1 -1
- package/dist/otel.d.ts +2 -0
- package/dist/otel.d.ts.map +1 -1
- package/dist/otel.js +5 -0
- package/dist/otel.js.map +1 -1
- package/dist/query-builder/api.d.ts +3 -3
- package/dist/query-builder/api.d.ts.map +1 -1
- package/dist/query-builder/impl.d.ts +4 -4
- package/dist/query-builder/impl.d.ts.map +1 -1
- package/dist/query-builder/impl.js.map +1 -1
- package/dist/query-builder/impl.test.js +16 -1
- package/dist/query-builder/impl.test.js.map +1 -1
- package/dist/query-info.d.ts +3 -3
- package/dist/query-info.d.ts.map +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 +23 -27
- package/dist/rehydrate-from-mutationlog.js.map +1 -1
- package/dist/schema/EventId.d.ts +27 -16
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +36 -11
- package/dist/schema/EventId.js.map +1 -1
- package/dist/schema/EventId.test.d.ts +2 -0
- package/dist/schema/EventId.test.d.ts.map +1 -0
- package/dist/schema/EventId.test.js +11 -0
- package/dist/schema/EventId.test.js.map +1 -0
- package/dist/schema/MutationEvent.d.ts +76 -82
- package/dist/schema/MutationEvent.d.ts.map +1 -1
- package/dist/schema/MutationEvent.js +53 -20
- 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 +8 -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.d.ts.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 +5 -2
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +20 -9
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/system-tables.d.ts +65 -47
- package/dist/schema/system-tables.d.ts.map +1 -1
- package/dist/schema/system-tables.js +24 -13
- package/dist/schema/system-tables.js.map +1 -1
- package/dist/schema/table-def.d.ts +18 -24
- 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/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 +6 -6
- package/dist/schema-management/migrations.d.ts.map +1 -1
- package/dist/schema-management/migrations.js +13 -8
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
- package/dist/schema-management/validate-mutation-defs.js +2 -2
- package/dist/schema-management/validate-mutation-defs.js.map +1 -1
- package/dist/sql-queries/misc.d.ts.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/{client-session-sync-processor.d.ts → ClientSessionSyncProcessor.d.ts} +25 -14
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -0
- package/dist/sync/ClientSessionSyncProcessor.js +199 -0
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -0
- 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/compact-events.d.ts.map +1 -1
- package/dist/sync/next/facts.d.ts.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 +3 -4
- 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 +6 -4
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +6 -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 +4 -4
- package/dist/sync/next/test/mutation-fixtures.d.ts.map +1 -1
- package/dist/sync/next/test/mutation-fixtures.js +12 -16
- package/dist/sync/next/test/mutation-fixtures.js.map +1 -1
- package/dist/sync/sync.d.ts +31 -16
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/sync.js +7 -3
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/syncstate.d.ts +177 -44
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +188 -56
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +162 -92
- 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/util.d.ts +2 -2
- package/dist/util.d.ts.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 +6 -6
- package/src/adapter-types.ts +80 -56
- package/src/derived-mutations.test.ts +1 -1
- package/src/derived-mutations.ts +13 -9
- package/src/devtools/devtools-messages-client-session.ts +141 -0
- package/src/devtools/devtools-messages-common.ts +106 -0
- package/src/devtools/devtools-messages-leader.ts +192 -0
- package/src/devtools/devtools-messages.ts +3 -243
- package/src/devtools/devtools-sessioninfo.ts +99 -0
- package/src/devtools/mod.ts +36 -0
- package/src/index.ts +2 -8
- package/src/init-singleton-tables.ts +2 -2
- package/src/leader-thread/LeaderSyncProcessor.ts +833 -0
- package/src/leader-thread/apply-mutation.ts +87 -43
- package/src/leader-thread/connection.ts +54 -9
- package/src/leader-thread/leader-worker-devtools.ts +199 -174
- package/src/leader-thread/make-leader-thread-layer.ts +89 -35
- package/src/leader-thread/mutationlog.ts +20 -14
- package/src/leader-thread/pull-queue-set.ts +10 -1
- package/src/leader-thread/recreate-db.ts +38 -25
- package/src/leader-thread/shutdown-channel.ts +2 -4
- package/src/leader-thread/types.ts +68 -34
- package/src/mutation.ts +17 -7
- package/src/otel.ts +8 -0
- package/src/query-builder/api.ts +4 -4
- package/src/query-builder/impl.test.ts +22 -1
- package/src/query-builder/impl.ts +2 -2
- package/src/query-info.ts +3 -3
- package/src/rehydrate-from-mutationlog.ts +28 -34
- package/src/schema/EventId.test.ts +12 -0
- package/src/schema/EventId.ts +49 -15
- package/src/schema/MutationEvent.ts +78 -31
- 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 +10 -20
- package/src/schema/schema-helpers.ts +1 -1
- package/src/schema/schema.ts +22 -10
- package/src/schema/system-tables.ts +24 -13
- package/src/schema/table-def.ts +17 -17
- package/src/schema-management/common.ts +3 -3
- package/src/schema-management/migrations.ts +19 -15
- package/src/schema-management/validate-mutation-defs.ts +2 -2
- 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 +313 -0
- package/src/sync/index.ts +1 -1
- 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 +13 -7
- 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 -19
- package/src/sync/sync.ts +26 -10
- package/src/sync/syncstate.test.ts +178 -98
- package/src/sync/syncstate.ts +170 -83
- package/src/sync/validate-push-payload.ts +7 -4
- package/src/version.ts +1 -1
- package/tmp/pack.tgz +0 -0
- package/tsconfig.json +1 -1
- package/dist/devtools/devtools-bridge.d.ts +0 -12
- package/dist/devtools/devtools-bridge.d.ts.map +0 -1
- package/dist/devtools/devtools-bridge.js +0 -2
- package/dist/devtools/devtools-bridge.js.map +0 -1
- package/dist/devtools/index.d.ts +0 -42
- package/dist/devtools/index.d.ts.map +0 -1
- package/dist/devtools/index.js +0 -48
- package/dist/devtools/index.js.map +0 -1
- package/dist/leader-thread/leader-sync-processor.d.ts +0 -47
- package/dist/leader-thread/leader-sync-processor.d.ts.map +0 -1
- package/dist/leader-thread/leader-sync-processor.js +0 -422
- package/dist/leader-thread/leader-sync-processor.js.map +0 -1
- package/dist/sync/client-session-sync-processor.d.ts.map +0 -1
- package/dist/sync/client-session-sync-processor.js +0 -131
- package/dist/sync/client-session-sync-processor.js.map +0 -1
- package/src/devtools/devtools-bridge.ts +0 -13
- package/src/devtools/index.ts +0 -48
- package/src/leader-thread/leader-sync-processor.ts +0 -666
- package/src/sync/client-session-sync-processor.ts +0 -207
@@ -0,0 +1,195 @@
|
|
1
|
+
import type { Nullable } from '@livestore/utils'
|
2
|
+
import type { Option, Types } from '@livestore/utils/effect'
|
3
|
+
import { Schema } from '@livestore/utils/effect'
|
4
|
+
|
5
|
+
import type * as SqliteAst from '../ast/sqlite.js'
|
6
|
+
import type { ColumnDefinition } from './field-defs.js'
|
7
|
+
|
8
|
+
export * from './field-defs.js'
|
9
|
+
|
10
|
+
export type DbSchema = {
|
11
|
+
[key: string]: TableDefinition<string, Columns>
|
12
|
+
}
|
13
|
+
|
14
|
+
/** Note when using the object-notation, the object keys are ignored and not used as table names */
|
15
|
+
export type DbSchemaInput = Record<string, TableDefinition<any, any>> | ReadonlyArray<TableDefinition<any, any>>
|
16
|
+
|
17
|
+
/**
|
18
|
+
* In case of ...
|
19
|
+
* - array: we use the table name of each array item (= table definition) as the object key
|
20
|
+
* - 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
|
21
|
+
*/
|
22
|
+
export type DbSchemaFromInputSchema<TSchemaInput extends DbSchemaInput> =
|
23
|
+
TSchemaInput extends ReadonlyArray<TableDefinition<any, any>>
|
24
|
+
? { [K in TSchemaInput[number] as K['name']]: K }
|
25
|
+
: TSchemaInput extends Record<string, TableDefinition<any, any>>
|
26
|
+
? { [K in keyof TSchemaInput as TSchemaInput[K]['name']]: TSchemaInput[K] }
|
27
|
+
: never
|
28
|
+
|
29
|
+
// TODO ensure via runtime check (possibly even via type-level check) that all index names are unique
|
30
|
+
export const makeDbSchema = <TDbSchemaInput extends DbSchemaInput>(
|
31
|
+
schema: TDbSchemaInput,
|
32
|
+
): DbSchemaFromInputSchema<TDbSchemaInput> => {
|
33
|
+
return Array.isArray(schema) ? Object.fromEntries(schema.map((_) => [_.name, _])) : (schema as any)
|
34
|
+
}
|
35
|
+
|
36
|
+
export const table = <TTableName extends string, TColumns extends Columns, TIndexes extends Index[]>(
|
37
|
+
name: TTableName,
|
38
|
+
columns: TColumns,
|
39
|
+
indexes?: TIndexes,
|
40
|
+
): TableDefinition<TTableName, TColumns> => {
|
41
|
+
const ast: SqliteAst.Table = {
|
42
|
+
_tag: 'table',
|
43
|
+
name,
|
44
|
+
columns: columsToAst(columns),
|
45
|
+
indexes: indexesToAst(indexes ?? []),
|
46
|
+
}
|
47
|
+
|
48
|
+
return { name, columns, indexes, ast }
|
49
|
+
}
|
50
|
+
|
51
|
+
export type AnyIfConstained<In, Out> = '__constrained' extends keyof In ? any : Out
|
52
|
+
export type EmptyObjIfConstained<In> = '__constrained' extends keyof In ? {} : In
|
53
|
+
|
54
|
+
export type StructSchemaForColumns<TCols extends ConstraintColumns> = Schema.Schema<
|
55
|
+
AnyIfConstained<TCols, { readonly [K in keyof TCols]: TCols[K]['schema']['Type'] }>,
|
56
|
+
AnyIfConstained<TCols, { readonly [K in keyof TCols]: TCols[K]['schema']['Encoded'] }>
|
57
|
+
>
|
58
|
+
|
59
|
+
export const structSchemaForTable = <TTableDefinition extends TableDefinition<any, any>>(
|
60
|
+
tableDef: TTableDefinition,
|
61
|
+
): StructSchemaForColumns<TTableDefinition['columns']> =>
|
62
|
+
Schema.Struct(Object.fromEntries(tableDef.ast.columns.map((column) => [column.name, column.schema]))).annotations({
|
63
|
+
title: tableDef.name,
|
64
|
+
}) as any
|
65
|
+
|
66
|
+
const columsToAst = (columns: Columns): ReadonlyArray<SqliteAst.Column> => {
|
67
|
+
return Object.entries(columns).map(([name, column]) => {
|
68
|
+
return {
|
69
|
+
_tag: 'column',
|
70
|
+
name,
|
71
|
+
schema: column.schema,
|
72
|
+
default: column.default as any,
|
73
|
+
nullable: column.nullable ?? false,
|
74
|
+
primaryKey: column.primaryKey ?? false,
|
75
|
+
type: { _tag: column.columnType },
|
76
|
+
} satisfies SqliteAst.Column
|
77
|
+
})
|
78
|
+
}
|
79
|
+
|
80
|
+
const indexesToAst = (indexes: ReadonlyArray<Index>): ReadonlyArray<SqliteAst.Index> => {
|
81
|
+
return indexes.map(
|
82
|
+
(_) => ({ _tag: 'index', columns: _.columns, name: _.name, unique: _.isUnique ?? false }) satisfies SqliteAst.Index,
|
83
|
+
)
|
84
|
+
}
|
85
|
+
|
86
|
+
/// Other
|
87
|
+
|
88
|
+
export type TableDefinition<TName extends string, TColumns extends Columns> = {
|
89
|
+
name: TName
|
90
|
+
columns: TColumns
|
91
|
+
indexes?: ReadonlyArray<Index>
|
92
|
+
ast: SqliteAst.Table
|
93
|
+
}
|
94
|
+
|
95
|
+
export type Columns = Record<string, ColumnDefinition<any, any>>
|
96
|
+
|
97
|
+
export type IsSingleColumn<TColumns extends Columns | ColumnDefinition<any, any>> =
|
98
|
+
TColumns extends ColumnDefinition<any, any> ? true : false
|
99
|
+
|
100
|
+
/**
|
101
|
+
* NOTE this is only needed to avoid a TS limitation where `StructSchemaForColumns` in the default case
|
102
|
+
* results in `Record<string, any>` instead of `any`. (Thanks to Andarist for the workaround)
|
103
|
+
*
|
104
|
+
* Hopefully this can be removed in the future
|
105
|
+
*/
|
106
|
+
|
107
|
+
export type ConstraintColumns = Record<string, ColumnDefinition<any, any>> & { __constrained?: never }
|
108
|
+
|
109
|
+
export type Index = {
|
110
|
+
name: string
|
111
|
+
columns: ReadonlyArray<string>
|
112
|
+
/** @default false */
|
113
|
+
isUnique?: boolean
|
114
|
+
}
|
115
|
+
|
116
|
+
export namespace FromTable {
|
117
|
+
// TODO this sometimes doesn't preserve the order of columns
|
118
|
+
export type RowDecoded<TTableDefinition extends TableDefinition<any, any>> = Types.Simplify<
|
119
|
+
Nullable<Pick<RowDecodedAll<TTableDefinition>, NullableColumnNames<TTableDefinition>>> &
|
120
|
+
Omit<RowDecodedAll<TTableDefinition>, NullableColumnNames<TTableDefinition>>
|
121
|
+
>
|
122
|
+
|
123
|
+
export type NullableColumnNames<TTableDefinition extends TableDefinition<any, any>> = FromColumns.NullableColumnNames<
|
124
|
+
TTableDefinition['columns']
|
125
|
+
>
|
126
|
+
|
127
|
+
export type Columns<TTableDefinition extends TableDefinition<any, any>> = {
|
128
|
+
[K in keyof TTableDefinition['columns']]: TTableDefinition['columns'][K]['columnType']
|
129
|
+
}
|
130
|
+
|
131
|
+
export type RowEncodeNonNullable<TTableDefinition extends TableDefinition<any, any>> = {
|
132
|
+
[K in keyof TTableDefinition['columns']]: Schema.Schema.Encoded<TTableDefinition['columns'][K]['schema']>
|
133
|
+
}
|
134
|
+
|
135
|
+
export type RowEncoded<TTableDefinition extends TableDefinition<any, any>> = Types.Simplify<
|
136
|
+
Nullable<Pick<RowEncodeNonNullable<TTableDefinition>, NullableColumnNames<TTableDefinition>>> &
|
137
|
+
Omit<RowEncodeNonNullable<TTableDefinition>, NullableColumnNames<TTableDefinition>>
|
138
|
+
>
|
139
|
+
|
140
|
+
// export type RowEncoded<TTableDefinition extends TableDefinition<any, any>> = NullableColumnNames<
|
141
|
+
// TTableDefinition['columns']
|
142
|
+
// >
|
143
|
+
|
144
|
+
// &
|
145
|
+
// Omit<RowEncodeNonNullable<TTableDefinition>, NullableColumnNames<TTableDefinition['columns']>>
|
146
|
+
// >
|
147
|
+
|
148
|
+
export type RowDecodedAll<TTableDefinition extends TableDefinition<any, any>> = {
|
149
|
+
[K in keyof TTableDefinition['columns']]: Schema.Schema.Type<TTableDefinition['columns'][K]['schema']>
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
export namespace FromColumns {
|
154
|
+
// TODO this sometimes doesn't preserve the order of columns
|
155
|
+
export type RowDecoded<TColumns extends Columns> = Types.Simplify<
|
156
|
+
Nullable<Pick<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>> &
|
157
|
+
Omit<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>
|
158
|
+
>
|
159
|
+
|
160
|
+
export type RowDecodedAll<TColumns extends Columns> = {
|
161
|
+
readonly [K in keyof TColumns]: Schema.Schema.Type<TColumns[K]['schema']>
|
162
|
+
}
|
163
|
+
|
164
|
+
export type RowEncoded<TColumns extends Columns> = Types.Simplify<
|
165
|
+
Nullable<Pick<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>> &
|
166
|
+
Omit<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>
|
167
|
+
>
|
168
|
+
|
169
|
+
export type RowEncodeNonNullable<TColumns extends Columns> = {
|
170
|
+
readonly [K in keyof TColumns]: Schema.Schema.Encoded<TColumns[K]['schema']>
|
171
|
+
}
|
172
|
+
|
173
|
+
export type NullableColumnNames<TColumns extends Columns> = keyof {
|
174
|
+
// TODO double check why there is a `true` in the type
|
175
|
+
[K in keyof TColumns as TColumns[K] extends ColumnDefinition<any, true> ? K : never]: {}
|
176
|
+
}
|
177
|
+
|
178
|
+
export type RequiredInsertColumns<TColumns extends Columns> = {
|
179
|
+
[K in keyof TColumns as TColumns[K]['nullable'] extends true
|
180
|
+
? never
|
181
|
+
: TColumns[K]['default'] extends Option.Some<any>
|
182
|
+
? never
|
183
|
+
: K]: {}
|
184
|
+
}
|
185
|
+
|
186
|
+
export type RequiredInsertColumnNames<TColumns extends Columns> = keyof RequiredInsertColumns<TColumns>
|
187
|
+
|
188
|
+
export type RequiresInsertValues<TColumns extends Columns> =
|
189
|
+
RequiredInsertColumnNames<TColumns> extends never ? false : true
|
190
|
+
|
191
|
+
export type InsertRowDecoded<TColumns extends Columns> = Types.Simplify<
|
192
|
+
Pick<RowDecodedAll<TColumns>, RequiredInsertColumnNames<TColumns>> &
|
193
|
+
Partial<Omit<RowDecodedAll<TColumns>, RequiredInsertColumnNames<TColumns>>>
|
194
|
+
>
|
195
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
// Based on https://stackoverflow.com/a/7616484
|
2
|
+
export const hashCode = (str: string) => {
|
3
|
+
let hash = 0,
|
4
|
+
i,
|
5
|
+
chr
|
6
|
+
if (str.length === 0) return hash
|
7
|
+
for (i = 0; i < str.length; i++) {
|
8
|
+
// eslint-disable-next-line unicorn/prefer-code-point
|
9
|
+
chr = str.charCodeAt(i)
|
10
|
+
hash = (hash << 5) - hash + chr
|
11
|
+
hash = Math.trunc(hash) // Convert to 32bit integer
|
12
|
+
}
|
13
|
+
return hash
|
14
|
+
}
|
package/src/schema/mod.ts
CHANGED
package/src/schema/mutations.ts
CHANGED
@@ -1,9 +1,11 @@
|
|
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
|
-
export type MutationDefMap =
|
5
|
+
export type MutationDefMap = {
|
6
|
+
map: Map<string | 'livestore.RawSql', MutationDef.Any>
|
7
|
+
wasProvided: boolean
|
8
|
+
}
|
7
9
|
export type MutationDefRecord = {
|
8
10
|
'livestore.RawSql': RawSqlMutation
|
9
11
|
[name: string]: MutationDef.Any
|
@@ -38,24 +40,17 @@ export type MutationDef<TName extends string, TFrom, TTo> = {
|
|
38
40
|
/** Warning: This feature is not fully implemented yet */
|
39
41
|
historyId: string
|
40
42
|
/**
|
41
|
-
* When set to true, the mutation won't be synced
|
43
|
+
* When set to true, the mutation won't be synced across clients but
|
42
44
|
*/
|
43
|
-
|
45
|
+
clientOnly: boolean
|
44
46
|
/** Warning: This feature is not fully implemented yet */
|
45
47
|
facts: FactsCallback<TTo> | undefined
|
46
48
|
}
|
47
49
|
|
48
50
|
/** Helper function to construct a partial mutation event */
|
49
|
-
(
|
50
|
-
args: TTo,
|
51
|
-
options?: {
|
52
|
-
id?: number
|
53
|
-
},
|
54
|
-
): {
|
51
|
+
(args: TTo): {
|
55
52
|
mutation: TName
|
56
53
|
args: TTo
|
57
|
-
// TODO remove/clean up after sync-next is fully implemented
|
58
|
-
id?: EventId.EventId
|
59
54
|
}
|
60
55
|
}
|
61
56
|
|
@@ -120,7 +115,7 @@ export type DefineMutationOptions<TTo> = {
|
|
120
115
|
/**
|
121
116
|
* When set to true, the mutation won't be synced over the network
|
122
117
|
*/
|
123
|
-
|
118
|
+
clientOnly?: boolean
|
124
119
|
}
|
125
120
|
|
126
121
|
// TODO possibly also allow for mutation event subsumption behaviour
|
@@ -130,12 +125,7 @@ export const defineMutation = <TName extends string, TFrom, TTo>(
|
|
130
125
|
sql: MutationDefSqlResult<NoInfer<TTo>>,
|
131
126
|
options?: DefineMutationOptions<TTo>,
|
132
127
|
): MutationDef<TName, TFrom, TTo> => {
|
133
|
-
const makePartialEvent = (
|
134
|
-
args: TTo,
|
135
|
-
options?: {
|
136
|
-
id?: EventId.EventId
|
137
|
-
},
|
138
|
-
) => ({ mutation: name, args, ...options })
|
128
|
+
const makePartialEvent = (args: TTo) => ({ mutation: name, args })
|
139
129
|
|
140
130
|
Object.defineProperty(makePartialEvent, 'name', { value: name })
|
141
131
|
Object.defineProperty(makePartialEvent, 'schema', { value: schema })
|
@@ -143,7 +133,7 @@ export const defineMutation = <TName extends string, TFrom, TTo>(
|
|
143
133
|
Object.defineProperty(makePartialEvent, 'options', {
|
144
134
|
value: {
|
145
135
|
historyId: options?.historyId ?? 'main',
|
146
|
-
|
136
|
+
clientOnly: options?.clientOnly ?? false,
|
147
137
|
facts: options?.facts
|
148
138
|
? (args, currentFacts) => {
|
149
139
|
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,
|
@@ -72,29 +72,32 @@ export const makeSchema = <TInputSchema extends InputSchema>(
|
|
72
72
|
tables.set(tableDef.sqliteDef.name, tableDef)
|
73
73
|
}
|
74
74
|
|
75
|
-
const mutations: MutationDefMap =
|
75
|
+
const mutations: MutationDefMap = {
|
76
|
+
map: new Map(),
|
77
|
+
wasProvided: inputSchema.mutations !== undefined,
|
78
|
+
}
|
76
79
|
|
77
80
|
if (isReadonlyArray(inputSchema.mutations)) {
|
78
81
|
for (const mutation of inputSchema.mutations) {
|
79
|
-
mutations.set(mutation.name, mutation)
|
82
|
+
mutations.map.set(mutation.name, mutation)
|
80
83
|
}
|
81
84
|
} else {
|
82
85
|
for (const mutation of Object.values(inputSchema.mutations ?? {})) {
|
83
|
-
if (mutations.has(mutation.name)) {
|
86
|
+
if (mutations.map.has(mutation.name)) {
|
84
87
|
shouldNeverHappen(`Duplicate mutation name: ${mutation.name}. Please use unique names for mutations.`)
|
85
88
|
}
|
86
|
-
mutations.set(mutation.name, mutation)
|
89
|
+
mutations.map.set(mutation.name, mutation)
|
87
90
|
}
|
88
91
|
}
|
89
92
|
|
90
|
-
mutations.set(rawSqlMutation.name, rawSqlMutation)
|
93
|
+
mutations.map.set(rawSqlMutation.name, rawSqlMutation)
|
91
94
|
|
92
95
|
for (const tableDef of tables.values()) {
|
93
96
|
if (tableHasDerivedMutations(tableDef)) {
|
94
97
|
const derivedMutationDefs = makeDerivedMutationDefsForTable(tableDef)
|
95
|
-
mutations.set(derivedMutationDefs.insert.name, derivedMutationDefs.insert)
|
96
|
-
mutations.set(derivedMutationDefs.update.name, derivedMutationDefs.update)
|
97
|
-
mutations.set(derivedMutationDefs.delete.name, derivedMutationDefs.delete)
|
98
|
+
mutations.map.set(derivedMutationDefs.insert.name, derivedMutationDefs.insert)
|
99
|
+
mutations.map.set(derivedMutationDefs.update.name, derivedMutationDefs.update)
|
100
|
+
mutations.map.set(derivedMutationDefs.delete.name, derivedMutationDefs.delete)
|
98
101
|
}
|
99
102
|
}
|
100
103
|
|
@@ -114,6 +117,15 @@ export const makeSchema = <TInputSchema extends InputSchema>(
|
|
114
117
|
} satisfies LiveStoreSchema
|
115
118
|
}
|
116
119
|
|
120
|
+
export const getMutationDef = <TSchema extends LiveStoreSchema>(schema: TSchema, mutationName: string) => {
|
121
|
+
const mutationDef = schema.mutations.map.get(mutationName)
|
122
|
+
if (mutationDef === undefined) {
|
123
|
+
const extraInfo = schema.mutations.wasProvided ? '' : ' Please provide \`mutations\` in the schema options.'
|
124
|
+
return shouldNeverHappen(`No mutation definition found for \`${mutationName}\`.${extraInfo}`)
|
125
|
+
}
|
126
|
+
return mutationDef
|
127
|
+
}
|
128
|
+
|
117
129
|
export namespace FromInputSchema {
|
118
130
|
export type DeriveSchema<TInputSchema extends InputSchema> = LiveStoreSchema<
|
119
131
|
DbSchemaFromInputSchemaTables<TInputSchema['tables']>,
|
@@ -1,6 +1,7 @@
|
|
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
|
+
import * as EventId from './EventId.js'
|
4
5
|
import type { FromTable } from './table-def.js'
|
5
6
|
import { table } from './table-def.js'
|
6
7
|
|
@@ -46,14 +47,15 @@ export const sessionChangesetMetaTable = table(
|
|
46
47
|
SESSION_CHANGESET_META_TABLE,
|
47
48
|
{
|
48
49
|
// TODO bring back primary key
|
49
|
-
idGlobal: SqliteDsl.integer({}),
|
50
|
-
|
51
|
-
|
52
|
-
// idLocal: SqliteDsl.integer({ primaryKey: true }),
|
53
|
-
changeset: SqliteDsl.blob({}),
|
50
|
+
idGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
|
51
|
+
idClient: SqliteDsl.integer({ schema: EventId.ClientEventId }),
|
52
|
+
changeset: SqliteDsl.blob({ nullable: true }),
|
54
53
|
debug: SqliteDsl.json({ nullable: true }),
|
55
54
|
},
|
56
|
-
{
|
55
|
+
{
|
56
|
+
disableAutomaticIdColumn: true,
|
57
|
+
indexes: [{ columns: ['idGlobal', 'idClient'], name: 'idx_session_changeset_id' }],
|
58
|
+
},
|
57
59
|
)
|
58
60
|
|
59
61
|
export type SessionChangesetMetaRow = FromTable.RowDecoded<typeof sessionChangesetMetaTable>
|
@@ -62,7 +64,7 @@ export const systemTables = [schemaMetaTable, schemaMutationsMetaTable, sessionC
|
|
62
64
|
|
63
65
|
/// Mutation log DB
|
64
66
|
|
65
|
-
export const SyncStatus = Schema.Literal('synced', 'pending', 'error', '
|
67
|
+
export const SyncStatus = Schema.Literal('synced', 'pending', 'error', 'clientOnly')
|
66
68
|
export type SyncStatus = typeof SyncStatus.Type
|
67
69
|
|
68
70
|
export const MUTATION_LOG_META_TABLE = 'mutation_log'
|
@@ -70,16 +72,25 @@ export const MUTATION_LOG_META_TABLE = 'mutation_log'
|
|
70
72
|
export const mutationLogMetaTable = table(
|
71
73
|
MUTATION_LOG_META_TABLE,
|
72
74
|
{
|
73
|
-
idGlobal: SqliteDsl.integer({ primaryKey: true }),
|
74
|
-
|
75
|
-
parentIdGlobal: SqliteDsl.integer({}),
|
76
|
-
|
75
|
+
idGlobal: SqliteDsl.integer({ primaryKey: true, schema: EventId.GlobalEventId }),
|
76
|
+
idClient: SqliteDsl.integer({ primaryKey: true, schema: EventId.ClientEventId }),
|
77
|
+
parentIdGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
|
78
|
+
parentIdClient: SqliteDsl.integer({ schema: EventId.ClientEventId }),
|
77
79
|
mutation: SqliteDsl.text({}),
|
78
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 }),
|
79
84
|
schemaHash: SqliteDsl.integer({}),
|
80
85
|
syncMetadataJson: SqliteDsl.text({ schema: Schema.parseJson(Schema.Option(Schema.JsonValue)) }),
|
81
86
|
},
|
82
|
-
{
|
87
|
+
{
|
88
|
+
disableAutomaticIdColumn: true,
|
89
|
+
indexes: [
|
90
|
+
{ columns: ['idGlobal'], name: 'idx_idGlobal' },
|
91
|
+
{ columns: ['idGlobal', 'idClient'], name: 'idx_mutationlog_id' },
|
92
|
+
],
|
93
|
+
},
|
83
94
|
)
|
84
95
|
|
85
96
|
export type MutationLogMetaRow = FromTable.RowDecoded<typeof mutationLogMetaTable>
|
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>
|
@@ -60,8 +59,7 @@ export type TableOptionsInput = Partial<{
|
|
60
59
|
deriveMutations:
|
61
60
|
| boolean
|
62
61
|
| {
|
63
|
-
|
64
|
-
localOnly?: boolean
|
62
|
+
clientOnly?: boolean
|
65
63
|
}
|
66
64
|
}>
|
67
65
|
|
@@ -115,7 +113,7 @@ export type TableOptions = {
|
|
115
113
|
/**
|
116
114
|
* When set to true, the mutations won't be synced over the network
|
117
115
|
*/
|
118
|
-
|
116
|
+
clientOnly: boolean
|
119
117
|
}
|
120
118
|
|
121
119
|
/** Derived based on whether the table definition has one or more columns (besides the `id` column) */
|
@@ -153,12 +151,12 @@ export const table = <
|
|
153
151
|
disableAutomaticIdColumn: options?.disableAutomaticIdColumn ?? false,
|
154
152
|
deriveMutations:
|
155
153
|
options?.deriveMutations === true
|
156
|
-
? { enabled: true as const,
|
154
|
+
? { enabled: true as const, clientOnly: false }
|
157
155
|
: options?.deriveMutations === false
|
158
156
|
? { enabled: false as const }
|
159
157
|
: options?.deriveMutations === undefined
|
160
158
|
? { enabled: false as const }
|
161
|
-
: { enabled: true as const,
|
159
|
+
: { enabled: true as const, clientOnly: options.deriveMutations.clientOnly ?? false },
|
162
160
|
isSingleColumn: SqliteDsl.isColumnDefinition(columnOrColumns) === true,
|
163
161
|
requiredInsertColumnNames: 'type-level-only',
|
164
162
|
}
|
@@ -234,7 +232,7 @@ export const table = <
|
|
234
232
|
export const tableHasDerivedMutations = <TTableDef extends TableDefBase>(
|
235
233
|
tableDef: TTableDef,
|
236
234
|
): tableDef is TTableDef & {
|
237
|
-
options: { deriveMutations: { enabled: true;
|
235
|
+
options: { deriveMutations: { enabled: true; clientOnly: boolean } }
|
238
236
|
} & DerivedMutationHelperFns<TTableDef['sqliteDef']['columns'], TTableDef['options']> =>
|
239
237
|
tableDef.options.deriveMutations.enabled === true
|
240
238
|
|
@@ -242,6 +240,8 @@ export const tableIsSingleton = <TTableDef extends TableDefBase>(
|
|
242
240
|
tableDef: TTableDef,
|
243
241
|
): tableDef is TTableDef & { options: { isSingleton: true } } => tableDef.options.isSingleton === true
|
244
242
|
|
243
|
+
export type PrettifyFlat<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
|
244
|
+
|
245
245
|
type SqliteTableDefForInput<
|
246
246
|
TName extends string,
|
247
247
|
TColumns extends SqliteDsl.Columns | SqliteDsl.ColumnDefinition<any, any>,
|
@@ -268,13 +268,13 @@ type WithDefaults<
|
|
268
268
|
isSingleton: TOptionsInput['isSingleton'] extends true ? true : false
|
269
269
|
disableAutomaticIdColumn: TOptionsInput['disableAutomaticIdColumn'] extends true ? true : false
|
270
270
|
deriveMutations: TOptionsInput['deriveMutations'] extends true
|
271
|
-
? { enabled: true;
|
271
|
+
? { enabled: true; clientOnly: false }
|
272
272
|
: TOptionsInput['deriveMutations'] extends false
|
273
273
|
? { enabled: false }
|
274
|
-
: TOptionsInput['deriveMutations'] extends {
|
274
|
+
: TOptionsInput['deriveMutations'] extends { clientOnly: boolean }
|
275
275
|
? {
|
276
276
|
enabled: true
|
277
|
-
|
277
|
+
clientOnly: TOptionsInput['deriveMutations']['clientOnly'] extends true ? true : false
|
278
278
|
}
|
279
279
|
: never
|
280
280
|
isSingleColumn: SqliteDsl.IsSingleColumn<TColumns>
|
@@ -283,7 +283,7 @@ type WithDefaults<
|
|
283
283
|
|
284
284
|
export namespace FromTable {
|
285
285
|
// TODO this sometimes doesn't preserve the order of columns
|
286
|
-
export type RowDecoded<TTableDef extends TableDefBase> =
|
286
|
+
export type RowDecoded<TTableDef extends TableDefBase> = Types.Simplify<
|
287
287
|
Nullable<Pick<RowDecodedAll<TTableDef>, NullableColumnNames<TTableDef>>> &
|
288
288
|
Omit<RowDecodedAll<TTableDef>, NullableColumnNames<TTableDef>>
|
289
289
|
>
|
@@ -302,7 +302,7 @@ export namespace FromTable {
|
|
302
302
|
>
|
303
303
|
}
|
304
304
|
|
305
|
-
export type RowEncoded<TTableDef extends TableDefBase> =
|
305
|
+
export type RowEncoded<TTableDef extends TableDefBase> = Types.Simplify<
|
306
306
|
Nullable<Pick<RowEncodeNonNullable<TTableDef>, NullableColumnNames<TTableDef>>> &
|
307
307
|
Omit<RowEncodeNonNullable<TTableDef>, NullableColumnNames<TTableDef>>
|
308
308
|
>
|
@@ -314,7 +314,7 @@ export namespace FromTable {
|
|
314
314
|
|
315
315
|
export namespace FromColumns {
|
316
316
|
// TODO this sometimes doesn't preserve the order of columns
|
317
|
-
export type RowDecoded<TColumns extends SqliteDsl.Columns> =
|
317
|
+
export type RowDecoded<TColumns extends SqliteDsl.Columns> = Types.Simplify<
|
318
318
|
Nullable<Pick<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>> &
|
319
319
|
Omit<RowDecodedAll<TColumns>, NullableColumnNames<TColumns>>
|
320
320
|
>
|
@@ -323,7 +323,7 @@ export namespace FromColumns {
|
|
323
323
|
[K in keyof TColumns]: Schema.Schema.Type<TColumns[K]['schema']>
|
324
324
|
}
|
325
325
|
|
326
|
-
export type RowEncoded<TColumns extends SqliteDsl.Columns> =
|
326
|
+
export type RowEncoded<TColumns extends SqliteDsl.Columns> = Types.Simplify<
|
327
327
|
Nullable<Pick<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>> &
|
328
328
|
Omit<RowEncodeNonNullable<TColumns>, NullableColumnNames<TColumns>>
|
329
329
|
>
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type {
|
1
|
+
import type { SqliteDb } from '../adapter-types.js'
|
2
2
|
import type { ParamsObject } from '../util.js'
|
3
3
|
import { prepareBindValues } from '../util.js'
|
4
4
|
|
@@ -6,7 +6,7 @@ import { prepareBindValues } from '../util.js'
|
|
6
6
|
// will require proper scope-aware cleanup etc (for testing and apps with multiple LiveStore instances)
|
7
7
|
// const cachedStmts = new Map<string, PreparedStatement>()
|
8
8
|
|
9
|
-
export const dbExecute = (db:
|
9
|
+
export const dbExecute = (db: SqliteDb, queryStr: string, bindValues?: ParamsObject) => {
|
10
10
|
// let stmt = cachedStmts.get(queryStr)
|
11
11
|
// if (!stmt) {
|
12
12
|
const stmt = db.prepare(queryStr)
|
@@ -20,7 +20,7 @@ export const dbExecute = (db: SynchronousDatabase, queryStr: string, bindValues?
|
|
20
20
|
stmt.finalize()
|
21
21
|
}
|
22
22
|
|
23
|
-
export const dbSelect = <T>(db:
|
23
|
+
export const dbSelect = <T>(db: SqliteDb, queryStr: string, bindValues?: ParamsObject) => {
|
24
24
|
// let stmt = cachedStmts.get(queryStr)
|
25
25
|
// if (!stmt) {
|
26
26
|
const stmt = db.prepare(queryStr)
|