@edge-base/server 0.1.1
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/admin-build/.gitkeep +0 -0
- package/admin-build/_app/env.js +1 -0
- package/admin-build/_app/immutable/assets/0.Bm6cF078.css +1 -0
- package/admin-build/_app/immutable/assets/1.BfW3pUNa.css +1 -0
- package/admin-build/_app/immutable/assets/11.CVmQOewb.css +1 -0
- package/admin-build/_app/immutable/assets/12.B1EhbRZT.css +1 -0
- package/admin-build/_app/immutable/assets/13.BvwYeuwE.css +1 -0
- package/admin-build/_app/immutable/assets/14.CdVfcO0R.css +1 -0
- package/admin-build/_app/immutable/assets/15.2yeZ66b-.css +1 -0
- package/admin-build/_app/immutable/assets/17.BVg0JEVu.css +1 -0
- package/admin-build/_app/immutable/assets/18.Rwnl3x_i.css +1 -0
- package/admin-build/_app/immutable/assets/20.DsPWA9AV.css +1 -0
- package/admin-build/_app/immutable/assets/21.Dz2RJ56c.css +1 -0
- package/admin-build/_app/immutable/assets/22.DwNLk5Ai.css +1 -0
- package/admin-build/_app/immutable/assets/23.CFpu0gOO.css +1 -0
- package/admin-build/_app/immutable/assets/24.Cy5LBeoJ.css +1 -0
- package/admin-build/_app/immutable/assets/25.pUyLVf-h.css +1 -0
- package/admin-build/_app/immutable/assets/26.DBcGrlXa.css +1 -0
- package/admin-build/_app/immutable/assets/27.BswYyAJD.css +1 -0
- package/admin-build/_app/immutable/assets/28.B4ueB1Kf.css +1 -0
- package/admin-build/_app/immutable/assets/29.B-qU6PdF.css +1 -0
- package/admin-build/_app/immutable/assets/3.Dg81Pgmd.css +1 -0
- package/admin-build/_app/immutable/assets/30.CsdWum94.css +1 -0
- package/admin-build/_app/immutable/assets/31.U6OwIp50.css +1 -0
- package/admin-build/_app/immutable/assets/4.CyawCCux.css +1 -0
- package/admin-build/_app/immutable/assets/5.C0YO2HTk.css +1 -0
- package/admin-build/_app/immutable/assets/8.Br5jd6kD.css +1 -0
- package/admin-build/_app/immutable/assets/Badge.EMYLHBxE.css +1 -0
- package/admin-build/_app/immutable/assets/Button.DpzMRTjK.css +1 -0
- package/admin-build/_app/immutable/assets/ConfirmDialog.DAnaWRRk.css +1 -0
- package/admin-build/_app/immutable/assets/EmptyState.CwKsu57Y.css +1 -0
- package/admin-build/_app/immutable/assets/Input.BDUSenmU.css +1 -0
- package/admin-build/_app/immutable/assets/Modal.Dm5B0Xie.css +1 -0
- package/admin-build/_app/immutable/assets/PageShell.CmU-Xh-b.css +1 -0
- package/admin-build/_app/immutable/assets/SchemaFieldEditor.g4NsCdno.css +1 -0
- package/admin-build/_app/immutable/assets/Select.BW4Keufm.css +1 -0
- package/admin-build/_app/immutable/assets/Skeleton.KWUulTKJ.css +1 -0
- package/admin-build/_app/immutable/assets/Tabs.CniGYb67.css +1 -0
- package/admin-build/_app/immutable/assets/TimeChart.BTCDAvmT.css +1 -0
- package/admin-build/_app/immutable/assets/Toggle.Cy_K12OM.css +1 -0
- package/admin-build/_app/immutable/assets/TopList.ClFzmPlA.css +1 -0
- package/admin-build/_app/immutable/chunks/7B47DvSx.js +1 -0
- package/admin-build/_app/immutable/chunks/7f08Id8e.js +1 -0
- package/admin-build/_app/immutable/chunks/8wJeQ7LN.js +1 -0
- package/admin-build/_app/immutable/chunks/B-h2afW5.js +1 -0
- package/admin-build/_app/immutable/chunks/B8vJP3wz.js +1 -0
- package/admin-build/_app/immutable/chunks/BR_fL5Yv.js +1 -0
- package/admin-build/_app/immutable/chunks/BY92tFS2.js +1 -0
- package/admin-build/_app/immutable/chunks/BcR-Rdj9.js +1 -0
- package/admin-build/_app/immutable/chunks/BdrwyZv8.js +1 -0
- package/admin-build/_app/immutable/chunks/Bh56EfQ_.js +1 -0
- package/admin-build/_app/immutable/chunks/BkrCkgYp.js +1 -0
- package/admin-build/_app/immutable/chunks/BmRjiP5k.js +1 -0
- package/admin-build/_app/immutable/chunks/BsokvhWC.js +1 -0
- package/admin-build/_app/immutable/chunks/C4D51vTW.js +1 -0
- package/admin-build/_app/immutable/chunks/C6puvcoR.js +2 -0
- package/admin-build/_app/immutable/chunks/CCKNu7m7.js +1 -0
- package/admin-build/_app/immutable/chunks/CWj6FrbW.js +1 -0
- package/admin-build/_app/immutable/chunks/Ce-ngf4p.js +5 -0
- package/admin-build/_app/immutable/chunks/Cs0GwzJA.js +1 -0
- package/admin-build/_app/immutable/chunks/CwROoZK0.js +1 -0
- package/admin-build/_app/immutable/chunks/CxCPv_Ut.js +1 -0
- package/admin-build/_app/immutable/chunks/CxbRue-5.js +1 -0
- package/admin-build/_app/immutable/chunks/CyqB6g-D.js +1 -0
- package/admin-build/_app/immutable/chunks/D5h5A1cc.js +2 -0
- package/admin-build/_app/immutable/chunks/DnyL7Zq-.js +1 -0
- package/admin-build/_app/immutable/chunks/DoPXzH7F.js +1 -0
- package/admin-build/_app/immutable/chunks/DrQSgw-f.js +1 -0
- package/admin-build/_app/immutable/chunks/DttM2zNO.js +1 -0
- package/admin-build/_app/immutable/chunks/DuXuUBWN.js +1 -0
- package/admin-build/_app/immutable/chunks/MdeqaOQx.js +10 -0
- package/admin-build/_app/immutable/chunks/NuUjtcO2.js +1 -0
- package/admin-build/_app/immutable/chunks/Q2nPFxS6.js +1 -0
- package/admin-build/_app/immutable/chunks/R6arueIl.js +1 -0
- package/admin-build/_app/immutable/chunks/UUazaC_N.js +1 -0
- package/admin-build/_app/immutable/chunks/cOYbrQxx.js +1 -0
- package/admin-build/_app/immutable/chunks/eFQHTGwA.js +1 -0
- package/admin-build/_app/immutable/chunks/ehbppgYb.js +1 -0
- package/admin-build/_app/immutable/chunks/glwixJlP.js +1 -0
- package/admin-build/_app/immutable/chunks/vApWTCBs.js +1 -0
- package/admin-build/_app/immutable/chunks/w89G9Xpi.js +1 -0
- package/admin-build/_app/immutable/chunks/wJsUhbfZ.js +1 -0
- package/admin-build/_app/immutable/chunks/zfauFM8P.js +1 -0
- package/admin-build/_app/immutable/entry/app.CcO-Uos3.js +2 -0
- package/admin-build/_app/immutable/entry/start.COebYq3I.js +1 -0
- package/admin-build/_app/immutable/nodes/0.CjtHKU-6.js +1 -0
- package/admin-build/_app/immutable/nodes/1.DEisjlM0.js +1 -0
- package/admin-build/_app/immutable/nodes/10.CvhdyWVB.js +1 -0
- package/admin-build/_app/immutable/nodes/11.DjHqcOvy.js +1 -0
- package/admin-build/_app/immutable/nodes/12.mQLz4Mj_.js +1 -0
- package/admin-build/_app/immutable/nodes/13.CBonZZyP.js +110 -0
- package/admin-build/_app/immutable/nodes/14.d-oiZL0j.js +3 -0
- package/admin-build/_app/immutable/nodes/15.CKPQsUYF.js +1 -0
- package/admin-build/_app/immutable/nodes/16.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/17.DayhKyEZ.js +1 -0
- package/admin-build/_app/immutable/nodes/18.DKwS0Ir0.js +1 -0
- package/admin-build/_app/immutable/nodes/19.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/2.BKoKrw1i.js +1 -0
- package/admin-build/_app/immutable/nodes/20.BvIkkkrW.js +1 -0
- package/admin-build/_app/immutable/nodes/21.DMaFhdHk.js +128 -0
- package/admin-build/_app/immutable/nodes/22.3xdgwuK1.js +1 -0
- package/admin-build/_app/immutable/nodes/23.8Bvgjbsl.js +112 -0
- package/admin-build/_app/immutable/nodes/24.DzSSzRhG.js +2 -0
- package/admin-build/_app/immutable/nodes/25.9KKYBnAE.js +2 -0
- package/admin-build/_app/immutable/nodes/26.Bhn9dfhY.js +1 -0
- package/admin-build/_app/immutable/nodes/27.kRLiC24G.js +1 -0
- package/admin-build/_app/immutable/nodes/28.BVIN1-7N.js +1 -0
- package/admin-build/_app/immutable/nodes/29.3yabZWj4.js +1 -0
- package/admin-build/_app/immutable/nodes/3.BFtSOkX7.js +2 -0
- package/admin-build/_app/immutable/nodes/30.CyCQlwaP.js +1 -0
- package/admin-build/_app/immutable/nodes/31.C4LDXjES.js +1 -0
- package/admin-build/_app/immutable/nodes/4.CvbiMlCa.js +1 -0
- package/admin-build/_app/immutable/nodes/5.C6BLv2eM.js +1 -0
- package/admin-build/_app/immutable/nodes/6.BcXvfl2P.js +1 -0
- package/admin-build/_app/immutable/nodes/7.CIuqhPiK.js +1 -0
- package/admin-build/_app/immutable/nodes/8.BQOR_JfO.js +1 -0
- package/admin-build/_app/immutable/nodes/9.NZqXQxPy.js +1 -0
- package/admin-build/_app/version.json +1 -0
- package/admin-build/favicon.svg +26 -0
- package/admin-build/index.html +45 -0
- package/openapi.json +19543 -0
- package/package.json +66 -0
- package/src/__tests__/admin-assets.test.ts +55 -0
- package/src/__tests__/admin-data-routes.test.ts +488 -0
- package/src/__tests__/admin-db-target.test.ts +103 -0
- package/src/__tests__/admin-routing.test.ts +31 -0
- package/src/__tests__/admin-user-management.test.ts +311 -0
- package/src/__tests__/analytics-query.test.ts +75 -0
- package/src/__tests__/auth-d1.test.ts +749 -0
- package/src/__tests__/auth-db-adapter.test.ts +73 -0
- package/src/__tests__/auth-jwt.test.ts +440 -0
- package/src/__tests__/auth-oauth.test.ts +389 -0
- package/src/__tests__/auth-password.test.ts +367 -0
- package/src/__tests__/auth-redirect.test.ts +87 -0
- package/src/__tests__/backup-restore.test.ts +711 -0
- package/src/__tests__/broadcast.test.ts +128 -0
- package/src/__tests__/cli.test.ts +178 -0
- package/src/__tests__/cloudflare-realtime.test.ts +113 -0
- package/src/__tests__/config.test.ts +469 -0
- package/src/__tests__/cors.test.ts +154 -0
- package/src/__tests__/cron.test.ts +302 -0
- package/src/__tests__/d1-handler.test.ts +402 -0
- package/src/__tests__/d1-sql.test.ts +120 -0
- package/src/__tests__/database-live-config.test.ts +42 -0
- package/src/__tests__/database-live-emitter.test.ts +56 -0
- package/src/__tests__/database-live-filters.test.ts +63 -0
- package/src/__tests__/database-live-route.test.ts +113 -0
- package/src/__tests__/db-sql.test.ts +163 -0
- package/src/__tests__/do-lifecycle.test.ts +263 -0
- package/src/__tests__/do-router.test.ts +729 -0
- package/src/__tests__/email-provider.test.ts +128 -0
- package/src/__tests__/email-templates.test.ts +528 -0
- package/src/__tests__/error-format.test.ts +250 -0
- package/src/__tests__/field-ops.test.ts +242 -0
- package/src/__tests__/functions-context.test.ts +334 -0
- package/src/__tests__/functions-d1-proxy.test.ts +229 -0
- package/src/__tests__/functions-registry-runtime-config.test.ts +17 -0
- package/src/__tests__/functions-route.test.ts +139 -0
- package/src/__tests__/internal-request.test.ts +77 -0
- package/src/__tests__/log-writer.test.ts +44 -0
- package/src/__tests__/logger.test.ts +58 -0
- package/src/__tests__/meta-admin-proxy.test.ts +48 -0
- package/src/__tests__/meta-export-coverage.test.ts +191 -0
- package/src/__tests__/meta-route-registration.test.ts +47 -0
- package/src/__tests__/namespace-dump.test.ts +28 -0
- package/src/__tests__/oauth-providers.test.ts +337 -0
- package/src/__tests__/openapi-coverage.test.ts +144 -0
- package/src/__tests__/pagination.test.ts +59 -0
- package/src/__tests__/password-policy.test.ts +191 -0
- package/src/__tests__/plugin-migrations.test.ts +379 -0
- package/src/__tests__/postgres-batch-compat.test.ts +133 -0
- package/src/__tests__/postgres-dialect.test.ts +328 -0
- package/src/__tests__/postgres-executor.test.ts +79 -0
- package/src/__tests__/postgres-field-ops-compat.test.ts +222 -0
- package/src/__tests__/postgres-schema-init.test.ts +105 -0
- package/src/__tests__/postgres-table-utils.test.ts +107 -0
- package/src/__tests__/presence.test.ts +199 -0
- package/src/__tests__/provider.test.ts +550 -0
- package/src/__tests__/public-user-profile.test.ts +339 -0
- package/src/__tests__/push-handlers.test.ts +179 -0
- package/src/__tests__/push-provider.test.ts +80 -0
- package/src/__tests__/push-token.test.ts +418 -0
- package/src/__tests__/query.test.ts +771 -0
- package/src/__tests__/rate-limit.test.ts +260 -0
- package/src/__tests__/room-access-policy.test.ts +101 -0
- package/src/__tests__/room-handler-context.test.ts +130 -0
- package/src/__tests__/room-monitoring.test.ts +138 -0
- package/src/__tests__/room-runtime-routing.test.ts +222 -0
- package/src/__tests__/room.test.ts +254 -0
- package/src/__tests__/route-parser.test.ts +490 -0
- package/src/__tests__/rules.test.ts +234 -0
- package/src/__tests__/runtime-surface-accounting.test.ts +120 -0
- package/src/__tests__/scheduled.test.ts +80 -0
- package/src/__tests__/schema.test.ts +1273 -0
- package/src/__tests__/security-hardening.test.ts +312 -0
- package/src/__tests__/server.unit.test.ts +333 -0
- package/src/__tests__/service-key-db-proxy.test.ts +650 -0
- package/src/__tests__/service-key-provider-bypass.test.ts +138 -0
- package/src/__tests__/service-key.test.ts +757 -0
- package/src/__tests__/smoke-skip-report.test.ts +72 -0
- package/src/__tests__/sms-provider.test.ts +39 -0
- package/src/__tests__/sql-route.test.ts +218 -0
- package/src/__tests__/storage-hook-context.test.ts +115 -0
- package/src/__tests__/totp.test.ts +200 -0
- package/src/__tests__/uuid.test.ts +144 -0
- package/src/__tests__/validation.test.ts +773 -0
- package/src/__tests__/websocket-pending.test.ts +163 -0
- package/src/_functions-registry.ts +51 -0
- package/src/bench-entry.ts +9 -0
- package/src/cloudflare-test.d.ts +1 -0
- package/src/durable-objects/auth-do.ts +49 -0
- package/src/durable-objects/database-do.ts +2240 -0
- package/src/durable-objects/database-live-do.ts +949 -0
- package/src/durable-objects/logs-do.ts +1200 -0
- package/src/durable-objects/room-runtime-base.ts +1604 -0
- package/src/durable-objects/rooms-do.ts +2191 -0
- package/src/generated-config.ts +6 -0
- package/src/index.ts +382 -0
- package/src/lib/admin-assets.ts +54 -0
- package/src/lib/admin-db-target.ts +301 -0
- package/src/lib/admin-routing.ts +35 -0
- package/src/lib/admin-user-management.ts +464 -0
- package/src/lib/analytics-adapter.ts +103 -0
- package/src/lib/analytics-query.ts +579 -0
- package/src/lib/auth-d1-service.ts +1193 -0
- package/src/lib/auth-d1.ts +1056 -0
- package/src/lib/auth-db-adapter.ts +289 -0
- package/src/lib/auth-redirect.ts +116 -0
- package/src/lib/cidr.ts +115 -0
- package/src/lib/client-ip.ts +51 -0
- package/src/lib/cloudflare-realtime.ts +251 -0
- package/src/lib/control-db.ts +36 -0
- package/src/lib/cron.ts +163 -0
- package/src/lib/d1-handler.ts +1425 -0
- package/src/lib/d1-schema-init.ts +255 -0
- package/src/lib/d1-sql.ts +33 -0
- package/src/lib/database-live-config.ts +24 -0
- package/src/lib/database-live-emitter.ts +111 -0
- package/src/lib/db-sql.ts +66 -0
- package/src/lib/do-retry.ts +36 -0
- package/src/lib/do-router.ts +270 -0
- package/src/lib/do-sql.ts +73 -0
- package/src/lib/email-provider.ts +379 -0
- package/src/lib/email-templates.ts +285 -0
- package/src/lib/email-translations.ts +422 -0
- package/src/lib/errors.ts +151 -0
- package/src/lib/functions.ts +2091 -0
- package/src/lib/hono.ts +56 -0
- package/src/lib/internal-request.ts +56 -0
- package/src/lib/jwt.ts +354 -0
- package/src/lib/log-writer.ts +272 -0
- package/src/lib/namespace-dump.ts +125 -0
- package/src/lib/oauth-providers.ts +1225 -0
- package/src/lib/op-parser.ts +99 -0
- package/src/lib/openapi.ts +146 -0
- package/src/lib/pagination.ts +19 -0
- package/src/lib/password-policy.ts +102 -0
- package/src/lib/password.ts +145 -0
- package/src/lib/plugin-migrations.ts +612 -0
- package/src/lib/postgres-executor.ts +203 -0
- package/src/lib/postgres-handler.ts +1102 -0
- package/src/lib/postgres-schema-init.ts +341 -0
- package/src/lib/postgres-table-utils.ts +87 -0
- package/src/lib/public-user-profile.ts +187 -0
- package/src/lib/push-provider.ts +409 -0
- package/src/lib/push-token.ts +294 -0
- package/src/lib/query-engine.ts +768 -0
- package/src/lib/room-monitoring.ts +97 -0
- package/src/lib/room-runtime.ts +14 -0
- package/src/lib/route-parser.ts +434 -0
- package/src/lib/schema.ts +538 -0
- package/src/lib/schemas.ts +152 -0
- package/src/lib/service-key.ts +419 -0
- package/src/lib/sms-provider.ts +230 -0
- package/src/lib/startup-config.ts +99 -0
- package/src/lib/totp.ts +242 -0
- package/src/lib/uuid.ts +87 -0
- package/src/lib/validation.ts +205 -0
- package/src/lib/version.ts +2 -0
- package/src/lib/websocket-pending.ts +40 -0
- package/src/middleware/auth.ts +169 -0
- package/src/middleware/captcha-verify.ts +217 -0
- package/src/middleware/cors.ts +159 -0
- package/src/middleware/error-handler.ts +54 -0
- package/src/middleware/internal-guard.ts +26 -0
- package/src/middleware/logger.ts +126 -0
- package/src/middleware/rate-limit.ts +283 -0
- package/src/middleware/rules.ts +475 -0
- package/src/routes/admin-auth.ts +447 -0
- package/src/routes/admin.ts +3501 -0
- package/src/routes/analytics-api.ts +290 -0
- package/src/routes/auth.ts +4222 -0
- package/src/routes/backup.ts +1466 -0
- package/src/routes/config.ts +53 -0
- package/src/routes/d1.ts +109 -0
- package/src/routes/database-live.ts +281 -0
- package/src/routes/functions.ts +155 -0
- package/src/routes/health.ts +32 -0
- package/src/routes/kv.ts +167 -0
- package/src/routes/oauth.ts +1055 -0
- package/src/routes/push.ts +1465 -0
- package/src/routes/room.ts +639 -0
- package/src/routes/schema-endpoint.ts +76 -0
- package/src/routes/sql.ts +176 -0
- package/src/routes/storage.ts +1674 -0
- package/src/routes/tables.ts +699 -0
- package/src/routes/users.ts +21 -0
- package/src/routes/vectorize.ts +372 -0
- package/src/types.ts +99 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 서버 단위 테스트 — PostgreSQL dialect (query engine + executor)
|
|
3
|
+
*
|
|
4
|
+
* 실행: cd packages/server && npx vitest run src/__tests__/postgres-dialect.test.ts
|
|
5
|
+
*
|
|
6
|
+
* 테스트 대상:
|
|
7
|
+
* A. Query engine — PostgreSQL dialect ($1,$2... bind params)
|
|
8
|
+
* B. Query engine — PostgreSQL contains (ILIKE)
|
|
9
|
+
* C. Query engine — PostgreSQL IN operator
|
|
10
|
+
* D. Query engine — PostgreSQL OR filters
|
|
11
|
+
* E. Query engine — PostgreSQL search (ILIKE across columns, no FTS5)
|
|
12
|
+
* F. Query engine — PostgreSQL countSql consistency
|
|
13
|
+
* G. Default dialect behavior (SQLite fallback)
|
|
14
|
+
* H. executePostgresQuery — export validation
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { describe, it, expect } from 'vitest';
|
|
18
|
+
import {
|
|
19
|
+
buildListQuery,
|
|
20
|
+
buildCountQuery,
|
|
21
|
+
buildGetQuery,
|
|
22
|
+
buildSearchQuery,
|
|
23
|
+
} from '../lib/query-engine.js';
|
|
24
|
+
import { executePostgresQuery } from '../lib/postgres-executor.js';
|
|
25
|
+
|
|
26
|
+
// ─── A. PostgreSQL dialect — bind params ($1, $2, ...) ────────────────────────
|
|
27
|
+
|
|
28
|
+
describe('PostgreSQL dialect — bind params', () => {
|
|
29
|
+
it('buildGetQuery uses $1 placeholder', () => {
|
|
30
|
+
const { sql, params } = buildGetQuery('products', 'p-1', undefined, 'postgres');
|
|
31
|
+
expect(sql).toContain('"id" = $1');
|
|
32
|
+
expect(sql).not.toContain('?');
|
|
33
|
+
expect(params).toEqual(['p-1']);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('buildGetQuery with fields uses $1', () => {
|
|
37
|
+
const { sql } = buildGetQuery('products', 'p-1', ['id', 'name'], 'postgres');
|
|
38
|
+
expect(sql).toContain('"id", "name"');
|
|
39
|
+
expect(sql).toContain('$1');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('buildListQuery no filters → default limit uses $1', () => {
|
|
43
|
+
const { sql, params } = buildListQuery('products', {}, 'postgres');
|
|
44
|
+
expect(sql).toContain('LIMIT $1');
|
|
45
|
+
expect(sql).not.toContain('?');
|
|
46
|
+
expect(params).toEqual([20]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('buildListQuery with offset → $1 OFFSET $2', () => {
|
|
50
|
+
const { sql, params } = buildListQuery('products', {
|
|
51
|
+
pagination: { limit: 10, offset: 5 },
|
|
52
|
+
}, 'postgres');
|
|
53
|
+
expect(sql).toContain('LIMIT $1 OFFSET $2');
|
|
54
|
+
expect(params).toEqual([10, 5]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('buildListQuery with filter + limit → correct $N sequence', () => {
|
|
58
|
+
const { sql, params } = buildListQuery('products', {
|
|
59
|
+
filters: [['status', '==', 'active']],
|
|
60
|
+
pagination: { limit: 10, offset: 0 },
|
|
61
|
+
}, 'postgres');
|
|
62
|
+
expect(sql).toContain('"status" = $1');
|
|
63
|
+
expect(sql).toContain('LIMIT $2 OFFSET $3');
|
|
64
|
+
expect(params).toEqual(['active', 10, 0]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('buildListQuery with multiple filters → $1, $2, ...', () => {
|
|
68
|
+
const { sql, params } = buildListQuery('products', {
|
|
69
|
+
filters: [['status', '==', 'active'], ['price', '>', 100]],
|
|
70
|
+
}, 'postgres');
|
|
71
|
+
expect(sql).toContain('"status" = $1');
|
|
72
|
+
expect(sql).toContain('"price" > $2');
|
|
73
|
+
expect(sql).toContain('LIMIT $3');
|
|
74
|
+
expect(params).toEqual(['active', 100, 20]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('buildListQuery cursor after → $1 for cursor, $2 for limit', () => {
|
|
78
|
+
const { sql, params } = buildListQuery('products', {
|
|
79
|
+
pagination: { after: 'cursor-abc', limit: 5 },
|
|
80
|
+
}, 'postgres');
|
|
81
|
+
expect(sql).toContain('"id" > $1');
|
|
82
|
+
expect(sql).toContain('LIMIT $2');
|
|
83
|
+
expect(params).toEqual(['cursor-abc', 5]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('buildListQuery cursor before → $1 for cursor, $2 for limit', () => {
|
|
87
|
+
const { sql, params } = buildListQuery('products', {
|
|
88
|
+
pagination: { before: 'cursor-xyz', limit: 5 },
|
|
89
|
+
}, 'postgres');
|
|
90
|
+
expect(sql).toContain('"id" < $1');
|
|
91
|
+
expect(sql).toContain('LIMIT $2');
|
|
92
|
+
expect(params).toEqual(['cursor-xyz', 5]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('buildCountQuery with filter → $1', () => {
|
|
96
|
+
const { sql, params } = buildCountQuery('products', [['status', '==', 'active']], undefined, 'postgres');
|
|
97
|
+
expect(sql).toContain('"status" = $1');
|
|
98
|
+
expect(sql).not.toContain('?');
|
|
99
|
+
expect(params).toEqual(['active']);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('buildCountQuery no filters → no placeholders', () => {
|
|
103
|
+
const { sql, params } = buildCountQuery('products', undefined, undefined, 'postgres');
|
|
104
|
+
expect(sql).toBe('SELECT COUNT(*) as total FROM "products"');
|
|
105
|
+
expect(params).toEqual([]);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ─── B. PostgreSQL dialect — contains operator (ILIKE) ────────────────────────
|
|
110
|
+
|
|
111
|
+
describe('PostgreSQL dialect — contains operator', () => {
|
|
112
|
+
it('contains uses ILIKE instead of INSTR', () => {
|
|
113
|
+
const { sql, params } = buildListQuery('products', {
|
|
114
|
+
filters: [['name', 'contains', 'widget']],
|
|
115
|
+
}, 'postgres');
|
|
116
|
+
expect(sql).toContain("ILIKE '%' || $1 || '%'");
|
|
117
|
+
expect(sql).not.toContain('INSTR');
|
|
118
|
+
expect(params).toContain('widget');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('SQLite contains still uses INSTR (default dialect)', () => {
|
|
122
|
+
const { sql } = buildListQuery('products', {
|
|
123
|
+
filters: [['name', 'contains', 'widget']],
|
|
124
|
+
});
|
|
125
|
+
expect(sql).toContain('INSTR("name", ?) > 0');
|
|
126
|
+
expect(sql).not.toContain('ILIKE');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ─── C. PostgreSQL dialect — IN operator ─────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
describe('PostgreSQL dialect — IN operator', () => {
|
|
133
|
+
it('in → IN ($1, $2, $3)', () => {
|
|
134
|
+
const { sql, params } = buildListQuery('products', {
|
|
135
|
+
filters: [['status', 'in', ['a', 'b', 'c']]],
|
|
136
|
+
}, 'postgres');
|
|
137
|
+
expect(sql).toContain('IN ($1, $2, $3)');
|
|
138
|
+
expect(params).toContain('a');
|
|
139
|
+
expect(params).toContain('b');
|
|
140
|
+
expect(params).toContain('c');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('not in → NOT IN ($1, $2)', () => {
|
|
144
|
+
const { sql } = buildListQuery('products', {
|
|
145
|
+
filters: [['status', 'not in', ['deleted', 'spam']]],
|
|
146
|
+
}, 'postgres');
|
|
147
|
+
expect(sql).toContain('NOT IN ($1, $2)');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// ─── D. PostgreSQL dialect — OR filters ───────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
describe('PostgreSQL dialect — OR filters', () => {
|
|
154
|
+
it('OR filters use $N placeholders', () => {
|
|
155
|
+
const { sql, params } = buildListQuery('products', {
|
|
156
|
+
orFilters: [['a', '==', 'v1'], ['b', '==', 'v2']],
|
|
157
|
+
}, 'postgres');
|
|
158
|
+
expect(sql).toContain('$1');
|
|
159
|
+
expect(sql).toContain('$2');
|
|
160
|
+
expect(sql).toContain('OR');
|
|
161
|
+
expect(sql).not.toContain('?');
|
|
162
|
+
expect(params).toContain('v1');
|
|
163
|
+
expect(params).toContain('v2');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('AND + OR combined with $N', () => {
|
|
167
|
+
const { sql, params } = buildListQuery('products', {
|
|
168
|
+
filters: [['category', '==', 'tech']],
|
|
169
|
+
orFilters: [['status', '==', 'draft'], ['status', '==', 'pub']],
|
|
170
|
+
}, 'postgres');
|
|
171
|
+
expect(sql).toContain('"category" = $1');
|
|
172
|
+
expect(sql).toContain('$2');
|
|
173
|
+
expect(sql).toContain('$3');
|
|
174
|
+
expect(sql).toContain('AND');
|
|
175
|
+
expect(sql).toContain('OR');
|
|
176
|
+
expect(params).toContain('tech');
|
|
177
|
+
expect(params).toContain('draft');
|
|
178
|
+
expect(params).toContain('pub');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ─── E. PostgreSQL dialect — search (ILIKE, no FTS5) ──────────────────────────
|
|
183
|
+
|
|
184
|
+
describe('PostgreSQL dialect — search', () => {
|
|
185
|
+
it('buildListQuery search serializes the row instead of casting the table name', () => {
|
|
186
|
+
const { sql, params } = buildListQuery('products', {
|
|
187
|
+
search: 'widget',
|
|
188
|
+
filters: [['status', '==', 'active']],
|
|
189
|
+
pagination: { limit: 10 },
|
|
190
|
+
}, 'postgres');
|
|
191
|
+
expect(sql).toContain('to_jsonb("products")::text ILIKE');
|
|
192
|
+
expect(sql).toContain('"status" = $1');
|
|
193
|
+
expect(sql).toContain('LIMIT $3 OFFSET $4');
|
|
194
|
+
expect(params).toEqual(['active', 'widget', 10, 0]);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('uses ILIKE instead of FTS5 MATCH', () => {
|
|
198
|
+
const { sql, params } = buildSearchQuery('products', 'widget', {
|
|
199
|
+
ftsFields: ['name', 'description'],
|
|
200
|
+
}, 'postgres');
|
|
201
|
+
expect(sql).toContain('ILIKE');
|
|
202
|
+
expect(sql).not.toContain('MATCH');
|
|
203
|
+
expect(sql).not.toContain('_fts');
|
|
204
|
+
expect(sql).not.toContain('highlight(');
|
|
205
|
+
expect(params).toContain('widget');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('search with no ftsFields uses id as default search field', () => {
|
|
209
|
+
const { sql } = buildSearchQuery('products', 'query', undefined, 'postgres');
|
|
210
|
+
expect(sql).toContain('"id"::text ILIKE');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('search ILIKE produces OR conditions for multiple fields', () => {
|
|
214
|
+
const { sql } = buildSearchQuery('products', 'q', {
|
|
215
|
+
ftsFields: ['name', 'description', 'sku'],
|
|
216
|
+
}, 'postgres');
|
|
217
|
+
expect(sql).toContain('OR');
|
|
218
|
+
expect(sql).toContain('"name"::text ILIKE');
|
|
219
|
+
expect(sql).toContain('"description"::text ILIKE');
|
|
220
|
+
expect(sql).toContain('"sku"::text ILIKE');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('search params: one per field + limit + offset', () => {
|
|
224
|
+
const { params } = buildSearchQuery('products', 'q', {
|
|
225
|
+
ftsFields: ['name', 'description'],
|
|
226
|
+
limit: 10,
|
|
227
|
+
offset: 5,
|
|
228
|
+
}, 'postgres');
|
|
229
|
+
// 2 fields + limit + offset
|
|
230
|
+
expect(params).toEqual(['q', 'q', 10, 5]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('search uses $N placeholders', () => {
|
|
234
|
+
const { sql } = buildSearchQuery('products', 'q', {
|
|
235
|
+
ftsFields: ['name'],
|
|
236
|
+
limit: 10,
|
|
237
|
+
offset: 0,
|
|
238
|
+
}, 'postgres');
|
|
239
|
+
expect(sql).toContain('$1');
|
|
240
|
+
expect(sql).toContain('$2');
|
|
241
|
+
expect(sql).toContain('$3');
|
|
242
|
+
expect(sql).not.toContain('?');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('SQLite search still uses FTS5 (default dialect)', () => {
|
|
246
|
+
const { sql } = buildSearchQuery('products', 'widget');
|
|
247
|
+
expect(sql).toContain('MATCH ?');
|
|
248
|
+
expect(sql).toContain('_fts');
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// ─── F. PostgreSQL dialect — countSql consistency ─────────────────────────────
|
|
253
|
+
|
|
254
|
+
describe('PostgreSQL dialect — countSql', () => {
|
|
255
|
+
it('countSql uses independent $N sequence', () => {
|
|
256
|
+
const { countSql, countParams } = buildListQuery('products', {
|
|
257
|
+
filters: [['status', '==', 'active']],
|
|
258
|
+
}, 'postgres');
|
|
259
|
+
// countSql gets its own BindTracker, so starts at $1
|
|
260
|
+
expect(countSql).toContain('"status" = $1');
|
|
261
|
+
expect(countParams).toEqual(['active']);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('no countSql for cursor pagination (postgres)', () => {
|
|
265
|
+
const { countSql } = buildListQuery('products', {
|
|
266
|
+
pagination: { after: 'c1' },
|
|
267
|
+
}, 'postgres');
|
|
268
|
+
expect(countSql).toBeUndefined();
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('countSql no filters → no WHERE (postgres)', () => {
|
|
272
|
+
const { countSql } = buildListQuery('products', {}, 'postgres');
|
|
273
|
+
expect(countSql).not.toContain('WHERE');
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('countSql includes postgres search predicates', () => {
|
|
277
|
+
const { countSql, countParams } = buildListQuery('products', {
|
|
278
|
+
filters: [['status', '==', 'active']],
|
|
279
|
+
search: 'widget',
|
|
280
|
+
}, 'postgres');
|
|
281
|
+
expect(countSql).toContain('"status" = $1');
|
|
282
|
+
expect(countSql).toContain('to_jsonb("products")::text ILIKE');
|
|
283
|
+
expect(countParams).toEqual(['active', 'widget']);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// ─── G. Default dialect behavior ───────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
describe('default dialect behavior', () => {
|
|
290
|
+
it('buildGetQuery default → sqlite (? placeholder)', () => {
|
|
291
|
+
const { sql } = buildGetQuery('t', 'id-1');
|
|
292
|
+
expect(sql).toContain('?');
|
|
293
|
+
expect(sql).not.toContain('$');
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it('buildListQuery default → sqlite', () => {
|
|
297
|
+
const { sql } = buildListQuery('t', {});
|
|
298
|
+
expect(sql).toContain('?');
|
|
299
|
+
expect(sql).not.toContain('$');
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('buildCountQuery default → sqlite', () => {
|
|
303
|
+
const { sql } = buildCountQuery('t', [['a', '==', 1]]);
|
|
304
|
+
expect(sql).toContain('?');
|
|
305
|
+
expect(sql).not.toContain('$');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('buildSearchQuery default → sqlite (FTS5)', () => {
|
|
309
|
+
const { sql } = buildSearchQuery('t', 'q');
|
|
310
|
+
expect(sql).toContain('MATCH ?');
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// ─── H. executePostgresQuery — export validation ─────────────────────────────
|
|
315
|
+
|
|
316
|
+
describe('executePostgresQuery', () => {
|
|
317
|
+
it('is a function', () => {
|
|
318
|
+
expect(typeof executePostgresQuery).toBe('function');
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('rejects invalid connection string', async () => {
|
|
322
|
+
// pg Client connect will fail against an unreachable host.
|
|
323
|
+
// Use connect_timeout=1 to fail quickly instead of default timeout.
|
|
324
|
+
await expect(
|
|
325
|
+
executePostgresQuery('postgres://user:pass@127.0.0.1:1/db?connect_timeout=1', 'SELECT 1', []),
|
|
326
|
+
).rejects.toThrow();
|
|
327
|
+
}, 10_000);
|
|
328
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
function mockPgClient() {
|
|
4
|
+
const connect = vi.fn().mockResolvedValue(undefined);
|
|
5
|
+
const query = vi.fn();
|
|
6
|
+
const end = vi.fn().mockResolvedValue(undefined);
|
|
7
|
+
const Client = vi.fn(() => ({
|
|
8
|
+
connect,
|
|
9
|
+
query,
|
|
10
|
+
end,
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.doMock('pg', () => ({ Client }));
|
|
14
|
+
|
|
15
|
+
return { Client, connect, query, end };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('postgres executor helpers', () => {
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
vi.resetModules();
|
|
21
|
+
vi.clearAllMocks();
|
|
22
|
+
vi.doUnmock('pg');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('executes a query and normalizes result metadata', async () => {
|
|
26
|
+
const pg = mockPgClient();
|
|
27
|
+
pg.query.mockResolvedValue({
|
|
28
|
+
fields: [{ name: 'id' }, { name: 'title' }],
|
|
29
|
+
rows: [{ id: 'post-1', title: 'Hello' }],
|
|
30
|
+
rowCount: 1,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const { executePostgresQuery } = await import('../lib/postgres-executor.js');
|
|
34
|
+
const result = await executePostgresQuery('postgres://edgebase:test@localhost/db', 'SELECT * FROM posts');
|
|
35
|
+
|
|
36
|
+
expect(pg.Client).toHaveBeenCalledWith({ connectionString: 'postgres://edgebase:test@localhost/db' });
|
|
37
|
+
expect(pg.connect).toHaveBeenCalledTimes(1);
|
|
38
|
+
expect(pg.query).toHaveBeenCalledWith('SELECT * FROM posts', []);
|
|
39
|
+
expect(pg.end).toHaveBeenCalledTimes(1);
|
|
40
|
+
expect(result).toEqual({
|
|
41
|
+
columns: ['id', 'title'],
|
|
42
|
+
rows: [{ id: 'post-1', title: 'Hello' }],
|
|
43
|
+
rowCount: 1,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('reuses a single client within withPostgresConnection and exposes a query helper', async () => {
|
|
48
|
+
const pg = mockPgClient();
|
|
49
|
+
pg.query.mockResolvedValue({
|
|
50
|
+
fields: [{ name: 'id' }],
|
|
51
|
+
rows: [{ id: 'row-1' }],
|
|
52
|
+
rowCount: 1,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const {
|
|
56
|
+
_resetPostgresPoolCache,
|
|
57
|
+
getProviderBindingName,
|
|
58
|
+
withPostgresConnection,
|
|
59
|
+
} = await import('../lib/postgres-executor.js');
|
|
60
|
+
|
|
61
|
+
await expect(_resetPostgresPoolCache()).resolves.toBeUndefined();
|
|
62
|
+
expect(getProviderBindingName('tenant-app')).toBe('DB_POSTGRES_TENANT_APP');
|
|
63
|
+
|
|
64
|
+
const result = await withPostgresConnection(
|
|
65
|
+
'postgres://edgebase:test@localhost/db',
|
|
66
|
+
async (query) => query('SELECT * FROM posts WHERE id = $1', ['row-1']),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(pg.Client).toHaveBeenCalledWith({ connectionString: 'postgres://edgebase:test@localhost/db' });
|
|
70
|
+
expect(pg.connect).toHaveBeenCalledTimes(1);
|
|
71
|
+
expect(pg.query).toHaveBeenCalledWith('SELECT * FROM posts WHERE id = $1', ['row-1']);
|
|
72
|
+
expect(pg.end).toHaveBeenCalledTimes(1);
|
|
73
|
+
expect(result).toEqual({
|
|
74
|
+
columns: ['id'],
|
|
75
|
+
rows: [{ id: 'row-1' }],
|
|
76
|
+
rowCount: 1,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { defineConfig } from '@edge-base/shared';
|
|
3
|
+
import { buildInternalHandlerContext } from '../lib/internal-request.js';
|
|
4
|
+
import type { Env } from '../types.js';
|
|
5
|
+
|
|
6
|
+
describe('postgres field operator compatibility', () => {
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
vi.resetModules();
|
|
9
|
+
vi.clearAllMocks();
|
|
10
|
+
vi.unstubAllGlobals();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it.each(['PATCH', 'PUT'])('translates %s field operators into PostgreSQL update SQL', async (method) => {
|
|
14
|
+
const executePostgresQuery = vi.fn()
|
|
15
|
+
.mockResolvedValueOnce({
|
|
16
|
+
rows: [{
|
|
17
|
+
id: 'post-1',
|
|
18
|
+
viewCount: 1,
|
|
19
|
+
avatar: 'old.png',
|
|
20
|
+
}],
|
|
21
|
+
rowCount: 1,
|
|
22
|
+
})
|
|
23
|
+
.mockResolvedValueOnce({
|
|
24
|
+
rows: [{
|
|
25
|
+
id: 'post-1',
|
|
26
|
+
viewCount: 2,
|
|
27
|
+
avatar: null,
|
|
28
|
+
}],
|
|
29
|
+
rowCount: 1,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
vi.doMock('../lib/postgres-executor.js', () => ({
|
|
33
|
+
executePostgresQuery,
|
|
34
|
+
ensureLocalDevPostgresSchema: vi.fn().mockResolvedValue(undefined),
|
|
35
|
+
withPostgresConnection: vi.fn(async (_connectionString, fn) =>
|
|
36
|
+
fn((sql: string, params: unknown[] = []) => executePostgresQuery(_connectionString, sql, params))),
|
|
37
|
+
getLocalDevPostgresExecOptions: vi.fn(() => undefined),
|
|
38
|
+
getProviderBindingName: () => 'DB_SHARED',
|
|
39
|
+
}));
|
|
40
|
+
vi.doMock('../lib/postgres-schema-init.js', () => ({
|
|
41
|
+
ensurePgSchema: vi.fn().mockResolvedValue(undefined),
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
const { handlePgRequest } = await import('../lib/postgres-handler.js');
|
|
45
|
+
|
|
46
|
+
const env = {
|
|
47
|
+
EDGEBASE_CONFIG: defineConfig({
|
|
48
|
+
release: true,
|
|
49
|
+
databases: {
|
|
50
|
+
shared: {
|
|
51
|
+
provider: 'postgres',
|
|
52
|
+
connectionString: 'DB_POSTGRES_SHARED_URL',
|
|
53
|
+
tables: {
|
|
54
|
+
posts: {
|
|
55
|
+
schema: {
|
|
56
|
+
title: { type: 'string' },
|
|
57
|
+
viewCount: { type: 'number' },
|
|
58
|
+
avatar: { type: 'string' },
|
|
59
|
+
},
|
|
60
|
+
access: {
|
|
61
|
+
update: () => true,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
DB_POSTGRES_SHARED_URL: 'postgres://edgebase:test@localhost/shared',
|
|
69
|
+
} as unknown as Env;
|
|
70
|
+
|
|
71
|
+
const request = new Request(
|
|
72
|
+
'http://internal/api/db/shared/tables/posts/post-1',
|
|
73
|
+
{
|
|
74
|
+
method,
|
|
75
|
+
headers: {
|
|
76
|
+
'Content-Type': 'application/json',
|
|
77
|
+
'X-Is-Service-Key': 'true',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const ctx = buildInternalHandlerContext({
|
|
83
|
+
env,
|
|
84
|
+
request,
|
|
85
|
+
body: {
|
|
86
|
+
viewCount: { $op: 'increment', value: 1 },
|
|
87
|
+
avatar: { $op: 'deleteField' },
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const response = await handlePgRequest(ctx, 'shared', 'posts', '/tables/posts/post-1');
|
|
92
|
+
expect(response.status).toBe(200);
|
|
93
|
+
expect(executePostgresQuery).toHaveBeenCalledTimes(2);
|
|
94
|
+
|
|
95
|
+
const updateSql = executePostgresQuery.mock.calls[1][1] as string;
|
|
96
|
+
const updateParams = executePostgresQuery.mock.calls[1][2] as unknown[];
|
|
97
|
+
|
|
98
|
+
expect(updateSql).toContain('"viewCount" = COALESCE("viewCount", 0) + $1');
|
|
99
|
+
expect(updateSql).toContain('"avatar" = NULL');
|
|
100
|
+
expect(updateSql).toContain('"updatedAt" = $2');
|
|
101
|
+
expect(updateSql).toContain('WHERE "id" = $3');
|
|
102
|
+
expect(updateParams[0]).toBe(1);
|
|
103
|
+
expect(typeof updateParams[1]).toBe('string');
|
|
104
|
+
expect(updateParams[2]).toBe('post-1');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('translates batch-by-filter update field operators into PostgreSQL update SQL', async () => {
|
|
108
|
+
const executePostgresQuery = vi.fn()
|
|
109
|
+
.mockResolvedValueOnce({
|
|
110
|
+
rows: [{ id: 'post-1' }],
|
|
111
|
+
rowCount: 1,
|
|
112
|
+
})
|
|
113
|
+
.mockResolvedValueOnce({
|
|
114
|
+
rows: [{
|
|
115
|
+
id: 'post-1',
|
|
116
|
+
viewCount: 3,
|
|
117
|
+
avatar: null,
|
|
118
|
+
}],
|
|
119
|
+
rowCount: 1,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
vi.doMock('../lib/postgres-executor.js', () => ({
|
|
123
|
+
executePostgresQuery,
|
|
124
|
+
ensureLocalDevPostgresSchema: vi.fn().mockResolvedValue(undefined),
|
|
125
|
+
withPostgresConnection: vi.fn(async (_connectionString, fn) =>
|
|
126
|
+
fn((sql: string, params: unknown[] = []) => executePostgresQuery(_connectionString, sql, params))),
|
|
127
|
+
getLocalDevPostgresExecOptions: vi.fn(() => undefined),
|
|
128
|
+
getProviderBindingName: () => 'DB_SHARED',
|
|
129
|
+
}));
|
|
130
|
+
vi.doMock('../lib/postgres-schema-init.js', () => ({
|
|
131
|
+
ensurePgSchema: vi.fn().mockResolvedValue(undefined),
|
|
132
|
+
}));
|
|
133
|
+
|
|
134
|
+
const { handlePgRequest } = await import('../lib/postgres-handler.js');
|
|
135
|
+
|
|
136
|
+
const env = {
|
|
137
|
+
EDGEBASE_CONFIG: defineConfig({
|
|
138
|
+
release: true,
|
|
139
|
+
databases: {
|
|
140
|
+
shared: {
|
|
141
|
+
provider: 'postgres',
|
|
142
|
+
connectionString: 'DB_POSTGRES_SHARED_URL',
|
|
143
|
+
tables: {
|
|
144
|
+
posts: {
|
|
145
|
+
schema: {
|
|
146
|
+
viewCount: { type: 'number' },
|
|
147
|
+
avatar: { type: 'string' },
|
|
148
|
+
},
|
|
149
|
+
access: {
|
|
150
|
+
update: () => true,
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
DB_POSTGRES_SHARED_URL: 'postgres://edgebase:test@localhost/shared',
|
|
158
|
+
} as unknown as Env;
|
|
159
|
+
|
|
160
|
+
const request = new Request(
|
|
161
|
+
'http://internal/api/db/shared/tables/posts/batch-by-filter',
|
|
162
|
+
{
|
|
163
|
+
method: 'POST',
|
|
164
|
+
headers: {
|
|
165
|
+
'Content-Type': 'application/json',
|
|
166
|
+
'X-Is-Service-Key': 'true',
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const waitUntil = vi.fn();
|
|
172
|
+
const executionCtx = { waitUntil } as unknown as ExecutionContext;
|
|
173
|
+
const ctx = buildInternalHandlerContext({
|
|
174
|
+
env,
|
|
175
|
+
request,
|
|
176
|
+
executionCtx,
|
|
177
|
+
body: {
|
|
178
|
+
action: 'update',
|
|
179
|
+
filter: [['id', '==', 'post-1']],
|
|
180
|
+
update: {
|
|
181
|
+
viewCount: { $op: 'increment', value: 2 },
|
|
182
|
+
avatar: { $op: 'deleteField' },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const response = await handlePgRequest(ctx, 'shared', 'posts', '/tables/posts/batch-by-filter');
|
|
188
|
+
const json = await response.json() as {
|
|
189
|
+
processed: number;
|
|
190
|
+
succeeded: number;
|
|
191
|
+
updated: number;
|
|
192
|
+
items: Array<Record<string, unknown>>;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
expect(response.status).toBe(200);
|
|
196
|
+
expect(json).toMatchObject({
|
|
197
|
+
processed: 1,
|
|
198
|
+
succeeded: 1,
|
|
199
|
+
updated: 1,
|
|
200
|
+
});
|
|
201
|
+
expect(executePostgresQuery).toHaveBeenCalledTimes(2);
|
|
202
|
+
|
|
203
|
+
const selectSql = executePostgresQuery.mock.calls[0][1] as string;
|
|
204
|
+
const selectParams = executePostgresQuery.mock.calls[0][2] as unknown[];
|
|
205
|
+
expect(selectSql).toContain('SELECT "posts"."id" FROM "posts"');
|
|
206
|
+
expect(selectSql).toContain('WHERE "id" = $1');
|
|
207
|
+
expect(selectSql).toContain('LIMIT $2');
|
|
208
|
+
expect(selectSql).toContain('OFFSET $3');
|
|
209
|
+
expect(selectParams).toEqual(['post-1', 500, 0]);
|
|
210
|
+
|
|
211
|
+
const updateSql = executePostgresQuery.mock.calls[1][1] as string;
|
|
212
|
+
const updateParams = executePostgresQuery.mock.calls[1][2] as unknown[];
|
|
213
|
+
|
|
214
|
+
expect(updateSql).toContain('SET "viewCount" = COALESCE("viewCount", 0) + $2, "avatar" = NULL');
|
|
215
|
+
expect(updateSql).toContain('"updatedAt" = $3');
|
|
216
|
+
expect(updateSql).toContain('WHERE "id" IN ($1)');
|
|
217
|
+
expect(updateParams[0]).toBe('post-1');
|
|
218
|
+
expect(updateParams[1]).toBe(2);
|
|
219
|
+
expect(typeof updateParams[2]).toBe('string');
|
|
220
|
+
expect(waitUntil).toHaveBeenCalledTimes(1);
|
|
221
|
+
});
|
|
222
|
+
});
|