sonamu 0.13.11 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/caster.d.ts.map +1 -1
- package/dist/api/caster.js +2 -1
- package/dist/api/code-converters.d.ts +5 -1
- package/dist/api/code-converters.d.ts.map +1 -1
- package/dist/api/code-converters.js +24 -7
- package/dist/api/config.d.ts +7 -0
- package/dist/api/config.d.ts.map +1 -1
- package/dist/api/config.js +1 -1
- package/dist/api/decorators.d.ts +1 -0
- package/dist/api/decorators.d.ts.map +1 -1
- package/dist/api/decorators.js +1 -1
- package/dist/api/guards.d.ts +37 -0
- package/dist/api/guards.d.ts.map +1 -0
- package/dist/api/guards.js +96 -0
- package/dist/api/sonamu.d.ts +14 -1
- package/dist/api/sonamu.d.ts.map +1 -1
- package/dist/api/sonamu.js +121 -25
- package/dist/api/websocket-helpers.d.ts +6 -1
- package/dist/api/websocket-helpers.d.ts.map +1 -1
- package/dist/api/websocket-helpers.js +7 -4
- package/dist/auth/auth-entity-policy.d.ts +32 -0
- package/dist/auth/auth-entity-policy.d.ts.map +1 -0
- package/dist/auth/auth-entity-policy.js +44 -0
- package/dist/auth/auth-secret.d.ts +24 -0
- package/dist/auth/auth-secret.d.ts.map +1 -0
- package/dist/auth/auth-secret.js +46 -0
- package/dist/auth/base-url.d.ts +39 -0
- package/dist/auth/base-url.d.ts.map +1 -0
- package/dist/auth/base-url.js +84 -0
- package/dist/auth/better-auth-entities.js +20 -7
- package/dist/auth/better-auth-options.d.ts +15 -0
- package/dist/auth/better-auth-options.d.ts.map +1 -0
- package/dist/auth/better-auth-options.js +38 -0
- package/dist/auth/better-auth-request.d.ts +8 -1
- package/dist/auth/better-auth-request.d.ts.map +1 -1
- package/dist/auth/better-auth-request.js +26 -3
- package/dist/auth/plugins/entity-definitions/admin.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/admin.js +6 -1
- package/dist/auth/plugins/entity-definitions/anonymous.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/anonymous.js +2 -1
- package/dist/auth/plugins/entity-definitions/api-key.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/api-key.js +18 -2
- package/dist/auth/plugins/entity-definitions/audit-log.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/audit-log.js +27 -1
- package/dist/auth/plugins/entity-definitions/jwt.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/jwt.js +4 -2
- package/dist/auth/plugins/entity-definitions/organization.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/organization.js +15 -1
- package/dist/auth/plugins/entity-definitions/passkey.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/passkey.js +9 -1
- package/dist/auth/plugins/entity-definitions/phone-number.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/phone-number.js +2 -1
- package/dist/auth/plugins/entity-definitions/sso.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/sso.js +7 -3
- package/dist/auth/plugins/entity-definitions/two-factor.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/two-factor.js +8 -3
- package/dist/auth/verify-email-guard.d.ts +37 -0
- package/dist/auth/verify-email-guard.d.ts.map +1 -0
- package/dist/auth/verify-email-guard.js +48 -0
- package/dist/bin/build-config.d.ts.map +1 -1
- package/dist/bin/build-config.js +6 -2
- package/dist/bin/typecheck-web.js +1 -1
- package/dist/cache/decorator.d.ts +12 -0
- package/dist/cache/decorator.d.ts.map +1 -1
- package/dist/cache/decorator.js +86 -19
- package/dist/cache/index.d.ts +1 -1
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +3 -3
- package/dist/cache/types.d.ts +16 -0
- package/dist/cache/types.d.ts.map +1 -1
- package/dist/cli/plugin-loader.d.ts +17 -0
- package/dist/cli/plugin-loader.d.ts.map +1 -0
- package/dist/cli/plugin-loader.js +178 -0
- package/dist/cli/plugin.d.ts +65 -0
- package/dist/cli/plugin.d.ts.map +1 -0
- package/dist/cli/plugin.js +7 -0
- package/dist/code-formatting/oxfmt.js +1 -1
- package/dist/code-formatting/oxlint.js +1 -1
- package/dist/database/db.d.ts +35 -10
- package/dist/database/db.d.ts.map +1 -1
- package/dist/database/db.js +37 -4
- package/dist/database/puri.d.ts +8 -0
- package/dist/database/puri.d.ts.map +1 -1
- package/dist/database/puri.js +108 -171
- package/dist/database/puri.types.d.ts +2 -0
- package/dist/database/puri.types.d.ts.map +1 -1
- package/dist/database/puri.types.js +1 -1
- package/dist/database/sql-expression-brand.d.ts +10 -0
- package/dist/database/sql-expression-brand.d.ts.map +1 -0
- package/dist/database/sql-expression-brand.js +10 -0
- package/dist/dict/en.d.ts +1 -0
- package/dist/dict/en.d.ts.map +1 -1
- package/dist/dict/en.js +2 -1
- package/dist/dict/ko.d.ts +1 -0
- package/dist/dict/ko.d.ts.map +1 -1
- package/dist/dict/ko.js +2 -1
- package/dist/dict/sonamu-dictionary.d.ts +23 -1
- package/dist/dict/sonamu-dictionary.d.ts.map +1 -1
- package/dist/dict/sonamu-dictionary.js +187 -32
- package/dist/entity/entity-codegen-safety.d.ts +105 -0
- package/dist/entity/entity-codegen-safety.d.ts.map +1 -0
- package/dist/entity/entity-codegen-safety.js +70 -0
- package/dist/entity/entity-manager.d.ts +32 -0
- package/dist/entity/entity-manager.d.ts.map +1 -1
- package/dist/entity/entity-manager.js +21 -2
- package/dist/entity/entity.d.ts +5 -2
- package/dist/entity/entity.d.ts.map +1 -1
- package/dist/entity/entity.js +19 -8
- package/dist/env.d.ts +19 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +31 -3
- package/dist/filter/utils.d.ts +2 -2
- package/dist/filter/utils.d.ts.map +1 -1
- package/dist/filter/utils.js +16 -11
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -6
- package/dist/logger/configure.d.ts.map +1 -1
- package/dist/logger/configure.js +10 -3
- package/dist/migration/code-generation.d.ts.map +1 -1
- package/dist/migration/code-generation.js +177 -96
- package/dist/migration/migrator.d.ts +16 -0
- package/dist/migration/migrator.d.ts.map +1 -1
- package/dist/migration/migrator.js +28 -3
- package/dist/migration/slack-confirm.d.ts +6 -2
- package/dist/migration/slack-confirm.d.ts.map +1 -1
- package/dist/migration/slack-confirm.js +11 -9
- package/dist/ssr/errors.d.ts +13 -0
- package/dist/ssr/errors.d.ts.map +1 -0
- package/dist/ssr/errors.js +12 -0
- package/dist/ssr/index.d.ts +1 -0
- package/dist/ssr/index.d.ts.map +1 -1
- package/dist/ssr/index.js +5 -2
- package/dist/ssr/renderer.d.ts +6 -0
- package/dist/ssr/renderer.d.ts.map +1 -1
- package/dist/ssr/renderer.js +10 -1
- package/dist/storage/base-file.d.ts +6 -2
- package/dist/storage/base-file.d.ts.map +1 -1
- package/dist/storage/base-file.js +10 -5
- package/dist/storage/buffered-file.d.ts +1 -0
- package/dist/storage/buffered-file.d.ts.map +1 -1
- package/dist/storage/buffered-file.js +5 -2
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +4 -2
- package/dist/storage/upload-safety.d.ts +47 -0
- package/dist/storage/upload-safety.d.ts.map +1 -0
- package/dist/storage/upload-safety.js +112 -0
- package/dist/stream/ws.d.ts +6 -0
- package/dist/stream/ws.d.ts.map +1 -1
- package/dist/stream/ws.js +52 -6
- package/dist/syncer/code-generator.d.ts +2 -0
- package/dist/syncer/code-generator.d.ts.map +1 -1
- package/dist/syncer/code-generator.js +21 -7
- package/dist/template/implementations/entity.template.d.ts.map +1 -1
- package/dist/template/implementations/entity.template.js +40 -35
- package/dist/template/implementations/generated.template.d.ts +10 -0
- package/dist/template/implementations/generated.template.d.ts.map +1 -1
- package/dist/template/implementations/generated.template.js +46 -7
- package/dist/template/implementations/generated_http.template.d.ts.map +1 -1
- package/dist/template/implementations/generated_http.template.js +3 -1
- package/dist/template/implementations/init_types.template.d.ts.map +1 -1
- package/dist/template/implementations/init_types.template.js +3 -8
- package/dist/template/implementations/model.template.d.ts.map +1 -1
- package/dist/template/implementations/model.template.js +9 -2
- package/dist/template/implementations/sd.template.d.ts +0 -1
- package/dist/template/implementations/sd.template.d.ts.map +1 -1
- package/dist/template/implementations/sd.template.js +2 -5
- package/dist/template/implementations/view_form.template.d.ts.map +1 -1
- package/dist/template/implementations/view_form.template.js +8 -6
- package/dist/template/implementations/view_list.template.d.ts.map +1 -1
- package/dist/template/implementations/view_list.template.js +8 -4
- package/dist/testing/dev-test-routes.d.ts.map +1 -1
- package/dist/testing/dev-test-routes.js +7 -4
- package/dist/tooling/__tests__/helpers/sonamu-test-root.js +1 -1
- package/dist/tooling/cdd-service.js +1 -1
- package/dist/tooling/index.d.ts +1 -0
- package/dist/tooling/index.d.ts.map +1 -1
- package/dist/tooling/index.js +3 -1
- package/dist/tsdown.api.config.js +1 -1
- package/dist/types/types.d.ts +81 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/types.js +2 -1
- package/dist/ui/__tests__/ui-test-helpers.d.ts +10 -0
- package/dist/ui/__tests__/ui-test-helpers.d.ts.map +1 -0
- package/dist/ui/__tests__/ui-test-helpers.js +75 -0
- package/dist/ui/access-guard.d.ts +42 -0
- package/dist/ui/access-guard.d.ts.map +1 -0
- package/dist/ui/access-guard.js +94 -0
- package/dist/ui/api.d.ts.map +1 -1
- package/dist/ui/api.js +40 -95
- package/dist/ui/dev-host-guard.d.ts +28 -0
- package/dist/ui/dev-host-guard.d.ts.map +1 -0
- package/dist/ui/dev-host-guard.js +67 -0
- package/dist/ui/migrations-api.d.ts.map +1 -1
- package/dist/ui/migrations-api.js +72 -16
- package/dist/ui/open-vscode-api.d.ts +4 -0
- package/dist/ui/open-vscode-api.d.ts.map +1 -0
- package/dist/ui/open-vscode-api.js +65 -0
- package/dist/ui/scaffolding-api.d.ts +18 -0
- package/dist/ui/scaffolding-api.d.ts.map +1 -0
- package/dist/ui/scaffolding-api.js +111 -0
- package/dist/ui/ui-db-presets.d.ts +19 -0
- package/dist/ui/ui-db-presets.d.ts.map +1 -0
- package/dist/ui/ui-db-presets.js +34 -0
- package/dist/ui/ui-request-guard.d.ts +11 -0
- package/dist/ui/ui-request-guard.d.ts.map +1 -0
- package/dist/ui/ui-request-guard.js +38 -0
- package/dist/ui-web/assets/{index-QDMyy31K.js → index-BPqP-tug.js} +39 -39
- package/dist/ui-web/index.html +2 -1
- package/package.json +1 -1
- package/src/__tests__/env.test.ts +121 -2
- package/src/api/__tests__/code-converters.union.test.ts +97 -0
- package/src/api/__tests__/guards.test.ts +233 -0
- package/src/api/__tests__/sonamu.auth-secret.test.ts +77 -0
- package/src/api/__tests__/sonamu.dev-host-guard.test.ts +130 -0
- package/src/api/__tests__/sonamu.guards.test.ts +327 -0
- package/src/api/__tests__/sonamu.upload-limits.test.ts +156 -0
- package/src/api/__tests__/websocket-helpers.test.ts +101 -0
- package/src/api/caster.ts +7 -0
- package/src/api/code-converters.ts +44 -12
- package/src/api/config.ts +8 -0
- package/src/api/decorators.ts +1 -0
- package/src/api/guards.ts +130 -0
- package/src/api/sonamu.ts +195 -39
- package/src/api/websocket-helpers.ts +11 -6
- package/src/auth/__tests__/auth-entity-policy.test.ts +59 -0
- package/src/auth/__tests__/auth-secret.test.ts +133 -0
- package/src/auth/__tests__/base-url-host-poisoning.test.ts +193 -0
- package/src/auth/__tests__/base-url.test.ts +117 -0
- package/src/auth/__tests__/better-auth-options.test.ts +194 -0
- package/src/auth/__tests__/better-auth-request.test.ts +45 -0
- package/src/auth/__tests__/verify-email-guard.test.ts +228 -0
- package/src/auth/auth-entity-policy.ts +48 -0
- package/src/auth/auth-secret.ts +77 -0
- package/src/auth/base-url.ts +109 -0
- package/src/auth/better-auth-entities.ts +30 -30
- package/src/auth/better-auth-options.ts +39 -0
- package/src/auth/better-auth-request.ts +28 -1
- package/src/auth/plugins/entity-definitions/admin.ts +5 -0
- package/src/auth/plugins/entity-definitions/anonymous.ts +9 -1
- package/src/auth/plugins/entity-definitions/api-key.ts +53 -16
- package/src/auth/plugins/entity-definitions/audit-log.ts +122 -21
- package/src/auth/plugins/entity-definitions/jwt.ts +5 -4
- package/src/auth/plugins/entity-definitions/organization.ts +26 -6
- package/src/auth/plugins/entity-definitions/passkey.ts +8 -7
- package/src/auth/plugins/entity-definitions/phone-number.ts +1 -0
- package/src/auth/plugins/entity-definitions/sso.ts +20 -15
- package/src/auth/plugins/entity-definitions/two-factor.ts +21 -5
- package/src/auth/verify-email-guard.ts +82 -0
- package/src/bin/__tests__/build-web-command.test.ts +24 -0
- package/src/bin/build-config.ts +5 -0
- package/src/cache/decorator.ts +116 -21
- package/src/cache/index.ts +6 -1
- package/src/cache/types.ts +18 -0
- package/src/cli/plugin-loader.ts +238 -0
- package/src/cli/plugin.ts +82 -0
- package/src/database/__tests__/db.test.ts +64 -1
- package/src/database/__tests__/puri.test.ts +56 -0
- package/src/database/db.ts +56 -6
- package/src/database/puri.ts +135 -125
- package/src/database/puri.types.test-d.ts +12 -0
- package/src/database/puri.types.ts +3 -0
- package/src/database/sql-expression-brand.ts +9 -0
- package/src/dict/__tests__/sonamu-dictionary.test.ts +217 -0
- package/src/dict/en.ts +2 -0
- package/src/dict/ko.ts +2 -0
- package/src/dict/sonamu-dictionary.ts +298 -38
- package/src/entity/__tests__/entity-codegen-safety.test.ts +326 -0
- package/src/entity/entity-codegen-safety.ts +126 -0
- package/src/entity/entity-manager.ts +29 -1
- package/src/entity/entity.ts +26 -12
- package/src/env.ts +52 -1
- package/src/filter/utils.ts +19 -7
- package/src/index.ts +4 -3
- package/src/logger/__tests__/configure.test.ts +51 -1
- package/src/logger/configure.ts +14 -1
- package/src/migration/__tests__/code-generation.escape.test.ts +322 -0
- package/src/migration/__tests__/migrator.test.ts +76 -1
- package/src/migration/code-generation.ts +247 -117
- package/src/migration/migrator.ts +45 -4
- package/src/migration/slack-confirm.ts +19 -5
- package/src/ssr/__tests__/preload-guard.test.ts +274 -0
- package/src/ssr/errors.ts +11 -0
- package/src/ssr/index.ts +1 -0
- package/src/ssr/renderer.ts +12 -0
- package/src/storage/__tests__/upload-safety.test.ts +179 -0
- package/src/storage/base-file.ts +8 -4
- package/src/storage/buffered-file.ts +3 -1
- package/src/storage/index.ts +9 -0
- package/src/storage/upload-safety.ts +155 -0
- package/src/stream/__tests__/ws.test.ts +97 -0
- package/src/stream/ws.ts +67 -5
- package/src/syncer/__tests__/code-generator-path-containment.test.ts +105 -0
- package/src/syncer/code-generator.ts +34 -6
- package/src/template/__tests__/generated.template.search-text.test.ts +5 -1
- package/src/template/__tests__/model.template.auth-entity.test.ts +42 -0
- package/src/template/__tests__/save-params.template.test.ts +219 -0
- package/src/template/implementations/entity.template.ts +53 -48
- package/src/template/implementations/generated.template.ts +87 -12
- package/src/template/implementations/generated_http.template.ts +5 -0
- package/src/template/implementations/init_types.template.ts +4 -22
- package/src/template/implementations/model.template.ts +15 -1
- package/src/template/implementations/sd.template.ts +4 -5
- package/src/template/implementations/view_form.template.ts +13 -5
- package/src/template/implementations/view_list.template.ts +12 -3
- package/src/testing/__tests__/dev-test-routes.test.ts +99 -0
- package/src/testing/dev-test-routes.ts +7 -3
- package/src/tooling/__tests__/cli-plugins.contract.test.ts +273 -0
- package/src/tooling/index.ts +1 -0
- package/src/types/types.ts +22 -1
- package/src/ui/__tests__/access-guard.test.ts +217 -0
- package/src/ui/__tests__/dev-host-guard.test.ts +161 -0
- package/src/ui/__tests__/fixture-api.test.ts +92 -0
- package/src/ui/__tests__/migrations-api.test.ts +352 -0
- package/src/ui/__tests__/open-vscode.test.ts +151 -0
- package/src/ui/__tests__/scaffolding-api.test.ts +232 -0
- package/src/ui/__tests__/ui-process-routes.test.ts +166 -0
- package/src/ui/__tests__/ui-test-helpers.ts +71 -0
- package/src/ui/access-guard.ts +115 -0
- package/src/ui/api.ts +65 -198
- package/src/ui/dev-host-guard.ts +109 -0
- package/src/ui/migrations-api.ts +130 -24
- package/src/ui/open-vscode-api.ts +82 -0
- package/src/ui/scaffolding-api.ts +177 -0
- package/src/ui/ui-db-presets.ts +41 -0
- package/src/ui/ui-request-guard.ts +53 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** A positional argument of a plugin command, in declaration order. */
|
|
2
|
+
export interface SonamuCliArgument {
|
|
3
|
+
/** camelCase key under `input.arguments`. */
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly description?: string;
|
|
6
|
+
/** Defaults to "string". */
|
|
7
|
+
readonly type?: "string" | "integer";
|
|
8
|
+
/** Defaults to true. Optional arguments must come after required ones. */
|
|
9
|
+
readonly required?: boolean;
|
|
10
|
+
/** Only the last argument may be variadic; its value is an array, or undefined when none is given. */
|
|
11
|
+
readonly variadic?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** An option of a plugin command. The camelCase key `maxRounds` is the flag `--max-rounds`. */
|
|
15
|
+
export interface SonamuCliOption {
|
|
16
|
+
readonly type: "string" | "integer" | "boolean";
|
|
17
|
+
readonly description?: string;
|
|
18
|
+
/** string and integer only. */
|
|
19
|
+
readonly required?: boolean;
|
|
20
|
+
/** string and integer only: the flag may repeat; the value is an array, or undefined when absent. */
|
|
21
|
+
readonly multiple?: boolean;
|
|
22
|
+
/** string only. */
|
|
23
|
+
readonly choices?: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type SonamuCliArgumentValue = string | number | readonly string[] | readonly number[];
|
|
27
|
+
export type SonamuCliOptionValue = SonamuCliArgumentValue | boolean;
|
|
28
|
+
|
|
29
|
+
export interface SonamuCliCommandInput {
|
|
30
|
+
readonly arguments: Readonly<Record<string, SonamuCliArgumentValue | undefined>>;
|
|
31
|
+
readonly options: Readonly<Record<string, SonamuCliOptionValue | undefined>>;
|
|
32
|
+
/** True only for a `mutation.dryRun` command invoked without `--execute`. */
|
|
33
|
+
readonly dryRun: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface SonamuCliCommandContext {
|
|
37
|
+
/** Dotted command id, for example "search.inspect". */
|
|
38
|
+
readonly command: string;
|
|
39
|
+
/** True when the caller asked for JSON output. */
|
|
40
|
+
readonly json: boolean;
|
|
41
|
+
/** True when the CLI may prompt on a terminal. */
|
|
42
|
+
readonly interactive: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type SonamuCliCommandResult =
|
|
46
|
+
| object
|
|
47
|
+
| string
|
|
48
|
+
| number
|
|
49
|
+
| boolean
|
|
50
|
+
| null
|
|
51
|
+
| void
|
|
52
|
+
| AsyncIterable<object>;
|
|
53
|
+
|
|
54
|
+
export interface SonamuCliCommand {
|
|
55
|
+
/** Command words after `sonamu`, for example ["search", "inspect"]. */
|
|
56
|
+
readonly path: readonly [string, ...string[]];
|
|
57
|
+
readonly description?: string;
|
|
58
|
+
readonly arguments?: readonly SonamuCliArgument[];
|
|
59
|
+
readonly options?: Readonly<Record<string, SonamuCliOption>>;
|
|
60
|
+
/**
|
|
61
|
+
* Present when the command changes state. It then passes the same approval boundary as built-in
|
|
62
|
+
* mutation commands (`--execute --confirm`, or a prompt on a terminal). With `dryRun: true` the
|
|
63
|
+
* command also runs unapproved without `--execute`, with `input.dryRun` set, and must change
|
|
64
|
+
* nothing in that mode.
|
|
65
|
+
*/
|
|
66
|
+
readonly mutation?: { readonly dryRun: boolean };
|
|
67
|
+
/** Runs after `Sonamu.init`. An object result is printed; an async iterable is streamed as events. */
|
|
68
|
+
run(
|
|
69
|
+
input: SonamuCliCommandInput,
|
|
70
|
+
context: SonamuCliCommandContext,
|
|
71
|
+
): SonamuCliCommandResult | Promise<SonamuCliCommandResult>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Commands contributed to the `sonamu` CLI through `cli.plugins` in sonamu.config.ts. */
|
|
75
|
+
export interface SonamuCliPlugin {
|
|
76
|
+
readonly name: string;
|
|
77
|
+
readonly commands: readonly SonamuCliCommand[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function defineCliPlugin(plugin: SonamuCliPlugin): SonamuCliPlugin {
|
|
81
|
+
return plugin;
|
|
82
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterEach, describe, expect, it } from "vitest";
|
|
2
2
|
|
|
3
3
|
import { type EnvironmentSnapshots } from "../../env";
|
|
4
|
-
import { DBClass } from "../db";
|
|
4
|
+
import { DBClass, DEFAULT_STATEMENT_TIMEOUT_MS, withDefaultStatementTimeout } from "../db";
|
|
5
5
|
|
|
6
6
|
describe("DBClass.generateDBConfig", () => {
|
|
7
7
|
const originalEnv = { ...process.env };
|
|
@@ -174,3 +174,66 @@ describe("DBClass.generateDBConfig", () => {
|
|
|
174
174
|
});
|
|
175
175
|
});
|
|
176
176
|
});
|
|
177
|
+
|
|
178
|
+
describe("withDefaultStatementTimeout", () => {
|
|
179
|
+
it("connection.options에 세션 수준 statement_timeout을 넣는다", () => {
|
|
180
|
+
const config = withDefaultStatementTimeout({ client: "pg", connection: { host: "h" } });
|
|
181
|
+
|
|
182
|
+
expect(config.connection).toEqual({
|
|
183
|
+
host: "h",
|
|
184
|
+
options: `-c statement_timeout=${DEFAULT_STATEMENT_TIMEOUT_MS}`,
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("기존 connection.options는 유지하고 뒤에 둔다", () => {
|
|
189
|
+
const config = withDefaultStatementTimeout(
|
|
190
|
+
{ client: "pg", connection: { options: "-c search_path=app" } },
|
|
191
|
+
500,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(config.connection).toEqual({
|
|
195
|
+
options: "-c statement_timeout=500 -c search_path=app",
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("사용자가 statement_timeout을 지정했으면 덮어쓰지 않는다", () => {
|
|
200
|
+
const byField = { client: "pg", connection: { statement_timeout: false as const } };
|
|
201
|
+
const byOptions = { client: "pg", connection: { options: "-c statement_timeout=0" } };
|
|
202
|
+
|
|
203
|
+
expect(withDefaultStatementTimeout(byField)).toBe(byField);
|
|
204
|
+
expect(withDefaultStatementTimeout(byOptions)).toBe(byOptions);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("잘못된 타임아웃 값은 거부한다", () => {
|
|
208
|
+
expect(() => withDefaultStatementTimeout({ client: "pg", connection: {} }, Number.NaN)).toThrow(
|
|
209
|
+
"Invalid statement timeout",
|
|
210
|
+
);
|
|
211
|
+
expect(() => withDefaultStatementTimeout({ client: "pg", connection: {} }, 0)).toThrow(
|
|
212
|
+
"Invalid statement timeout",
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("DBClass.getDB 런타임 풀 statement_timeout", () => {
|
|
218
|
+
const originalNodeEnv = process.env.NODE_ENV;
|
|
219
|
+
|
|
220
|
+
afterEach(() => {
|
|
221
|
+
process.env.NODE_ENV = originalNodeEnv;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("요청 처리용 w/r 풀에만 기본 statement_timeout을 적용하고 마이그레이션용 프리셋은 제외한다", async () => {
|
|
225
|
+
process.env.NODE_ENV = "production";
|
|
226
|
+
const db = new DBClass();
|
|
227
|
+
db.setConfig(db.generateDBConfig({ database: "pg", defaultOptions: {} }, "Miomock"));
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
const expected = `-c statement_timeout=${DEFAULT_STATEMENT_TIMEOUT_MS}`;
|
|
231
|
+
|
|
232
|
+
expect(db.getDB("w").client.config.connection).toMatchObject({ options: expected });
|
|
233
|
+
expect(db.getDB("r").client.config.connection).toMatchObject({ options: expected });
|
|
234
|
+
expect(db.getDB("production").client.config.connection).not.toHaveProperty("options");
|
|
235
|
+
} finally {
|
|
236
|
+
await db.destroy();
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -328,6 +328,42 @@ describe("Puri onConflict JSON 직렬화", () => {
|
|
|
328
328
|
});
|
|
329
329
|
});
|
|
330
330
|
|
|
331
|
+
describe("Puri SqlExpression 브랜드 판정", () => {
|
|
332
|
+
// 클라이언트 JSON처럼 검증 없이 들어온 sql_expression 모양 객체입니다.
|
|
333
|
+
const forgedPayload = () =>
|
|
334
|
+
JSON.parse(
|
|
335
|
+
'{"_type":"sql_expression","_return":"string","_sql":"(SELECT password FROM users WHERE id = 1)","_params":[]}',
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
it("onConflict 객체 update의 위조 표현식을 SQL이 아닌 JSON 값으로 바인딩한다", () => {
|
|
339
|
+
const forged = forgedPayload();
|
|
340
|
+
const query = withJsonTableSpec(["payload"], () =>
|
|
341
|
+
usersQuery()
|
|
342
|
+
.insert({ id: 1, name: "user", department_id: null })
|
|
343
|
+
.onConflict("id", { update: { payload: forged } })
|
|
344
|
+
.knexQuery.toSQL(),
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
expect(query.sql).not.toContain("SELECT password");
|
|
348
|
+
expect(query.bindings.at(-1)).toBe(JSON.stringify(forged));
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("orderBy의 위조 표현식은 거부하고 정식 표현식은 SQL로 넣는다", () => {
|
|
352
|
+
expect(() => usersQuery().orderBy(forgedPayload())).toThrow(TypeError);
|
|
353
|
+
|
|
354
|
+
const query = usersQuery()
|
|
355
|
+
.orderBy(Puri.rawNumber("length(??)", ["users.name"]), "desc")
|
|
356
|
+
.toQuery();
|
|
357
|
+
expect(query).toContain('order by length("users"."name") desc');
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("JSON으로 왕복한 정식 표현식은 브랜드를 잃어 SQL로 쓰이지 않는다", () => {
|
|
361
|
+
const roundTripped = JSON.parse(JSON.stringify(Puri.rawString("'x'")));
|
|
362
|
+
|
|
363
|
+
expect(() => usersQuery().select({ leaked: roundTripped })).toThrow(TypeError);
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
331
367
|
describe("Puri ensureJoin", () => {
|
|
332
368
|
it("기존의 동일한 JOIN을 재사용한다", () => {
|
|
333
369
|
const query = usersQuery()
|
|
@@ -501,3 +537,23 @@ describe("Puri ensureJoin", () => {
|
|
|
501
537
|
expect(query.toQuery().match(/join "departments" as "department"/g)).toHaveLength(1);
|
|
502
538
|
});
|
|
503
539
|
});
|
|
540
|
+
|
|
541
|
+
describe("Puri limit/offset 입력 검증", () => {
|
|
542
|
+
it.each([Number.NaN, Number.POSITIVE_INFINITY, -1, 1.5])("limit(%s)는 오류를 던진다", (value) => {
|
|
543
|
+
expect(() => usersQuery().limit(value)).toThrow("Invalid limit");
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it.each([Number.NaN, Number.POSITIVE_INFINITY, -1, 1.5])(
|
|
547
|
+
"offset(%s)는 오류를 던진다",
|
|
548
|
+
(value) => {
|
|
549
|
+
expect(() => usersQuery().offset(value)).toThrow("Invalid offset");
|
|
550
|
+
},
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
it("0 이상의 정수는 그대로 LIMIT/OFFSET으로 만든다", () => {
|
|
554
|
+
expect(usersQuery().limit(0).offset(0).toQuery()).toBe('select * from "users" limit 0');
|
|
555
|
+
expect(usersQuery().limit(10).offset(20).toQuery()).toBe(
|
|
556
|
+
'select * from "users" limit 10 offset 20',
|
|
557
|
+
);
|
|
558
|
+
});
|
|
559
|
+
});
|
package/src/database/db.ts
CHANGED
|
@@ -56,7 +56,51 @@ export type SonamuReadonlyDBPreset =
|
|
|
56
56
|
export type SonamuDBPreset = SonamuMainDBPreset | SonamuReadonlyDBPreset;
|
|
57
57
|
export type DBPreset = "w" | "r" | SonamuDBPreset;
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
/** preset별 PostgreSQL 연결 설정. `connection`은 `Knex.PgConnectionConfig` 객체만 받습니다. */
|
|
60
|
+
export type SonamuDBConfig = Record<SonamuDBPreset, DatabaseConfig>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 요청 처리용 런타임 풀(`DB.getDB("w" | "r")`)에 적용하는 기본 `statement_timeout`(ms).
|
|
64
|
+
* 커넥션 획득 대기 한도(30초)보다 짧게 잡아, 느린 쿼리가 풀을 점유해도 대기 중인 요청이
|
|
65
|
+
* 획득 타임아웃으로 실패하기 전에 커넥션을 돌려받게 한다.
|
|
66
|
+
*/
|
|
67
|
+
export const DEFAULT_STATEMENT_TIMEOUT_MS = 15_000;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 커넥션 설정에 세션 수준 `statement_timeout` 기본값을 넣은 새 설정을 반환합니다.
|
|
71
|
+
* 이미 지정되어 있거나 `connection`이 일반 객체가 아니면 받은 설정을 그대로 반환합니다.
|
|
72
|
+
*
|
|
73
|
+
* - 접속 시 startup 파라미터(`options: "-c statement_timeout=..."`)로 한 번만 설정하므로
|
|
74
|
+
* 쿼리마다 추가 비용이 없습니다. pg-native(libpq)는 `statement_timeout` 필드를 전달하지
|
|
75
|
+
* 않고 `options`만 전달하므로 두 드라이버 모두에 적용되도록 `options`를 씁니다.
|
|
76
|
+
* - 사용자가 `connection.statement_timeout` 키를 지정했거나(`false`/`0` 포함)
|
|
77
|
+
* `connection.options`에 `statement_timeout`을 이미 넣었다면 그대로 둡니다.
|
|
78
|
+
* - 기존 `connection.options` 값은 유지하고 그 앞에 붙입니다.
|
|
79
|
+
*
|
|
80
|
+
* @throws {Error} `timeoutMs`가 양의 안전한 정수가 아닐 때
|
|
81
|
+
*/
|
|
82
|
+
export function withDefaultStatementTimeout(
|
|
83
|
+
config: DatabaseConfig,
|
|
84
|
+
timeoutMs: number = DEFAULT_STATEMENT_TIMEOUT_MS,
|
|
85
|
+
): DatabaseConfig {
|
|
86
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0) {
|
|
87
|
+
throw new Error(`Invalid statement timeout: ${String(timeoutMs)}`);
|
|
88
|
+
}
|
|
89
|
+
const connection = config.connection;
|
|
90
|
+
if (!isPlainObject(connection)) {
|
|
91
|
+
return config;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const existingOptions = connection.options ?? "";
|
|
95
|
+
if ("statement_timeout" in connection || existingOptions.includes("statement_timeout")) {
|
|
96
|
+
return config;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const options = [`-c statement_timeout=${timeoutMs}`, existingOptions]
|
|
100
|
+
.filter((option) => option !== "")
|
|
101
|
+
.join(" ");
|
|
102
|
+
return { ...config, connection: { ...connection, options } };
|
|
103
|
+
}
|
|
60
104
|
|
|
61
105
|
function isConcretePreset(value: DBPreset): value is SonamuDBPreset {
|
|
62
106
|
return value !== "w" && value !== "r";
|
|
@@ -235,7 +279,9 @@ export class DBClass {
|
|
|
235
279
|
|
|
236
280
|
if (!this[instanceName]) {
|
|
237
281
|
const config = this.getDBConfig(which);
|
|
238
|
-
|
|
282
|
+
// 비인증 요청의 느린 쿼리가 풀 전체를 무기한 점유하지 않도록 요청 처리용 풀에만 적용한다.
|
|
283
|
+
// 마이그레이션·픽스처·테스트용 인스턴스는 긴 DDL/대량 복사가 끊기지 않도록 제외한다.
|
|
284
|
+
this[instanceName] = createKnexInstance(withDefaultStatementTimeout(config));
|
|
239
285
|
}
|
|
240
286
|
|
|
241
287
|
return this[instanceName];
|
|
@@ -271,7 +317,11 @@ export class DBClass {
|
|
|
271
317
|
return db;
|
|
272
318
|
}
|
|
273
319
|
|
|
274
|
-
|
|
320
|
+
/**
|
|
321
|
+
* preset의 연결 설정을 반환합니다. `"w"`·`"r"`은 현재 환경의 preset으로 해석하며,
|
|
322
|
+
* `getDB()`가 요청 처리용 풀에 더하는 기본 `statement_timeout`은 포함하지 않습니다.
|
|
323
|
+
*/
|
|
324
|
+
getDBConfig(which: DBPreset): DatabaseConfig {
|
|
275
325
|
const dbConfig = this.getCurrentConfig();
|
|
276
326
|
|
|
277
327
|
if (isConcretePreset(which)) {
|
|
@@ -349,7 +399,7 @@ export class DBClass {
|
|
|
349
399
|
return snapshots[preset];
|
|
350
400
|
};
|
|
351
401
|
|
|
352
|
-
const mainConfig = (preset: SonamuMainDBPreset):
|
|
402
|
+
const mainConfig = (preset: SonamuMainDBPreset): DatabaseConfig =>
|
|
353
403
|
mergeConfigs(defaultKnexConfig, {
|
|
354
404
|
connection: connectionFromEnv({
|
|
355
405
|
baseName,
|
|
@@ -358,8 +408,8 @@ export class DBClass {
|
|
|
358
408
|
env: envForPreset(preset),
|
|
359
409
|
}),
|
|
360
410
|
});
|
|
361
|
-
const readonlyConfig = (environment: SonamuEnvironment):
|
|
362
|
-
mergeConfigs<
|
|
411
|
+
const readonlyConfig = (environment: SonamuEnvironment): DatabaseConfig =>
|
|
412
|
+
mergeConfigs<DatabaseConfig>(defaultKnexConfig, mainConfig(environment), {
|
|
363
413
|
connection: connectionFromEnv({
|
|
364
414
|
baseName,
|
|
365
415
|
suffix: environment,
|