@h-ai/cli 0.1.0-alpha9 → 1.0.0-beta.2
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/README.en.md +229 -0
- package/README.ja.md +229 -0
- package/README.md +175 -113
- package/dist/chunk-AKV2CQZV.js +1497 -0
- package/dist/chunk-AKV2CQZV.js.map +1 -0
- package/dist/chunk-EF7WOFWZ.js +7557 -0
- package/dist/chunk-EF7WOFWZ.js.map +1 -0
- package/dist/chunk-MUHRBJSW.js +29272 -0
- package/dist/chunk-MUHRBJSW.js.map +1 -0
- package/dist/chunk-ZSDEVXZ7.js +38 -0
- package/dist/{chunk-2H7UOFLK.js.map → chunk-ZSDEVXZ7.js.map} +1 -1
- package/dist/cli-config-templates-HHFHNXBN.js +5 -0
- package/dist/cli-config-templates-HHFHNXBN.js.map +1 -0
- package/dist/cli-skill-templates-PN4FLGBC.js +5 -0
- package/dist/cli-skill-templates-PN4FLGBC.js.map +1 -0
- package/dist/index.js +4444 -554
- package/dist/index.js.map +1 -1
- package/dist/metafile-esm.json +1 -0
- package/dist/src-TEBHR5I5.js +5 -0
- package/dist/src-TEBHR5I5.js.map +1 -0
- package/package.json +35 -15
- package/templates/README.md +56 -114
- package/templates/apps/admin/messages/en-US.json +7 -0
- package/templates/apps/admin/messages/ja-JP.json +40 -0
- package/templates/apps/admin/messages/zh-CN.json +7 -0
- package/templates/apps/admin/src/routes/+layout.svelte.hbs +65 -8
- package/templates/apps/admin/src/routes/+page.svelte.hbs +35 -14
- package/templates/apps/admin/src/routes/admin/+layout.svelte.hbs +54 -22
- package/templates/apps/admin/src/routes/admin/+page.svelte.hbs +33 -24
- package/templates/apps/admin/src/routes/admin/settings/+page.svelte.hbs +16 -13
- package/templates/apps/api/.dockerignore.hbs +25 -0
- package/templates/apps/api/.gitignore.hbs +22 -0
- package/templates/apps/api/.npmrc.hbs +1 -0
- package/templates/apps/api/apps/{{projectName}}-contract/README.md.hbs +39 -0
- package/templates/apps/api/apps/{{projectName}}-contract/package.json.hbs +41 -0
- package/templates/apps/api/apps/{{projectName}}-contract/src/app-contract.ts.hbs +27 -0
- package/templates/apps/api/apps/{{projectName}}-contract/src/app-schemas.ts.hbs +28 -0
- package/templates/apps/api/apps/{{projectName}}-contract/src/index.ts.hbs +5 -0
- package/templates/apps/api/apps/{{projectName}}-contract/src/{{projectName}}-contract.ts.hbs +16 -0
- package/templates/apps/api/apps/{{projectName}}-contract/tests/contract.test.ts.hbs +18 -0
- package/templates/apps/api/apps/{{projectName}}-contract/tsconfig.json.hbs +12 -0
- package/templates/apps/api/apps/{{projectName}}-contract/tsup.config.ts.hbs +11 -0
- package/templates/apps/api/apps/{{projectName}}-contract/vitest.config.ts.hbs +9 -0
- package/templates/apps/api/apps/{{projectName}}-serv/README.md.hbs +60 -0
- package/templates/apps/api/apps/{{projectName}}-serv/config/_core.yml.hbs +17 -0
- package/templates/apps/api/apps/{{projectName}}-serv/config/_serv.yml.hbs +54 -0
- package/templates/apps/api/apps/{{projectName}}-serv/data/.gitkeep.hbs +1 -0
- package/templates/apps/api/apps/{{projectName}}-serv/package.json.hbs +66 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/app.ts.hbs +119 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/build-metadata.ts.hbs +7 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/index.ts.hbs +41 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/lib/server/init.ts.hbs +165 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/server/procedures/app-procedures.ts.hbs +57 -0
- package/templates/apps/api/apps/{{projectName}}-serv/src/server/procedures/index.ts.hbs +41 -0
- package/templates/apps/api/apps/{{projectName}}-serv/tests/service.test.ts.hbs +176 -0
- package/templates/apps/api/apps/{{projectName}}-serv/tsconfig.json.hbs +16 -0
- package/templates/apps/api/apps/{{projectName}}-serv/tsup.config.ts.hbs +20 -0
- package/templates/apps/api/apps/{{projectName}}-serv/vitest.config.ts.hbs +12 -0
- package/templates/apps/api/e2e/api-serv.spec.ts.hbs +113 -0
- package/templates/apps/api/e2e/capture-evidence.ts.hbs +23 -0
- package/templates/apps/api/eslint.config.js.hbs +30 -0
- package/templates/apps/api/hai.json.hbs +4 -0
- package/templates/apps/api/images/serv.Dockerfile.hbs +37 -0
- package/templates/apps/api/package.json.hbs +53 -0
- package/templates/apps/api/playwright.config.ts.hbs +67 -0
- package/templates/apps/api/pnpm-workspace.yaml.hbs +40 -0
- package/templates/apps/api/tsconfig.base.json +14 -0
- package/templates/apps/fullstack/.dockerignore.hbs +25 -0
- package/templates/apps/fullstack/.gitignore.hbs +38 -0
- package/templates/apps/fullstack/.npmrc.hbs +1 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/.env.example.hbs +10 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/README.md.hbs +67 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/capacitor.config.ts.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/eslint.config.js.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/index.html.hbs +22 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/messages/en-US.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/messages/ja-JP.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/messages/zh-CN.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/package.json.hbs +86 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/project.inlang/settings.json.hbs +17 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/public/runtime-config.js.hbs +4 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/App.svelte.hbs +59 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/app.css.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/app.d.ts.hbs +8 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/build-metadata.ts.hbs +9 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/lib/capacitor.ts.hbs +19 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/src/main.ts.hbs +21 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/svelte.config.js.hbs +16 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/tsconfig.json.hbs +23 -0
- package/templates/apps/fullstack/apps/{{projectName}}-app/vite.config.ts.hbs +74 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/.env.example.hbs +6 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/README.md.hbs +64 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/eslint.config.js.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/index.html.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/messages/en-US.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/messages/ja-JP.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/messages/zh-CN.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/package.json.hbs +66 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/project.inlang/settings.json.hbs +17 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src/App.svelte.hbs +25 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src/app.css.hbs +18 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src/app.d.ts.hbs +8 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src/build-metadata.ts.hbs +9 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src/main.ts.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/Cargo.toml.hbs +33 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/build.rs.hbs +6 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/capabilities/default.json.hbs +16 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/icons/128x128.png +0 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/icons/128x128@2x.png +0 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/icons/32x32.png +0 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/icons/icon.icns +0 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/icons/icon.ico +0 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/src/lib.rs.hbs +91 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/src/main.rs.hbs +8 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/src-tauri/tauri.conf.json.hbs +46 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/svelte.config.js.hbs +14 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/tsconfig.json.hbs +22 -0
- package/templates/apps/fullstack/apps/{{projectName}}-desktop/vite.config.ts.hbs +89 -0
- package/templates/apps/fullstack/apps/{{projectName}}-miniapp/README.md.hbs +25 -0
- package/templates/apps/fullstack/apps/{{projectName}}-miniapp/messages/en-US.json.hbs +12 -0
- package/templates/apps/fullstack/apps/{{projectName}}-miniapp/messages/ja-JP.json.hbs +12 -0
- package/templates/apps/fullstack/apps/{{projectName}}-miniapp/messages/zh-CN.json.hbs +12 -0
- package/templates/apps/fullstack/apps/{{projectName}}-miniapp/project.inlang/settings.json.hbs +17 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/README.md.hbs +54 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/eslint.config.js.hbs +20 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/index.html.hbs +21 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/messages/en-US.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/messages/ja-JP.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/messages/zh-CN.json.hbs +7 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/package.json.hbs +53 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/project.inlang/settings.json.hbs +17 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/public/runtime-config.js.hbs +1 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/src/App.svelte.hbs +18 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/src/app.css.hbs +18 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/src/app.d.ts.hbs +8 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/src/build-metadata.ts.hbs +9 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/src/main.ts.hbs +14 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/svelte.config.js.hbs +16 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/tsconfig.json.hbs +23 -0
- package/templates/apps/fullstack/apps/{{projectName}}-web/vite.config.ts.hbs +76 -0
- package/templates/apps/fullstack/docs/security-configuration.md.hbs +349 -0
- package/templates/apps/fullstack/e2e/fullstack.spec.ts.hbs +988 -0
- package/templates/apps/fullstack/e2e/session-close.spec.ts.hbs +119 -0
- package/templates/apps/fullstack/eslint.config.js.hbs +41 -0
- package/templates/apps/fullstack/hai.json.hbs +11 -0
- package/templates/apps/fullstack/images/serv.Dockerfile.hbs +38 -0
- package/templates/apps/fullstack/images/web/19-runtime-config.envsh.hbs +5 -0
- package/templates/apps/fullstack/images/web/nginx.conf.hbs +24 -0
- package/templates/apps/fullstack/images/web/runtime-config.js.template.hbs +3 -0
- package/templates/apps/fullstack/images/web.Dockerfile.hbs +37 -0
- package/templates/apps/fullstack/package.json.hbs +61 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/README.md.hbs +40 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/package.json.hbs +30 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/admin-contract.ts.hbs +78 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/admin-schemas.ts.hbs +134 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/app-contract.ts.hbs +20 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/app-schemas.ts.hbs +35 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/index.ts.hbs +7 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/src/{{projectName}}-contract.ts.hbs +23 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/tests/contract.test.ts.hbs +31 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/tsconfig.json.hbs +9 -0
- package/templates/apps/fullstack/packages/{{projectName}}-contract/tsup.config.ts.hbs +9 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/README.md.hbs +84 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_ai.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_audit.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_cache.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_core.yml.hbs +22 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_db.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_feedback.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_iam.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_payment.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_reach.yml.hbs +60 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_scheduler.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_serv.yml.hbs +64 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_storage.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/config/_vecdb.yml.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/data/.gitkeep.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/package.json.hbs +62 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/app-server.ts.hbs +115 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/build-metadata.ts.hbs +7 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/index.ts.hbs +40 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/admin-service.ts.hbs +294 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/audit-jobs.ts.hbs +28 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/business-settings.ts.hbs +50 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/messages.ts.hbs +50 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/procedures/admin-procedures.ts.hbs +106 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/procedures/app-procedures.ts.hbs +49 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/procedures/index.ts.hbs +34 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/runtime.ts.hbs +159 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/src/server/security-roles.ts.hbs +73 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/tests/app-server.test.ts.hbs +505 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/tests/runtime.test.ts.hbs +72 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/tests/security.test.ts.hbs +183 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/tsconfig.json.hbs +10 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/tsup.config.ts.hbs +18 -0
- package/templates/apps/fullstack/packages/{{projectName}}-serv/vitest.config.ts.hbs +12 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/.gitignore.hbs +10 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/README.md.hbs +97 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/messages/en-US.json.hbs +164 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/messages/ja-JP.json.hbs +164 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/messages/zh-CN.json.hbs +164 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/package.json.hbs +55 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/project.inlang/settings.json.hbs +11 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/index.ts.hbs +7 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/api/api-client.ts.hbs +94 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/api/feedback-client.ts.hbs +36 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/api/sensitive-actions.svelte.ts.hbs +16 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/api/session-recovery.svelte.ts.hbs +4 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/components/AdminWorkspace.svelte.hbs +122 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/components/AppShell.svelte.hbs +90 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/components/FeedbackEntry.svelte.hbs +86 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/audit.svelte.hbs +94 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/configuration.svelte.hbs +25 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/feedback.svelte.hbs +157 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/roles.svelte.hbs +77 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/settings.svelte.hbs +44 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/pages/users.svelte.hbs +216 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/security/role-label.ts.hbs +12 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/stores/guide-store.ts.hbs +55 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/stores/management-store.svelte.ts.hbs +614 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/src/lib/stores/theme-store.svelte.ts.hbs +23 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/svelte.config.js.hbs +1 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/tests/api-client.test.ts.hbs +150 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/tests/management-store.test.ts.hbs +253 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/tsconfig.json.hbs +17 -0
- package/templates/apps/fullstack/packages/{{projectName}}-shared/vitest.config.ts.hbs +7 -0
- package/templates/apps/fullstack/playwright.config.ts.hbs +114 -0
- package/templates/apps/fullstack/playwright.session-close.config.ts.hbs +18 -0
- package/templates/apps/fullstack/pnpm-workspace.yaml.hbs +38 -0
- package/templates/apps/fullstack/tsconfig.base.json +14 -0
- package/templates/apps/h5/messages/en-US.json +3 -0
- package/templates/apps/h5/messages/ja-JP.json +40 -0
- package/templates/apps/h5/messages/zh-CN.json +3 -0
- package/templates/apps/h5/src/routes/+layout.svelte.hbs +85 -30
- package/templates/apps/h5/src/routes/+page.svelte.hbs +20 -8
- package/templates/apps/h5/src/routes/discover/+page.svelte.hbs +17 -6
- package/templates/apps/h5/src/routes/profile/+page.svelte.hbs +17 -6
- package/templates/apps/mobile-app/.env.hbs +3 -0
- package/templates/apps/{android-app → mobile-app}/capacitor.config.ts.hbs +2 -2
- package/templates/apps/mobile-app/index.html.hbs +14 -0
- package/templates/apps/mobile-app/messages/en-US.json +36 -0
- package/templates/apps/mobile-app/messages/ja-JP.json +36 -0
- package/templates/apps/mobile-app/messages/zh-CN.json +36 -0
- package/templates/apps/mobile-app/public/runtime-config.js +1 -0
- package/templates/apps/mobile-app/src/App.svelte.hbs +153 -0
- package/templates/apps/mobile-app/src/lib/api.ts.hbs +213 -0
- package/templates/apps/mobile-app/src/lib/app-config.ts.hbs +14 -0
- package/templates/apps/{android-app → mobile-app}/src/lib/capacitor.ts +6 -7
- package/templates/apps/mobile-app/src/lib/pages/home.svelte.hbs +58 -0
- package/templates/apps/mobile-app/src/lib/pages/profile.svelte.hbs +13 -0
- package/templates/apps/mobile-app/src/lib/pages/tasks.svelte.hbs +16 -0
- package/templates/apps/mobile-app/src/main.ts.hbs +11 -0
- package/templates/apps/website/messages/en-US.json +4 -2
- package/templates/apps/website/messages/ja-JP.json +34 -0
- package/templates/apps/website/messages/zh-CN.json +4 -2
- package/templates/apps/website/scripts/static-website.mjs.hbs +67 -0
- package/templates/apps/website/src/routes/+layout.svelte.hbs +77 -26
- package/templates/apps/website/src/routes/+layout.ts +3 -0
- package/templates/apps/website/src/routes/+page.svelte.hbs +31 -12
- package/templates/apps/website/src/routes/about/+page.svelte.hbs +11 -6
- package/templates/apps/website/src/routes/api/health/+server.ts +3 -0
- package/templates/apps/website/src/routes/contact/+page.svelte.hbs +44 -34
- package/templates/apps/website/src/routes/services/+page.svelte.hbs +27 -40
- package/templates/base/.dockerignore.hbs +27 -0
- package/templates/base/.gitignore.hbs +41 -0
- package/templates/base/.npmrc.hbs +1 -0
- package/templates/base/e2e/app.spec.ts.hbs +213 -0
- package/templates/base/e2e/helpers.ts +1 -1
- package/templates/base/eslint-rules.js +45 -0
- package/templates/base/eslint.config.js.hbs +34 -0
- package/templates/base/hai.json.hbs +4 -0
- package/templates/base/images/web/nginx.conf.hbs +24 -0
- package/templates/base/images/web.Dockerfile.hbs +72 -0
- package/templates/base/package.json.hbs +125 -49
- package/templates/base/playwright.config.ts +44 -6
- package/templates/base/pnpm-workspace.yaml.hbs +40 -0
- package/templates/base/src/app.css.hbs +40 -0
- package/templates/base/src/app.d.ts.hbs +14 -1
- package/templates/base/src/app.html.hbs +13 -0
- package/templates/base/src/hooks.server.ts.hbs +115 -43
- package/templates/base/src/lib/build-metadata.ts.hbs +7 -0
- package/templates/base/src/lib/server/init.ts.hbs +125 -152
- package/templates/base/src/lib/server/kit-config.ts.hbs +40 -0
- package/templates/base/src/routes/+layout.server.ts.hbs +35 -0
- package/templates/base/static/favicon.png +0 -0
- package/templates/base/svelte.config.js.hbs +84 -5
- package/templates/base/tests/scaffold.test.ts.hbs +102 -0
- package/templates/base/tsconfig.json.hbs +50 -0
- package/templates/base/vite.config.ts.hbs +117 -12
- package/templates/base/vitest.config.ts.hbs +19 -0
- package/templates/features/ai/routes/api/chat/+server.ts +6 -7
- package/templates/features/iam/routes-admin/(auth)/+layout.svelte.hbs +3 -17
- package/templates/features/iam/routes-admin/(auth)/auth/forgot-password/+page.svelte.hbs +10 -0
- package/templates/features/iam/routes-admin/(auth)/auth/login/+page.svelte.hbs +9 -96
- package/templates/features/iam/routes-admin/(auth)/auth/register/+page.svelte.hbs +9 -122
- package/templates/features/iam/routes-admin/(auth)/auth/reset-password/+page.svelte.hbs +10 -0
- package/templates/features/iam/routes-h5/auth/forgot-password/+page.svelte.hbs +10 -0
- package/templates/features/iam/routes-h5/auth/login/+page.svelte.hbs +9 -96
- package/templates/features/iam/routes-h5/auth/register/+page.svelte.hbs +9 -112
- package/templates/features/iam/routes-h5/auth/reset-password/+page.svelte.hbs +10 -0
- package/templates/features/iam/routes-shared/api/auth/captcha/+server.ts.hbs +4 -0
- package/templates/features/iam/routes-shared/api/auth/config/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/login/+server.ts +1 -29
- package/templates/features/iam/routes-shared/api/auth/login/ldap/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/login/otp/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/logout/+server.ts +13 -6
- package/templates/features/iam/routes-shared/api/auth/otp/send/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/password/renew/+server.ts +5 -0
- package/templates/features/iam/routes-shared/api/auth/password-reset/confirm/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/password-reset/request/+server.ts +4 -0
- package/templates/features/iam/routes-shared/api/auth/reauthenticate/+server.ts +5 -0
- package/templates/features/iam/routes-shared/api/auth/register/+server.ts +1 -23
- package/templates/features/iam/routes-shared/api/auth/session/close/+server.ts.hbs +4 -0
- package/templates/features/iam/routes-website/auth/forgot-password/+page.svelte.hbs +10 -0
- package/templates/features/iam/routes-website/auth/login/+page.svelte.hbs +11 -0
- package/templates/features/iam/routes-website/auth/register/+page.svelte.hbs +11 -0
- package/templates/features/iam/routes-website/auth/reset-password/+page.svelte.hbs +10 -0
- package/templates/features/payment/routes/api/v1/payment/create/+server.ts +25 -23
- package/templates/features/payment/routes/api/v1/payment/notify/[provider]/+server.ts +5 -2
- package/templates/features/storage/routes/api/upload/+server.ts +10 -8
- package/templates/i18n/project.inlang/settings.json +2 -2
- package/templates/shared/.gitattributes +11 -0
- package/templates/shared/build-metadata.ts +42 -0
- package/templates/shared/container-runtime-config.sh +30 -0
- package/templates/shared/e2e-env.ts +98 -0
- package/templates/shared/hash-router.svelte.ts +83 -0
- package/templates/shared/public-base-path.mjs +19 -0
- package/templates/shared/run-e2e.mjs +104 -0
- package/templates/shared/vite-runtime-config.ts +56 -0
- package/templates/skills/AGENTS.md +75 -16
- package/templates/skills/CLAUDE.md +0 -11
- package/templates/skills/bridges/admin/AGENTS.md +89 -0
- package/templates/skills/bridges/api/AGENTS.md +83 -0
- package/templates/skills/bridges/fullstack/AGENTS.md.hbs +102 -0
- package/templates/skills/bridges/h5/AGENTS.md +88 -0
- package/templates/skills/bridges/mobile-app/AGENTS.md +100 -0
- package/templates/skills/bridges/website/AGENTS.md +88 -0
- package/templates/skills/copilot-instructions.md +3 -59
- package/templates/skills/hai-ai/SKILL.md +44 -715
- package/templates/skills/hai-ai/guide.md +457 -0
- package/templates/skills/hai-ai/reference.md +292 -0
- package/templates/skills/hai-api-client/SKILL.md +26 -339
- package/templates/skills/hai-api-client/reference.md +180 -0
- package/templates/skills/hai-api-contract/SKILL.md +36 -0
- package/templates/skills/hai-api-contract/reference.md +280 -0
- package/templates/skills/hai-app-create/SKILL.md +38 -351
- package/templates/skills/hai-app-create/collaboration.md +52 -0
- package/templates/skills/hai-app-create/development.md +243 -0
- package/templates/skills/hai-app-create/reference.md +376 -0
- package/templates/skills/hai-app-review/SKILL.md +57 -126
- package/templates/skills/hai-app-review/differential.md +34 -0
- package/templates/skills/hai-app-tests/SKILL.md +34 -454
- package/templates/skills/hai-app-tests/reference.md +462 -0
- package/templates/skills/hai-app-tests/validation.md +42 -0
- package/templates/skills/hai-audit/SKILL.md +37 -122
- package/templates/skills/hai-audit/reference.md +184 -0
- package/templates/skills/hai-build/SKILL.md +62 -315
- package/templates/skills/hai-build/paths.md +30 -0
- package/templates/skills/hai-build/reference.md +353 -0
- package/templates/skills/hai-cache/SKILL.md +26 -175
- package/templates/skills/hai-cache/reference.md +229 -0
- package/templates/skills/hai-capacitor/SKILL.md +24 -364
- package/templates/skills/hai-capacitor/reference.md +358 -0
- package/templates/skills/hai-change-plan/SKILL.md +46 -0
- package/templates/skills/hai-ci/SKILL.md +114 -0
- package/templates/skills/hai-core/SKILL.md +31 -469
- package/templates/skills/hai-core/reference.md +557 -0
- package/templates/skills/hai-core/request-access.md +65 -0
- package/templates/skills/hai-crypto/SKILL.md +29 -180
- package/templates/skills/hai-crypto/reference.md +317 -0
- package/templates/skills/hai-datapipe/SKILL.md +24 -207
- package/templates/skills/hai-datapipe/reference.md +204 -0
- package/templates/skills/hai-deploy/SKILL.md +53 -178
- package/templates/skills/hai-deploy/credentials.md +19 -0
- package/templates/skills/hai-deploy/middleware.md +129 -0
- package/templates/skills/hai-feedback/SKILL.md +85 -0
- package/templates/skills/hai-framework-sync/SKILL.md +105 -0
- package/templates/skills/hai-framework-sync/sync.md +49 -0
- package/templates/skills/hai-iam/SKILL.md +53 -325
- package/templates/skills/hai-iam/reference.md +535 -0
- package/templates/skills/hai-kit/SKILL.md +33 -329
- package/templates/skills/hai-kit/reference.md +202 -0
- package/templates/skills/hai-payment/SKILL.md +28 -262
- package/templates/skills/hai-payment/reference.md +295 -0
- package/templates/skills/hai-pr-review/SKILL.md +41 -0
- package/templates/skills/hai-reach/SKILL.md +28 -300
- package/templates/skills/hai-reach/reference.md +363 -0
- package/templates/skills/hai-reldb/SKILL.md +28 -449
- package/templates/skills/hai-reldb/reference.md +500 -0
- package/templates/skills/hai-scheduler/SKILL.md +26 -174
- package/templates/skills/hai-scheduler/reference.md +216 -0
- package/templates/skills/hai-serv/SKILL.md +60 -0
- package/templates/skills/hai-serv/reference.md +792 -0
- package/templates/skills/hai-spec-implement/SKILL.md +50 -0
- package/templates/skills/hai-spec-implement/changes.md +67 -0
- package/templates/skills/hai-spec-implement/evaluation.md +18 -0
- package/templates/skills/hai-spec-implement/handoff.md +47 -0
- package/templates/skills/hai-spec-implement/templates.md +117 -0
- package/templates/skills/hai-spec-implement/workflow.md +56 -0
- package/templates/skills/hai-spec-review/SKILL.md +57 -0
- package/templates/skills/hai-spec-review/checklist.md +71 -0
- package/templates/skills/hai-storage/SKILL.md +31 -262
- package/templates/skills/hai-storage/reference.md +284 -0
- package/templates/skills/hai-ui/SKILL.md +62 -238
- package/templates/skills/hai-ui/guide.md +81 -0
- package/templates/skills/hai-ui/reference.md +705 -0
- package/templates/skills/hai-ui-design/SKILL.md +31 -0
- package/templates/skills/hai-ui-design/patterns.md +31 -0
- package/templates/skills/hai-ui-review/SKILL.md +32 -0
- package/templates/skills/hai-ui-review/checklist.md +29 -0
- package/templates/skills/hai-vecdb/SKILL.md +25 -267
- package/templates/skills/hai-vecdb/reference.md +325 -0
- package/templates/skills/opencode.json +0 -5
- package/dist/chunk-2H7UOFLK.js +0 -10
- package/dist/chunk-LANIDJC7.js +0 -659
- package/dist/chunk-LANIDJC7.js.map +0 -1
- package/dist/chunk-YV7WPUI6.js +0 -206
- package/dist/chunk-YV7WPUI6.js.map +0 -1
- package/dist/config-templates-S5RI2MC6.js +0 -5
- package/dist/config-templates-S5RI2MC6.js.map +0 -1
- package/dist/skill-templates-N5DU5FNX.js +0 -5
- package/dist/skill-templates-N5DU5FNX.js.map +0 -1
- package/dist/src-XJ6YXNAF.js +0 -15030
- package/dist/src-XJ6YXNAF.js.map +0 -1
- package/templates/apps/android-app/.env.hbs +0 -3
- package/templates/apps/android-app/messages/en-US.json +0 -24
- package/templates/apps/android-app/messages/zh-CN.json +0 -24
- package/templates/apps/android-app/src/lib/api.ts.hbs +0 -30
- package/templates/apps/android-app/src/routes/+layout.svelte.hbs +0 -42
- package/templates/apps/android-app/src/routes/+layout.ts +0 -7
- package/templates/apps/android-app/src/routes/+page.svelte.hbs +0 -27
- package/templates/apps/api/src/routes/+layout.svelte +0 -14
- package/templates/apps/api/src/routes/+page.svelte +0 -11
- package/templates/apps/api/src/routes/api/v1/health/+server.ts +0 -14
- package/templates/apps/api/src/routes/api/v1/items/+server.ts +0 -37
- package/templates/apps/api/src/routes/api/v1/items/[id]/+server.ts +0 -30
- package/templates/base/src/app.css +0 -2
- package/templates/base/tsconfig.json +0 -8
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
# hai-reldb — 详细用法
|
|
2
|
+
|
|
3
|
+
从 [SKILL.md](SKILL.md) 选择主题后读取对应小节。代码片段展示 API 用法;省略的业务变量、schema、依赖与 return 上下文需由应用补齐。
|
|
4
|
+
|
|
5
|
+
> `@h-ai/reldb` 提供统一的数据库操作接口,支持 SQLite、PostgreSQL、MySQL,包含 DDL、SQL、CRUD 抽象、事务与分页。
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
派生 `BaseReldbCrudRepository` 的自定义 SQL 方法先检查 `await this.ensureReady()`;构造函数启动自动建表不等于仓库就绪,服务 init 必须等待候选仓库的就绪入口再发布。PostgreSQL 自动提交的单条 `CREATE TABLE/INDEX IF NOT EXISTS` 仅对 `23505/pg_class_relname_nsp_index`(建表还包括 `pg_type_typname_nsp_index`)目录竞争,或建表时 `42710` 且服务端 routine 为 `heap_create_with_catalog` / `TypeCreate` 的行类型竞争重执行一次;重试仍失败则保留错误,DDL 与 SQL 入口一致;事务、业务唯一约束及其他错误不自动重试。初始化连接探测仅对临时 DNS 错误 `EAI_AGAIN` 最多尝试三次,间隔 250/500 ms;不重放业务 SQL。
|
|
9
|
+
|
|
10
|
+
## 运行环境
|
|
11
|
+
|
|
12
|
+
> ⚠️ **服务端模块(Node.js only)。** 浏览器端请通过 `@h-ai/api-contract` + `@h-ai/serv` 暴露的 API 端点间接访问数据库。
|
|
13
|
+
|
|
14
|
+
## 适用场景
|
|
15
|
+
|
|
16
|
+
- 新增或修改数据库访问逻辑(SQL/DDL/CRUD/事务)
|
|
17
|
+
- 使用 `reldb.crud.table` 或 `BaseReldbCrudRepository` 构建数据仓库
|
|
18
|
+
- 处理分页查询与分页结果规范化
|
|
19
|
+
- 基于 `HaiReldbError` 做错误分支处理
|
|
20
|
+
- JSON 列的路径提取、设置、插入、删除、合并操作(跨数据库统一语法)
|
|
21
|
+
|
|
22
|
+
## 使用步骤
|
|
23
|
+
|
|
24
|
+
### 1. 配置
|
|
25
|
+
|
|
26
|
+
只做校验时从 `@h-ai/reldb/schemas` 导入同一组 Schema/类型;该入口只依赖 Zod,无数据库实例或驱动。MySQL `ssl` 只接受 `false` 或严格对象;`{}` 默认开启证书链和 DNS 身份验证,不能用 `true` 或 PostgreSQL 字符串模式。实际连接仍需服务端驱动。
|
|
27
|
+
|
|
28
|
+
```yaml
|
|
29
|
+
# config/_db.yml
|
|
30
|
+
type: sqlite
|
|
31
|
+
database: ./data/app.db
|
|
32
|
+
# PostgreSQL/MySQL 额外字段:
|
|
33
|
+
# host: localhost
|
|
34
|
+
# port: 5432
|
|
35
|
+
# user: postgres
|
|
36
|
+
# password: ''
|
|
37
|
+
operationLog:
|
|
38
|
+
read: false # query / get / queryPage
|
|
39
|
+
write: false # DDL, execute / batch, tx.begin / tx.commit / tx.rollback
|
|
40
|
+
maxLength: 1000
|
|
41
|
+
level: debug # info | debug | trace
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
约定环境变量优先于 YAML,例如 `HAI_DB_TYPE`、`HAI_DB_DATABASE`、
|
|
45
|
+
`HAI_DB_OPERATIONLOG_LEVEL`。
|
|
46
|
+
|
|
47
|
+
`operationLog` 在 reldb provider-base 的 DML 操作层统一输出日志,不要在 `reldb-main.ts`、各 raw provider 或调用方重复包装。`read` 覆盖 `query/get/queryPage`,`write` 覆盖 DDL、`execute/batch` 与 `tx.begin/commit/rollback`,`maxLength` 限制安全数量摘要的字符数(0省略摘要);SQL原文、参数值、DDL默认值和驱动错误内容不写入日志,`level` 默认 `debug`。
|
|
48
|
+
|
|
49
|
+
### 2. 初始化与关闭
|
|
50
|
+
|
|
51
|
+
MySQL `pool` 仅支持 `{ max: 10, acquireTimeout: 10000, queueLimit: 1000 }`(也是省略时默认值)。`acquireTimeout` 为 1–2147483647 毫秒的整数,覆盖排队和建连;queueLimit 为正整数。SQL/DDL/CRUD/batch/tx 共用有界 FIFO,超时移除并返回 `TIMEOUT`,满额返回 `POOL_EXHAUSTED`;迟到建连结算前仍占槽位,只归还连接,不执行 SQL。close 立即取消排队,再等待借用/事务和驱动有界建连完成,不中止已启动 SQL。min/idleTimeout/mysql.timezone 明确拒绝,不承诺 MySQL 最小保留或可配置空闲回收;PostgreSQL 保留自己的 min/idleTimeout/acquireTimeout=0 契约,min 不会预建连接。
|
|
52
|
+
|
|
53
|
+
MySQL 仅传 mysql:// URL 即可连接,优先于 host/port/database/user/password;无 URL 时 database 必填。禁止 URL query/fragment,pool/mysql.charset/ssl/operationLog 仍独立适用。ssl 仅 false 或严格对象,空对象默认校验证书链和 DNS 身份;ca/cert/key 为 PEM 内容,cert/key 必须成对。当前驱动不验证 IP 身份,验证开启的 IP 目标返回 CONFIG_ERROR;关闭验证有固定风险日志,加密不代表身份认证。环境变量为 HAI_DB_POOL_* / HAI_DB_SSL_*,min/idleTimeout 仅适用 PostgreSQL,queueLimit/verifyIdentity 仅适用 MySQL。
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
// trustedCaPem 从部署信任配置提供。
|
|
57
|
+
const result = await reldb.init({ type: 'mysql', host: 'db.example.com', database: 'app', pool: { max: 10, acquireTimeout: 5000, queueLimit: 100 }, ssl: { ca: trustedCaPem } })
|
|
58
|
+
if (!result.success) return result
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
PostgreSQL 空闲连接错误已监听,由池移除并在下一次查询替换;日志仅含固定说明与安全 code/name,不记录驱动错误原文。关闭会等待已借出事务完成,应用必须提交/回滚并释放句柄。
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { core } from '@h-ai/core'
|
|
65
|
+
import { reldb } from '@h-ai/reldb'
|
|
66
|
+
|
|
67
|
+
await reldb.init(core.config.get('db'))
|
|
68
|
+
// ... 使用数据库
|
|
69
|
+
const closed = await reldb.close()
|
|
70
|
+
if (!closed.success) throw new Error(closed.error.message)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
初始化只在健康检查成功后发布连接;失败时释放候选资源。若清理仍失败,初始化错误的 `error.ext.cleanupPending` 为 `true`。`reldb.close()` 返回 `Promise<HaiResult<void>>`,等待正在进行的初始化,并合并并发关闭。开始清理后 `isInitialized` 为 `false`,配置与业务操作不再可用;清理失败仍保留资源,后续 `close()` 或 `init()` 先重试释放,成功前不会创建替代连接。调用方必须检查关闭结果。
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const closed = await reldb.close()
|
|
78
|
+
if (!closed.success) {
|
|
79
|
+
// 处理清理故障后有限重试;再次失败应向应用报告。
|
|
80
|
+
const retried = await reldb.close()
|
|
81
|
+
if (!retried.success) throw new Error(retried.error.message)
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
`reldb.config` 返回脱敏后的配置快照;数据库密码、URL 内嵌凭证和 TLS 私钥 `ssl.key` 会隐藏,不能用快照回放原始连接配置。
|
|
87
|
+
|
|
88
|
+
### 3. 选择操作接口
|
|
89
|
+
|
|
90
|
+
| 接口 | 用途 | 入口 |
|
|
91
|
+
| ---- | ------------------ | ---------------------------- |
|
|
92
|
+
| DDL | 建表/索引/字段变更 | `reldb.ddl` |
|
|
93
|
+
| SQL | 原始查询/执行/分页 | `reldb.sql` |
|
|
94
|
+
| CRUD | 通用增删改查 | `reldb.crud.table(config)` |
|
|
95
|
+
| 仓库 | 业务数据仓库封装 | `extends BaseReldbCrudRepository` |
|
|
96
|
+
| 事务 | 事务管理 | `reldb.tx` |
|
|
97
|
+
| 分页 | 分页参数与结果 | `reldb.pagination` |
|
|
98
|
+
| JSON | JSON 路径操作 | `reldb.json` |
|
|
99
|
+
|
|
100
|
+
## 核心 API
|
|
101
|
+
|
|
102
|
+
事务边界:`tx.wrap` 对正常返回值一律提交,不识别失败 HaiResult;回调内检查每步结果,失败抛错以触发回滚(外层返回 TRANSACTION_FAILED)。需保留原错误码时使用手动事务。
|
|
103
|
+
|
|
104
|
+
### DDL — `reldb.ddl`
|
|
105
|
+
|
|
106
|
+
| 方法 | 签名 | 说明 |
|
|
107
|
+
| ------------- | ----------------------------------------------------------------------- | -------------------------- |
|
|
108
|
+
| `createTable` | `(tableName, columns: ReldbTableDef, ifNotExists?: boolean) => HaiResult<void>` | 建表(默认 IF NOT EXISTS) |
|
|
109
|
+
| `dropTable` | `(tableName, ifExists?: boolean) => HaiResult<void>` | 删除表(默认 IF EXISTS) |
|
|
110
|
+
| `addColumn` | `(tableName, columnName, columnDef: ReldbColumnDef) => HaiResult<void>` | 添加列 |
|
|
111
|
+
| `dropColumn` | `(tableName, columnName) => HaiResult<void>` | 删除列 |
|
|
112
|
+
| `renameTable` | `(oldName, newName) => HaiResult<void>` | 重命名表 |
|
|
113
|
+
| `createIndex` | `(tableName, indexName, indexDef: ReldbIndexDef) => HaiResult<void>` | 创建索引 |
|
|
114
|
+
| `dropIndex` | `(indexName, ifExists?: boolean) => HaiResult<void>` | 删除索引 |
|
|
115
|
+
| `raw` | `(sql: string) => HaiResult<void>` | 执行原始 DDL |
|
|
116
|
+
|
|
117
|
+
> 所有方法均返回 `Promise<HaiResult<void>>`,上表省略异步与错误类型。
|
|
118
|
+
|
|
119
|
+
**ReldbTableDef**(列名到列定义的映射):
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const columns: ReldbTableDef = {
|
|
123
|
+
id: { type: 'INTEGER', primaryKey: true, autoIncrement: true },
|
|
124
|
+
name: { type: 'TEXT', notNull: true },
|
|
125
|
+
email: { type: 'TEXT', unique: true },
|
|
126
|
+
score: { type: 'REAL', defaultValue: 0 },
|
|
127
|
+
active: { type: 'BOOLEAN' },
|
|
128
|
+
metadata: { type: 'JSON' },
|
|
129
|
+
created_at: { type: 'TIMESTAMP' },
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**ReldbColumnDef**(列类型与约束):
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
interface ReldbColumnDef {
|
|
137
|
+
type: 'TEXT' | 'INTEGER' | 'BIGINT' | 'REAL' | 'BLOB' | 'BOOLEAN' | 'TIMESTAMP' | 'JSON'
|
|
138
|
+
primaryKey?: boolean
|
|
139
|
+
autoIncrement?: boolean
|
|
140
|
+
notNull?: boolean
|
|
141
|
+
unique?: boolean
|
|
142
|
+
defaultValue?: string | number | boolean | null
|
|
143
|
+
references?: { table: string, column: string, onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION', onUpdate?: '...' }
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**跨数据库类型映射**:
|
|
148
|
+
|
|
149
|
+
| 类型 | SQLite | PostgreSQL | MySQL |
|
|
150
|
+
| --------- | ------- | ---------------- | ------------ |
|
|
151
|
+
| TEXT | TEXT | TEXT | VARCHAR(255) |
|
|
152
|
+
| INTEGER | INTEGER | INTEGER/BIGSERIAL | INT/BIGINT |
|
|
153
|
+
| BIGINT | INTEGER(64 位) | BIGINT | BIGINT |
|
|
154
|
+
| REAL | REAL | DOUBLE PRECISION | DOUBLE |
|
|
155
|
+
| BLOB | BLOB | BYTEA | BLOB |
|
|
156
|
+
| BOOLEAN | INTEGER | BOOLEAN | TINYINT(1) |
|
|
157
|
+
| TIMESTAMP | INTEGER | TIMESTAMP | DATETIME |
|
|
158
|
+
| JSON | TEXT | JSONB | JSON |
|
|
159
|
+
|
|
160
|
+
> MySQL 将 TEXT 映射为 VARCHAR(255) 以支持索引和 UNIQUE 约束。INTEGER + autoIncrement 在 MySQL 映射为 BIGINT。
|
|
161
|
+
|
|
162
|
+
Unix 毫秒使用 BIGINT,不能使用 PostgreSQL/MySQL 的普通 INTEGER。原始 SQL/CRUD 与 BaseReldbCrudRepository 的 BIGINT 保留 driver 的 number/string/bigint 类型;业务需要 number 时须在读取边界校验非负整数和 Number.isSafeInteger,拒绝精度丢失。TIMESTAMP 经 BaseReldbCrudRepository 映射为 Date。
|
|
163
|
+
|
|
164
|
+
原始 `sql.query<T>` / `queryPage<T>` 的泛型只声明类型,不执行字段/日期/JSON 转换;JOIN 等自定义查询须自行加引号保留别名大小写,并在仓库边界映射驱动值。基类读取将 SQL NULL 映射为 undefined、JSON 解析为对象;若业务公开契约要求 null 或 JSON 字符串,仓库需显式归一化。
|
|
165
|
+
|
|
166
|
+
### SQL — `reldb.sql`
|
|
167
|
+
|
|
168
|
+
| 方法 | 签名 | 说明 |
|
|
169
|
+
| ----------- | ----------------------------------------------------------------- | ------------ |
|
|
170
|
+
| `query` | `<T>(sql, params?) => HaiResult<T[]>` | 查询返回多行 |
|
|
171
|
+
| `get` | `<T>(sql, params?) => HaiResult<T \| null>` | 查询返回单行 |
|
|
172
|
+
| `execute` | `(sql, params?) => HaiResult<ExecuteResult>` | 执行写操作 |
|
|
173
|
+
| `batch` | `(statements: Array<{ sql, params? }>) => HaiResult<void>` | 批量执行 |
|
|
174
|
+
| `queryPage` | `<T>(options: PaginationQueryOptions) => HaiResult<PaginatedResult>` | 分页查询 |
|
|
175
|
+
|
|
176
|
+
> 所有方法均返回 `Promise<HaiResult<T>>`,上表省略异步与错误类型。
|
|
177
|
+
|
|
178
|
+
**参数占位符统一使用 `?`**(PostgreSQL 的 `$n` 由 Provider 自动转换):
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
const result = await reldb.sql.query<User>(
|
|
182
|
+
'SELECT * FROM users WHERE status = ? AND age > ?',
|
|
183
|
+
['active', 18],
|
|
184
|
+
)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**ExecuteResult**:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
interface ExecuteResult {
|
|
191
|
+
changes: number // 影响的行数
|
|
192
|
+
lastInsertRowid?: number | bigint // INSERT 时生效(PostgreSQL 不返回此字段)
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**分页查询**:
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
const page = await reldb.sql.queryPage<User>({
|
|
200
|
+
sql: 'SELECT * FROM users WHERE status = ? ORDER BY id',
|
|
201
|
+
params: ['active'],
|
|
202
|
+
pagination: { page: 1, pageSize: 20 },
|
|
203
|
+
overrides: { maxPageSize: 100 }, // 可选:限制最大页大小
|
|
204
|
+
})
|
|
205
|
+
// page.data => { items, total, page, pageSize }
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### CRUD — `reldb.crud.table(config)`
|
|
209
|
+
|
|
210
|
+
通过配置创建单表 CRUD 仓库,免写 SQL:
|
|
211
|
+
|
|
212
|
+
`CrudConfig.dbType` 必填,必须与实际初始化的数据库后端一致(含环境变量覆盖后的类型)。以下为 SQLite 示例;切换到 PostgreSQL/MySQL 时同步修改。`BaseReldbCrudRepository` 会从已初始化的 `reldb.config.type` 获取类型,无需手动传入。
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const userCrud = reldb.crud.table<{ id: number, name: string, email: string }>({
|
|
216
|
+
table: 'users',
|
|
217
|
+
idColumn: 'id', // 主键列,默认 'id'
|
|
218
|
+
select: ['id', 'name', 'email'], // 查询列,默认 '*'
|
|
219
|
+
createColumns: ['name', 'email'], // 允许插入的列
|
|
220
|
+
updateColumns: ['name', 'email'], // 允许更新的列
|
|
221
|
+
dbType: 'sqlite', // 必填,与本例初始化后端一致
|
|
222
|
+
})
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
返回的 ReldbCrudRepository 方法:
|
|
226
|
+
|
|
227
|
+
| 方法 | 签名 | 说明 |
|
|
228
|
+
| ------------ | ---------------------------------------------- | ------------ |
|
|
229
|
+
| `create` | `(data, tx?) => HaiResult<ExecuteResult>` | 创建单条 |
|
|
230
|
+
| `createMany` | `(items, tx?) => HaiResult<void>` | 批量创建 |
|
|
231
|
+
| `findById` | `(id, tx?) => HaiResult<T \| null>` | 按 ID 查询 |
|
|
232
|
+
| `findAll` | `(options?, tx?) => HaiResult<T[]>` | 条件查询 |
|
|
233
|
+
| `findPage` | `(options, tx?) => HaiResult<PaginatedResult<T>>` | 分页查询 |
|
|
234
|
+
| `updateById` | `(id, data, tx?) => HaiResult<ExecuteResult>` | 按 ID 更新 |
|
|
235
|
+
| `deleteById` | `(id, tx?) => HaiResult<ExecuteResult>` | 按 ID 删除 |
|
|
236
|
+
| `count` | `(options?, tx?) => HaiResult<number>` | 计数 |
|
|
237
|
+
| `exists` | `(options?, tx?) => HaiResult<boolean>` | 条件是否存在 |
|
|
238
|
+
| `existsById` | `(id, tx?) => HaiResult<boolean>` | ID 是否存在 |
|
|
239
|
+
|
|
240
|
+
> 所有方法均支持可选 `tx` 事务参数。`create`/`updateById` 中的 `data` 会根据 `createColumns`/`updateColumns` 白名单过滤列。
|
|
241
|
+
|
|
242
|
+
**使用示例**:
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
// 单条插入
|
|
246
|
+
const result = await userCrud.create({ name: '张三', email: 'test@example.com' })
|
|
247
|
+
// result.data → { changes: 1, lastInsertRowid: 1 }
|
|
248
|
+
|
|
249
|
+
// 批量插入
|
|
250
|
+
await userCrud.createMany([
|
|
251
|
+
{ name: '用户A', email: 'a@test.com' },
|
|
252
|
+
{ name: '用户B', email: 'b@test.com' },
|
|
253
|
+
])
|
|
254
|
+
|
|
255
|
+
// 主键查找
|
|
256
|
+
const user = await userCrud.findById(1)
|
|
257
|
+
// user.data → { id: 1, name: '张三', email: 'test@example.com' } | null
|
|
258
|
+
|
|
259
|
+
// 条件查询(where + params 占位符)
|
|
260
|
+
const actives = await userCrud.findAll({
|
|
261
|
+
where: 'name LIKE ?',
|
|
262
|
+
params: ['%张%'],
|
|
263
|
+
orderBy: 'id DESC',
|
|
264
|
+
limit: 10,
|
|
265
|
+
offset: 0,
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
// 分页查询
|
|
269
|
+
const page = await userCrud.findPage({
|
|
270
|
+
where: 'name LIKE ?',
|
|
271
|
+
params: ['%张%'],
|
|
272
|
+
orderBy: 'id DESC',
|
|
273
|
+
pagination: { page: 1, pageSize: 20 },
|
|
274
|
+
})
|
|
275
|
+
// page.data → { items: [...], total: 100, page: 1, pageSize: 20 }
|
|
276
|
+
|
|
277
|
+
// 更新 / 删除
|
|
278
|
+
await userCrud.updateById(1, { name: '新名字' })
|
|
279
|
+
await userCrud.deleteById(1)
|
|
280
|
+
|
|
281
|
+
// 计数 / 存在性
|
|
282
|
+
const total = await userCrud.count({ where: 'name LIKE ?', params: ['%张%'] })
|
|
283
|
+
const has = await userCrud.exists({ where: 'email = ?', params: ['test@example.com'] })
|
|
284
|
+
const found = await userCrud.existsById(1)
|
|
285
|
+
|
|
286
|
+
// 事务中使用(所有方法均支持 tx 参数)
|
|
287
|
+
await reldb.tx.wrap(async (tx) => {
|
|
288
|
+
const created = await userCrud.create({ name: '用户A', email: 'a@test.com' }, tx)
|
|
289
|
+
if (!created.success) throw new Error(created.error.message)
|
|
290
|
+
const updated = await userCrud.updateById(1, { name: '新名字' }, tx)
|
|
291
|
+
if (!updated.success) throw new Error(updated.error.message)
|
|
292
|
+
const user = await userCrud.findById(1, tx)
|
|
293
|
+
if (!user.success) throw new Error(user.error.message)
|
|
294
|
+
return user.data
|
|
295
|
+
})
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### BaseReldbCrudRepository
|
|
299
|
+
|
|
300
|
+
业务仓库基类,提供字段映射、自动建表与类型转换能力:
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
import { BaseReldbCrudRepository } from '@h-ai/reldb'
|
|
304
|
+
|
|
305
|
+
interface User { id: number, name: string, email: string, createdAt: Date, updatedAt: Date }
|
|
306
|
+
|
|
307
|
+
class UserRepository extends BaseReldbCrudRepository<User> {
|
|
308
|
+
constructor() {
|
|
309
|
+
super(db, {
|
|
310
|
+
table: 'users',
|
|
311
|
+
idColumn: 'id',
|
|
312
|
+
fields: [
|
|
313
|
+
{ fieldName: 'id', columnName: 'id', def: { type: 'INTEGER', primaryKey: true, autoIncrement: true }, select: true, create: false, update: false },
|
|
314
|
+
{ fieldName: 'name', columnName: 'name', def: { type: 'TEXT', notNull: true }, select: true, create: true, update: true },
|
|
315
|
+
{ fieldName: 'email', columnName: 'email', def: { type: 'TEXT', notNull: true }, select: true, create: true, update: true },
|
|
316
|
+
{ fieldName: 'createdAt', columnName: 'created_at', def: { type: 'TIMESTAMP', notNull: true }, select: true, create: true, update: false },
|
|
317
|
+
{ fieldName: 'updatedAt', columnName: 'updated_at', def: { type: 'TIMESTAMP', notNull: true }, select: true, create: true, update: false },
|
|
318
|
+
],
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** 自定义查询示例 */
|
|
323
|
+
async findByEmail(email: string, tx?: DmlWithTxOperations) {
|
|
324
|
+
return this.sql(tx).get<User>('SELECT * FROM users WHERE email = ?', [email])
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
**`this.sql(tx?)`**:返回 `DmlOperations`(`reldb.sql` 或传入的事务句柄),自动适配事务场景。
|
|
330
|
+
|
|
331
|
+
**自动能力**:
|
|
332
|
+
|
|
333
|
+
- `createdAt` / `updatedAt` 字段自动填充时间戳
|
|
334
|
+
- 主键生成(非 autoIncrement 主键默认使用随机 UUID,底层带安全上下文守卫,不可用时回退;业务侧手动生成 UUID 一律用 `core.id.uuid()`)
|
|
335
|
+
- BOOLEAN → 1/0(SQLite/MySQL)或 true/false(PostgreSQL)
|
|
336
|
+
- TIMESTAMP → 毫秒时间戳(SQLite)或 Date(PG/MySQL)
|
|
337
|
+
- JSON 写入统一序列化为字符串;数据库列为 SQLite TEXT、PostgreSQL JSONB、MySQL JSON,基类读取时解析为对象
|
|
338
|
+
|
|
339
|
+
### 事务 — `reldb.tx`
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
// 方式1:wrap(自动 commit/rollback)
|
|
343
|
+
const result = await reldb.tx.wrap(async (tx) => {
|
|
344
|
+
const inserted = await tx.execute('INSERT INTO users (name) VALUES (?)', ['张三'])
|
|
345
|
+
if (!inserted.success) throw new Error(inserted.error.message)
|
|
346
|
+
const logged = await tx.execute('INSERT INTO logs (action) VALUES (?)', ['user_created'])
|
|
347
|
+
if (!logged.success) throw new Error(logged.error.message)
|
|
348
|
+
return 'done'
|
|
349
|
+
})
|
|
350
|
+
// result.data === 'done'(正常返回自动 commit)
|
|
351
|
+
// 回调抛异常 → 自动 rollback → result.success === false
|
|
352
|
+
|
|
353
|
+
// 方式2:手动管理,保留原领域错误(置于 async 函数内)
|
|
354
|
+
const txResult = await reldb.tx.begin()
|
|
355
|
+
if (!txResult.success) return txResult
|
|
356
|
+
const tx = txResult.data
|
|
357
|
+
const inserted = await tx.execute('INSERT INTO users (name) VALUES (?)', ['李四'])
|
|
358
|
+
if (!inserted.success) {
|
|
359
|
+
const rollback = await tx.rollback()
|
|
360
|
+
if (!rollback.success) return rollback
|
|
361
|
+
return inserted
|
|
362
|
+
}
|
|
363
|
+
return tx.commit()
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
**事务内可用操作**:
|
|
367
|
+
|
|
368
|
+
- `tx.query / tx.get / tx.execute / tx.batch / tx.queryPage`
|
|
369
|
+
- `tx.crud.table(config)` — 事务内的 CRUD 仓库
|
|
370
|
+
- `tx.commit() / tx.rollback()`
|
|
371
|
+
|
|
372
|
+
### 分页 — `reldb.pagination`
|
|
373
|
+
|
|
374
|
+
| 方法 | 签名 | 说明 |
|
|
375
|
+
| ----------- | ----------------------------------------------------- | -------------------------------------- |
|
|
376
|
+
| `normalize` | `(options?, overrides?) => NormalizedPagination` | 规范化分页参数(含 offset/limit 计算) |
|
|
377
|
+
| `build` | `<T>(items, total, pagination) => PaginatedResult<T>` | 构建分页结果对象 |
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
// PaginatedResult 结构
|
|
381
|
+
interface PaginatedResult<T> {
|
|
382
|
+
items: T[]
|
|
383
|
+
total: number
|
|
384
|
+
page: number
|
|
385
|
+
pageSize: number
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// 默认值:page=1, pageSize=20, maxPageSize=200
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### JSON — `reldb.json`
|
|
392
|
+
|
|
393
|
+
通过 `reldb.json` 构建跨数据库统一的 JSON 路径操作 SQL 表达式,返回 `{ sql, params }` 可直接嵌入 `reldb.sql.*`。
|
|
394
|
+
|
|
395
|
+
支持简单路径 `$.key`、`$.key.subkey`、`$[0]`。
|
|
396
|
+
|
|
397
|
+
| 操作 | SQLite | PostgreSQL | MySQL |
|
|
398
|
+
| --------- | ------------------ | ------------------------ | ---------------------- |
|
|
399
|
+
| `extract` | `json_extract` | `#>` (text[]) | `JSON_EXTRACT` |
|
|
400
|
+
| `set` | `json_set` | `jsonb_set` | `JSON_SET` |
|
|
401
|
+
| `insert` | `json_insert` | `jsonb_insert` | `JSON_INSERT` |
|
|
402
|
+
| `remove` | `json_remove` | `#-` (text[]) | `JSON_REMOVE` |
|
|
403
|
+
| `merge` | `json_patch` | 参数化递归 CTE | `JSON_MERGE_PATCH` |
|
|
404
|
+
|
|
405
|
+
`merge` 的补丁根限于可 JSON 序列化对象:三种后端均按 RFC 7396 递归保留未变键、用 null 删除指定键、整体替换数组;PostgreSQL 使用参数化递归 CTE,在同一次 SQL 内更新当前行,不做应用先读再写。路径和值全部参数化,column 只能由开发者静态编写。路径解析只支持简单点号/方括号形式,不是完整 SQL/JSON Path 校验器;PostgreSQL set 要求父路径存在,insert 对已有对象键报错且数组按原生位置插入,不应把不同后端的原生路径行为视为完全相同。
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
import { reldb, type ReldbFunctions } from '@h-ai/reldb'
|
|
409
|
+
|
|
410
|
+
// db 已初始化,users 表已有 settings/profile JSON 列。
|
|
411
|
+
async function updateSettings(db: ReldbFunctions, userId: string) {
|
|
412
|
+
const extracted = db.json.extract('settings', '$.theme')
|
|
413
|
+
const rows = await db.sql.query(`SELECT * FROM users WHERE ${extracted.sql} = ?`, [...extracted.params, '"dark"'])
|
|
414
|
+
if (!rows.success) return rows
|
|
415
|
+
|
|
416
|
+
const changed = db.json.set('settings', '$.theme', 'dark')
|
|
417
|
+
const updated = await db.sql.execute(`UPDATE users SET settings = ${changed.sql} WHERE id = ?`, [...changed.params, userId])
|
|
418
|
+
if (!updated.success) return updated
|
|
419
|
+
|
|
420
|
+
const removed = db.json.remove('settings', '$.deprecated')
|
|
421
|
+
const deleted = await db.sql.execute(`UPDATE users SET settings = ${removed.sql} WHERE id = ?`, [...removed.params, userId])
|
|
422
|
+
if (!deleted.success) return deleted
|
|
423
|
+
|
|
424
|
+
const patch = db.json.merge('profile', { bio: '新简介', avatar: null })
|
|
425
|
+
return db.sql.execute(`UPDATE users SET profile = ${patch.sql} WHERE id = ?`, [...patch.params, userId])
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const result = await updateSettings(reldb, 'u1')
|
|
429
|
+
if (!result.success) throw new Error(result.error.message)
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
> `column` 参数为列名,**禁止传入用户输入**(开发者负责安全性)。
|
|
433
|
+
|
|
434
|
+
## 错误码 — `HaiReldbError`
|
|
435
|
+
|
|
436
|
+
| 错误码 | code | 说明 |
|
|
437
|
+
|--------|------|------|
|
|
438
|
+
| `HaiReldbError.CONNECTION_FAILED` | `hai:reldb:001` | 数据库连接失败 |
|
|
439
|
+
| `HaiReldbError.QUERY_FAILED` | `hai:reldb:002` | 查询或执行失败 |
|
|
440
|
+
| `HaiReldbError.CONSTRAINT_VIOLATION` | `hai:reldb:003` | 约束违反 |
|
|
441
|
+
| `HaiReldbError.TRANSACTION_FAILED` | `hai:reldb:004` | 事务失败 |
|
|
442
|
+
| `HaiReldbError.MIGRATION_FAILED` | `hai:reldb:005` | 迁移失败 |
|
|
443
|
+
| `HaiReldbError.RECORD_NOT_FOUND` | `hai:reldb:006` | 记录不存在 |
|
|
444
|
+
| `HaiReldbError.DUPLICATE_ENTRY` | `hai:reldb:007` | 重复条目 |
|
|
445
|
+
| `HaiReldbError.DEADLOCK` | `hai:reldb:008` | 死锁 |
|
|
446
|
+
| `HaiReldbError.TIMEOUT` | `hai:reldb:009` | 超时 |
|
|
447
|
+
| `HaiReldbError.NOT_INITIALIZED` | `hai:reldb:010` | 数据库未初始化 |
|
|
448
|
+
| `HaiReldbError.POOL_EXHAUSTED` | `hai:reldb:011` | 连接池耗尽 |
|
|
449
|
+
| `HaiReldbError.UNSUPPORTED_TYPE` | `hai:reldb:012` | 不支持的数据库类型 |
|
|
450
|
+
| `HaiReldbError.CONFIG_ERROR` | `hai:reldb:013` | 配置错误 |
|
|
451
|
+
| `HaiReldbError.DDL_FAILED` | `hai:reldb:020` | DDL 操作失败 |
|
|
452
|
+
|
|
453
|
+
## 常见模式
|
|
454
|
+
|
|
455
|
+
### API 端点中使用
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
import { reldb, HaiReldbError } from '@h-ai/reldb'
|
|
459
|
+
import { kit } from '@h-ai/kit'
|
|
460
|
+
|
|
461
|
+
export const GET = kit.handler(async ({ url }) => {
|
|
462
|
+
// query 返回校验后的数据,失败抛 Response,由 kit.handler 处理。
|
|
463
|
+
const pagination = kit.validate.query(url, PageSchema)
|
|
464
|
+
const result = await userCrud.findPage({ pagination, orderBy: 'id ASC' })
|
|
465
|
+
if (!result.success)
|
|
466
|
+
return kit.response.error(result.error.code, result.error.message)
|
|
467
|
+
return kit.response.ok(result.data)
|
|
468
|
+
})
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
### 事务跨仓库
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
const result = await reldb.tx.wrap(async (tx) => {
|
|
475
|
+
const user = await userRepo.create(userData, tx)
|
|
476
|
+
if (!user.success)
|
|
477
|
+
throw new Error(user.error.message)
|
|
478
|
+
|
|
479
|
+
const profile = await profileRepo.create({ userId: user.data.lastInsertRowid, ...profileData }, tx)
|
|
480
|
+
if (!profile.success) throw new Error(profile.error.message)
|
|
481
|
+
return user.data
|
|
482
|
+
})
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### BaseReldbCrudRepository 事务集成
|
|
486
|
+
|
|
487
|
+
与上述事务相同:每个 Repository 方法传入同一 tx,并检查每次 HaiResult;失败 rollback,全部成功后 commit。不要只等待 Promise 完成就提交。
|
|
488
|
+
|
|
489
|
+
## 相关 Skills
|
|
490
|
+
|
|
491
|
+
- `hai-build`:模块初始化顺序
|
|
492
|
+
- `hai-core`:配置管理、HaiResult 模型
|
|
493
|
+
- `hai-iam`:IAM 模块内部使用 reldb 进行用户/角色/权限存储
|
|
494
|
+
- `hai-cache`:缓存穿透保护中配合 reldb 使用
|
|
495
|
+
|
|
496
|
+
## 生产共享状态
|
|
497
|
+
|
|
498
|
+
生产仅使用 PostgreSQL/MySQL;SQLite(含磁盘文件)只用于开发/明确测试,不能作为跨主机共享状态。 NODE_ENV 或 HAI_CORE_ENV 为 production 时在初始化成功前检查,只有 HAI_E2E=1/HAI_E2E_MOCK=1 明确测试豁免。不要将租约当作最终写入条件,外部未知效果须按稳定业务键核对。
|
|
499
|
+
|
|
500
|
+
MySQL 空 url(含空环境变量占位)按未配置处理,使用分字段;非空仍严格校验。
|