@livestore/common 0.3.0-dev.5 → 0.3.0-dev.51
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 -13
- 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 +62 -0
- package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -0
- package/dist/leader-thread/LeaderSyncProcessor.js +595 -0
- package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -0
- 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 +1 -1
- 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 +23 -11
- package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
- package/dist/leader-thread/make-leader-thread-layer.js +72 -47
- 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 +33 -31
- 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 +87 -40
- 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/EventId.d.ts +43 -25
- package/dist/schema/EventId.d.ts.map +1 -1
- package/dist/schema/EventId.js +56 -18
- 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/EventNumber.d.ts +57 -0
- package/dist/schema/EventNumber.d.ts.map +1 -0
- package/dist/schema/EventNumber.js +82 -0
- package/dist/schema/EventNumber.js.map +1 -0
- package/dist/schema/EventNumber.test.d.ts +2 -0
- package/dist/schema/EventNumber.test.d.ts.map +1 -0
- package/dist/schema/EventNumber.test.js +11 -0
- package/dist/schema/EventNumber.test.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 +66 -0
- package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -0
- package/dist/sync/ClientSessionSyncProcessor.js +209 -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/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} +38 -28
- 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 +60 -25
- 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 +310 -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 +13 -6
- 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 -15
- package/src/leader-thread/LeaderSyncProcessor.ts +940 -0
- 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 +128 -78
- package/src/leader-thread/materialize-event.ts +173 -0
- package/src/leader-thread/mod.ts +1 -1
- package/src/leader-thread/recreate-db.ts +38 -39
- package/src/leader-thread/shutdown-channel.ts +2 -4
- package/src/leader-thread/types.ts +96 -50
- 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 +332 -0
- package/src/sync/index.ts +1 -1
- 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 +60 -24
- package/src/sync/syncstate.test.ts +347 -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 -8
- package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
- package/dist/leader-thread/apply-mutation.js +0 -95
- 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 -425
- 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 -13
- 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 -72
- package/dist/rehydrate-from-mutationlog.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/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 -58
- 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 -166
- 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 -143
- package/src/leader-thread/leader-sync-processor.ts +0 -670
- package/src/leader-thread/mutationlog.ts +0 -46
- package/src/leader-thread/pull-queue-set.ts +0 -58
- package/src/mutation.ts +0 -81
- 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 -127
- package/src/schema/EventId.ts +0 -60
- package/src/schema/MutationEvent.ts +0 -180
- package/src/schema/mutations.ts +0 -192
- package/src/schema/system-tables.ts +0 -104
- package/src/schema/table-def.ts +0 -343
- package/src/schema-management/validate-mutation-defs.ts +0 -63
- package/src/sync/client-session-sync-processor.ts +0 -207
- 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,239 +0,0 @@
|
|
1
|
-
import { Schema } from '@livestore/utils/effect'
|
2
|
-
import { describe, expect, it } from 'vitest'
|
3
|
-
|
4
|
-
import { DbSchema } from '../schema/mod.js'
|
5
|
-
import { getResultSchema } from './impl.js'
|
6
|
-
|
7
|
-
const todos = DbSchema.table(
|
8
|
-
'todos',
|
9
|
-
{
|
10
|
-
id: DbSchema.text({ primaryKey: true }),
|
11
|
-
text: DbSchema.text({ default: '', nullable: false }),
|
12
|
-
completed: DbSchema.boolean({ default: false, nullable: false }),
|
13
|
-
status: DbSchema.text({ schema: Schema.Literal('active', 'completed') }),
|
14
|
-
deletedAt: DbSchema.datetime({ nullable: true }),
|
15
|
-
// TODO consider leaning more into Effect schema
|
16
|
-
// other: Schema.Number.pipe(DbSchema.asInteger),
|
17
|
-
},
|
18
|
-
{ deriveMutations: true },
|
19
|
-
)
|
20
|
-
|
21
|
-
const comments = DbSchema.table('comments', {
|
22
|
-
id: DbSchema.text({ primaryKey: true }),
|
23
|
-
text: DbSchema.text({ default: '', nullable: false }),
|
24
|
-
todoId: DbSchema.text({}),
|
25
|
-
})
|
26
|
-
|
27
|
-
const db = { todos: todos.query, comments: comments.query }
|
28
|
-
|
29
|
-
describe('query builder', () => {
|
30
|
-
describe('result schema', () => {
|
31
|
-
it('should print the schema', () => {
|
32
|
-
expect(String(getResultSchema(db.todos))).toMatchInlineSnapshot(`"ReadonlyArray<todos>"`)
|
33
|
-
})
|
34
|
-
})
|
35
|
-
|
36
|
-
describe('basic queries', () => {
|
37
|
-
it('should handle simple SELECT queries', () => {
|
38
|
-
expect(db.todos.asSql()).toMatchInlineSnapshot(`
|
39
|
-
{
|
40
|
-
"bindValues": [],
|
41
|
-
"query": "SELECT * FROM 'todos'",
|
42
|
-
}
|
43
|
-
`)
|
44
|
-
|
45
|
-
expect(db.todos.select('id', 'text').asSql()).toMatchInlineSnapshot(`
|
46
|
-
{
|
47
|
-
"bindValues": [],
|
48
|
-
"query": "SELECT id, text FROM 'todos'",
|
49
|
-
}
|
50
|
-
`)
|
51
|
-
})
|
52
|
-
|
53
|
-
it('should handle WHERE clauses', () => {
|
54
|
-
expect(db.todos.select('id', 'text').where('completed', true).asSql()).toMatchInlineSnapshot(`
|
55
|
-
{
|
56
|
-
"bindValues": [
|
57
|
-
1,
|
58
|
-
],
|
59
|
-
"query": "SELECT id, text FROM 'todos' WHERE completed = ?",
|
60
|
-
}
|
61
|
-
`)
|
62
|
-
expect(db.todos.select('id', 'text').where('completed', '!=', true).asSql()).toMatchInlineSnapshot(`
|
63
|
-
{
|
64
|
-
"bindValues": [
|
65
|
-
1,
|
66
|
-
],
|
67
|
-
"query": "SELECT id, text FROM 'todos' WHERE completed != ?",
|
68
|
-
}
|
69
|
-
`)
|
70
|
-
expect(db.todos.select('id', 'text').where({ completed: true }).asSql()).toMatchInlineSnapshot(`
|
71
|
-
{
|
72
|
-
"bindValues": [
|
73
|
-
1,
|
74
|
-
],
|
75
|
-
"query": "SELECT id, text FROM 'todos' WHERE completed = ?",
|
76
|
-
}
|
77
|
-
`)
|
78
|
-
expect(db.todos.select('id', 'text').where({ completed: undefined }).asSql()).toMatchInlineSnapshot(`
|
79
|
-
{
|
80
|
-
"bindValues": [],
|
81
|
-
"query": "SELECT id, text FROM 'todos'",
|
82
|
-
}
|
83
|
-
`)
|
84
|
-
expect(
|
85
|
-
db.todos
|
86
|
-
.select('id', 'text')
|
87
|
-
.where({ deletedAt: { op: '<=', value: new Date('2024-01-01') } })
|
88
|
-
.asSql(),
|
89
|
-
).toMatchInlineSnapshot(`
|
90
|
-
{
|
91
|
-
"bindValues": [
|
92
|
-
"2024-01-01T00:00:00.000Z",
|
93
|
-
],
|
94
|
-
"query": "SELECT id, text FROM 'todos' WHERE deletedAt <= ?",
|
95
|
-
}
|
96
|
-
`)
|
97
|
-
expect(
|
98
|
-
db.todos
|
99
|
-
.select('id', 'text')
|
100
|
-
.where({ status: { op: 'IN', value: ['active'] } })
|
101
|
-
.asSql(),
|
102
|
-
).toMatchInlineSnapshot(`
|
103
|
-
{
|
104
|
-
"bindValues": [
|
105
|
-
"active",
|
106
|
-
],
|
107
|
-
"query": "SELECT id, text FROM 'todos' WHERE status IN (?)",
|
108
|
-
}
|
109
|
-
`)
|
110
|
-
expect(
|
111
|
-
db.todos
|
112
|
-
.select('id', 'text')
|
113
|
-
.where({ status: { op: 'NOT IN', value: ['active', 'completed'] } })
|
114
|
-
.asSql(),
|
115
|
-
).toMatchInlineSnapshot(`
|
116
|
-
{
|
117
|
-
"bindValues": [
|
118
|
-
"active",
|
119
|
-
"completed",
|
120
|
-
],
|
121
|
-
"query": "SELECT id, text FROM 'todos' WHERE status NOT IN (?, ?)",
|
122
|
-
}
|
123
|
-
`)
|
124
|
-
})
|
125
|
-
|
126
|
-
it('should handle OFFSET and LIMIT clauses', () => {
|
127
|
-
expect(db.todos.select('id', 'text').where('completed', true).offset(10).limit(10).asSql())
|
128
|
-
.toMatchInlineSnapshot(`
|
129
|
-
{
|
130
|
-
"bindValues": [
|
131
|
-
1,
|
132
|
-
10,
|
133
|
-
10,
|
134
|
-
],
|
135
|
-
"query": "SELECT id, text FROM 'todos' WHERE completed = ? OFFSET ? LIMIT ?",
|
136
|
-
}
|
137
|
-
`)
|
138
|
-
})
|
139
|
-
|
140
|
-
it('should handle COUNT queries', () => {
|
141
|
-
expect(db.todos.count().asSql()).toMatchInlineSnapshot(`
|
142
|
-
{
|
143
|
-
"bindValues": [],
|
144
|
-
"query": "SELECT COUNT(*) as count FROM 'todos'",
|
145
|
-
}
|
146
|
-
`)
|
147
|
-
expect(db.todos.count().where('completed', true).asSql()).toMatchInlineSnapshot(`
|
148
|
-
{
|
149
|
-
"bindValues": [
|
150
|
-
1,
|
151
|
-
],
|
152
|
-
"query": "SELECT COUNT(*) as count FROM 'todos' WHERE completed = ?",
|
153
|
-
}
|
154
|
-
`)
|
155
|
-
})
|
156
|
-
|
157
|
-
it('should handle NULL comparisons', () => {
|
158
|
-
expect(db.todos.select('id', 'text').where('deletedAt', '=', null).asSql()).toMatchInlineSnapshot(`
|
159
|
-
{
|
160
|
-
"bindValues": [],
|
161
|
-
"query": "SELECT id, text FROM 'todos' WHERE deletedAt IS NULL",
|
162
|
-
}
|
163
|
-
`)
|
164
|
-
expect(db.todos.select('id', 'text').where('deletedAt', '!=', null).asSql()).toMatchInlineSnapshot(`
|
165
|
-
{
|
166
|
-
"bindValues": [],
|
167
|
-
"query": "SELECT id, text FROM 'todos' WHERE deletedAt IS NOT NULL",
|
168
|
-
}
|
169
|
-
`)
|
170
|
-
})
|
171
|
-
|
172
|
-
it('should handle orderBy', () => {
|
173
|
-
expect(db.todos.orderBy('completed', 'desc').asSql()).toMatchInlineSnapshot(`
|
174
|
-
{
|
175
|
-
"bindValues": [],
|
176
|
-
"query": "SELECT * FROM 'todos' ORDER BY completed desc",
|
177
|
-
}
|
178
|
-
`)
|
179
|
-
|
180
|
-
expect(db.todos.orderBy([{ col: 'completed', direction: 'desc' }]).asSql()).toMatchInlineSnapshot(`
|
181
|
-
{
|
182
|
-
"bindValues": [],
|
183
|
-
"query": "SELECT * FROM 'todos' ORDER BY completed desc",
|
184
|
-
}
|
185
|
-
`)
|
186
|
-
|
187
|
-
expect(db.todos.orderBy([]).asSql()).toMatchInlineSnapshot(`
|
188
|
-
{
|
189
|
-
"bindValues": [],
|
190
|
-
"query": "SELECT * FROM 'todos'",
|
191
|
-
}
|
192
|
-
`)
|
193
|
-
})
|
194
|
-
})
|
195
|
-
|
196
|
-
describe('row queries', () => {
|
197
|
-
it('should handle row queries', () => {
|
198
|
-
expect(db.todos.row('123', { insertValues: { status: 'completed' } }).asSql()).toMatchInlineSnapshot(`
|
199
|
-
{
|
200
|
-
"bindValues": [
|
201
|
-
"123",
|
202
|
-
],
|
203
|
-
"query": "SELECT * FROM 'todos' WHERE id = ?",
|
204
|
-
}
|
205
|
-
`)
|
206
|
-
})
|
207
|
-
})
|
208
|
-
})
|
209
|
-
|
210
|
-
// TODO nested queries
|
211
|
-
// const rawSql = <A, I>(sql: string, params: { [key: string]: any }, schema: Schema.Schema<A, I>) =>
|
212
|
-
// ({
|
213
|
-
// sql,
|
214
|
-
// params,
|
215
|
-
// schema,
|
216
|
-
// }) as any as QueryBuilder<A, any>
|
217
|
-
|
218
|
-
// Translates to
|
219
|
-
// SELECT todos.*, (SELECT COUNT(*) FROM comments WHERE comments.todoId = todos.id) AS commentsCount
|
220
|
-
// FROM todos WHERE todos.completed = true
|
221
|
-
// const q4CommentsCountSchema = Schema.Struct({ count: Schema.Number }).pipe(
|
222
|
-
// Schema.pluck('count'),
|
223
|
-
// Schema.Array,
|
224
|
-
// Schema.headOrElse(),
|
225
|
-
// )
|
226
|
-
// const _q4$ = db.todos
|
227
|
-
// .select({
|
228
|
-
// commentsCount: (ref) =>
|
229
|
-
// rawSql(
|
230
|
-
// sql`SELECT COUNT(*) as count FROM comments WHERE comments.todoId = $todoId`,
|
231
|
-
// { todoId: ref },
|
232
|
-
// q4CommentsCountSchema,
|
233
|
-
// ),
|
234
|
-
// })
|
235
|
-
// .where({ completed: true })
|
236
|
-
|
237
|
-
// const _q5$ = db.todos
|
238
|
-
// .select({ commentsCount: (todoId: TODO) => comments.query.where({ todoId }).count() })
|
239
|
-
// .where({ completed: true })
|
@@ -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
|
-
// }
|