@livestore/common 0.3.0-dev.8 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/__tests__/fixture.d.ts +83 -221
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +33 -11
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/adapter-types.d.ts +120 -64
- package/dist/adapter-types.d.ts.map +1 -1
- package/dist/adapter-types.js +39 -8
- package/dist/adapter-types.js.map +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/debug-info.js +1 -0
- package/dist/debug-info.js.map +1 -1
- package/dist/devtools/devtools-messages-client-session.d.ts +390 -0
- package/dist/devtools/devtools-messages-client-session.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-client-session.js +97 -0
- package/dist/devtools/devtools-messages-client-session.js.map +1 -0
- package/dist/devtools/devtools-messages-common.d.ts +68 -0
- package/dist/devtools/devtools-messages-common.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-common.js +60 -0
- package/dist/devtools/devtools-messages-common.js.map +1 -0
- package/dist/devtools/devtools-messages-leader.d.ts +394 -0
- package/dist/devtools/devtools-messages-leader.d.ts.map +1 -0
- package/dist/devtools/devtools-messages-leader.js +147 -0
- package/dist/devtools/devtools-messages-leader.js.map +1 -0
- package/dist/devtools/devtools-messages.d.ts +3 -580
- package/dist/devtools/devtools-messages.d.ts.map +1 -1
- package/dist/devtools/devtools-messages.js +3 -174
- package/dist/devtools/devtools-messages.js.map +1 -1
- package/dist/devtools/devtools-sessioninfo.d.ts +32 -0
- package/dist/devtools/devtools-sessioninfo.d.ts.map +1 -0
- package/dist/devtools/devtools-sessioninfo.js +36 -0
- package/dist/devtools/devtools-sessioninfo.js.map +1 -0
- package/dist/devtools/mod.d.ts +55 -0
- package/dist/devtools/mod.d.ts.map +1 -0
- package/dist/devtools/mod.js +33 -0
- package/dist/devtools/mod.js.map +1 -0
- package/dist/index.d.ts +7 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -9
- package/dist/index.js.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.d.ts +45 -30
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
- package/dist/leader-thread/LeaderSyncProcessor.js +484 -321
- package/dist/leader-thread/LeaderSyncProcessor.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/eventlog.d.ts +27 -0
- package/dist/leader-thread/eventlog.d.ts.map +1 -0
- package/dist/leader-thread/eventlog.js +119 -0
- package/dist/leader-thread/eventlog.js.map +1 -0
- package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
- package/dist/leader-thread/leader-worker-devtools.js +155 -80
- package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.d.ts +22 -9
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +67 -45
- package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
- package/dist/leader-thread/materialize-event.d.ts +16 -0
- package/dist/leader-thread/materialize-event.d.ts.map +1 -0
- package/dist/leader-thread/materialize-event.js +109 -0
- package/dist/leader-thread/materialize-event.js.map +1 -0
- package/dist/leader-thread/mod.d.ts +1 -1
- package/dist/leader-thread/mod.d.ts.map +1 -1
- package/dist/leader-thread/mod.js +1 -1
- package/dist/leader-thread/mod.js.map +1 -1
- package/dist/leader-thread/recreate-db.d.ts +4 -2
- package/dist/leader-thread/recreate-db.d.ts.map +1 -1
- package/dist/leader-thread/recreate-db.js +28 -32
- 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 +85 -38
- 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/make-client-session.d.ts +23 -0
- package/dist/make-client-session.d.ts.map +1 -0
- package/dist/make-client-session.js +57 -0
- package/dist/make-client-session.js.map +1 -0
- package/dist/materializer-helper.d.ts +23 -0
- package/dist/materializer-helper.d.ts.map +1 -0
- package/dist/materializer-helper.js +86 -0
- package/dist/materializer-helper.js.map +1 -0
- 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/rematerialize-from-eventlog.d.ts +14 -0
- package/dist/rematerialize-from-eventlog.d.ts.map +1 -0
- package/dist/rematerialize-from-eventlog.js +64 -0
- package/dist/rematerialize-from-eventlog.js.map +1 -0
- package/dist/schema/EventDef.d.ts +146 -0
- package/dist/schema/EventDef.d.ts.map +1 -0
- package/dist/schema/EventDef.js +58 -0
- package/dist/schema/EventDef.js.map +1 -0
- package/dist/schema/EventSequenceNumber.d.ts +57 -0
- package/dist/schema/EventSequenceNumber.d.ts.map +1 -0
- package/dist/schema/EventSequenceNumber.js +82 -0
- package/dist/schema/EventSequenceNumber.js.map +1 -0
- package/dist/schema/EventSequenceNumber.test.d.ts +2 -0
- package/dist/schema/EventSequenceNumber.test.d.ts.map +1 -0
- package/dist/schema/EventSequenceNumber.test.js +11 -0
- package/dist/schema/EventSequenceNumber.test.js.map +1 -0
- package/dist/schema/LiveStoreEvent.d.ts +257 -0
- package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
- package/dist/schema/LiveStoreEvent.js +117 -0
- package/dist/schema/LiveStoreEvent.js.map +1 -0
- package/dist/schema/events.d.ts +2 -0
- package/dist/schema/events.d.ts.map +1 -0
- package/dist/schema/events.js +2 -0
- package/dist/schema/events.js.map +1 -0
- package/dist/schema/mod.d.ts +8 -6
- package/dist/schema/mod.d.ts.map +1 -1
- package/dist/schema/mod.js +8 -6
- package/dist/schema/mod.js.map +1 -1
- package/dist/schema/schema.d.ts +50 -32
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +36 -43
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/state/mod.d.ts +3 -0
- package/dist/schema/state/mod.d.ts.map +1 -0
- package/dist/schema/state/mod.js +3 -0
- package/dist/schema/state/mod.js.map +1 -0
- package/dist/schema/state/sqlite/client-document-def.d.ts +223 -0
- package/dist/schema/state/sqlite/client-document-def.d.ts.map +1 -0
- package/dist/schema/state/sqlite/client-document-def.js +170 -0
- package/dist/schema/state/sqlite/client-document-def.js.map +1 -0
- package/dist/schema/state/sqlite/client-document-def.test.d.ts +2 -0
- package/dist/schema/state/sqlite/client-document-def.test.d.ts.map +1 -0
- package/dist/schema/state/sqlite/client-document-def.test.js +201 -0
- package/dist/schema/state/sqlite/client-document-def.test.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts +69 -0
- package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/ast/sqlite.js +71 -0
- package/dist/schema/state/sqlite/db-schema/ast/sqlite.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/ast/validate.d.ts +3 -0
- package/dist/schema/state/sqlite/db-schema/ast/validate.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/ast/validate.js +12 -0
- package/dist/schema/state/sqlite/db-schema/ast/validate.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts +90 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js +87 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.d.ts +2 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js +29 -0
- package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts +90 -0
- package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/dsl/mod.js +41 -0
- package/dist/schema/state/sqlite/db-schema/dsl/mod.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/hash.d.ts +2 -0
- package/dist/schema/state/sqlite/db-schema/hash.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/hash.js +14 -0
- package/dist/schema/state/sqlite/db-schema/hash.js.map +1 -0
- package/dist/schema/state/sqlite/db-schema/mod.d.ts +3 -0
- package/dist/schema/state/sqlite/db-schema/mod.d.ts.map +1 -0
- package/dist/schema/state/sqlite/db-schema/mod.js +3 -0
- package/dist/schema/state/sqlite/db-schema/mod.js.map +1 -0
- package/dist/schema/state/sqlite/mod.d.ts +17 -0
- package/dist/schema/state/sqlite/mod.d.ts.map +1 -0
- package/dist/schema/state/sqlite/mod.js +41 -0
- package/dist/schema/state/sqlite/mod.js.map +1 -0
- package/dist/schema/state/sqlite/query-builder/api.d.ts +294 -0
- package/dist/schema/state/sqlite/query-builder/api.d.ts.map +1 -0
- package/dist/schema/state/sqlite/query-builder/api.js +6 -0
- package/dist/schema/state/sqlite/query-builder/api.js.map +1 -0
- package/dist/schema/state/sqlite/query-builder/astToSql.d.ts +7 -0
- package/dist/schema/state/sqlite/query-builder/astToSql.d.ts.map +1 -0
- package/dist/schema/state/sqlite/query-builder/astToSql.js +190 -0
- package/dist/schema/state/sqlite/query-builder/astToSql.js.map +1 -0
- package/dist/schema/state/sqlite/query-builder/impl.d.ts +7 -0
- package/dist/schema/state/sqlite/query-builder/impl.d.ts.map +1 -0
- package/dist/schema/state/sqlite/query-builder/impl.js +286 -0
- package/dist/schema/state/sqlite/query-builder/impl.js.map +1 -0
- package/dist/schema/state/sqlite/query-builder/impl.test.d.ts +87 -0
- package/dist/schema/state/sqlite/query-builder/impl.test.d.ts.map +1 -0
- package/dist/schema/state/sqlite/query-builder/impl.test.js +563 -0
- package/dist/schema/state/sqlite/query-builder/impl.test.js.map +1 -0
- package/dist/{query-builder → schema/state/sqlite/query-builder}/mod.d.ts +7 -0
- package/dist/schema/state/sqlite/query-builder/mod.d.ts.map +1 -0
- package/dist/{query-builder → schema/state/sqlite/query-builder}/mod.js +7 -0
- package/dist/schema/state/sqlite/query-builder/mod.js.map +1 -0
- package/dist/schema/state/sqlite/schema-helpers.d.ts.map +1 -0
- package/dist/schema/{schema-helpers.js → state/sqlite/schema-helpers.js} +1 -1
- package/dist/schema/state/sqlite/schema-helpers.js.map +1 -0
- package/dist/schema/state/sqlite/system-tables.d.ts +574 -0
- package/dist/schema/state/sqlite/system-tables.d.ts.map +1 -0
- package/dist/schema/state/sqlite/system-tables.js +88 -0
- package/dist/schema/state/sqlite/system-tables.js.map +1 -0
- package/dist/schema/state/sqlite/table-def.d.ts +84 -0
- package/dist/schema/state/sqlite/table-def.d.ts.map +1 -0
- package/dist/schema/state/sqlite/table-def.js +36 -0
- package/dist/schema/state/sqlite/table-def.js.map +1 -0
- package/dist/schema-management/common.d.ts +7 -7
- 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 +27 -18
- package/dist/schema-management/migrations.js.map +1 -1
- package/dist/schema-management/validate-schema.d.ts +8 -0
- package/dist/schema-management/validate-schema.d.ts.map +1 -0
- package/dist/schema-management/validate-schema.js +39 -0
- package/dist/schema-management/validate-schema.js.map +1 -0
- 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/ClientSessionSyncProcessor.d.ts +40 -19
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
- package/dist/sync/ClientSessionSyncProcessor.js +150 -72
- package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
- package/dist/sync/next/compact-events.d.ts.map +1 -1
- package/dist/sync/next/compact-events.js +38 -35
- package/dist/sync/next/compact-events.js.map +1 -1
- package/dist/sync/next/facts.d.ts +21 -21
- package/dist/sync/next/facts.d.ts.map +1 -1
- package/dist/sync/next/facts.js +11 -11
- package/dist/sync/next/facts.js.map +1 -1
- package/dist/sync/next/history-dag-common.d.ts +9 -7
- package/dist/sync/next/history-dag-common.d.ts.map +1 -1
- package/dist/sync/next/history-dag-common.js +10 -5
- package/dist/sync/next/history-dag-common.js.map +1 -1
- package/dist/sync/next/history-dag.d.ts +0 -2
- package/dist/sync/next/history-dag.d.ts.map +1 -1
- package/dist/sync/next/history-dag.js +16 -14
- package/dist/sync/next/history-dag.js.map +1 -1
- package/dist/sync/next/rebase-events.d.ts +10 -8
- package/dist/sync/next/rebase-events.d.ts.map +1 -1
- package/dist/sync/next/rebase-events.js +18 -10
- package/dist/sync/next/rebase-events.js.map +1 -1
- package/dist/sync/next/test/compact-events.calculator.test.js +39 -34
- package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
- package/dist/sync/next/test/compact-events.test.js +77 -77
- package/dist/sync/next/test/compact-events.test.js.map +1 -1
- package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +35 -25
- package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
- package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +81 -38
- package/dist/sync/next/test/event-fixtures.js.map +1 -0
- package/dist/sync/next/test/mod.d.ts +1 -1
- package/dist/sync/next/test/mod.d.ts.map +1 -1
- package/dist/sync/next/test/mod.js +1 -1
- package/dist/sync/next/test/mod.js.map +1 -1
- package/dist/sync/sync.d.ts +46 -21
- package/dist/sync/sync.d.ts.map +1 -1
- package/dist/sync/sync.js +10 -6
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/syncstate.d.ts +213 -82
- package/dist/sync/syncstate.d.ts.map +1 -1
- package/dist/sync/syncstate.js +337 -139
- package/dist/sync/syncstate.js.map +1 -1
- package/dist/sync/syncstate.test.js +309 -286
- 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 +4 -4
- 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 +2 -2
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +2 -2
- package/dist/version.js.map +1 -1
- package/package.json +10 -4
- package/src/__tests__/fixture.ts +36 -15
- package/src/adapter-types.ts +107 -68
- package/src/debug-info.ts +1 -0
- package/src/devtools/devtools-messages-client-session.ts +142 -0
- package/src/devtools/devtools-messages-common.ts +115 -0
- package/src/devtools/devtools-messages-leader.ts +191 -0
- package/src/devtools/devtools-messages.ts +3 -246
- package/src/devtools/devtools-sessioninfo.ts +101 -0
- package/src/devtools/mod.ts +59 -0
- package/src/index.ts +7 -9
- package/src/leader-thread/LeaderSyncProcessor.ts +738 -477
- package/src/leader-thread/connection.ts +54 -9
- package/src/leader-thread/eventlog.ts +199 -0
- package/src/leader-thread/leader-worker-devtools.ts +227 -104
- package/src/leader-thread/make-leader-thread-layer.ts +121 -72
- package/src/leader-thread/materialize-event.ts +173 -0
- package/src/leader-thread/mod.ts +1 -1
- package/src/leader-thread/recreate-db.ts +33 -38
- package/src/leader-thread/shutdown-channel.ts +2 -4
- package/src/leader-thread/types.ts +94 -48
- package/src/make-client-session.ts +136 -0
- package/src/materializer-helper.ts +138 -0
- package/src/otel.ts +8 -0
- package/src/rematerialize-from-eventlog.ts +117 -0
- package/src/schema/EventDef.ts +227 -0
- package/src/schema/EventSequenceNumber.test.ts +12 -0
- package/src/schema/EventSequenceNumber.ts +121 -0
- package/src/schema/LiveStoreEvent.ts +240 -0
- package/src/schema/events.ts +1 -0
- package/src/schema/mod.ts +8 -6
- package/src/schema/schema.ts +88 -84
- package/src/schema/state/mod.ts +2 -0
- package/src/schema/state/sqlite/client-document-def.test.ts +238 -0
- package/src/schema/state/sqlite/client-document-def.ts +444 -0
- package/src/schema/state/sqlite/db-schema/ast/sqlite.ts +142 -0
- package/src/schema/state/sqlite/db-schema/ast/validate.ts +13 -0
- package/src/schema/state/sqlite/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +206 -0
- package/src/schema/state/sqlite/db-schema/dsl/field-defs.test.ts +35 -0
- package/src/schema/state/sqlite/db-schema/dsl/field-defs.ts +242 -0
- package/src/schema/state/sqlite/db-schema/dsl/mod.ts +222 -0
- package/src/schema/state/sqlite/db-schema/hash.ts +14 -0
- package/src/schema/state/sqlite/db-schema/mod.ts +2 -0
- package/src/schema/state/sqlite/mod.ts +73 -0
- package/src/schema/state/sqlite/query-builder/api.ts +440 -0
- package/src/schema/state/sqlite/query-builder/astToSql.ts +232 -0
- package/src/schema/state/sqlite/query-builder/impl.test.ts +617 -0
- package/src/schema/state/sqlite/query-builder/impl.ts +351 -0
- package/src/{query-builder → schema/state/sqlite/query-builder}/mod.ts +7 -0
- package/src/schema/{schema-helpers.ts → state/sqlite/schema-helpers.ts} +1 -1
- package/src/schema/state/sqlite/system-tables.ts +117 -0
- package/src/schema/state/sqlite/table-def.ts +197 -0
- package/src/schema-management/common.ts +7 -7
- package/src/schema-management/migrations.ts +37 -31
- package/src/schema-management/validate-schema.ts +61 -0
- 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 +220 -94
- package/src/sync/next/compact-events.ts +38 -35
- package/src/sync/next/facts.ts +43 -41
- package/src/sync/next/history-dag-common.ts +17 -10
- package/src/sync/next/history-dag.ts +16 -17
- package/src/sync/next/rebase-events.ts +29 -17
- package/src/sync/next/test/compact-events.calculator.test.ts +46 -46
- package/src/sync/next/test/compact-events.test.ts +79 -79
- package/src/sync/next/test/event-fixtures.ts +226 -0
- package/src/sync/next/test/mod.ts +1 -1
- package/src/sync/sync.ts +46 -21
- package/src/sync/syncstate.test.ts +346 -320
- package/src/sync/syncstate.ts +422 -230
- package/src/sync/validate-push-payload.ts +6 -6
- package/src/version.ts +2 -2
- package/dist/derived-mutations.d.ts +0 -109
- package/dist/derived-mutations.d.ts.map +0 -1
- package/dist/derived-mutations.js +0 -54
- package/dist/derived-mutations.js.map +0 -1
- package/dist/derived-mutations.test.d.ts +0 -2
- package/dist/derived-mutations.test.d.ts.map +0 -1
- package/dist/derived-mutations.test.js +0 -93
- package/dist/derived-mutations.test.js.map +0 -1
- package/dist/devtools/devtools-bridge.d.ts +0 -13
- 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/devtools-window-message.d.ts +0 -29
- package/dist/devtools/devtools-window-message.d.ts.map +0 -1
- package/dist/devtools/devtools-window-message.js +0 -33
- package/dist/devtools/devtools-window-message.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/init-singleton-tables.d.ts +0 -4
- package/dist/init-singleton-tables.d.ts.map +0 -1
- package/dist/init-singleton-tables.js +0 -16
- package/dist/init-singleton-tables.js.map +0 -1
- package/dist/leader-thread/apply-mutation.d.ts +0 -11
- package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
- package/dist/leader-thread/apply-mutation.js +0 -107
- package/dist/leader-thread/apply-mutation.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 -430
- package/dist/leader-thread/leader-sync-processor.js.map +0 -1
- package/dist/leader-thread/mutationlog.d.ts +0 -10
- package/dist/leader-thread/mutationlog.d.ts.map +0 -1
- package/dist/leader-thread/mutationlog.js +0 -28
- package/dist/leader-thread/mutationlog.js.map +0 -1
- package/dist/leader-thread/pull-queue-set.d.ts +0 -7
- package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
- package/dist/leader-thread/pull-queue-set.js +0 -39
- package/dist/leader-thread/pull-queue-set.js.map +0 -1
- package/dist/mutation.d.ts +0 -20
- package/dist/mutation.d.ts.map +0 -1
- package/dist/mutation.js +0 -57
- package/dist/mutation.js.map +0 -1
- package/dist/query-builder/api.d.ts +0 -190
- package/dist/query-builder/api.d.ts.map +0 -1
- package/dist/query-builder/api.js +0 -8
- package/dist/query-builder/api.js.map +0 -1
- package/dist/query-builder/impl.d.ts +0 -12
- package/dist/query-builder/impl.d.ts.map +0 -1
- package/dist/query-builder/impl.js +0 -244
- package/dist/query-builder/impl.js.map +0 -1
- package/dist/query-builder/impl.test.d.ts +0 -2
- package/dist/query-builder/impl.test.d.ts.map +0 -1
- package/dist/query-builder/impl.test.js +0 -212
- package/dist/query-builder/impl.test.js.map +0 -1
- package/dist/query-builder/mod.d.ts.map +0 -1
- package/dist/query-builder/mod.js.map +0 -1
- package/dist/query-info.d.ts +0 -38
- package/dist/query-info.d.ts.map +0 -1
- package/dist/query-info.js +0 -7
- package/dist/query-info.js.map +0 -1
- package/dist/rehydrate-from-mutationlog.d.ts +0 -14
- package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
- package/dist/rehydrate-from-mutationlog.js +0 -66
- package/dist/rehydrate-from-mutationlog.js.map +0 -1
- package/dist/schema/EventId.d.ts +0 -39
- package/dist/schema/EventId.d.ts.map +0 -1
- package/dist/schema/EventId.js +0 -38
- package/dist/schema/EventId.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.d.ts +0 -166
- package/dist/schema/MutationEvent.d.ts.map +0 -1
- package/dist/schema/MutationEvent.js +0 -72
- package/dist/schema/MutationEvent.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/schema/mutations.d.ts +0 -107
- package/dist/schema/mutations.d.ts.map +0 -1
- package/dist/schema/mutations.js +0 -42
- package/dist/schema/mutations.js.map +0 -1
- package/dist/schema/schema-helpers.d.ts.map +0 -1
- package/dist/schema/schema-helpers.js.map +0 -1
- package/dist/schema/system-tables.d.ts +0 -399
- package/dist/schema/system-tables.d.ts.map +0 -1
- package/dist/schema/system-tables.js +0 -59
- package/dist/schema/system-tables.js.map +0 -1
- package/dist/schema/table-def.d.ts +0 -156
- package/dist/schema/table-def.d.ts.map +0 -1
- package/dist/schema/table-def.js +0 -79
- package/dist/schema/table-def.js.map +0 -1
- package/dist/schema-management/validate-mutation-defs.d.ts +0 -8
- package/dist/schema-management/validate-mutation-defs.d.ts.map +0 -1
- package/dist/schema-management/validate-mutation-defs.js +0 -39
- package/dist/schema-management/validate-mutation-defs.js.map +0 -1
- package/dist/sync/client-session-sync-processor.d.ts +0 -45
- 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/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
- package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
- package/src/derived-mutations.test.ts +0 -101
- package/src/derived-mutations.ts +0 -170
- package/src/devtools/devtools-bridge.ts +0 -14
- package/src/devtools/devtools-window-message.ts +0 -27
- package/src/devtools/index.ts +0 -48
- package/src/init-singleton-tables.ts +0 -24
- package/src/leader-thread/apply-mutation.ts +0 -161
- package/src/leader-thread/mutationlog.ts +0 -46
- package/src/leader-thread/pull-queue-set.ts +0 -58
- package/src/mutation.ts +0 -91
- package/src/query-builder/api.ts +0 -289
- package/src/query-builder/impl.test.ts +0 -239
- package/src/query-builder/impl.ts +0 -285
- package/src/query-info.ts +0 -78
- package/src/rehydrate-from-mutationlog.ts +0 -119
- package/src/schema/EventId.test.ts +0 -12
- package/src/schema/EventId.ts +0 -60
- package/src/schema/MutationEvent.ts +0 -181
- package/src/schema/mutations.ts +0 -192
- package/src/schema/system-tables.ts +0 -105
- package/src/schema/table-def.ts +0 -343
- package/src/schema-management/validate-mutation-defs.ts +0 -63
- package/src/sync/next/test/mutation-fixtures.ts +0 -224
- package/tsconfig.json +0 -11
- /package/dist/schema/{schema-helpers.d.ts → state/sqlite/schema-helpers.d.ts} +0 -0
@@ -1,285 +0,0 @@
|
|
1
|
-
import { Option, Predicate, Schema } from '@livestore/utils/effect'
|
2
|
-
|
3
|
-
import type { QueryInfo } from '../query-info.js'
|
4
|
-
import type { DbSchema } from '../schema/mod.js'
|
5
|
-
import type { QueryBuilder, QueryBuilderAst } from './api.js'
|
6
|
-
import { QueryBuilderAstSymbol, TypeId } from './api.js'
|
7
|
-
|
8
|
-
export const makeQueryBuilder = <TResult, TTableDef extends DbSchema.TableDefBase>(
|
9
|
-
tableDef: TTableDef,
|
10
|
-
ast: QueryBuilderAst = emptyAst(tableDef),
|
11
|
-
): QueryBuilder<TResult, TTableDef, never, QueryInfo.None> => {
|
12
|
-
const api = {
|
13
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
14
|
-
select() {
|
15
|
-
assertQueryBuilderAst(ast)
|
16
|
-
|
17
|
-
// eslint-disable-next-line prefer-rest-params
|
18
|
-
const params = [...arguments]
|
19
|
-
|
20
|
-
if (params.length === 2 && typeof params[0] === 'string' && typeof params[1] === 'object') {
|
21
|
-
const [col, options] = params as any as [string, { pluck: boolean }]
|
22
|
-
return makeQueryBuilder(tableDef, {
|
23
|
-
...ast,
|
24
|
-
resultSchemaSingle: options.pluck ? ast.resultSchemaSingle.pipe(Schema.pluck(col)) : ast.resultSchemaSingle,
|
25
|
-
select: { columns: [col] },
|
26
|
-
})
|
27
|
-
}
|
28
|
-
|
29
|
-
const columns = params as unknown as ReadonlyArray<string>
|
30
|
-
|
31
|
-
return makeQueryBuilder(tableDef, {
|
32
|
-
...ast,
|
33
|
-
resultSchemaSingle:
|
34
|
-
columns.length === 0 ? ast.resultSchemaSingle : ast.resultSchemaSingle.pipe(Schema.pick(...columns)),
|
35
|
-
select: { columns },
|
36
|
-
}) as any
|
37
|
-
},
|
38
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
39
|
-
where() {
|
40
|
-
if (isRowQuery(ast)) return invalidQueryBuilder()
|
41
|
-
|
42
|
-
if (arguments.length === 1) {
|
43
|
-
// eslint-disable-next-line prefer-rest-params
|
44
|
-
const params = arguments[0]
|
45
|
-
const newOps = Object.entries(params)
|
46
|
-
.filter(([, value]) => value !== undefined)
|
47
|
-
.map<QueryBuilderAst.Where>(([col, value]) =>
|
48
|
-
Predicate.hasProperty(value, 'op') && Predicate.hasProperty(value, 'value')
|
49
|
-
? ({ col, op: value.op, value: value.value } as any)
|
50
|
-
: { col, op: '=', value },
|
51
|
-
)
|
52
|
-
|
53
|
-
return makeQueryBuilder(tableDef, {
|
54
|
-
...ast,
|
55
|
-
where: [...ast.where, ...newOps],
|
56
|
-
}) as any
|
57
|
-
}
|
58
|
-
|
59
|
-
// eslint-disable-next-line prefer-rest-params
|
60
|
-
const [col, opOrValue, valueOrUndefined] = arguments
|
61
|
-
const op = valueOrUndefined === undefined ? '=' : opOrValue
|
62
|
-
const value = valueOrUndefined === undefined ? opOrValue : valueOrUndefined
|
63
|
-
return makeQueryBuilder(tableDef, {
|
64
|
-
...ast,
|
65
|
-
where: [...ast.where, { col, op, value }],
|
66
|
-
})
|
67
|
-
},
|
68
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
69
|
-
orderBy() {
|
70
|
-
assertQueryBuilderAst(ast)
|
71
|
-
|
72
|
-
if (arguments.length === 0 || arguments.length > 2) return invalidQueryBuilder()
|
73
|
-
|
74
|
-
if (arguments.length === 1) {
|
75
|
-
// eslint-disable-next-line prefer-rest-params
|
76
|
-
const params = arguments[0] as QueryBuilder.OrderByParams<TTableDef>
|
77
|
-
return makeQueryBuilder(tableDef, {
|
78
|
-
...ast,
|
79
|
-
orderBy: [...ast.orderBy, ...params],
|
80
|
-
})
|
81
|
-
}
|
82
|
-
|
83
|
-
// eslint-disable-next-line prefer-rest-params
|
84
|
-
const [col, direction] = arguments as any as [keyof TTableDef['sqliteDef']['columns'] & string, 'asc' | 'desc']
|
85
|
-
|
86
|
-
return makeQueryBuilder(tableDef, {
|
87
|
-
...ast,
|
88
|
-
orderBy: [...ast.orderBy, { col, direction }],
|
89
|
-
}) as any
|
90
|
-
},
|
91
|
-
limit: (limit) => {
|
92
|
-
assertQueryBuilderAst(ast)
|
93
|
-
|
94
|
-
return makeQueryBuilder(tableDef, { ...ast, limit: Option.some(limit) })
|
95
|
-
},
|
96
|
-
offset: (offset) => {
|
97
|
-
assertQueryBuilderAst(ast)
|
98
|
-
|
99
|
-
return makeQueryBuilder(tableDef, { ...ast, offset: Option.some(offset) })
|
100
|
-
},
|
101
|
-
count: () => {
|
102
|
-
if (isRowQuery(ast)) return invalidQueryBuilder()
|
103
|
-
|
104
|
-
return makeQueryBuilder(tableDef, {
|
105
|
-
...ast,
|
106
|
-
resultSchema: Schema.Struct({ count: Schema.Number }).pipe(
|
107
|
-
Schema.pluck('count'),
|
108
|
-
Schema.Array,
|
109
|
-
Schema.headOrElse(),
|
110
|
-
),
|
111
|
-
_tag: 'CountQuery',
|
112
|
-
})
|
113
|
-
},
|
114
|
-
first: (options) => {
|
115
|
-
assertQueryBuilderAst(ast)
|
116
|
-
|
117
|
-
if (ast.limit._tag === 'Some') return invalidQueryBuilder(`.first() can't be called after .limit()`)
|
118
|
-
|
119
|
-
return makeQueryBuilder(tableDef, {
|
120
|
-
...ast,
|
121
|
-
limit: Option.some(1),
|
122
|
-
// TODO improve
|
123
|
-
pickFirst: options?.fallback ? { fallback: options.fallback } : { fallback: () => undefined },
|
124
|
-
})
|
125
|
-
},
|
126
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
127
|
-
row() {
|
128
|
-
// eslint-disable-next-line prefer-rest-params
|
129
|
-
const params = [...arguments]
|
130
|
-
|
131
|
-
let id: string
|
132
|
-
|
133
|
-
if (tableDef.options.isSingleton) {
|
134
|
-
id = tableDef.sqliteDef.columns.id!.default.pipe(Option.getOrThrow)
|
135
|
-
} else {
|
136
|
-
id = params[0] as string
|
137
|
-
if (id === undefined) {
|
138
|
-
invalidQueryBuilder(`Id missing for row query on non-singleton table ${tableDef.sqliteDef.name}`)
|
139
|
-
}
|
140
|
-
}
|
141
|
-
|
142
|
-
// TODO validate all required columns are present and values are matching the schema
|
143
|
-
const insertValues: Record<string, unknown> = params[1]?.insertValues ?? {}
|
144
|
-
|
145
|
-
return makeQueryBuilder(tableDef, {
|
146
|
-
_tag: 'RowQuery',
|
147
|
-
id,
|
148
|
-
tableDef,
|
149
|
-
insertValues,
|
150
|
-
}) as any
|
151
|
-
},
|
152
|
-
} satisfies QueryBuilder.ApiFull<TResult, TTableDef, never, QueryInfo.None>
|
153
|
-
|
154
|
-
return {
|
155
|
-
[TypeId]: TypeId,
|
156
|
-
[QueryBuilderAstSymbol]: ast,
|
157
|
-
asSql: () => astToSql(ast),
|
158
|
-
toString: () => {
|
159
|
-
try {
|
160
|
-
return astToSql(ast).query
|
161
|
-
} catch (cause) {
|
162
|
-
console.debug(`QueryBuilder.toString(): Error converting query builder to string`, cause, ast)
|
163
|
-
return `Error converting query builder to string`
|
164
|
-
}
|
165
|
-
},
|
166
|
-
...api,
|
167
|
-
} satisfies QueryBuilder<TResult, TTableDef>
|
168
|
-
}
|
169
|
-
|
170
|
-
const emptyAst = (tableDef: DbSchema.TableDefBase) =>
|
171
|
-
({
|
172
|
-
_tag: 'SelectQuery',
|
173
|
-
columns: [],
|
174
|
-
pickFirst: false,
|
175
|
-
select: { columns: [] },
|
176
|
-
orderBy: [],
|
177
|
-
offset: Option.none(),
|
178
|
-
limit: Option.none(),
|
179
|
-
tableDef,
|
180
|
-
where: [],
|
181
|
-
resultSchemaSingle: tableDef.schema,
|
182
|
-
}) satisfies QueryBuilderAst
|
183
|
-
|
184
|
-
const astToSql = (ast: QueryBuilderAst) => {
|
185
|
-
if (isRowQuery(ast)) {
|
186
|
-
// TODO
|
187
|
-
return { query: `SELECT * FROM '${ast.tableDef.sqliteDef.name}' WHERE id = ?`, bindValues: [ast.id as TODO] }
|
188
|
-
}
|
189
|
-
|
190
|
-
const bindValues: unknown[] = []
|
191
|
-
|
192
|
-
// TODO bind values
|
193
|
-
const whereStmt =
|
194
|
-
ast.where.length > 0
|
195
|
-
? `WHERE ${ast.where
|
196
|
-
.map(({ col, op, value }) => {
|
197
|
-
if (value === null) {
|
198
|
-
if (op !== '=' && op !== '!=') {
|
199
|
-
throw new Error(`Unsupported operator for NULL value: ${op}`)
|
200
|
-
}
|
201
|
-
const opStmt = op === '=' ? 'IS' : 'IS NOT'
|
202
|
-
return `${col} ${opStmt} NULL`
|
203
|
-
} else {
|
204
|
-
const colDef = ast.tableDef.sqliteDef.columns[col]
|
205
|
-
if (colDef === undefined) {
|
206
|
-
throw new Error(`Column ${col} not found`)
|
207
|
-
}
|
208
|
-
const isArray = op === 'IN' || op === 'NOT IN'
|
209
|
-
const colSchema = isArray ? Schema.Array(colDef.schema) : colDef.schema
|
210
|
-
const encodedValue = Schema.encodeSync(colSchema)(value)
|
211
|
-
|
212
|
-
if (isArray) {
|
213
|
-
bindValues.push(...encodedValue)
|
214
|
-
const placeholders = Array.from({ length: encodedValue.length }, () => '?').join(', ')
|
215
|
-
return `${col} ${op} (${placeholders})`
|
216
|
-
} else {
|
217
|
-
bindValues.push(encodedValue)
|
218
|
-
return `${col} ${op} ?`
|
219
|
-
}
|
220
|
-
}
|
221
|
-
})
|
222
|
-
.join(' AND ')}`
|
223
|
-
: ''
|
224
|
-
|
225
|
-
if (ast._tag === 'CountQuery') {
|
226
|
-
const selectFromStmt = `SELECT COUNT(*) as count FROM '${ast.tableDef.sqliteDef.name}'`
|
227
|
-
const query = [selectFromStmt, whereStmt].filter((_) => _.length > 0).join(' ')
|
228
|
-
return { query, bindValues }
|
229
|
-
}
|
230
|
-
const columnsStmt = ast.select.columns.length === 0 ? '*' : ast.select.columns.join(', ')
|
231
|
-
const selectStmt = `SELECT ${columnsStmt}`
|
232
|
-
const fromStmt = `FROM '${ast.tableDef.sqliteDef.name}'`
|
233
|
-
|
234
|
-
const orderByStmt =
|
235
|
-
ast.orderBy.length > 0
|
236
|
-
? `ORDER BY ${ast.orderBy.map(({ col, direction }) => `${col} ${direction}`).join(', ')}`
|
237
|
-
: ''
|
238
|
-
|
239
|
-
const limitStmt = ast.limit._tag === 'Some' ? `LIMIT ?` : ''
|
240
|
-
if (ast.limit._tag === 'Some') bindValues.push(ast.limit.value)
|
241
|
-
|
242
|
-
const offsetStmt = ast.offset._tag === 'Some' ? `OFFSET ?` : ''
|
243
|
-
if (ast.offset._tag === 'Some') bindValues.push(ast.offset.value)
|
244
|
-
|
245
|
-
const query = [selectStmt, fromStmt, whereStmt, orderByStmt, offsetStmt, limitStmt]
|
246
|
-
.map((_) => _.trim())
|
247
|
-
.filter((_) => _.length > 0)
|
248
|
-
.join(' ')
|
249
|
-
|
250
|
-
// TODO bind values
|
251
|
-
return { query, bindValues }
|
252
|
-
}
|
253
|
-
|
254
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
255
|
-
function assertQueryBuilderAst(ast: QueryBuilderAst): asserts ast is QueryBuilderAst.SelectQuery {
|
256
|
-
if (ast._tag !== 'SelectQuery') {
|
257
|
-
throw new Error('Expected SelectQuery but got ' + ast._tag)
|
258
|
-
}
|
259
|
-
}
|
260
|
-
|
261
|
-
const isRowQuery = (ast: QueryBuilderAst): ast is QueryBuilderAst.RowQuery => ast._tag === 'RowQuery'
|
262
|
-
|
263
|
-
export const invalidQueryBuilder = (msg?: string) => {
|
264
|
-
throw new Error('Invalid query builder' + (msg ? `: ${msg}` : ''))
|
265
|
-
}
|
266
|
-
|
267
|
-
export const getResultSchema = (qb: QueryBuilder<any, any, any>) => {
|
268
|
-
const queryAst = qb[QueryBuilderAstSymbol]
|
269
|
-
if (queryAst._tag === 'SelectQuery') {
|
270
|
-
const arraySchema = Schema.Array(queryAst.resultSchemaSingle)
|
271
|
-
if (queryAst.pickFirst !== false) {
|
272
|
-
return arraySchema.pipe(Schema.headOrElse(queryAst.pickFirst.fallback))
|
273
|
-
}
|
274
|
-
|
275
|
-
return arraySchema
|
276
|
-
} else if (queryAst._tag === 'CountQuery') {
|
277
|
-
return Schema.Struct({ count: Schema.Number }).pipe(Schema.pluck('count'), Schema.Array, Schema.headOrElse())
|
278
|
-
} else {
|
279
|
-
if (queryAst.tableDef.options.isSingleColumn) {
|
280
|
-
return queryAst.tableDef.schema.pipe(Schema.pluck('value'), Schema.Array, Schema.headOrElse())
|
281
|
-
} else {
|
282
|
-
return queryAst.tableDef.schema.pipe(Schema.Array, Schema.headOrElse())
|
283
|
-
}
|
284
|
-
}
|
285
|
-
}
|
package/src/query-info.ts
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
import type { SessionIdSymbol } from './adapter-types.js'
|
2
|
-
import type { DbSchema } from './schema/mod.js'
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Semantic information about a query with supported cases being:
|
6
|
-
* - a whole row
|
7
|
-
* - a single column value
|
8
|
-
* - a sub value in a JSON column
|
9
|
-
*
|
10
|
-
* This information is currently only used for derived mutations.
|
11
|
-
*/
|
12
|
-
export type QueryInfo = QueryInfo.None | QueryInfo.Row | QueryInfo.Col | QueryInfo.ColJsonValue
|
13
|
-
// export type QueryInfo<TTableDef extends DbSchema.TableDefBase = DbSchema.TableDefBase> =
|
14
|
-
// | QueryInfo.None
|
15
|
-
// | QueryInfo.Row<TTableDef>
|
16
|
-
// | QueryInfo.ColJsonValue<TTableDef, GetJsonColumn<TTableDef>>
|
17
|
-
// | QueryInfo.Col<TTableDef, keyof TTableDef['sqliteDef']['columns']>
|
18
|
-
|
19
|
-
export namespace QueryInfo {
|
20
|
-
export type None = {
|
21
|
-
_tag: 'None'
|
22
|
-
}
|
23
|
-
|
24
|
-
export type Row = {
|
25
|
-
_tag: 'Row'
|
26
|
-
table: DbSchema.TableDefBase
|
27
|
-
id: string | SessionIdSymbol
|
28
|
-
}
|
29
|
-
|
30
|
-
export type Col = {
|
31
|
-
_tag: 'Col'
|
32
|
-
table: DbSchema.TableDefBase
|
33
|
-
id: string | SessionIdSymbol
|
34
|
-
column: string
|
35
|
-
}
|
36
|
-
|
37
|
-
export type ColJsonValue = {
|
38
|
-
_tag: 'ColJsonValue'
|
39
|
-
table: DbSchema.TableDefBase
|
40
|
-
id: string | SessionIdSymbol
|
41
|
-
column: string
|
42
|
-
/**
|
43
|
-
* example: `$.tabs[3].items[2]` (`$` referring to the column value)
|
44
|
-
*/
|
45
|
-
jsonPath: string
|
46
|
-
}
|
47
|
-
|
48
|
-
// NOTE maybe we want to bring back type-params back like below
|
49
|
-
// export type Row<TTableDef extends DbSchema.TableDefBase> = {
|
50
|
-
// _tag: 'Row'
|
51
|
-
// table: TTableDef
|
52
|
-
// id: string | SessionIdSymbol
|
53
|
-
// }
|
54
|
-
|
55
|
-
// export type Col<TTableDef extends DbSchema.TableDefBase, TColName extends keyof TTableDef['sqliteDef']['columns']> = {
|
56
|
-
// _tag: 'Col'
|
57
|
-
// table: TTableDef
|
58
|
-
// id: string | SessionIdSymbol
|
59
|
-
// column: TColName
|
60
|
-
// }
|
61
|
-
|
62
|
-
// export type ColJsonValue<TTableDef extends DbSchema.TableDefBase, TColName extends GetJsonColumn<TTableDef>> = {
|
63
|
-
// _tag: 'ColJsonValue'
|
64
|
-
// table: TTableDef
|
65
|
-
// id: string | SessionIdSymbol
|
66
|
-
// column: TColName
|
67
|
-
// /**
|
68
|
-
// * example: `$.tabs[3].items[2]` (`$` referring to the column value)
|
69
|
-
// */
|
70
|
-
// jsonPath: string
|
71
|
-
// }
|
72
|
-
}
|
73
|
-
|
74
|
-
// type GetJsonColumn<TTableDef extends DbSchema.TableDefBase> = keyof {
|
75
|
-
// [ColName in keyof TTableDef['sqliteDef']['columns'] as TTableDef['sqliteDef']['columns'][ColName]['columnType'] extends 'text'
|
76
|
-
// ? ColName
|
77
|
-
// : never]: {}
|
78
|
-
// }
|
@@ -1,119 +0,0 @@
|
|
1
|
-
import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
|
2
|
-
import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
|
3
|
-
|
4
|
-
import { type MigrationOptionsFromMutationLog, type SynchronousDatabase, UnexpectedError } from './adapter-types.js'
|
5
|
-
import { makeApplyMutation } from './leader-thread/apply-mutation.js'
|
6
|
-
import type { LiveStoreSchema, MutationDef, MutationEvent, MutationLogMetaRow } from './schema/mod.js'
|
7
|
-
import { EventId, MUTATION_LOG_META_TABLE } from './schema/mod.js'
|
8
|
-
import type { PreparedBindValues } from './util.js'
|
9
|
-
import { sql } from './util.js'
|
10
|
-
|
11
|
-
export const rehydrateFromMutationLog = ({
|
12
|
-
logDb,
|
13
|
-
// TODO re-use this db when bringing back the boot in-memory db implementation
|
14
|
-
// db,
|
15
|
-
schema,
|
16
|
-
migrationOptions,
|
17
|
-
onProgress,
|
18
|
-
}: {
|
19
|
-
logDb: SynchronousDatabase
|
20
|
-
db: SynchronousDatabase
|
21
|
-
schema: LiveStoreSchema
|
22
|
-
migrationOptions: MigrationOptionsFromMutationLog
|
23
|
-
onProgress: (_: { done: number; total: number }) => Effect.Effect<void>
|
24
|
-
}) =>
|
25
|
-
Effect.gen(function* () {
|
26
|
-
const mutationsCount = logDb.select<{ count: number }>(
|
27
|
-
`SELECT COUNT(*) AS count FROM ${MUTATION_LOG_META_TABLE}`,
|
28
|
-
)[0]!.count
|
29
|
-
|
30
|
-
const hashMutation = memoizeByRef((mutation: MutationDef.Any) => Schema.hash(mutation.schema))
|
31
|
-
|
32
|
-
const applyMutation = yield* makeApplyMutation
|
33
|
-
|
34
|
-
const processMutation = (row: MutationLogMetaRow) =>
|
35
|
-
Effect.gen(function* () {
|
36
|
-
const mutationDef = schema.mutations.get(row.mutation) ?? shouldNeverHappen(`Unknown mutation ${row.mutation}`)
|
37
|
-
|
38
|
-
if (migrationOptions.excludeMutations?.has(row.mutation) === true) return
|
39
|
-
|
40
|
-
if (hashMutation(mutationDef) !== row.schemaHash) {
|
41
|
-
yield* Effect.logWarning(
|
42
|
-
`Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`,
|
43
|
-
)
|
44
|
-
}
|
45
|
-
|
46
|
-
const args = JSON.parse(row.argsJson)
|
47
|
-
|
48
|
-
// Checking whether the schema has changed in an incompatible way
|
49
|
-
yield* Schema.decodeUnknown(mutationDef.schema)(args).pipe(
|
50
|
-
Effect.mapError((cause) =>
|
51
|
-
UnexpectedError.make({
|
52
|
-
cause,
|
53
|
-
note: `\
|
54
|
-
There was an error during rehydrating from the mutation log while decoding
|
55
|
-
the persisted mutation event args for mutation "${row.mutation}".
|
56
|
-
This likely means the schema has changed in an incompatible way.
|
57
|
-
`,
|
58
|
-
}),
|
59
|
-
),
|
60
|
-
)
|
61
|
-
|
62
|
-
const mutationEventEncoded = {
|
63
|
-
id: { global: row.idGlobal, local: row.idLocal },
|
64
|
-
parentId: { global: row.parentIdGlobal, local: row.parentIdLocal },
|
65
|
-
mutation: row.mutation,
|
66
|
-
args,
|
67
|
-
} satisfies MutationEvent.AnyEncoded
|
68
|
-
|
69
|
-
yield* applyMutation(mutationEventEncoded, { skipMutationLog: true })
|
70
|
-
}).pipe(Effect.withSpan(`@livestore/common:rehydrateFromMutationLog:processMutation`))
|
71
|
-
|
72
|
-
const CHUNK_SIZE = 100
|
73
|
-
|
74
|
-
const stmt = logDb.prepare(sql`\
|
75
|
-
SELECT * FROM ${MUTATION_LOG_META_TABLE}
|
76
|
-
WHERE idGlobal > $idGlobal OR (idGlobal = $idGlobal AND idLocal > $idLocal)
|
77
|
-
ORDER BY idGlobal ASC, idLocal ASC
|
78
|
-
LIMIT ${CHUNK_SIZE}
|
79
|
-
`)
|
80
|
-
|
81
|
-
let processedMutations = 0
|
82
|
-
|
83
|
-
yield* Stream.unfoldChunk<Chunk.Chunk<MutationLogMetaRow> | { _tag: 'Initial ' }, MutationLogMetaRow>(
|
84
|
-
{ _tag: 'Initial ' },
|
85
|
-
(item) => {
|
86
|
-
// End stream if no more rows
|
87
|
-
if (Chunk.isChunk(item) && item.length === 0) return Option.none()
|
88
|
-
|
89
|
-
const lastId = Chunk.isChunk(item)
|
90
|
-
? Chunk.last(item).pipe(
|
91
|
-
Option.map((_) => ({ global: _.idGlobal, local: _.idLocal })),
|
92
|
-
Option.getOrElse(() => EventId.ROOT),
|
93
|
-
)
|
94
|
-
: EventId.ROOT
|
95
|
-
const nextItem = Chunk.fromIterable(
|
96
|
-
stmt.select<MutationLogMetaRow>({
|
97
|
-
$idGlobal: lastId?.global,
|
98
|
-
$idLocal: lastId?.local,
|
99
|
-
} as any as PreparedBindValues),
|
100
|
-
)
|
101
|
-
const prevItem = Chunk.isChunk(item) ? item : Chunk.empty()
|
102
|
-
return Option.some([prevItem, nextItem])
|
103
|
-
},
|
104
|
-
).pipe(
|
105
|
-
Stream.bufferChunks({ capacity: 2 }),
|
106
|
-
Stream.tap((row) =>
|
107
|
-
Effect.gen(function* () {
|
108
|
-
yield* processMutation(row)
|
109
|
-
|
110
|
-
processedMutations++
|
111
|
-
yield* onProgress({ done: processedMutations, total: mutationsCount })
|
112
|
-
}),
|
113
|
-
),
|
114
|
-
Stream.runDrain,
|
115
|
-
)
|
116
|
-
}).pipe(
|
117
|
-
Effect.withPerformanceMeasure('@livestore/common:rehydrateFromMutationLog'),
|
118
|
-
Effect.withSpan('@livestore/common:rehydrateFromMutationLog'),
|
119
|
-
)
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import { Vitest } from '@livestore/utils/node-vitest'
|
2
|
-
import { expect } from 'vitest'
|
3
|
-
|
4
|
-
import { EventId } from './mod.js'
|
5
|
-
|
6
|
-
Vitest.describe('EventId', () => {
|
7
|
-
Vitest.test('nextPair', () => {
|
8
|
-
const e_0_0 = EventId.make({ global: 0, local: 0 })
|
9
|
-
expect(EventId.nextPair(e_0_0, false).id).toStrictEqual({ global: 1, local: 0 })
|
10
|
-
expect(EventId.nextPair(e_0_0, true).id).toStrictEqual({ global: 0, local: 1 })
|
11
|
-
})
|
12
|
-
})
|
package/src/schema/EventId.ts
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
import { Brand, Schema } from '@livestore/utils/effect'
|
2
|
-
|
3
|
-
export type LocalEventId = Brand.Branded<number, 'LocalEventId'>
|
4
|
-
export const localEventId = Brand.nominal<LocalEventId>()
|
5
|
-
export const LocalEventId = Schema.fromBrand(localEventId)(Schema.Int)
|
6
|
-
|
7
|
-
export type GlobalEventId = Brand.Branded<number, 'GlobalEventId'>
|
8
|
-
export const globalEventId = Brand.nominal<GlobalEventId>()
|
9
|
-
export const GlobalEventId = Schema.fromBrand(globalEventId)(Schema.Int)
|
10
|
-
|
11
|
-
export const localDefault = 0 as any as LocalEventId
|
12
|
-
|
13
|
-
/**
|
14
|
-
* LiveStore event id value consisting of a globally unique event sequence number
|
15
|
-
* and a local sequence number.
|
16
|
-
*
|
17
|
-
* The local sequence number is only used for localOnly mutations and starts from 0 for each global sequence number.
|
18
|
-
*/
|
19
|
-
export type EventId = { global: GlobalEventId; local: LocalEventId }
|
20
|
-
|
21
|
-
export const EventId = Schema.Struct({
|
22
|
-
global: GlobalEventId,
|
23
|
-
local: LocalEventId,
|
24
|
-
}).annotations({ title: 'LiveStore.EventId' })
|
25
|
-
|
26
|
-
/**
|
27
|
-
* Compare two event ids i.e. checks if the first event id is less than the second.
|
28
|
-
*/
|
29
|
-
export const compare = (a: EventId, b: EventId) => {
|
30
|
-
if (a.global !== b.global) {
|
31
|
-
return a.global - b.global
|
32
|
-
}
|
33
|
-
return a.local - b.local
|
34
|
-
}
|
35
|
-
|
36
|
-
export const isEqual = (a: EventId, b: EventId) => a.global === b.global && a.local === b.local
|
37
|
-
|
38
|
-
export type EventIdPair = { id: EventId; parentId: EventId }
|
39
|
-
|
40
|
-
export const ROOT = { global: -1 as any as GlobalEventId, local: localDefault } satisfies EventId
|
41
|
-
|
42
|
-
export const isGreaterThan = (a: EventId, b: EventId) => {
|
43
|
-
return a.global > b.global || (a.global === b.global && a.local > b.local)
|
44
|
-
}
|
45
|
-
|
46
|
-
export const make = (id: EventId | typeof EventId.Encoded): EventId => {
|
47
|
-
return Schema.is(EventId)(id) ? id : Schema.decodeSync(EventId)(id)
|
48
|
-
}
|
49
|
-
|
50
|
-
export const nextPair = (id: EventId, isLocal: boolean): EventIdPair => {
|
51
|
-
if (isLocal) {
|
52
|
-
return { id: { global: id.global, local: (id.local + 1) as any as LocalEventId }, parentId: id }
|
53
|
-
}
|
54
|
-
|
55
|
-
return {
|
56
|
-
id: { global: (id.global + 1) as any as GlobalEventId, local: localDefault },
|
57
|
-
// NOTE we always point to `local: 0` for non-localOnly mutations
|
58
|
-
parentId: { global: id.global, local: localDefault },
|
59
|
-
}
|
60
|
-
}
|