@effect-app/infra 4.0.0-beta.26 → 4.0.0-beta.261
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/CHANGELOG.md +1973 -0
- package/_check.sh +1 -1
- package/dist/CUPS.d.ts +30 -11
- package/dist/CUPS.d.ts.map +1 -1
- package/dist/CUPS.js +35 -14
- package/dist/ClusterCosmos.d.ts +64 -0
- package/dist/ClusterCosmos.d.ts.map +1 -0
- package/dist/ClusterCosmos.js +501 -0
- package/dist/ClusterServiceBus.d.ts +67 -0
- package/dist/ClusterServiceBus.d.ts.map +1 -0
- package/dist/ClusterServiceBus.js +82 -0
- package/dist/ContextProvider.d.ts +34 -0
- package/dist/ContextProvider.d.ts.map +1 -0
- package/dist/ContextProvider.js +40 -0
- package/dist/Emailer/Sendgrid.d.ts +111 -147
- package/dist/Emailer/Sendgrid.d.ts.map +1 -1
- package/dist/Emailer/Sendgrid.js +24 -19
- package/dist/Emailer/fake.d.ts +2 -2
- package/dist/Emailer/fake.d.ts.map +1 -1
- package/dist/Emailer/fake.js +4 -4
- package/dist/MainFiberSet.d.ts +12 -9
- package/dist/MainFiberSet.d.ts.map +1 -1
- package/dist/MainFiberSet.js +10 -6
- package/dist/QueueMaker/SQLQueue.d.ts +8 -9
- package/dist/QueueMaker/SQLQueue.d.ts.map +1 -1
- package/dist/QueueMaker/SQLQueue.js +138 -120
- package/dist/QueueMaker/errors.d.ts +5 -3
- package/dist/QueueMaker/errors.d.ts.map +1 -1
- package/dist/QueueMaker/errors.js +4 -2
- package/dist/QueueMaker/memQueue.d.ts +10 -6
- package/dist/QueueMaker/memQueue.d.ts.map +1 -1
- package/dist/QueueMaker/memQueue.js +84 -68
- package/dist/QueueMaker/sbqueue.d.ts +9 -5
- package/dist/QueueMaker/sbqueue.d.ts.map +1 -1
- package/dist/QueueMaker/sbqueue.js +60 -58
- package/dist/RequestFiberSet.d.ts +10 -7
- package/dist/RequestFiberSet.d.ts.map +1 -1
- package/dist/RequestFiberSet.js +13 -8
- package/dist/SQL/Model.d.ts +468 -0
- package/dist/SQL/Model.d.ts.map +1 -0
- package/dist/SQL/Model.js +469 -0
- package/dist/SQL.d.ts +2 -0
- package/dist/SQL.d.ts.map +1 -0
- package/dist/{adapters/SQL.js → SQL.js} +1 -1
- package/dist/ServiceBus.d.ts +61 -0
- package/dist/ServiceBus.d.ts.map +1 -0
- package/dist/ServiceBus.js +108 -0
- package/dist/Store/Cosmos/query.d.ts +15 -4
- package/dist/Store/Cosmos/query.d.ts.map +1 -1
- package/dist/Store/Cosmos/query.js +179 -41
- package/dist/Store/Cosmos.d.ts +3 -3
- package/dist/Store/Cosmos.d.ts.map +1 -1
- package/dist/Store/Cosmos.js +344 -246
- package/dist/Store/Disk.d.ts +5 -5
- package/dist/Store/Disk.d.ts.map +1 -1
- package/dist/Store/Disk.js +78 -38
- package/dist/Store/Memory.d.ts +7 -10
- package/dist/Store/Memory.d.ts.map +1 -1
- package/dist/Store/Memory.js +326 -66
- package/dist/Store/SQL/Pg.d.ts +4 -0
- package/dist/Store/SQL/Pg.d.ts.map +1 -0
- package/dist/Store/SQL/Pg.js +232 -0
- package/dist/Store/SQL/query.d.ts +49 -0
- package/dist/Store/SQL/query.d.ts.map +1 -0
- package/dist/Store/SQL/query.js +527 -0
- package/dist/Store/SQL.d.ts +21 -0
- package/dist/Store/SQL.d.ts.map +1 -0
- package/dist/Store/SQL.js +449 -0
- package/dist/Store/codeFilter.d.ts +5 -5
- package/dist/Store/codeFilter.d.ts.map +1 -1
- package/dist/Store/codeFilter.js +6 -3
- package/dist/Store/index.d.ts +7 -5
- package/dist/Store/index.d.ts.map +1 -1
- package/dist/Store/index.js +18 -5
- package/dist/Store/utils.d.ts +4 -3
- package/dist/Store/utils.d.ts.map +1 -1
- package/dist/Store/utils.js +5 -5
- package/dist/WorkflowEngineCosmos.d.ts +29 -0
- package/dist/WorkflowEngineCosmos.d.ts.map +1 -0
- package/dist/WorkflowEngineCosmos.js +521 -0
- package/dist/WorkflowEngineSqlite.d.ts +24 -0
- package/dist/WorkflowEngineSqlite.d.ts.map +1 -0
- package/dist/WorkflowEngineSqlite.js +550 -0
- package/dist/arbs.d.ts +2 -2
- package/dist/arbs.d.ts.map +1 -1
- package/dist/arbs.js +5 -3
- package/dist/codec.d.ts +5 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +5 -0
- package/dist/cosmos-client.d.ts +16 -0
- package/dist/cosmos-client.d.ts.map +1 -0
- package/dist/cosmos-client.js +11 -0
- package/dist/errorReporter.d.ts +7 -5
- package/dist/errorReporter.d.ts.map +1 -1
- package/dist/errorReporter.js +23 -27
- package/dist/errors.d.ts +1 -1
- package/dist/fileUtil.d.ts +2 -2
- package/dist/fileUtil.d.ts.map +1 -1
- package/dist/fileUtil.js +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/internal/RequestContextMiddleware.d.ts +5 -0
- package/dist/internal/RequestContextMiddleware.d.ts.map +1 -0
- package/dist/internal/RequestContextMiddleware.js +45 -0
- package/dist/internal/auth.d.ts +53 -0
- package/dist/internal/auth.d.ts.map +1 -0
- package/dist/internal/auth.js +180 -0
- package/dist/internal/events.d.ts +11 -0
- package/dist/internal/events.d.ts.map +1 -0
- package/dist/internal/events.js +49 -0
- package/dist/internal/health.d.ts +3 -0
- package/dist/internal/health.d.ts.map +1 -0
- package/dist/internal/health.js +5 -0
- package/dist/layerUtils.d.ts +32 -0
- package/dist/layerUtils.d.ts.map +1 -0
- package/dist/layerUtils.js +17 -0
- package/dist/logger/jsonLogger.d.ts +2 -2
- package/dist/logger/jsonLogger.d.ts.map +1 -1
- package/dist/logger/jsonLogger.js +5 -3
- package/dist/logger/logFmtLogger.d.ts +2 -2
- package/dist/logger/logFmtLogger.d.ts.map +1 -1
- package/dist/logger/logFmtLogger.js +3 -3
- package/dist/logger/shared.d.ts +3 -3
- package/dist/logger/shared.d.ts.map +1 -1
- package/dist/logger/shared.js +5 -5
- package/dist/logger.d.ts +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/memQueue.d.ts +15 -0
- package/dist/memQueue.d.ts.map +1 -0
- package/dist/memQueue.js +21 -0
- package/dist/middlewares.d.ts +10 -0
- package/dist/middlewares.d.ts.map +1 -0
- package/dist/{api/middlewares.js → middlewares.js} +1 -1
- package/dist/mongo-client.d.ts +11 -0
- package/dist/mongo-client.d.ts.map +1 -0
- package/dist/mongo-client.js +15 -0
- package/dist/otel.d.ts +75 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +65 -0
- package/dist/rateLimit.d.ts +12 -4
- package/dist/rateLimit.d.ts.map +1 -1
- package/dist/rateLimit.js +7 -12
- package/dist/redis-client.d.ts +42 -0
- package/dist/redis-client.d.ts.map +1 -0
- package/dist/redis-client.js +98 -0
- package/dist/reportError.d.ts +4 -0
- package/dist/reportError.d.ts.map +1 -0
- package/dist/reportError.js +28 -0
- package/dist/routing/middleware/RouterMiddleware.d.ts +16 -0
- package/dist/routing/middleware/RouterMiddleware.d.ts.map +1 -0
- package/dist/{api/routing → routing}/middleware/RouterMiddleware.js +1 -1
- package/dist/routing/middleware/middleware.d.ts +48 -0
- package/dist/routing/middleware/middleware.d.ts.map +1 -0
- package/dist/routing/middleware/middleware.js +128 -0
- package/dist/routing/middleware.d.ts +3 -0
- package/dist/routing/middleware.d.ts.map +1 -0
- package/dist/{api/routing → routing}/middleware.js +1 -2
- package/dist/routing/schema/jwt.d.ts +4 -0
- package/dist/routing/schema/jwt.d.ts.map +1 -0
- package/dist/routing/schema/jwt.js +13 -0
- package/dist/routing/tsort.d.ts +8 -0
- package/dist/routing/tsort.d.ts.map +1 -0
- package/dist/routing/tsort.js +51 -0
- package/dist/routing/utils.d.ts +19 -0
- package/dist/routing/utils.d.ts.map +1 -0
- package/dist/routing/utils.js +45 -0
- package/dist/routing.d.ts +184 -0
- package/dist/routing.d.ts.map +1 -0
- package/dist/routing.js +236 -0
- package/dist/test.d.ts +3 -3
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +2 -2
- package/dist/util.d.ts +3 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +14 -0
- package/dist/vitest.d.ts +1 -1
- package/docs/cluster-storage.md +26 -0
- package/docs/workflow-engine.md +262 -0
- package/examples/query.ts +47 -39
- package/package.json +31 -345
- package/run.sh +1 -0
- package/src/CUPS.ts +52 -13
- package/src/ClusterCosmos.ts +984 -0
- package/src/ClusterServiceBus.ts +242 -0
- package/src/{api/ContextProvider.ts → ContextProvider.ts} +19 -16
- package/src/Emailer/Sendgrid.ts +82 -59
- package/src/Emailer/fake.ts +3 -3
- package/src/MainFiberSet.ts +12 -10
- package/src/QueueMaker/SQLQueue.ts +153 -156
- package/src/QueueMaker/errors.ts +3 -1
- package/src/QueueMaker/memQueue.ts +113 -107
- package/src/QueueMaker/sbqueue.ts +78 -90
- package/src/RequestFiberSet.ts +13 -8
- package/src/{adapters/SQL → SQL}/Model.ts +42 -41
- package/src/ServiceBus.ts +219 -0
- package/src/Store/Cosmos/query.ts +216 -52
- package/src/Store/Cosmos.ts +493 -353
- package/src/Store/Disk.ts +109 -69
- package/src/Store/Memory.ts +365 -96
- package/src/Store/SQL/Pg.ts +363 -0
- package/src/Store/SQL/query.ts +603 -0
- package/src/Store/SQL.ts +735 -0
- package/src/Store/codeFilter.ts +8 -5
- package/src/Store/index.ts +21 -6
- package/src/Store/utils.ts +26 -24
- package/src/WorkflowEngineCosmos.ts +719 -0
- package/src/WorkflowEngineSqlite.ts +813 -0
- package/src/arbs.ts +5 -3
- package/src/{adapters/cosmos-client.ts → cosmos-client.ts} +5 -3
- package/src/errorReporter.ts +66 -76
- package/src/fileUtil.ts +1 -1
- package/src/index.ts +2 -1
- package/src/{api/internal → internal}/RequestContextMiddleware.ts +23 -6
- package/src/internal/auth.ts +272 -0
- package/src/{api/internal → internal}/events.ts +22 -13
- package/src/{api/layerUtils.ts → layerUtils.ts} +14 -10
- package/src/logger/jsonLogger.ts +4 -2
- package/src/logger/logFmtLogger.ts +2 -2
- package/src/logger/shared.ts +5 -4
- package/src/{adapters/memQueue.ts → memQueue.ts} +5 -4
- package/src/{adapters/mongo-client.ts → mongo-client.ts} +4 -2
- package/src/otel.ts +152 -0
- package/src/rateLimit.ts +34 -23
- package/src/{adapters/redis-client.ts → redis-client.ts} +7 -3
- package/src/{api/reportError.ts → reportError.ts} +3 -2
- package/src/{api/routing → routing}/middleware/RouterMiddleware.ts +5 -4
- package/src/{api/routing → routing}/middleware/middleware.ts +62 -17
- package/src/routing/middleware.ts +4 -0
- package/src/{api/routing → routing}/schema/jwt.ts +2 -1
- package/src/{api/routing → routing}/utils.ts +2 -1
- package/src/routing.ts +768 -0
- package/src/test.ts +2 -2
- package/test/auth.test.ts +101 -0
- package/test/cluster-cosmos.test.ts +590 -0
- package/test/cluster-servicebus.test.ts +180 -0
- package/test/cluster-sqlite.test.ts +207 -0
- package/test/contextProvider.test.ts +15 -12
- package/test/controller.test.ts +28 -32
- package/test/cosmos-query.test.ts +159 -0
- package/test/dist/_check-agg-infer.test-d.d.ts +2 -0
- package/test/dist/_check-agg-infer.test-d.d.ts.map +1 -0
- package/test/dist/_check-agg-infer.test-d.js +19 -0
- package/test/dist/_check-proj-infer.test-d.d.ts +2 -0
- package/test/dist/_check-proj-infer.test-d.d.ts.map +1 -0
- package/test/dist/_check-proj-infer.test-d.js +16 -0
- package/test/dist/_check-tighten.test-d.d.ts +2 -0
- package/test/dist/_check-tighten.test-d.d.ts.map +1 -0
- package/test/dist/_check-tighten.test-d.js +21 -0
- package/test/dist/auth.test.d.ts.map +1 -0
- package/test/dist/cluster-cosmos.test.d.ts.map +1 -0
- package/test/dist/cluster-servicebus.test.d.ts.map +1 -0
- package/test/dist/cluster-sqlite.test.d.ts.map +1 -0
- package/test/dist/contextProvider.test.d.ts.map +1 -1
- package/test/dist/controller.test.d.ts.map +1 -1
- package/test/dist/cosmos-query.test.d.ts.map +1 -0
- package/test/dist/date-query.test.d.ts.map +1 -0
- package/test/dist/fixtures.d.ts +30 -12
- package/test/dist/fixtures.d.ts.map +1 -1
- package/test/dist/fixtures.js +17 -10
- package/test/dist/query.test.d.ts.map +1 -1
- package/test/dist/rawQuery.test.d.ts.map +1 -1
- package/test/dist/repository-ext.test.d.ts.map +1 -0
- package/test/dist/requires.test.d.ts.map +1 -1
- package/test/dist/router-generator.test.d.ts.map +1 -0
- package/test/dist/routing-interruptibility.test.d.ts.map +1 -0
- package/test/dist/rpc-context-map-streaming.test.d.ts.map +1 -0
- package/test/dist/rpc-e2e-invalidation.test.d.ts.map +1 -0
- package/test/dist/rpc-multi-middleware.test.d.ts.map +1 -1
- package/test/dist/rpc-stream-fullstack.test.d.ts.map +1 -0
- package/test/dist/sql-store.test.d.ts.map +1 -0
- package/test/dist/workflow-engine-cosmos.test.d.ts.map +1 -0
- package/test/dist/workflow-engine-sqlite.test.d.ts.map +1 -0
- package/test/fixtures.ts +16 -9
- package/test/layerUtils.test.ts +2 -2
- package/test/query.test.ts +905 -40
- package/test/rawQuery.test.ts +340 -22
- package/test/repository-ext.test.ts +62 -0
- package/test/requires.test.ts +10 -5
- package/test/router-generator.test.ts +187 -0
- package/test/routing-interruptibility.test.ts +66 -0
- package/test/rpc-context-map-streaming.test.ts +262 -0
- package/test/rpc-e2e-invalidation.test.ts +256 -0
- package/test/rpc-multi-middleware.test.ts +85 -10
- package/test/rpc-stream-fullstack.test.ts +304 -0
- package/test/sql-store.test.ts +1711 -0
- package/test/validateSample.test.ts +26 -14
- package/test/workflow-engine-cosmos.test.ts +354 -0
- package/test/workflow-engine-sqlite.test.ts +299 -0
- package/tsconfig.examples.json +1 -1
- package/tsconfig.json +2 -1
- package/tsconfig.json.bak +2 -2
- package/tsconfig.src.json +35 -35
- package/tsconfig.test.json +2 -2
- package/dist/Emailer/service.d.ts +0 -55
- package/dist/Emailer/service.d.ts.map +0 -1
- package/dist/Emailer/service.js +0 -6
- package/dist/Emailer.d.ts +0 -2
- package/dist/Emailer.d.ts.map +0 -1
- package/dist/Emailer.js +0 -2
- package/dist/Model/Repository/ext.d.ts +0 -41
- package/dist/Model/Repository/ext.d.ts.map +0 -1
- package/dist/Model/Repository/ext.js +0 -65
- package/dist/Model/Repository/internal/internal.d.ts +0 -59
- package/dist/Model/Repository/internal/internal.d.ts.map +0 -1
- package/dist/Model/Repository/internal/internal.js +0 -316
- package/dist/Model/Repository/legacy.d.ts +0 -19
- package/dist/Model/Repository/legacy.d.ts.map +0 -1
- package/dist/Model/Repository/legacy.js +0 -2
- package/dist/Model/Repository/makeRepo.d.ts +0 -49
- package/dist/Model/Repository/makeRepo.d.ts.map +0 -1
- package/dist/Model/Repository/makeRepo.js +0 -24
- package/dist/Model/Repository/service.d.ts +0 -89
- package/dist/Model/Repository/service.d.ts.map +0 -1
- package/dist/Model/Repository/service.js +0 -2
- package/dist/Model/Repository/validation.d.ts +0 -42
- package/dist/Model/Repository/validation.d.ts.map +0 -1
- package/dist/Model/Repository/validation.js +0 -32
- package/dist/Model/Repository.d.ts +0 -6
- package/dist/Model/Repository.d.ts.map +0 -1
- package/dist/Model/Repository.js +0 -6
- package/dist/Model/dsl.d.ts +0 -32
- package/dist/Model/dsl.d.ts.map +0 -1
- package/dist/Model/dsl.js +0 -44
- package/dist/Model/filter/filterApi.d.ts +0 -30
- package/dist/Model/filter/filterApi.d.ts.map +0 -1
- package/dist/Model/filter/filterApi.js +0 -2
- package/dist/Model/filter/types/errors.d.ts +0 -29
- package/dist/Model/filter/types/errors.d.ts.map +0 -1
- package/dist/Model/filter/types/errors.js +0 -2
- package/dist/Model/filter/types/fields.d.ts +0 -15
- package/dist/Model/filter/types/fields.d.ts.map +0 -1
- package/dist/Model/filter/types/fields.js +0 -2
- package/dist/Model/filter/types/path/common.d.ts +0 -316
- package/dist/Model/filter/types/path/common.d.ts.map +0 -1
- package/dist/Model/filter/types/path/common.js +0 -2
- package/dist/Model/filter/types/path/eager.d.ts +0 -95
- package/dist/Model/filter/types/path/eager.d.ts.map +0 -1
- package/dist/Model/filter/types/path/eager.js +0 -31
- package/dist/Model/filter/types/path/index.d.ts +0 -4
- package/dist/Model/filter/types/path/index.d.ts.map +0 -1
- package/dist/Model/filter/types/path/index.js +0 -3
- package/dist/Model/filter/types/utils.d.ts +0 -79
- package/dist/Model/filter/types/utils.d.ts.map +0 -1
- package/dist/Model/filter/types/utils.js +0 -2
- package/dist/Model/filter/types/validator.d.ts +0 -30
- package/dist/Model/filter/types/validator.d.ts.map +0 -1
- package/dist/Model/filter/types/validator.js +0 -2
- package/dist/Model/filter/types.d.ts +0 -5
- package/dist/Model/filter/types.d.ts.map +0 -1
- package/dist/Model/filter/types.js +0 -7
- package/dist/Model/query/dsl.d.ts +0 -248
- package/dist/Model/query/dsl.d.ts.map +0 -1
- package/dist/Model/query/dsl.js +0 -104
- package/dist/Model/query/new-kid-interpreter.d.ts +0 -28
- package/dist/Model/query/new-kid-interpreter.d.ts.map +0 -1
- package/dist/Model/query/new-kid-interpreter.js +0 -165
- package/dist/Model/query.d.ts +0 -15
- package/dist/Model/query.d.ts.map +0 -1
- package/dist/Model/query.js +0 -3
- package/dist/Model.d.ts +0 -4
- package/dist/Model.d.ts.map +0 -1
- package/dist/Model.js +0 -4
- package/dist/Operations.d.ts +0 -55
- package/dist/Operations.d.ts.map +0 -1
- package/dist/Operations.js +0 -102
- package/dist/OperationsRepo.d.ts +0 -41
- package/dist/OperationsRepo.d.ts.map +0 -1
- package/dist/OperationsRepo.js +0 -14
- package/dist/QueueMaker/service.d.ts +0 -11
- package/dist/QueueMaker/service.d.ts.map +0 -1
- package/dist/QueueMaker/service.js +0 -4
- package/dist/RequestContext.d.ts +0 -63
- package/dist/RequestContext.d.ts.map +0 -1
- package/dist/RequestContext.js +0 -49
- package/dist/Store/ContextMapContainer.d.ts +0 -14
- package/dist/Store/ContextMapContainer.d.ts.map +0 -1
- package/dist/Store/ContextMapContainer.js +0 -16
- package/dist/Store/service.d.ts +0 -108
- package/dist/Store/service.d.ts.map +0 -1
- package/dist/Store/service.js +0 -71
- package/dist/Store.d.ts +0 -2
- package/dist/Store.d.ts.map +0 -1
- package/dist/Store.js +0 -2
- package/dist/adapters/SQL/Model.d.ts +0 -479
- package/dist/adapters/SQL/Model.d.ts.map +0 -1
- package/dist/adapters/SQL/Model.js +0 -478
- package/dist/adapters/SQL.d.ts +0 -2
- package/dist/adapters/SQL.d.ts.map +0 -1
- package/dist/adapters/ServiceBus.d.ts +0 -58
- package/dist/adapters/ServiceBus.d.ts.map +0 -1
- package/dist/adapters/ServiceBus.js +0 -99
- package/dist/adapters/cosmos-client.d.ts +0 -14
- package/dist/adapters/cosmos-client.d.ts.map +0 -1
- package/dist/adapters/cosmos-client.js +0 -9
- package/dist/adapters/index.d.ts +0 -2
- package/dist/adapters/index.d.ts.map +0 -1
- package/dist/adapters/index.js +0 -2
- package/dist/adapters/logger.d.ts +0 -9
- package/dist/adapters/logger.d.ts.map +0 -1
- package/dist/adapters/logger.js +0 -3
- package/dist/adapters/memQueue.d.ts +0 -13
- package/dist/adapters/memQueue.d.ts.map +0 -1
- package/dist/adapters/memQueue.js +0 -20
- package/dist/adapters/mongo-client.d.ts +0 -10
- package/dist/adapters/mongo-client.d.ts.map +0 -1
- package/dist/adapters/mongo-client.js +0 -13
- package/dist/adapters/redis-client.d.ts +0 -39
- package/dist/adapters/redis-client.d.ts.map +0 -1
- package/dist/adapters/redis-client.js +0 -94
- package/dist/api/ContextProvider.d.ts +0 -31
- package/dist/api/ContextProvider.d.ts.map +0 -1
- package/dist/api/ContextProvider.js +0 -38
- package/dist/api/codec.d.ts +0 -5
- package/dist/api/codec.d.ts.map +0 -1
- package/dist/api/codec.js +0 -5
- package/dist/api/internal/RequestContextMiddleware.d.ts +0 -5
- package/dist/api/internal/RequestContextMiddleware.d.ts.map +0 -1
- package/dist/api/internal/RequestContextMiddleware.js +0 -35
- package/dist/api/internal/auth.d.ts +0 -15
- package/dist/api/internal/auth.d.ts.map +0 -1
- package/dist/api/internal/auth.js +0 -47
- package/dist/api/internal/events.d.ts +0 -9
- package/dist/api/internal/events.d.ts.map +0 -1
- package/dist/api/internal/events.js +0 -42
- package/dist/api/internal/health.d.ts +0 -3
- package/dist/api/internal/health.d.ts.map +0 -1
- package/dist/api/internal/health.js +0 -5
- package/dist/api/layerUtils.d.ts +0 -24
- package/dist/api/layerUtils.d.ts.map +0 -1
- package/dist/api/layerUtils.js +0 -16
- package/dist/api/middlewares.d.ts +0 -10
- package/dist/api/middlewares.d.ts.map +0 -1
- package/dist/api/reportError.d.ts +0 -4
- package/dist/api/reportError.d.ts.map +0 -1
- package/dist/api/reportError.js +0 -27
- package/dist/api/routing/middleware/RouterMiddleware.d.ts +0 -15
- package/dist/api/routing/middleware/RouterMiddleware.d.ts.map +0 -1
- package/dist/api/routing/middleware/middleware.d.ts +0 -9
- package/dist/api/routing/middleware/middleware.d.ts.map +0 -1
- package/dist/api/routing/middleware/middleware.js +0 -92
- package/dist/api/routing/middleware.d.ts +0 -4
- package/dist/api/routing/middleware.d.ts.map +0 -1
- package/dist/api/routing/schema/jwt.d.ts +0 -4
- package/dist/api/routing/schema/jwt.d.ts.map +0 -1
- package/dist/api/routing/schema/jwt.js +0 -12
- package/dist/api/routing/tsort.d.ts +0 -8
- package/dist/api/routing/tsort.d.ts.map +0 -1
- package/dist/api/routing/tsort.js +0 -51
- package/dist/api/routing/utils.d.ts +0 -19
- package/dist/api/routing/utils.d.ts.map +0 -1
- package/dist/api/routing/utils.js +0 -44
- package/dist/api/routing.d.ts +0 -138
- package/dist/api/routing.d.ts.map +0 -1
- package/dist/api/routing.js +0 -166
- package/dist/api/setupRequest.d.ts +0 -12
- package/dist/api/setupRequest.d.ts.map +0 -1
- package/dist/api/setupRequest.js +0 -44
- package/dist/api/util.d.ts +0 -3
- package/dist/api/util.d.ts.map +0 -1
- package/dist/api/util.js +0 -14
- package/eslint.config.mjs +0 -24
- package/src/Emailer/service.ts +0 -52
- package/src/Emailer.ts +0 -1
- package/src/Model/Repository/ext.ts +0 -283
- package/src/Model/Repository/internal/internal.ts +0 -577
- package/src/Model/Repository/legacy.ts +0 -27
- package/src/Model/Repository/makeRepo.ts +0 -139
- package/src/Model/Repository/service.ts +0 -627
- package/src/Model/Repository/validation.ts +0 -31
- package/src/Model/Repository.ts +0 -5
- package/src/Model/dsl.ts +0 -128
- package/src/Model/filter/filterApi.ts +0 -60
- package/src/Model/filter/types/errors.ts +0 -47
- package/src/Model/filter/types/fields.ts +0 -50
- package/src/Model/filter/types/path/common.ts +0 -404
- package/src/Model/filter/types/path/eager.ts +0 -298
- package/src/Model/filter/types/path/index.ts +0 -4
- package/src/Model/filter/types/utils.ts +0 -128
- package/src/Model/filter/types/validator.ts +0 -46
- package/src/Model/filter/types.ts +0 -6
- package/src/Model/query/dsl.ts +0 -2110
- package/src/Model/query/new-kid-interpreter.ts +0 -210
- package/src/Model/query.ts +0 -13
- package/src/Model.ts +0 -3
- package/src/Operations.ts +0 -235
- package/src/OperationsRepo.ts +0 -16
- package/src/QueueMaker/service.ts +0 -17
- package/src/RequestContext.ts +0 -63
- package/src/Store/ContextMapContainer.ts +0 -20
- package/src/Store/service.ts +0 -184
- package/src/Store.ts +0 -1
- package/src/adapters/ServiceBus.ts +0 -209
- package/src/adapters/index.ts +0 -0
- package/src/adapters/logger.ts +0 -3
- package/src/api/internal/auth.ts +0 -68
- package/src/api/routing/middleware.ts +0 -6
- package/src/api/routing.ts +0 -598
- package/src/api/setupRequest.ts +0 -84
- /package/src/{adapters/SQL.ts → SQL.ts} +0 -0
- /package/src/{api/codec.ts → codec.ts} +0 -0
- /package/src/{api/internal → internal}/health.ts +0 -0
- /package/src/{api/middlewares.ts → middlewares.ts} +0 -0
- /package/src/{api/routing → routing}/tsort.ts +0 -0
- /package/src/{api/util.ts → util.ts} +0 -0
|
@@ -0,0 +1,1711 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import type Sqlite from "better-sqlite3"
|
|
3
|
+
import BetterSqlite from "better-sqlite3"
|
|
4
|
+
import { describe, expect, it } from "vitest"
|
|
5
|
+
import { parseRow } from "../src/Store/SQL.js"
|
|
6
|
+
import { buildWhereSQLQuery, pgDialect, sqliteDialect } from "../src/Store/SQL/query.js"
|
|
7
|
+
import { makeETag } from "../src/Store/utils.js"
|
|
8
|
+
|
|
9
|
+
const query = (db: Sqlite.Database, sql: string, params: unknown[] = []) =>
|
|
10
|
+
db.prepare(sql).all(...params as any[]) as any[]
|
|
11
|
+
|
|
12
|
+
// --- Query builder unit tests ---
|
|
13
|
+
|
|
14
|
+
describe("SQL query builder (SQLite dialect)", () => {
|
|
15
|
+
it("where eq string", () => {
|
|
16
|
+
const result = buildWhereSQLQuery(
|
|
17
|
+
sqliteDialect,
|
|
18
|
+
"id",
|
|
19
|
+
[{ t: "where", path: "name", op: "eq", value: "John" }],
|
|
20
|
+
"users",
|
|
21
|
+
{}
|
|
22
|
+
)
|
|
23
|
+
expect(result.sql).toContain("json_extract(data, '$.name') = ?")
|
|
24
|
+
expect(result.params).toContain("John")
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it("where eq number", () => {
|
|
28
|
+
const result = buildWhereSQLQuery(
|
|
29
|
+
sqliteDialect,
|
|
30
|
+
"id",
|
|
31
|
+
[{ t: "where", path: "age", op: "eq", value: 25 as any }],
|
|
32
|
+
"users",
|
|
33
|
+
{}
|
|
34
|
+
)
|
|
35
|
+
expect(result.sql).toContain("json_extract(data, '$.age') = ?")
|
|
36
|
+
expect(result.params).toContain(25)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it("where gt", () => {
|
|
40
|
+
const result = buildWhereSQLQuery(
|
|
41
|
+
sqliteDialect,
|
|
42
|
+
"id",
|
|
43
|
+
[{ t: "where", path: "age", op: "gt", value: 18 as any }],
|
|
44
|
+
"users",
|
|
45
|
+
{}
|
|
46
|
+
)
|
|
47
|
+
expect(result.sql).toContain("json_extract(data, '$.age') > ?")
|
|
48
|
+
expect(result.params).toContain(18)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it("where or", () => {
|
|
52
|
+
const result = buildWhereSQLQuery(
|
|
53
|
+
sqliteDialect,
|
|
54
|
+
"id",
|
|
55
|
+
[
|
|
56
|
+
{ t: "where", path: "name", op: "eq", value: "Alice" },
|
|
57
|
+
{ t: "or", path: "name", op: "eq", value: "Bob" }
|
|
58
|
+
],
|
|
59
|
+
"users",
|
|
60
|
+
{}
|
|
61
|
+
)
|
|
62
|
+
expect(result.sql).toContain("= ?")
|
|
63
|
+
expect(result.sql).toContain("OR")
|
|
64
|
+
expect(result.params).toEqual(expect.arrayContaining(["Alice", "Bob"]))
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it("where and", () => {
|
|
68
|
+
const result = buildWhereSQLQuery(
|
|
69
|
+
sqliteDialect,
|
|
70
|
+
"id",
|
|
71
|
+
[
|
|
72
|
+
{ t: "where", path: "name", op: "eq", value: "Alice" },
|
|
73
|
+
{ t: "and", path: "age", op: "gt", value: 18 as any }
|
|
74
|
+
],
|
|
75
|
+
"users",
|
|
76
|
+
{}
|
|
77
|
+
)
|
|
78
|
+
expect(result.sql).toContain("AND")
|
|
79
|
+
expect(result.params).toEqual(expect.arrayContaining(["Alice", 18]))
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it("where in", () => {
|
|
83
|
+
const result = buildWhereSQLQuery(
|
|
84
|
+
sqliteDialect,
|
|
85
|
+
"id",
|
|
86
|
+
[{ t: "where", path: "id", op: "in", value: ["a", "b", "c"] as any }],
|
|
87
|
+
"users",
|
|
88
|
+
{}
|
|
89
|
+
)
|
|
90
|
+
expect(result.sql).toContain("id IN (?, ?, ?)")
|
|
91
|
+
expect(result.params).toEqual(expect.arrayContaining(["a", "b", "c"]))
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it("where null", () => {
|
|
95
|
+
const result = buildWhereSQLQuery(
|
|
96
|
+
sqliteDialect,
|
|
97
|
+
"id",
|
|
98
|
+
[{ t: "where", path: "status", op: "eq", value: null as any }],
|
|
99
|
+
"users",
|
|
100
|
+
{}
|
|
101
|
+
)
|
|
102
|
+
expect(result.sql).toContain("IS NULL")
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("where neq null", () => {
|
|
106
|
+
const result = buildWhereSQLQuery(
|
|
107
|
+
sqliteDialect,
|
|
108
|
+
"id",
|
|
109
|
+
[{ t: "where", path: "status", op: "neq", value: null as any }],
|
|
110
|
+
"users",
|
|
111
|
+
{}
|
|
112
|
+
)
|
|
113
|
+
expect(result.sql).toContain("IS NOT NULL")
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("where contains", () => {
|
|
117
|
+
const result = buildWhereSQLQuery(
|
|
118
|
+
sqliteDialect,
|
|
119
|
+
"id",
|
|
120
|
+
[{ t: "where", path: "name", op: "contains", value: "oh" }],
|
|
121
|
+
"users",
|
|
122
|
+
{}
|
|
123
|
+
)
|
|
124
|
+
expect(result.sql).toContain("LIKE")
|
|
125
|
+
expect(result.sql).toContain("LOWER")
|
|
126
|
+
expect(result.params).toContain("%oh%")
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it("where startsWith", () => {
|
|
130
|
+
const result = buildWhereSQLQuery(
|
|
131
|
+
sqliteDialect,
|
|
132
|
+
"id",
|
|
133
|
+
[{ t: "where", path: "name", op: "startsWith", value: "Jo" }],
|
|
134
|
+
"users",
|
|
135
|
+
{}
|
|
136
|
+
)
|
|
137
|
+
expect(result.sql).toContain("LIKE")
|
|
138
|
+
expect(result.params).toContain("Jo%")
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it("where endsWith", () => {
|
|
142
|
+
const result = buildWhereSQLQuery(
|
|
143
|
+
sqliteDialect,
|
|
144
|
+
"id",
|
|
145
|
+
[{ t: "where", path: "name", op: "endsWith", value: "hn" }],
|
|
146
|
+
"users",
|
|
147
|
+
{}
|
|
148
|
+
)
|
|
149
|
+
expect(result.sql).toContain("LIKE")
|
|
150
|
+
expect(result.params).toContain("%hn")
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it("where includes (array contains)", () => {
|
|
154
|
+
const result = buildWhereSQLQuery(
|
|
155
|
+
sqliteDialect,
|
|
156
|
+
"id",
|
|
157
|
+
[{ t: "where", path: "tags", op: "includes", value: "admin" }],
|
|
158
|
+
"users",
|
|
159
|
+
{}
|
|
160
|
+
)
|
|
161
|
+
expect(result.sql).toContain("json_each")
|
|
162
|
+
expect(result.sql).toContain("value = ?")
|
|
163
|
+
expect(result.params).toContain("admin")
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it("where includes-any (array contains any)", () => {
|
|
167
|
+
const result = buildWhereSQLQuery(
|
|
168
|
+
sqliteDialect,
|
|
169
|
+
"id",
|
|
170
|
+
[{ t: "where", path: "tags", op: "includes-any", value: ["admin", "user"] as any }],
|
|
171
|
+
"users",
|
|
172
|
+
{}
|
|
173
|
+
)
|
|
174
|
+
expect(result.sql).toContain("json_each")
|
|
175
|
+
expect(result.sql).toContain("IN")
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it("nested scopes", () => {
|
|
179
|
+
const result = buildWhereSQLQuery(
|
|
180
|
+
sqliteDialect,
|
|
181
|
+
"id",
|
|
182
|
+
[
|
|
183
|
+
{ t: "where", path: "a", op: "eq", value: "1" },
|
|
184
|
+
{
|
|
185
|
+
t: "or-scope",
|
|
186
|
+
result: [
|
|
187
|
+
{ t: "where", path: "b", op: "eq", value: "2" },
|
|
188
|
+
{ t: "and", path: "c", op: "eq", value: "3" }
|
|
189
|
+
],
|
|
190
|
+
relation: "some" as const
|
|
191
|
+
}
|
|
192
|
+
],
|
|
193
|
+
"test",
|
|
194
|
+
{}
|
|
195
|
+
)
|
|
196
|
+
expect(result.sql).toContain("OR (")
|
|
197
|
+
expect(result.sql).toContain("AND")
|
|
198
|
+
expect(result.params).toEqual(expect.arrayContaining(["1", "2", "3"]))
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it("id key maps to id column", () => {
|
|
202
|
+
const result = buildWhereSQLQuery(
|
|
203
|
+
sqliteDialect,
|
|
204
|
+
"myId",
|
|
205
|
+
[{ t: "where", path: "myId", op: "eq", value: "123" }],
|
|
206
|
+
"users",
|
|
207
|
+
{}
|
|
208
|
+
)
|
|
209
|
+
expect(result.sql).toContain("id = ?")
|
|
210
|
+
expect(result.sql).not.toContain("json_extract")
|
|
211
|
+
expect(result.params).toContain("123")
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
it("order + limit + skip", () => {
|
|
215
|
+
const result = buildWhereSQLQuery(
|
|
216
|
+
sqliteDialect,
|
|
217
|
+
"id",
|
|
218
|
+
[],
|
|
219
|
+
"users",
|
|
220
|
+
{},
|
|
221
|
+
undefined,
|
|
222
|
+
[{ key: "name", direction: "ASC" }] as any,
|
|
223
|
+
5,
|
|
224
|
+
10
|
|
225
|
+
)
|
|
226
|
+
expect(result.sql).toContain("ORDER BY")
|
|
227
|
+
expect(result.sql).toContain("ASC")
|
|
228
|
+
expect(result.sql).toContain("LIMIT")
|
|
229
|
+
expect(result.sql).toContain("OFFSET")
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it("computed relation count projection", () => {
|
|
233
|
+
const result = buildWhereSQLQuery(
|
|
234
|
+
sqliteDialect,
|
|
235
|
+
"id",
|
|
236
|
+
[],
|
|
237
|
+
"users",
|
|
238
|
+
{},
|
|
239
|
+
[{
|
|
240
|
+
key: "pickedCount",
|
|
241
|
+
computed: {
|
|
242
|
+
_tag: "relation-count",
|
|
243
|
+
path: "items",
|
|
244
|
+
filter: [{ t: "where", path: "items.-1.description", op: "contains", value: "picked" }]
|
|
245
|
+
}
|
|
246
|
+
}]
|
|
247
|
+
)
|
|
248
|
+
expect(result.sql).toContain(`SELECT COUNT(1) FROM json_each(data, '$.items') AS _items`)
|
|
249
|
+
expect(result.sql).toContain(`AS "pickedCount"`)
|
|
250
|
+
expect(result.params).toContain("%picked%")
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it("computed relation any projection (sqlite bool encoding)", () => {
|
|
254
|
+
const result = buildWhereSQLQuery(
|
|
255
|
+
sqliteDialect,
|
|
256
|
+
"id",
|
|
257
|
+
[],
|
|
258
|
+
"users",
|
|
259
|
+
{},
|
|
260
|
+
[{
|
|
261
|
+
key: "hasPicked",
|
|
262
|
+
computed: {
|
|
263
|
+
_tag: "relation-any",
|
|
264
|
+
path: "items",
|
|
265
|
+
filter: [{ t: "where", path: "items.-1.description", op: "contains", value: "picked" }]
|
|
266
|
+
}
|
|
267
|
+
}]
|
|
268
|
+
)
|
|
269
|
+
expect(result.sql).toContain("CASE WHEN EXISTS(")
|
|
270
|
+
expect(result.sql).toContain(`AS "hasPicked"`)
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it("computed relation every projection (sqlite emits NOT EXISTS NOT)", () => {
|
|
274
|
+
const result = buildWhereSQLQuery(
|
|
275
|
+
sqliteDialect,
|
|
276
|
+
"id",
|
|
277
|
+
[],
|
|
278
|
+
"users",
|
|
279
|
+
{},
|
|
280
|
+
[{
|
|
281
|
+
key: "allPicked",
|
|
282
|
+
computed: {
|
|
283
|
+
_tag: "relation-every",
|
|
284
|
+
path: "items",
|
|
285
|
+
filter: [{ t: "where", path: "items.-1.state._tag", op: "eq", value: "picked" }]
|
|
286
|
+
}
|
|
287
|
+
}]
|
|
288
|
+
)
|
|
289
|
+
expect(result.sql).toContain(`NOT EXISTS(SELECT 1 FROM json_each(data, '$.items') AS _items WHERE NOT (`)
|
|
290
|
+
expect(result.sql).toContain(`AS "allPicked"`)
|
|
291
|
+
expect(result.params).toContain("picked")
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
it("computed relation-every with no filter degenerates to true", () => {
|
|
295
|
+
const result = buildWhereSQLQuery(
|
|
296
|
+
sqliteDialect,
|
|
297
|
+
"id",
|
|
298
|
+
[],
|
|
299
|
+
"users",
|
|
300
|
+
{},
|
|
301
|
+
[{
|
|
302
|
+
key: "allPicked",
|
|
303
|
+
computed: { _tag: "relation-every", path: "items", filter: [] }
|
|
304
|
+
}]
|
|
305
|
+
)
|
|
306
|
+
expect(result.sql).toContain("CASE WHEN 1=1")
|
|
307
|
+
expect(result.sql).toContain(`AS "allPicked"`)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
it("computed relation-distinct-count projection (sqlite)", () => {
|
|
311
|
+
const result = buildWhereSQLQuery(
|
|
312
|
+
sqliteDialect,
|
|
313
|
+
"id",
|
|
314
|
+
[],
|
|
315
|
+
"users",
|
|
316
|
+
{},
|
|
317
|
+
[{
|
|
318
|
+
key: "positionCount",
|
|
319
|
+
computed: {
|
|
320
|
+
_tag: "relation-distinct-count",
|
|
321
|
+
path: "items",
|
|
322
|
+
field: "rowId",
|
|
323
|
+
filter: [{ t: "where", path: "items.-1.state._tag", op: "neq", value: "cancelled" }]
|
|
324
|
+
}
|
|
325
|
+
}]
|
|
326
|
+
)
|
|
327
|
+
expect(result.sql).toContain(`SELECT COUNT(DISTINCT json_extract(_items.value, '$.rowId'))`)
|
|
328
|
+
expect(result.sql).toContain(`AS "positionCount"`)
|
|
329
|
+
expect(result.params).toContain("cancelled")
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
it("computed relation-sum projection (sqlite casts to REAL)", () => {
|
|
333
|
+
const result = buildWhereSQLQuery(
|
|
334
|
+
sqliteDialect,
|
|
335
|
+
"id",
|
|
336
|
+
[],
|
|
337
|
+
"users",
|
|
338
|
+
{},
|
|
339
|
+
[{
|
|
340
|
+
key: "totalWeight",
|
|
341
|
+
computed: { _tag: "relation-sum", path: "items", field: "weight", filter: [] }
|
|
342
|
+
}]
|
|
343
|
+
)
|
|
344
|
+
expect(result.sql).toContain(`SELECT COALESCE(SUM(CAST(json_extract(_items.value, '$.weight') AS REAL)), 0)`)
|
|
345
|
+
expect(result.sql).toContain(`AS "totalWeight"`)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
it("computed relation-sum-expr projection (sqlite)", () => {
|
|
349
|
+
const result = buildWhereSQLQuery(
|
|
350
|
+
sqliteDialect,
|
|
351
|
+
"id",
|
|
352
|
+
[],
|
|
353
|
+
"users",
|
|
354
|
+
{},
|
|
355
|
+
[{
|
|
356
|
+
key: "totalWeighted",
|
|
357
|
+
computed: {
|
|
358
|
+
_tag: "relation-sum-expr",
|
|
359
|
+
path: "items",
|
|
360
|
+
expression: {
|
|
361
|
+
_tag: "mul",
|
|
362
|
+
left: { _tag: "field", field: "weight" },
|
|
363
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
364
|
+
},
|
|
365
|
+
filter: []
|
|
366
|
+
}
|
|
367
|
+
}]
|
|
368
|
+
)
|
|
369
|
+
expect(result.sql).toContain(
|
|
370
|
+
`COALESCE(SUM((CAST(json_extract(_items.value, '$.weight') AS REAL) * CAST(json_extract(_items.value, '$.tradeUnit.amount') AS REAL))), 0)`
|
|
371
|
+
)
|
|
372
|
+
expect(result.sql).toContain(`AS "totalWeighted"`)
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
it("computed relation-sum-expr-by projection (sqlite)", () => {
|
|
376
|
+
const result = buildWhereSQLQuery(
|
|
377
|
+
sqliteDialect,
|
|
378
|
+
"id",
|
|
379
|
+
[],
|
|
380
|
+
"users",
|
|
381
|
+
{},
|
|
382
|
+
[{
|
|
383
|
+
key: "totalsByUnit",
|
|
384
|
+
computed: {
|
|
385
|
+
_tag: "relation-sum-expr-by",
|
|
386
|
+
path: "items",
|
|
387
|
+
expression: {
|
|
388
|
+
_tag: "mul",
|
|
389
|
+
left: { _tag: "field", field: "weight" },
|
|
390
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
391
|
+
},
|
|
392
|
+
unit: "tradeUnit.unit",
|
|
393
|
+
filter: []
|
|
394
|
+
}
|
|
395
|
+
}]
|
|
396
|
+
)
|
|
397
|
+
expect(result.sql).toContain(`json_group_array(json_object('unit', __unit, 'total', __total))`)
|
|
398
|
+
expect(result.sql).toContain(`GROUP BY json_extract(_items.value, '$.tradeUnit.unit')`)
|
|
399
|
+
expect(result.sql).toContain(`AS "totalsByUnit"`)
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
it("computed relation-sum-expr-normalized projection (sqlite)", () => {
|
|
403
|
+
const result = buildWhereSQLQuery(
|
|
404
|
+
sqliteDialect,
|
|
405
|
+
"id",
|
|
406
|
+
[],
|
|
407
|
+
"users",
|
|
408
|
+
{},
|
|
409
|
+
[{
|
|
410
|
+
key: "totalKg",
|
|
411
|
+
computed: {
|
|
412
|
+
_tag: "relation-sum-expr-normalized",
|
|
413
|
+
path: "items",
|
|
414
|
+
expression: {
|
|
415
|
+
_tag: "mul",
|
|
416
|
+
left: { _tag: "field", field: "weight" },
|
|
417
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
418
|
+
},
|
|
419
|
+
unit: "tradeUnit.unit",
|
|
420
|
+
toBase: "kg",
|
|
421
|
+
factors: { g: 0.001 },
|
|
422
|
+
filter: []
|
|
423
|
+
}
|
|
424
|
+
}]
|
|
425
|
+
)
|
|
426
|
+
expect(result.sql).toContain(
|
|
427
|
+
`CASE json_extract(_items.value, '$.tradeUnit.unit') WHEN 'kg' THEN 1 WHEN 'g' THEN 0.001 ELSE NULL END`
|
|
428
|
+
)
|
|
429
|
+
expect(result.sql).toContain(`AS "totalKg"`)
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
it("computed relation-collect (non-distinct) projection (sqlite)", () => {
|
|
433
|
+
const result = buildWhereSQLQuery(
|
|
434
|
+
sqliteDialect,
|
|
435
|
+
"id",
|
|
436
|
+
[],
|
|
437
|
+
"users",
|
|
438
|
+
{},
|
|
439
|
+
[{
|
|
440
|
+
key: "tags",
|
|
441
|
+
computed: { _tag: "relation-collect", path: "items", field: "articleId", distinct: false, filter: [] }
|
|
442
|
+
}]
|
|
443
|
+
)
|
|
444
|
+
expect(result.sql).toContain(
|
|
445
|
+
`SELECT COALESCE(json_group_array(json_extract(_items.value, '$.articleId')), json_array())`
|
|
446
|
+
)
|
|
447
|
+
expect(result.sql).toContain(`AS "tags"`)
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
it("computed relation-collect (distinct) emits inner DISTINCT subquery (sqlite)", () => {
|
|
451
|
+
const result = buildWhereSQLQuery(
|
|
452
|
+
sqliteDialect,
|
|
453
|
+
"id",
|
|
454
|
+
[],
|
|
455
|
+
"users",
|
|
456
|
+
{},
|
|
457
|
+
[{
|
|
458
|
+
key: "tags",
|
|
459
|
+
computed: { _tag: "relation-collect", path: "items", field: "articleId", distinct: true, filter: [] }
|
|
460
|
+
}]
|
|
461
|
+
)
|
|
462
|
+
expect(result.sql).toContain(`json_group_array(__v)`)
|
|
463
|
+
expect(result.sql).toContain(`SELECT DISTINCT json_extract(_items.value, '$.articleId') AS __v`)
|
|
464
|
+
expect(result.sql).toContain(`AS "tags"`)
|
|
465
|
+
})
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
describe("SQL query builder (PostgreSQL dialect)", () => {
|
|
469
|
+
it("where eq string uses ->> operator", () => {
|
|
470
|
+
const result = buildWhereSQLQuery(
|
|
471
|
+
pgDialect,
|
|
472
|
+
"id",
|
|
473
|
+
[{ t: "where", path: "name", op: "eq", value: "John" }],
|
|
474
|
+
"users",
|
|
475
|
+
{}
|
|
476
|
+
)
|
|
477
|
+
expect(result.sql).toContain("data->>'name'")
|
|
478
|
+
expect(result.sql).toContain("$1")
|
|
479
|
+
expect(result.params).toContain("John")
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
it("where contains uses ILIKE", () => {
|
|
483
|
+
const result = buildWhereSQLQuery(
|
|
484
|
+
pgDialect,
|
|
485
|
+
"id",
|
|
486
|
+
[{ t: "where", path: "name", op: "contains", value: "oh" }],
|
|
487
|
+
"users",
|
|
488
|
+
{}
|
|
489
|
+
)
|
|
490
|
+
expect(result.sql).toContain("ILIKE")
|
|
491
|
+
expect(result.params).toContain("%oh%")
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
it("where in uses $N placeholders", () => {
|
|
495
|
+
const result = buildWhereSQLQuery(
|
|
496
|
+
pgDialect,
|
|
497
|
+
"id",
|
|
498
|
+
[{ t: "where", path: "status", op: "in", value: ["active", "pending"] as any }],
|
|
499
|
+
"users",
|
|
500
|
+
{}
|
|
501
|
+
)
|
|
502
|
+
expect(result.sql).toContain("$1")
|
|
503
|
+
expect(result.sql).toContain("$2")
|
|
504
|
+
expect(result.params).toEqual(expect.arrayContaining(["active", "pending"]))
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
it("where includes uses @> jsonb operator", () => {
|
|
508
|
+
const result = buildWhereSQLQuery(
|
|
509
|
+
pgDialect,
|
|
510
|
+
"id",
|
|
511
|
+
[{ t: "where", path: "tags", op: "includes", value: "admin" }],
|
|
512
|
+
"users",
|
|
513
|
+
{}
|
|
514
|
+
)
|
|
515
|
+
expect(result.sql).toContain("@>")
|
|
516
|
+
expect(result.sql).toContain("jsonb")
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
it("nested path uses chained -> operators", () => {
|
|
520
|
+
const result = buildWhereSQLQuery(
|
|
521
|
+
pgDialect,
|
|
522
|
+
"id",
|
|
523
|
+
[{ t: "where", path: "address.city", op: "eq", value: "NYC" }],
|
|
524
|
+
"users",
|
|
525
|
+
{}
|
|
526
|
+
)
|
|
527
|
+
expect(result.sql).toContain("data->'address'->>'city'")
|
|
528
|
+
})
|
|
529
|
+
|
|
530
|
+
it("computed relation any projection", () => {
|
|
531
|
+
const result = buildWhereSQLQuery(
|
|
532
|
+
pgDialect,
|
|
533
|
+
"id",
|
|
534
|
+
[],
|
|
535
|
+
"users",
|
|
536
|
+
{},
|
|
537
|
+
[{
|
|
538
|
+
key: "hasPicked",
|
|
539
|
+
computed: {
|
|
540
|
+
_tag: "relation-any",
|
|
541
|
+
path: "items",
|
|
542
|
+
filter: [{ t: "where", path: "items.-1.description", op: "contains", value: "picked" }]
|
|
543
|
+
}
|
|
544
|
+
}]
|
|
545
|
+
)
|
|
546
|
+
expect(result.sql).toContain("EXISTS(SELECT 1 FROM jsonb_array_elements(data->'items') AS _items")
|
|
547
|
+
expect(result.sql).toContain(`AS "hasPicked"`)
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
it("computed relation-every (pg)", () => {
|
|
551
|
+
const result = buildWhereSQLQuery(
|
|
552
|
+
pgDialect,
|
|
553
|
+
"id",
|
|
554
|
+
[],
|
|
555
|
+
"users",
|
|
556
|
+
{},
|
|
557
|
+
[{
|
|
558
|
+
key: "allPicked",
|
|
559
|
+
computed: {
|
|
560
|
+
_tag: "relation-every",
|
|
561
|
+
path: "items",
|
|
562
|
+
filter: [{ t: "where", path: "items.-1.state._tag", op: "eq", value: "picked" }]
|
|
563
|
+
}
|
|
564
|
+
}]
|
|
565
|
+
)
|
|
566
|
+
expect(result.sql).toContain(`NOT EXISTS(SELECT 1 FROM jsonb_array_elements(data->'items') AS _items WHERE NOT (`)
|
|
567
|
+
expect(result.sql).toContain(`AS "allPicked"`)
|
|
568
|
+
})
|
|
569
|
+
|
|
570
|
+
it("computed relation-distinct-count (pg)", () => {
|
|
571
|
+
const result = buildWhereSQLQuery(
|
|
572
|
+
pgDialect,
|
|
573
|
+
"id",
|
|
574
|
+
[],
|
|
575
|
+
"users",
|
|
576
|
+
{},
|
|
577
|
+
[{
|
|
578
|
+
key: "positions",
|
|
579
|
+
computed: { _tag: "relation-distinct-count", path: "items", field: "rowId", filter: [] }
|
|
580
|
+
}]
|
|
581
|
+
)
|
|
582
|
+
expect(result.sql).toContain(`COUNT(DISTINCT _items->>'rowId')`)
|
|
583
|
+
expect(result.sql).toContain(`AS "positions"`)
|
|
584
|
+
})
|
|
585
|
+
|
|
586
|
+
it("computed relation-sum (pg casts via ::numeric)", () => {
|
|
587
|
+
const result = buildWhereSQLQuery(
|
|
588
|
+
pgDialect,
|
|
589
|
+
"id",
|
|
590
|
+
[],
|
|
591
|
+
"users",
|
|
592
|
+
{},
|
|
593
|
+
[{
|
|
594
|
+
key: "totalWeight",
|
|
595
|
+
computed: { _tag: "relation-sum", path: "items", field: "weight", filter: [] }
|
|
596
|
+
}]
|
|
597
|
+
)
|
|
598
|
+
expect(result.sql).toContain(`COALESCE(SUM((_items->>'weight')::numeric), 0)`)
|
|
599
|
+
expect(result.sql).toContain(`AS "totalWeight"`)
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
it("computed relation-sum-expr (pg)", () => {
|
|
603
|
+
const result = buildWhereSQLQuery(
|
|
604
|
+
pgDialect,
|
|
605
|
+
"id",
|
|
606
|
+
[],
|
|
607
|
+
"users",
|
|
608
|
+
{},
|
|
609
|
+
[{
|
|
610
|
+
key: "totalWeighted",
|
|
611
|
+
computed: {
|
|
612
|
+
_tag: "relation-sum-expr",
|
|
613
|
+
path: "items",
|
|
614
|
+
expression: {
|
|
615
|
+
_tag: "mul",
|
|
616
|
+
left: { _tag: "field", field: "weight" },
|
|
617
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
618
|
+
},
|
|
619
|
+
filter: []
|
|
620
|
+
}
|
|
621
|
+
}]
|
|
622
|
+
)
|
|
623
|
+
expect(result.sql).toContain(
|
|
624
|
+
`COALESCE(SUM(((_items->>'weight')::numeric * (_items->'tradeUnit'->>'amount')::numeric)), 0)`
|
|
625
|
+
)
|
|
626
|
+
expect(result.sql).toContain(`AS "totalWeighted"`)
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
it("computed relation-sum-expr-by (pg)", () => {
|
|
630
|
+
const result = buildWhereSQLQuery(
|
|
631
|
+
pgDialect,
|
|
632
|
+
"id",
|
|
633
|
+
[],
|
|
634
|
+
"users",
|
|
635
|
+
{},
|
|
636
|
+
[{
|
|
637
|
+
key: "totalsByUnit",
|
|
638
|
+
computed: {
|
|
639
|
+
_tag: "relation-sum-expr-by",
|
|
640
|
+
path: "items",
|
|
641
|
+
expression: {
|
|
642
|
+
_tag: "mul",
|
|
643
|
+
left: { _tag: "field", field: "weight" },
|
|
644
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
645
|
+
},
|
|
646
|
+
unit: "tradeUnit.unit",
|
|
647
|
+
filter: []
|
|
648
|
+
}
|
|
649
|
+
}]
|
|
650
|
+
)
|
|
651
|
+
expect(result.sql).toContain(`jsonb_agg(jsonb_build_object('unit', __unit, 'total', __total))`)
|
|
652
|
+
expect(result.sql).toContain(`GROUP BY _items->'tradeUnit'->>'unit'`)
|
|
653
|
+
expect(result.sql).toContain(`AS "totalsByUnit"`)
|
|
654
|
+
})
|
|
655
|
+
|
|
656
|
+
it("computed relation-sum-expr-normalized (pg)", () => {
|
|
657
|
+
const result = buildWhereSQLQuery(
|
|
658
|
+
pgDialect,
|
|
659
|
+
"id",
|
|
660
|
+
[],
|
|
661
|
+
"users",
|
|
662
|
+
{},
|
|
663
|
+
[{
|
|
664
|
+
key: "totalKg",
|
|
665
|
+
computed: {
|
|
666
|
+
_tag: "relation-sum-expr-normalized",
|
|
667
|
+
path: "items",
|
|
668
|
+
expression: {
|
|
669
|
+
_tag: "mul",
|
|
670
|
+
left: { _tag: "field", field: "weight" },
|
|
671
|
+
right: { _tag: "field", field: "tradeUnit.amount" }
|
|
672
|
+
},
|
|
673
|
+
unit: "tradeUnit.unit",
|
|
674
|
+
toBase: "kg",
|
|
675
|
+
factors: { g: 0.001 },
|
|
676
|
+
filter: []
|
|
677
|
+
}
|
|
678
|
+
}]
|
|
679
|
+
)
|
|
680
|
+
expect(result.sql).toContain(`CASE _items->'tradeUnit'->>'unit' WHEN 'kg' THEN 1 WHEN 'g' THEN 0.001 ELSE NULL END`)
|
|
681
|
+
expect(result.sql).toContain(`AS "totalKg"`)
|
|
682
|
+
})
|
|
683
|
+
|
|
684
|
+
it("computed relation-collect (pg jsonb_agg)", () => {
|
|
685
|
+
const result = buildWhereSQLQuery(
|
|
686
|
+
pgDialect,
|
|
687
|
+
"id",
|
|
688
|
+
[],
|
|
689
|
+
"users",
|
|
690
|
+
{},
|
|
691
|
+
[{
|
|
692
|
+
key: "ids",
|
|
693
|
+
computed: { _tag: "relation-collect", path: "items", field: "articleId", distinct: false, filter: [] }
|
|
694
|
+
}]
|
|
695
|
+
)
|
|
696
|
+
expect(result.sql).toContain(`COALESCE(jsonb_agg(_items->>'articleId'), '[]'::jsonb)`)
|
|
697
|
+
expect(result.sql).toContain(`AS "ids"`)
|
|
698
|
+
})
|
|
699
|
+
|
|
700
|
+
it("computed relation-collect distinct (pg jsonb_agg DISTINCT)", () => {
|
|
701
|
+
const result = buildWhereSQLQuery(
|
|
702
|
+
pgDialect,
|
|
703
|
+
"id",
|
|
704
|
+
[],
|
|
705
|
+
"users",
|
|
706
|
+
{},
|
|
707
|
+
[{
|
|
708
|
+
key: "ids",
|
|
709
|
+
computed: { _tag: "relation-collect", path: "items", field: "articleId", distinct: true, filter: [] }
|
|
710
|
+
}]
|
|
711
|
+
)
|
|
712
|
+
expect(result.sql).toContain(`COALESCE(jsonb_agg(DISTINCT _items->>'articleId'), '[]'::jsonb)`)
|
|
713
|
+
})
|
|
714
|
+
})
|
|
715
|
+
|
|
716
|
+
// --- Integration tests with in-memory SQLite (direct, no Effect SQL client) ---
|
|
717
|
+
|
|
718
|
+
describe("SQL Store (SQLite integration)", () => {
|
|
719
|
+
const withDb = (fn: (db: Sqlite.Database) => void) => {
|
|
720
|
+
const db = new BetterSqlite(":memory:")
|
|
721
|
+
db.pragma("journal_mode = WAL")
|
|
722
|
+
try {
|
|
723
|
+
fn(db)
|
|
724
|
+
} finally {
|
|
725
|
+
db.close()
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
it("creates table and seeds data", () =>
|
|
730
|
+
withDb((db) => {
|
|
731
|
+
db.exec(
|
|
732
|
+
`CREATE TABLE IF NOT EXISTS "test_items" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
|
|
733
|
+
)
|
|
734
|
+
db
|
|
735
|
+
.prepare(`INSERT INTO "test_items" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
736
|
+
.run("1", "etag1", JSON.stringify({ name: "Alice", age: 30 }))
|
|
737
|
+
|
|
738
|
+
const rows = db.prepare(`SELECT * FROM "test_items"`).all()
|
|
739
|
+
expect(rows.length).toBe(1)
|
|
740
|
+
expect((rows[0] as any).id).toBe("1")
|
|
741
|
+
}))
|
|
742
|
+
|
|
743
|
+
it("data column should not contain _etag or id", () =>
|
|
744
|
+
withDb((db) => {
|
|
745
|
+
db.exec(
|
|
746
|
+
`CREATE TABLE IF NOT EXISTS "test_clean" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
|
|
747
|
+
)
|
|
748
|
+
// Simulate what toRow now produces: data without id or _etag
|
|
749
|
+
const data = { name: "Alice", age: 30, tags: ["admin"] }
|
|
750
|
+
db
|
|
751
|
+
.prepare(`INSERT INTO "test_clean" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
752
|
+
.run("1", "etag1", JSON.stringify(data))
|
|
753
|
+
|
|
754
|
+
const row = db.prepare(`SELECT * FROM "test_clean" WHERE id = ?`).get("1") as any
|
|
755
|
+
const parsed = JSON.parse(row.data) as any
|
|
756
|
+
expect(parsed).not.toHaveProperty("id")
|
|
757
|
+
expect(parsed).not.toHaveProperty("_etag")
|
|
758
|
+
expect(parsed.name).toBe("Alice")
|
|
759
|
+
expect(parsed.age).toBe(30)
|
|
760
|
+
expect(parsed.tags).toEqual(["admin"])
|
|
761
|
+
// id and _etag come from their own columns
|
|
762
|
+
expect(row.id).toBe("1")
|
|
763
|
+
expect(row._etag).toBe("etag1")
|
|
764
|
+
}))
|
|
765
|
+
|
|
766
|
+
it("backward compat: rows with id/_etag in data still work with queries", () =>
|
|
767
|
+
withDb((db) => {
|
|
768
|
+
db.exec(
|
|
769
|
+
`CREATE TABLE IF NOT EXISTS "test_compat" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
|
|
770
|
+
)
|
|
771
|
+
// Old format: id and _etag inside data
|
|
772
|
+
db
|
|
773
|
+
.prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
774
|
+
.run("1", "etag1", JSON.stringify({ id: "1", _etag: "old_etag", name: "Alice", age: 30 }))
|
|
775
|
+
// New format: id and _etag stripped from data
|
|
776
|
+
db
|
|
777
|
+
.prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
778
|
+
.run("2", "etag2", JSON.stringify({ name: "Bob", age: 25 }))
|
|
779
|
+
|
|
780
|
+
// Both should be queryable by name
|
|
781
|
+
const q1 = buildWhereSQLQuery(
|
|
782
|
+
sqliteDialect,
|
|
783
|
+
"id",
|
|
784
|
+
[{ t: "where", path: "name", op: "eq", value: "Alice" }],
|
|
785
|
+
"test_compat",
|
|
786
|
+
{}
|
|
787
|
+
)
|
|
788
|
+
const r1 = query(db, q1.sql, q1.params)
|
|
789
|
+
expect(r1.length).toBe(1)
|
|
790
|
+
expect((r1[0] as any).id).toBe("1")
|
|
791
|
+
|
|
792
|
+
const q2 = buildWhereSQLQuery(
|
|
793
|
+
sqliteDialect,
|
|
794
|
+
"id",
|
|
795
|
+
[{ t: "where", path: "name", op: "eq", value: "Bob" }],
|
|
796
|
+
"test_compat",
|
|
797
|
+
{}
|
|
798
|
+
)
|
|
799
|
+
const r2 = query(db, q2.sql, q2.params)
|
|
800
|
+
expect(r2.length).toBe(1)
|
|
801
|
+
expect((r2[0] as any).id).toBe("2")
|
|
802
|
+
|
|
803
|
+
// Both queryable by id column
|
|
804
|
+
const q3 = buildWhereSQLQuery(
|
|
805
|
+
sqliteDialect,
|
|
806
|
+
"id",
|
|
807
|
+
[{ t: "where", path: "id", op: "in", value: ["1", "2"] as any }],
|
|
808
|
+
"test_compat",
|
|
809
|
+
{}
|
|
810
|
+
)
|
|
811
|
+
expect(query(db, q3.sql, q3.params).length).toBe(2)
|
|
812
|
+
}))
|
|
813
|
+
|
|
814
|
+
it("queries work when data does not contain id", () =>
|
|
815
|
+
withDb((db) => {
|
|
816
|
+
db.exec(
|
|
817
|
+
`CREATE TABLE IF NOT EXISTS "test_noid" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
|
|
818
|
+
)
|
|
819
|
+
const people = [
|
|
820
|
+
{ name: "Alice", age: 30 },
|
|
821
|
+
{ name: "Bob", age: 25 },
|
|
822
|
+
{ name: "Charlie", age: 35 }
|
|
823
|
+
]
|
|
824
|
+
const insert = db.prepare(
|
|
825
|
+
`INSERT INTO "test_noid" (id, _etag, data) VALUES (?, ?, ?)`
|
|
826
|
+
)
|
|
827
|
+
people.forEach((p, i) => insert.run(String(i + 1), `etag_${i + 1}`, JSON.stringify(p)))
|
|
828
|
+
|
|
829
|
+
// Filter by field in data
|
|
830
|
+
const q1 = buildWhereSQLQuery(
|
|
831
|
+
sqliteDialect,
|
|
832
|
+
"id",
|
|
833
|
+
[{ t: "where", path: "age", op: "gt", value: 28 as any }],
|
|
834
|
+
"test_noid",
|
|
835
|
+
{}
|
|
836
|
+
)
|
|
837
|
+
expect(query(db, q1.sql, q1.params).length).toBe(2) // Alice(30), Charlie(35)
|
|
838
|
+
|
|
839
|
+
// Filter by id column
|
|
840
|
+
const q2 = buildWhereSQLQuery(
|
|
841
|
+
sqliteDialect,
|
|
842
|
+
"id",
|
|
843
|
+
[{ t: "where", path: "id", op: "eq", value: "2" }],
|
|
844
|
+
"test_noid",
|
|
845
|
+
{}
|
|
846
|
+
)
|
|
847
|
+
const r2 = query(db, q2.sql, q2.params)
|
|
848
|
+
expect(r2.length).toBe(1)
|
|
849
|
+
expect((r2[0] as any).id).toBe("2")
|
|
850
|
+
expect((JSON.parse((r2[0] as any).data) as any).name).toBe("Bob")
|
|
851
|
+
|
|
852
|
+
// Order + limit still works
|
|
853
|
+
const q3 = buildWhereSQLQuery(
|
|
854
|
+
sqliteDialect,
|
|
855
|
+
"id",
|
|
856
|
+
[],
|
|
857
|
+
"test_noid",
|
|
858
|
+
{},
|
|
859
|
+
undefined,
|
|
860
|
+
[{ key: "age", direction: "ASC" }] as any,
|
|
861
|
+
undefined,
|
|
862
|
+
2
|
|
863
|
+
)
|
|
864
|
+
const r3 = query(db, q3.sql, q3.params)
|
|
865
|
+
expect(r3.length).toBe(2)
|
|
866
|
+
expect((JSON.parse((r3[0] as any).data) as any).name).toBe("Bob") // youngest first
|
|
867
|
+
}))
|
|
868
|
+
|
|
869
|
+
it("query builder generates valid SQL for SQLite", () =>
|
|
870
|
+
withDb((db) => {
|
|
871
|
+
db.exec(
|
|
872
|
+
`CREATE TABLE IF NOT EXISTS "test_people" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
|
|
873
|
+
)
|
|
874
|
+
|
|
875
|
+
const people = [
|
|
876
|
+
{ id: "1", name: "Alice", age: 30, tags: ["admin", "user"] },
|
|
877
|
+
{ id: "2", name: "Bob", age: 25, tags: ["user"] },
|
|
878
|
+
{ id: "3", name: "Charlie", age: 35, tags: ["admin"] },
|
|
879
|
+
{ id: "4", name: "Diana", age: 28, tags: ["user", "editor"] }
|
|
880
|
+
]
|
|
881
|
+
|
|
882
|
+
const insert = db.prepare(
|
|
883
|
+
`INSERT INTO "test_people" (id, _etag, data) VALUES (?, ?, ?)`
|
|
884
|
+
)
|
|
885
|
+
for (const p of people) {
|
|
886
|
+
const { id, ...data } = p
|
|
887
|
+
insert.run(id, `etag_${id}`, JSON.stringify(data))
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// Test eq
|
|
891
|
+
const q1 = buildWhereSQLQuery(
|
|
892
|
+
sqliteDialect,
|
|
893
|
+
"id",
|
|
894
|
+
[{ t: "where", path: "name", op: "eq", value: "Alice" }],
|
|
895
|
+
"test_people",
|
|
896
|
+
{}
|
|
897
|
+
)
|
|
898
|
+
expect(query(db, q1.sql, q1.params).length).toBe(1)
|
|
899
|
+
expect((JSON.parse((query(db, q1.sql, q1.params)[0] as any).data) as any).name).toBe("Alice")
|
|
900
|
+
|
|
901
|
+
// Test gt
|
|
902
|
+
const q2 = buildWhereSQLQuery(
|
|
903
|
+
sqliteDialect,
|
|
904
|
+
"id",
|
|
905
|
+
[{ t: "where", path: "age", op: "gt", value: 28 as any }],
|
|
906
|
+
"test_people",
|
|
907
|
+
{}
|
|
908
|
+
)
|
|
909
|
+
expect(query(db, q2.sql, q2.params).length).toBe(2)
|
|
910
|
+
|
|
911
|
+
// Test OR
|
|
912
|
+
const q3 = buildWhereSQLQuery(
|
|
913
|
+
sqliteDialect,
|
|
914
|
+
"id",
|
|
915
|
+
[
|
|
916
|
+
{ t: "where", path: "name", op: "eq", value: "Alice" },
|
|
917
|
+
{ t: "or", path: "name", op: "eq", value: "Bob" }
|
|
918
|
+
],
|
|
919
|
+
"test_people",
|
|
920
|
+
{}
|
|
921
|
+
)
|
|
922
|
+
expect(query(db, q3.sql, q3.params).length).toBe(2)
|
|
923
|
+
|
|
924
|
+
// Test AND
|
|
925
|
+
const q4 = buildWhereSQLQuery(
|
|
926
|
+
sqliteDialect,
|
|
927
|
+
"id",
|
|
928
|
+
[
|
|
929
|
+
{ t: "where", path: "name", op: "eq", value: "Alice" },
|
|
930
|
+
{ t: "and", path: "age", op: "gt", value: 25 as any }
|
|
931
|
+
],
|
|
932
|
+
"test_people",
|
|
933
|
+
{}
|
|
934
|
+
)
|
|
935
|
+
const r4 = query(db, q4.sql, q4.params)
|
|
936
|
+
expect(r4.length).toBe(1)
|
|
937
|
+
expect((JSON.parse((r4[0] as any).data) as any).name).toBe("Alice")
|
|
938
|
+
|
|
939
|
+
// Test IN
|
|
940
|
+
const q5 = buildWhereSQLQuery(
|
|
941
|
+
sqliteDialect,
|
|
942
|
+
"id",
|
|
943
|
+
[{ t: "where", path: "id", op: "in", value: ["1", "3"] as any }],
|
|
944
|
+
"test_people",
|
|
945
|
+
{}
|
|
946
|
+
)
|
|
947
|
+
expect(query(db, q5.sql, q5.params).length).toBe(2)
|
|
948
|
+
|
|
949
|
+
// Test contains (string)
|
|
950
|
+
const q6 = buildWhereSQLQuery(
|
|
951
|
+
sqliteDialect,
|
|
952
|
+
"id",
|
|
953
|
+
[{ t: "where", path: "name", op: "contains", value: "li" }],
|
|
954
|
+
"test_people",
|
|
955
|
+
{}
|
|
956
|
+
)
|
|
957
|
+
expect(query(db, q6.sql, q6.params).length).toBe(2) // Alice, Charlie
|
|
958
|
+
|
|
959
|
+
// Test startsWith
|
|
960
|
+
const q7 = buildWhereSQLQuery(
|
|
961
|
+
sqliteDialect,
|
|
962
|
+
"id",
|
|
963
|
+
[{ t: "where", path: "name", op: "startsWith", value: "Al" }],
|
|
964
|
+
"test_people",
|
|
965
|
+
{}
|
|
966
|
+
)
|
|
967
|
+
const r7 = query(db, q7.sql, q7.params)
|
|
968
|
+
expect(r7.length).toBe(1)
|
|
969
|
+
expect((JSON.parse((r7[0] as any).data) as any).name).toBe("Alice")
|
|
970
|
+
|
|
971
|
+
// Test includes (array)
|
|
972
|
+
const q8 = buildWhereSQLQuery(
|
|
973
|
+
sqliteDialect,
|
|
974
|
+
"id",
|
|
975
|
+
[{ t: "where", path: "tags", op: "includes", value: "admin" }],
|
|
976
|
+
"test_people",
|
|
977
|
+
{}
|
|
978
|
+
)
|
|
979
|
+
expect(query(db, q8.sql, q8.params).length).toBe(2) // Alice, Charlie
|
|
980
|
+
|
|
981
|
+
// Test nested scope: where name = Alice OR (age > 30 AND name contains 'ar')
|
|
982
|
+
const q9 = buildWhereSQLQuery(
|
|
983
|
+
sqliteDialect,
|
|
984
|
+
"id",
|
|
985
|
+
[
|
|
986
|
+
{ t: "where", path: "name", op: "eq", value: "Alice" },
|
|
987
|
+
{
|
|
988
|
+
t: "or-scope",
|
|
989
|
+
result: [
|
|
990
|
+
{ t: "where", path: "age", op: "gt", value: 30 as any },
|
|
991
|
+
{ t: "and", path: "name", op: "contains", value: "ar" }
|
|
992
|
+
],
|
|
993
|
+
relation: "some"
|
|
994
|
+
}
|
|
995
|
+
],
|
|
996
|
+
"test_people",
|
|
997
|
+
{}
|
|
998
|
+
)
|
|
999
|
+
expect(query(db, q9.sql, q9.params).length).toBe(2) // Alice + Charlie
|
|
1000
|
+
|
|
1001
|
+
// Test order + limit
|
|
1002
|
+
const q10 = buildWhereSQLQuery(
|
|
1003
|
+
sqliteDialect,
|
|
1004
|
+
"id",
|
|
1005
|
+
[],
|
|
1006
|
+
"test_people",
|
|
1007
|
+
{},
|
|
1008
|
+
undefined,
|
|
1009
|
+
[{ key: "age", direction: "DESC" }] as any,
|
|
1010
|
+
undefined,
|
|
1011
|
+
2
|
|
1012
|
+
)
|
|
1013
|
+
const r10 = query(db, q10.sql, q10.params)
|
|
1014
|
+
expect(r10.length).toBe(2)
|
|
1015
|
+
expect((JSON.parse((r10[0] as any).data) as any).name).toBe("Charlie") // oldest first
|
|
1016
|
+
}))
|
|
1017
|
+
|
|
1018
|
+
it("computed relation-every / distinct-count / sum / collect run on SQLite", () =>
|
|
1019
|
+
withDb((db) => {
|
|
1020
|
+
db.exec(`CREATE TABLE "test_orders" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1021
|
+
const orders = [
|
|
1022
|
+
{
|
|
1023
|
+
id: "o1",
|
|
1024
|
+
items: [
|
|
1025
|
+
{ rowId: "r1", articleId: "A", weight: 1.5, state: { _tag: "picked" } },
|
|
1026
|
+
{ rowId: "r2", articleId: "A", weight: 2.5, state: { _tag: "picked" } },
|
|
1027
|
+
{ rowId: "r2", articleId: "B", weight: 0.25, state: { _tag: "picking" } }
|
|
1028
|
+
]
|
|
1029
|
+
},
|
|
1030
|
+
{
|
|
1031
|
+
id: "o2",
|
|
1032
|
+
items: [
|
|
1033
|
+
{ rowId: "r9", articleId: "Z", weight: 10, state: { _tag: "packed" } }
|
|
1034
|
+
]
|
|
1035
|
+
}
|
|
1036
|
+
]
|
|
1037
|
+
const insert = db.prepare(`INSERT INTO "test_orders" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1038
|
+
for (const o of orders) {
|
|
1039
|
+
const { id, ...data } = o
|
|
1040
|
+
insert.run(id, `etag_${id}`, JSON.stringify(data))
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
const q = buildWhereSQLQuery(
|
|
1044
|
+
sqliteDialect,
|
|
1045
|
+
"id",
|
|
1046
|
+
[],
|
|
1047
|
+
"test_orders",
|
|
1048
|
+
{},
|
|
1049
|
+
[
|
|
1050
|
+
{
|
|
1051
|
+
key: "allPicked",
|
|
1052
|
+
computed: {
|
|
1053
|
+
_tag: "relation-every",
|
|
1054
|
+
path: "items",
|
|
1055
|
+
filter: [{ t: "where", path: "items.-1.state._tag", op: "eq", value: "picked" }]
|
|
1056
|
+
}
|
|
1057
|
+
},
|
|
1058
|
+
{
|
|
1059
|
+
key: "positionCount",
|
|
1060
|
+
computed: { _tag: "relation-distinct-count", path: "items", field: "rowId", filter: [] }
|
|
1061
|
+
},
|
|
1062
|
+
{
|
|
1063
|
+
key: "totalWeight",
|
|
1064
|
+
computed: { _tag: "relation-sum", path: "items", field: "weight", filter: [] }
|
|
1065
|
+
},
|
|
1066
|
+
{
|
|
1067
|
+
key: "articleIds",
|
|
1068
|
+
computed: {
|
|
1069
|
+
_tag: "relation-collect",
|
|
1070
|
+
path: "items",
|
|
1071
|
+
field: "articleId",
|
|
1072
|
+
distinct: true,
|
|
1073
|
+
filter: []
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
] as any,
|
|
1077
|
+
[{ key: "id", direction: "ASC" }] as any
|
|
1078
|
+
)
|
|
1079
|
+
const rows = query(db, q.sql, q.params) as Array<Record<string, unknown>>
|
|
1080
|
+
expect(rows.length).toBe(2)
|
|
1081
|
+
// o1: not all picked (one is "picking")
|
|
1082
|
+
expect(JSON.parse(rows[0]!["allPicked"] as string)).toBe(false)
|
|
1083
|
+
expect(rows[0]!["positionCount"]).toBe(2)
|
|
1084
|
+
expect(rows[0]!["totalWeight"]).toBeCloseTo(4.25)
|
|
1085
|
+
expect((JSON.parse(rows[0]!["articleIds"] as string) as string[]).sort()).toEqual(["A", "B"])
|
|
1086
|
+
// o2: all packed (so not "picked"), allPicked = !exists(NOT picked) = false
|
|
1087
|
+
expect(JSON.parse(rows[1]!["allPicked"] as string)).toBe(false)
|
|
1088
|
+
expect(rows[1]!["positionCount"]).toBe(1)
|
|
1089
|
+
expect(rows[1]!["totalWeight"]).toBeCloseTo(10)
|
|
1090
|
+
expect(JSON.parse(rows[1]!["articleIds"] as string)).toEqual(["Z"])
|
|
1091
|
+
}))
|
|
1092
|
+
|
|
1093
|
+
it("computed relation-every is true when all items match filter", () =>
|
|
1094
|
+
withDb((db) => {
|
|
1095
|
+
db.exec(`CREATE TABLE "test_every" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1096
|
+
db.prepare(`INSERT INTO "test_every" (id, _etag, data) VALUES (?, ?, ?)`).run(
|
|
1097
|
+
"1",
|
|
1098
|
+
"e",
|
|
1099
|
+
JSON.stringify({
|
|
1100
|
+
items: [
|
|
1101
|
+
{ state: { _tag: "picked" } },
|
|
1102
|
+
{ state: { _tag: "picked" } }
|
|
1103
|
+
]
|
|
1104
|
+
})
|
|
1105
|
+
)
|
|
1106
|
+
|
|
1107
|
+
const q = buildWhereSQLQuery(
|
|
1108
|
+
sqliteDialect,
|
|
1109
|
+
"id",
|
|
1110
|
+
[],
|
|
1111
|
+
"test_every",
|
|
1112
|
+
{},
|
|
1113
|
+
[{
|
|
1114
|
+
key: "allPicked",
|
|
1115
|
+
computed: {
|
|
1116
|
+
_tag: "relation-every",
|
|
1117
|
+
path: "items",
|
|
1118
|
+
filter: [{ t: "where", path: "items.-1.state._tag", op: "eq", value: "picked" }]
|
|
1119
|
+
}
|
|
1120
|
+
}]
|
|
1121
|
+
)
|
|
1122
|
+
const rows = query(db, q.sql, q.params) as Array<Record<string, unknown>>
|
|
1123
|
+
expect(JSON.parse(rows[0]!["allPicked"] as string)).toBe(true)
|
|
1124
|
+
}))
|
|
1125
|
+
|
|
1126
|
+
it("namespace param is in correct position for SQLite positional placeholders", () =>
|
|
1127
|
+
withDb((db) => {
|
|
1128
|
+
db.exec(
|
|
1129
|
+
`CREATE TABLE IF NOT EXISTS "test_ns" (id TEXT NOT NULL, _namespace TEXT NOT NULL DEFAULT 'primary', _etag TEXT, data JSON NOT NULL, PRIMARY KEY (id, _namespace))`
|
|
1130
|
+
)
|
|
1131
|
+
const insert = db.prepare(
|
|
1132
|
+
`INSERT INTO "test_ns" (id, _namespace, _etag, data) VALUES (?, ?, ?, ?)`
|
|
1133
|
+
)
|
|
1134
|
+
insert.run("1", "primary", "e1", JSON.stringify({ name: "Alice", role: "admin" }))
|
|
1135
|
+
insert.run("2", "primary", "e2", JSON.stringify({ name: "Bob", role: "user" }))
|
|
1136
|
+
insert.run("3", "other", "e3", JSON.stringify({ name: "Charlie", role: "admin" }))
|
|
1137
|
+
|
|
1138
|
+
// Build a filter query: role != 'deleted'
|
|
1139
|
+
const q = buildWhereSQLQuery(
|
|
1140
|
+
sqliteDialect,
|
|
1141
|
+
"id",
|
|
1142
|
+
[{ t: "where", path: "role", op: "neq", value: "deleted" }],
|
|
1143
|
+
"test_ns",
|
|
1144
|
+
{}
|
|
1145
|
+
)
|
|
1146
|
+
|
|
1147
|
+
// Simulate what SQL.ts does: prepend _namespace = ? and put ns FIRST in params
|
|
1148
|
+
const hasWhere = q.sql.includes("WHERE")
|
|
1149
|
+
const nsSql = hasWhere
|
|
1150
|
+
? q.sql.replace("WHERE", `WHERE _namespace = ? AND`)
|
|
1151
|
+
: q.sql.replace(`FROM "test_ns"`, `FROM "test_ns" WHERE _namespace = ?`)
|
|
1152
|
+
const params = ["primary", ...q.params]
|
|
1153
|
+
|
|
1154
|
+
const results = query(db, nsSql, params)
|
|
1155
|
+
// Should only get Alice and Bob (primary namespace), not Charlie (other namespace)
|
|
1156
|
+
expect(results.length).toBe(2)
|
|
1157
|
+
const names = results.map((r) => (JSON.parse((r as any).data) as any).name).sort()
|
|
1158
|
+
expect(names).toEqual(["Alice", "Bob"])
|
|
1159
|
+
}))
|
|
1160
|
+
|
|
1161
|
+
it("aggregate: agg-count-when with GROUP BY on aliased path field (SQLite)", () =>
|
|
1162
|
+
withDb((db) => {
|
|
1163
|
+
db.exec(`CREATE TABLE "test_agg" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1164
|
+
const rows = [
|
|
1165
|
+
{ city: "NYC", state: "active" },
|
|
1166
|
+
{ city: "NYC", state: "inactive" },
|
|
1167
|
+
{ city: "NYC", state: "active" },
|
|
1168
|
+
{ city: "LA", state: "active" }
|
|
1169
|
+
]
|
|
1170
|
+
const insert = db.prepare(`INSERT INTO "test_agg" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1171
|
+
rows.forEach((r, i) => insert.run(String(i + 1), `e${i + 1}`, JSON.stringify(r)))
|
|
1172
|
+
|
|
1173
|
+
const q = buildWhereSQLQuery(
|
|
1174
|
+
sqliteDialect,
|
|
1175
|
+
"id",
|
|
1176
|
+
[],
|
|
1177
|
+
"test_agg",
|
|
1178
|
+
{},
|
|
1179
|
+
[
|
|
1180
|
+
{ key: "city", path: "city" },
|
|
1181
|
+
{
|
|
1182
|
+
key: "activeCount",
|
|
1183
|
+
aggregate: {
|
|
1184
|
+
_tag: "agg-count-when",
|
|
1185
|
+
filter: [{ t: "where", path: "state", op: "eq", value: "active" }]
|
|
1186
|
+
}
|
|
1187
|
+
},
|
|
1188
|
+
{ key: "total", aggregate: { _tag: "agg-count" } }
|
|
1189
|
+
] as any,
|
|
1190
|
+
[{ key: "city", direction: "ASC" }] as any
|
|
1191
|
+
)
|
|
1192
|
+
|
|
1193
|
+
expect(q.sql).toContain("GROUP BY")
|
|
1194
|
+
expect(q.sql).toContain("COUNT(CASE WHEN")
|
|
1195
|
+
|
|
1196
|
+
const result = query(db, q.sql, q.params) as Array<Record<string, unknown>>
|
|
1197
|
+
expect(result.length).toBe(2)
|
|
1198
|
+
|
|
1199
|
+
const la = result.find((r) => r["city"] === "LA")!
|
|
1200
|
+
expect(la["activeCount"]).toBe(1)
|
|
1201
|
+
expect(la["total"]).toBe(1)
|
|
1202
|
+
|
|
1203
|
+
const nyc = result.find((r) => r["city"] === "NYC")!
|
|
1204
|
+
expect(nyc["activeCount"]).toBe(2)
|
|
1205
|
+
expect(nyc["total"]).toBe(3)
|
|
1206
|
+
}))
|
|
1207
|
+
|
|
1208
|
+
it("aggregate: agg-sum and agg-min / agg-max (SQLite)", () =>
|
|
1209
|
+
withDb((db) => {
|
|
1210
|
+
db.exec(`CREATE TABLE "test_agg2" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1211
|
+
const rows = [
|
|
1212
|
+
{ dept: "eng", salary: 100 },
|
|
1213
|
+
{ dept: "eng", salary: 200 },
|
|
1214
|
+
{ dept: "hr", salary: 50 }
|
|
1215
|
+
]
|
|
1216
|
+
const insert = db.prepare(`INSERT INTO "test_agg2" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1217
|
+
rows.forEach((r, i) => insert.run(String(i + 1), `e${i + 1}`, JSON.stringify(r)))
|
|
1218
|
+
|
|
1219
|
+
const q = buildWhereSQLQuery(
|
|
1220
|
+
sqliteDialect,
|
|
1221
|
+
"id",
|
|
1222
|
+
[],
|
|
1223
|
+
"test_agg2",
|
|
1224
|
+
{},
|
|
1225
|
+
[
|
|
1226
|
+
{ key: "dept", path: "dept" },
|
|
1227
|
+
{ key: "totalSalary", aggregate: { _tag: "agg-sum", field: "salary" } },
|
|
1228
|
+
{ key: "minSalary", aggregate: { _tag: "agg-min", field: "salary" } },
|
|
1229
|
+
{ key: "maxSalary", aggregate: { _tag: "agg-max", field: "salary" } }
|
|
1230
|
+
] as any,
|
|
1231
|
+
[{ key: "dept", direction: "ASC" }] as any
|
|
1232
|
+
)
|
|
1233
|
+
|
|
1234
|
+
const result = query(db, q.sql, q.params) as Array<Record<string, unknown>>
|
|
1235
|
+
expect(result.length).toBe(2)
|
|
1236
|
+
|
|
1237
|
+
const eng = result.find((r) => r["dept"] === "eng")!
|
|
1238
|
+
expect(eng["totalSalary"]).toBeCloseTo(300)
|
|
1239
|
+
expect(eng["minSalary"]).toBeCloseTo(100)
|
|
1240
|
+
expect(eng["maxSalary"]).toBeCloseTo(200)
|
|
1241
|
+
|
|
1242
|
+
const hr = result.find((r) => r["dept"] === "hr")!
|
|
1243
|
+
expect(hr["totalSalary"]).toBeCloseTo(50)
|
|
1244
|
+
}))
|
|
1245
|
+
|
|
1246
|
+
it("aggregate: nested path field grouping (SQLite)", () =>
|
|
1247
|
+
withDb((db) => {
|
|
1248
|
+
db.exec(`CREATE TABLE "test_agg3" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1249
|
+
const rows = [
|
|
1250
|
+
{ address: { city: "NYC" }, status: "open" },
|
|
1251
|
+
{ address: { city: "NYC" }, status: "closed" },
|
|
1252
|
+
{ address: { city: "LA" }, status: "open" }
|
|
1253
|
+
]
|
|
1254
|
+
const insert = db.prepare(`INSERT INTO "test_agg3" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1255
|
+
rows.forEach((r, i) => insert.run(String(i + 1), `e${i + 1}`, JSON.stringify(r)))
|
|
1256
|
+
|
|
1257
|
+
const q = buildWhereSQLQuery(
|
|
1258
|
+
sqliteDialect,
|
|
1259
|
+
"id",
|
|
1260
|
+
[],
|
|
1261
|
+
"test_agg3",
|
|
1262
|
+
{},
|
|
1263
|
+
[
|
|
1264
|
+
{ key: "city", path: "address.city" },
|
|
1265
|
+
{ key: "total", aggregate: { _tag: "agg-count" } }
|
|
1266
|
+
] as any,
|
|
1267
|
+
[{ key: "city", direction: "ASC" }] as any
|
|
1268
|
+
)
|
|
1269
|
+
|
|
1270
|
+
expect(q.sql).toContain("GROUP BY")
|
|
1271
|
+
|
|
1272
|
+
const result = query(db, q.sql, q.params) as Array<Record<string, unknown>>
|
|
1273
|
+
expect(result.length).toBe(2)
|
|
1274
|
+
const la = result.find((r) => r["city"] === "LA")!
|
|
1275
|
+
expect(la["total"]).toBe(1)
|
|
1276
|
+
const nyc = result.find((r) => r["city"] === "NYC")!
|
|
1277
|
+
expect(nyc["total"]).toBe(2)
|
|
1278
|
+
}))
|
|
1279
|
+
})
|
|
1280
|
+
|
|
1281
|
+
// --- boolean WHERE clause tests (regression: where("x", true) must work per dialect) ---
|
|
1282
|
+
|
|
1283
|
+
describe("boolean WHERE clauses — query builder", () => {
|
|
1284
|
+
it("sqlite: eq true serializes bool → 1 integer", () => {
|
|
1285
|
+
const result = buildWhereSQLQuery(
|
|
1286
|
+
sqliteDialect,
|
|
1287
|
+
"id",
|
|
1288
|
+
[{ t: "where", path: "flag", op: "eq", value: true as any }],
|
|
1289
|
+
"t",
|
|
1290
|
+
{}
|
|
1291
|
+
)
|
|
1292
|
+
expect(result.sql).toContain("json_extract(data, '$.flag') = ?")
|
|
1293
|
+
expect(result.params).toEqual([1])
|
|
1294
|
+
})
|
|
1295
|
+
|
|
1296
|
+
it("sqlite: eq false serializes bool → 0 integer", () => {
|
|
1297
|
+
const result = buildWhereSQLQuery(
|
|
1298
|
+
sqliteDialect,
|
|
1299
|
+
"id",
|
|
1300
|
+
[{ t: "where", path: "flag", op: "eq", value: false as any }],
|
|
1301
|
+
"t",
|
|
1302
|
+
{}
|
|
1303
|
+
)
|
|
1304
|
+
expect(result.params).toEqual([0])
|
|
1305
|
+
})
|
|
1306
|
+
|
|
1307
|
+
it("sqlite: neq/in also serialize booleans", () => {
|
|
1308
|
+
const r1 = buildWhereSQLQuery(
|
|
1309
|
+
sqliteDialect,
|
|
1310
|
+
"id",
|
|
1311
|
+
[{ t: "where", path: "flag", op: "neq", value: true as any }],
|
|
1312
|
+
"t",
|
|
1313
|
+
{}
|
|
1314
|
+
)
|
|
1315
|
+
expect(r1.params).toEqual([1])
|
|
1316
|
+
|
|
1317
|
+
const r2 = buildWhereSQLQuery(
|
|
1318
|
+
sqliteDialect,
|
|
1319
|
+
"id",
|
|
1320
|
+
[{ t: "where", path: "flag", op: "in", value: [true, false] as any }],
|
|
1321
|
+
"t",
|
|
1322
|
+
{}
|
|
1323
|
+
)
|
|
1324
|
+
expect(r2.params).toEqual([1, 0])
|
|
1325
|
+
})
|
|
1326
|
+
|
|
1327
|
+
it("pg: eq true serializes bool → 'true' text (matches ->> output)", () => {
|
|
1328
|
+
const result = buildWhereSQLQuery(
|
|
1329
|
+
pgDialect,
|
|
1330
|
+
"id",
|
|
1331
|
+
[{ t: "where", path: "flag", op: "eq", value: true as any }],
|
|
1332
|
+
"t",
|
|
1333
|
+
{}
|
|
1334
|
+
)
|
|
1335
|
+
expect(result.sql).toContain("data->>'flag' = $1")
|
|
1336
|
+
expect(result.params).toEqual(["true"])
|
|
1337
|
+
})
|
|
1338
|
+
|
|
1339
|
+
it("pg: eq false serializes bool → 'false' text", () => {
|
|
1340
|
+
const result = buildWhereSQLQuery(
|
|
1341
|
+
pgDialect,
|
|
1342
|
+
"id",
|
|
1343
|
+
[{ t: "where", path: "flag", op: "eq", value: false as any }],
|
|
1344
|
+
"t",
|
|
1345
|
+
{}
|
|
1346
|
+
)
|
|
1347
|
+
expect(result.params).toEqual(["false"])
|
|
1348
|
+
})
|
|
1349
|
+
|
|
1350
|
+
it("pg: in with booleans serializes as text list", () => {
|
|
1351
|
+
const result = buildWhereSQLQuery(
|
|
1352
|
+
pgDialect,
|
|
1353
|
+
"id",
|
|
1354
|
+
[{ t: "where", path: "flag", op: "in", value: [true, false] as any }],
|
|
1355
|
+
"t",
|
|
1356
|
+
{}
|
|
1357
|
+
)
|
|
1358
|
+
expect(result.params).toEqual(["true", "false"])
|
|
1359
|
+
})
|
|
1360
|
+
|
|
1361
|
+
it("non-boolean scalars pass through unchanged (sqlite)", () => {
|
|
1362
|
+
const result = buildWhereSQLQuery(
|
|
1363
|
+
sqliteDialect,
|
|
1364
|
+
"id",
|
|
1365
|
+
[{ t: "where", path: "name", op: "eq", value: "Alice" }],
|
|
1366
|
+
"t",
|
|
1367
|
+
{}
|
|
1368
|
+
)
|
|
1369
|
+
expect(result.params).toEqual(["Alice"])
|
|
1370
|
+
})
|
|
1371
|
+
|
|
1372
|
+
it("non-boolean scalars pass through unchanged (pg)", () => {
|
|
1373
|
+
const result = buildWhereSQLQuery(
|
|
1374
|
+
pgDialect,
|
|
1375
|
+
"id",
|
|
1376
|
+
[{ t: "where", path: "age", op: "gt", value: 18 as any }],
|
|
1377
|
+
"t",
|
|
1378
|
+
{}
|
|
1379
|
+
)
|
|
1380
|
+
expect(result.params).toEqual([18])
|
|
1381
|
+
})
|
|
1382
|
+
})
|
|
1383
|
+
|
|
1384
|
+
describe("boolean WHERE clauses — SQLite integration (end-to-end)", () => {
|
|
1385
|
+
const withDb = (fn: (db: Sqlite.Database) => void) => {
|
|
1386
|
+
const db = new BetterSqlite(":memory:")
|
|
1387
|
+
try {
|
|
1388
|
+
fn(db)
|
|
1389
|
+
} finally {
|
|
1390
|
+
db.close()
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
it("where flag = true matches only true rows", () =>
|
|
1395
|
+
withDb((db) => {
|
|
1396
|
+
db.exec(`CREATE TABLE "t" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1397
|
+
db
|
|
1398
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1399
|
+
.run("1", "e", JSON.stringify({ flag: true, name: "Alice" }))
|
|
1400
|
+
db
|
|
1401
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1402
|
+
.run("2", "e", JSON.stringify({ flag: false, name: "Bob" }))
|
|
1403
|
+
|
|
1404
|
+
const q = buildWhereSQLQuery(
|
|
1405
|
+
sqliteDialect,
|
|
1406
|
+
"id",
|
|
1407
|
+
[{ t: "where", path: "flag", op: "eq", value: true as any }],
|
|
1408
|
+
"t",
|
|
1409
|
+
{}
|
|
1410
|
+
)
|
|
1411
|
+
const rows = query(db, q.sql, q.params)
|
|
1412
|
+
expect(rows.length).toBe(1)
|
|
1413
|
+
expect((JSON.parse((rows[0] as any).data) as any).name).toBe("Alice")
|
|
1414
|
+
}))
|
|
1415
|
+
|
|
1416
|
+
it("where flag = false matches only false rows", () =>
|
|
1417
|
+
withDb((db) => {
|
|
1418
|
+
db.exec(`CREATE TABLE "t" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1419
|
+
db
|
|
1420
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1421
|
+
.run("1", "e", JSON.stringify({ flag: true, name: "Alice" }))
|
|
1422
|
+
db
|
|
1423
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1424
|
+
.run("2", "e", JSON.stringify({ flag: false, name: "Bob" }))
|
|
1425
|
+
|
|
1426
|
+
const q = buildWhereSQLQuery(
|
|
1427
|
+
sqliteDialect,
|
|
1428
|
+
"id",
|
|
1429
|
+
[{ t: "where", path: "flag", op: "eq", value: false as any }],
|
|
1430
|
+
"t",
|
|
1431
|
+
{}
|
|
1432
|
+
)
|
|
1433
|
+
const rows = query(db, q.sql, q.params)
|
|
1434
|
+
expect(rows.length).toBe(1)
|
|
1435
|
+
expect((JSON.parse((rows[0] as any).data) as any).name).toBe("Bob")
|
|
1436
|
+
}))
|
|
1437
|
+
|
|
1438
|
+
it("where nested boolean path works", () =>
|
|
1439
|
+
withDb((db) => {
|
|
1440
|
+
db.exec(`CREATE TABLE "t" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1441
|
+
db
|
|
1442
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1443
|
+
.run("1", "e", JSON.stringify({ meta: { active: true }, name: "Alice" }))
|
|
1444
|
+
db
|
|
1445
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1446
|
+
.run("2", "e", JSON.stringify({ meta: { active: false }, name: "Bob" }))
|
|
1447
|
+
|
|
1448
|
+
const q = buildWhereSQLQuery(
|
|
1449
|
+
sqliteDialect,
|
|
1450
|
+
"id",
|
|
1451
|
+
[{ t: "where", path: "meta.active", op: "eq", value: true as any }],
|
|
1452
|
+
"t",
|
|
1453
|
+
{}
|
|
1454
|
+
)
|
|
1455
|
+
const rows = query(db, q.sql, q.params)
|
|
1456
|
+
expect(rows.length).toBe(1)
|
|
1457
|
+
expect((JSON.parse((rows[0] as any).data) as any).name).toBe("Alice")
|
|
1458
|
+
}))
|
|
1459
|
+
|
|
1460
|
+
it("where neq boolean works", () =>
|
|
1461
|
+
withDb((db) => {
|
|
1462
|
+
db.exec(`CREATE TABLE "t" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`)
|
|
1463
|
+
db
|
|
1464
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1465
|
+
.run("1", "e", JSON.stringify({ flag: true, name: "Alice" }))
|
|
1466
|
+
db
|
|
1467
|
+
.prepare(`INSERT INTO "t" (id, _etag, data) VALUES (?, ?, ?)`)
|
|
1468
|
+
.run("2", "e", JSON.stringify({ flag: false, name: "Bob" }))
|
|
1469
|
+
|
|
1470
|
+
const q = buildWhereSQLQuery(
|
|
1471
|
+
sqliteDialect,
|
|
1472
|
+
"id",
|
|
1473
|
+
[{ t: "where", path: "flag", op: "neq", value: true as any }],
|
|
1474
|
+
"t",
|
|
1475
|
+
{}
|
|
1476
|
+
)
|
|
1477
|
+
const rows = query(db, q.sql, q.params)
|
|
1478
|
+
expect(rows.length).toBe(1)
|
|
1479
|
+
expect((JSON.parse((rows[0] as any).data) as any).name).toBe("Bob")
|
|
1480
|
+
}))
|
|
1481
|
+
})
|
|
1482
|
+
|
|
1483
|
+
// --- jsonExtractJson dialect tests (regression: booleans must survive round-trip) ---
|
|
1484
|
+
|
|
1485
|
+
describe("sqliteDialect.jsonExtractJson boolean round-trip", () => {
|
|
1486
|
+
const withDb = (fn: (db: Sqlite.Database) => void) => {
|
|
1487
|
+
const db = new BetterSqlite(":memory:")
|
|
1488
|
+
try {
|
|
1489
|
+
fn(db)
|
|
1490
|
+
} finally {
|
|
1491
|
+
db.close()
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
it("SQL uses json_type CASE to preserve booleans", () => {
|
|
1496
|
+
const sql = sqliteDialect.jsonExtractJson("flag")
|
|
1497
|
+
expect(sql).toContain("json_type(data, '$.flag')")
|
|
1498
|
+
expect(sql).toContain("'true'")
|
|
1499
|
+
expect(sql).toContain("'false'")
|
|
1500
|
+
expect(sql).toContain("json_quote(json_extract(data, '$.flag'))")
|
|
1501
|
+
})
|
|
1502
|
+
|
|
1503
|
+
it("boolean false round-trips as false (not 0)", () =>
|
|
1504
|
+
withDb((db) => {
|
|
1505
|
+
db.exec(`CREATE TABLE "t" (data JSON NOT NULL)`)
|
|
1506
|
+
db.prepare(`INSERT INTO "t" (data) VALUES (?)`).run(JSON.stringify({ flag: false }))
|
|
1507
|
+
|
|
1508
|
+
const row = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("flag")} AS v FROM "t"`).get() as any
|
|
1509
|
+
const parsed = JSON.parse(row.v)
|
|
1510
|
+
expect(parsed).toBe(false)
|
|
1511
|
+
expect(typeof parsed).toBe("boolean")
|
|
1512
|
+
}))
|
|
1513
|
+
|
|
1514
|
+
it("boolean true round-trips as true (not 1)", () =>
|
|
1515
|
+
withDb((db) => {
|
|
1516
|
+
db.exec(`CREATE TABLE "t" (data JSON NOT NULL)`)
|
|
1517
|
+
db.prepare(`INSERT INTO "t" (data) VALUES (?)`).run(JSON.stringify({ flag: true }))
|
|
1518
|
+
|
|
1519
|
+
const row = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("flag")} AS v FROM "t"`).get() as any
|
|
1520
|
+
const parsed = JSON.parse(row.v)
|
|
1521
|
+
expect(parsed).toBe(true)
|
|
1522
|
+
expect(typeof parsed).toBe("boolean")
|
|
1523
|
+
}))
|
|
1524
|
+
|
|
1525
|
+
it("nested boolean round-trips correctly", () =>
|
|
1526
|
+
withDb((db) => {
|
|
1527
|
+
db.exec(`CREATE TABLE "t" (data JSON NOT NULL)`)
|
|
1528
|
+
db.prepare(`INSERT INTO "t" (data) VALUES (?)`).run(
|
|
1529
|
+
JSON.stringify({ nested: { flag: false } })
|
|
1530
|
+
)
|
|
1531
|
+
|
|
1532
|
+
const row = db
|
|
1533
|
+
.prepare(`SELECT ${sqliteDialect.jsonExtractJson("nested.flag")} AS v FROM "t"`)
|
|
1534
|
+
.get() as any
|
|
1535
|
+
const parsed = JSON.parse(row.v)
|
|
1536
|
+
expect(parsed).toBe(false)
|
|
1537
|
+
expect(typeof parsed).toBe("boolean")
|
|
1538
|
+
}))
|
|
1539
|
+
|
|
1540
|
+
it("numbers 0 and 1 still parse as numbers (not booleans)", () =>
|
|
1541
|
+
withDb((db) => {
|
|
1542
|
+
db.exec(`CREATE TABLE "t" (data JSON NOT NULL)`)
|
|
1543
|
+
db.prepare(`INSERT INTO "t" (data) VALUES (?)`).run(JSON.stringify({ zero: 0, one: 1 }))
|
|
1544
|
+
|
|
1545
|
+
const zRow = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("zero")} AS v FROM "t"`).get() as any
|
|
1546
|
+
const oRow = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("one")} AS v FROM "t"`).get() as any
|
|
1547
|
+
expect(JSON.parse(zRow.v)).toBe(0)
|
|
1548
|
+
expect(typeof JSON.parse(zRow.v)).toBe("number")
|
|
1549
|
+
expect(JSON.parse(oRow.v)).toBe(1)
|
|
1550
|
+
expect(typeof JSON.parse(oRow.v)).toBe("number")
|
|
1551
|
+
}))
|
|
1552
|
+
|
|
1553
|
+
it("strings, arrays, and null still round-trip correctly", () =>
|
|
1554
|
+
withDb((db) => {
|
|
1555
|
+
db.exec(`CREATE TABLE "t" (data JSON NOT NULL)`)
|
|
1556
|
+
db.prepare(`INSERT INTO "t" (data) VALUES (?)`).run(
|
|
1557
|
+
JSON.stringify({ s: "hello", arr: [1, 2, 3], nil: null })
|
|
1558
|
+
)
|
|
1559
|
+
|
|
1560
|
+
const sRow = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("s")} AS v FROM "t"`).get() as any
|
|
1561
|
+
const aRow = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("arr")} AS v FROM "t"`).get() as any
|
|
1562
|
+
const nRow = db.prepare(`SELECT ${sqliteDialect.jsonExtractJson("nil")} AS v FROM "t"`).get() as any
|
|
1563
|
+
expect(JSON.parse(sRow.v)).toBe("hello")
|
|
1564
|
+
expect(JSON.parse(aRow.v)).toEqual([1, 2, 3])
|
|
1565
|
+
expect(JSON.parse(nRow.v)).toBe(null)
|
|
1566
|
+
}))
|
|
1567
|
+
})
|
|
1568
|
+
|
|
1569
|
+
// --- toRow stripping and parseRow reconstruction tests ---
|
|
1570
|
+
|
|
1571
|
+
describe("toRow strips _etag and id from data", () => {
|
|
1572
|
+
// Replicate the toRow logic from SQL.ts to test in isolation
|
|
1573
|
+
const toRow = <IdKey extends PropertyKey>(e: any, idKey: IdKey) => {
|
|
1574
|
+
const newE = makeETag(e)
|
|
1575
|
+
const id = newE[idKey] as string
|
|
1576
|
+
const { _etag, [idKey]: _id, ...rest } = newE as any
|
|
1577
|
+
const data = JSON.stringify(rest)
|
|
1578
|
+
return { id, _etag: newE._etag!, data, item: newE }
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
it("data JSON does not contain _etag", () => {
|
|
1582
|
+
const row = toRow({ id: "1", _etag: undefined, name: "Alice", age: 30 }, "id")
|
|
1583
|
+
const parsed = JSON.parse(row.data) as any
|
|
1584
|
+
expect(parsed).not.toHaveProperty("_etag")
|
|
1585
|
+
expect(parsed.name).toBe("Alice")
|
|
1586
|
+
expect(parsed.age).toBe(30)
|
|
1587
|
+
})
|
|
1588
|
+
|
|
1589
|
+
it("data JSON does not contain id field", () => {
|
|
1590
|
+
const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
|
|
1591
|
+
const parsed = JSON.parse(row.data) as any
|
|
1592
|
+
expect(parsed).not.toHaveProperty("id")
|
|
1593
|
+
expect(parsed.name).toBe("Alice")
|
|
1594
|
+
})
|
|
1595
|
+
|
|
1596
|
+
it("data JSON does not contain custom idKey field", () => {
|
|
1597
|
+
const row = toRow({ myId: "abc", _etag: undefined, name: "Bob" }, "myId")
|
|
1598
|
+
const parsed = JSON.parse(row.data) as any
|
|
1599
|
+
expect(parsed).not.toHaveProperty("myId")
|
|
1600
|
+
expect(parsed.name).toBe("Bob")
|
|
1601
|
+
expect(row.id).toBe("abc")
|
|
1602
|
+
})
|
|
1603
|
+
|
|
1604
|
+
it("id and _etag are returned as separate fields", () => {
|
|
1605
|
+
const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
|
|
1606
|
+
expect(row.id).toBe("1")
|
|
1607
|
+
expect(typeof row._etag).toBe("string")
|
|
1608
|
+
expect(row._etag.length).toBeGreaterThan(0)
|
|
1609
|
+
})
|
|
1610
|
+
|
|
1611
|
+
it("item still contains all fields including _etag and id", () => {
|
|
1612
|
+
const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
|
|
1613
|
+
expect(row.item.id).toBe("1")
|
|
1614
|
+
expect(row.item._etag).toBe(row._etag)
|
|
1615
|
+
expect(row.item.name).toBe("Alice")
|
|
1616
|
+
})
|
|
1617
|
+
|
|
1618
|
+
it("preserves nested objects in data", () => {
|
|
1619
|
+
const row = toRow({ id: "1", _etag: undefined, address: { city: "NYC", zip: "10001" } }, "id")
|
|
1620
|
+
const parsed = JSON.parse(row.data) as any
|
|
1621
|
+
expect(parsed.address).toEqual({ city: "NYC", zip: "10001" })
|
|
1622
|
+
expect(parsed).not.toHaveProperty("id")
|
|
1623
|
+
expect(parsed).not.toHaveProperty("_etag")
|
|
1624
|
+
})
|
|
1625
|
+
})
|
|
1626
|
+
|
|
1627
|
+
describe("parseRow reconstructs full object from row", () => {
|
|
1628
|
+
it("re-injects id from row column using idKey", () => {
|
|
1629
|
+
const result: any = parseRow(
|
|
1630
|
+
{ id: "42", _etag: "etag1", data: JSON.stringify({ name: "Alice", age: 30 }) },
|
|
1631
|
+
"id",
|
|
1632
|
+
{}
|
|
1633
|
+
)
|
|
1634
|
+
expect(result.id).toBe("42")
|
|
1635
|
+
expect(result.name).toBe("Alice")
|
|
1636
|
+
expect(result.age).toBe(30)
|
|
1637
|
+
expect(result._etag).toBe("etag1")
|
|
1638
|
+
})
|
|
1639
|
+
|
|
1640
|
+
it("re-injects custom idKey from row column", () => {
|
|
1641
|
+
const result: any = parseRow(
|
|
1642
|
+
{ id: "abc", _etag: "etag2", data: JSON.stringify({ name: "Bob" }) },
|
|
1643
|
+
"myId",
|
|
1644
|
+
{}
|
|
1645
|
+
)
|
|
1646
|
+
expect(result.myId).toBe("abc")
|
|
1647
|
+
expect(result.name).toBe("Bob")
|
|
1648
|
+
expect(result._etag).toBe("etag2")
|
|
1649
|
+
})
|
|
1650
|
+
|
|
1651
|
+
it("uses _etag from row column, not from data", () => {
|
|
1652
|
+
const result: any = parseRow(
|
|
1653
|
+
{ id: "1", _etag: "column_etag", data: JSON.stringify({ _etag: "stale_data_etag", name: "Alice" }) },
|
|
1654
|
+
"id",
|
|
1655
|
+
{}
|
|
1656
|
+
)
|
|
1657
|
+
expect(result._etag).toBe("column_etag")
|
|
1658
|
+
})
|
|
1659
|
+
|
|
1660
|
+
it("uses id from row column, not from data", () => {
|
|
1661
|
+
const result: any = parseRow(
|
|
1662
|
+
{ id: "correct_id", _etag: "e1", data: JSON.stringify({ id: "wrong_id", name: "Alice" }) },
|
|
1663
|
+
"id",
|
|
1664
|
+
{}
|
|
1665
|
+
)
|
|
1666
|
+
expect(result.id).toBe("correct_id")
|
|
1667
|
+
})
|
|
1668
|
+
|
|
1669
|
+
it("applies defaultValues for missing fields", () => {
|
|
1670
|
+
const result: any = parseRow(
|
|
1671
|
+
{ id: "1", _etag: "e1", data: JSON.stringify({ name: "Alice" }) },
|
|
1672
|
+
"id",
|
|
1673
|
+
{ status: "active", role: "user" }
|
|
1674
|
+
)
|
|
1675
|
+
expect(result.name).toBe("Alice")
|
|
1676
|
+
expect(result.status).toBe("active")
|
|
1677
|
+
expect(result.role).toBe("user")
|
|
1678
|
+
})
|
|
1679
|
+
|
|
1680
|
+
it("data fields override defaultValues", () => {
|
|
1681
|
+
const result: any = parseRow(
|
|
1682
|
+
{ id: "1", _etag: "e1", data: JSON.stringify({ name: "Alice", status: "inactive" }) },
|
|
1683
|
+
"id",
|
|
1684
|
+
{ status: "active" }
|
|
1685
|
+
)
|
|
1686
|
+
expect(result.status).toBe("inactive")
|
|
1687
|
+
})
|
|
1688
|
+
|
|
1689
|
+
it("handles null _etag from row", () => {
|
|
1690
|
+
const result: any = parseRow(
|
|
1691
|
+
{ id: "1", _etag: null, data: JSON.stringify({ name: "Alice" }) },
|
|
1692
|
+
"id",
|
|
1693
|
+
{}
|
|
1694
|
+
)
|
|
1695
|
+
expect(result._etag).toBeUndefined()
|
|
1696
|
+
})
|
|
1697
|
+
|
|
1698
|
+
it("round-trip: toRow then parseRow reconstructs the original", () => {
|
|
1699
|
+
const original = { id: "1", _etag: undefined as string | undefined, name: "Alice", age: 30, tags: ["admin"] }
|
|
1700
|
+
const newE = makeETag(original)
|
|
1701
|
+
const { _etag, id: _id, ...rest } = newE as any
|
|
1702
|
+
const row = { id: newE.id, _etag: newE._etag!, data: JSON.stringify(rest) }
|
|
1703
|
+
|
|
1704
|
+
const reconstructed: any = parseRow(row, "id", {})
|
|
1705
|
+
expect(reconstructed.id).toBe("1")
|
|
1706
|
+
expect(reconstructed.name).toBe("Alice")
|
|
1707
|
+
expect(reconstructed.age).toBe(30)
|
|
1708
|
+
expect(reconstructed.tags).toEqual(["admin"])
|
|
1709
|
+
expect(reconstructed._etag).toBe(newE._etag)
|
|
1710
|
+
})
|
|
1711
|
+
})
|