sonamu 0.13.10 → 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.
Files changed (330) hide show
  1. package/dist/api/caster.d.ts.map +1 -1
  2. package/dist/api/caster.js +2 -1
  3. package/dist/api/code-converters.d.ts +5 -1
  4. package/dist/api/code-converters.d.ts.map +1 -1
  5. package/dist/api/code-converters.js +24 -7
  6. package/dist/api/config.d.ts +7 -0
  7. package/dist/api/config.d.ts.map +1 -1
  8. package/dist/api/config.js +1 -1
  9. package/dist/api/decorators.d.ts +1 -0
  10. package/dist/api/decorators.d.ts.map +1 -1
  11. package/dist/api/decorators.js +1 -1
  12. package/dist/api/guards.d.ts +37 -0
  13. package/dist/api/guards.d.ts.map +1 -0
  14. package/dist/api/guards.js +96 -0
  15. package/dist/api/sonamu.d.ts +14 -1
  16. package/dist/api/sonamu.d.ts.map +1 -1
  17. package/dist/api/sonamu.js +121 -25
  18. package/dist/api/websocket-helpers.d.ts +6 -1
  19. package/dist/api/websocket-helpers.d.ts.map +1 -1
  20. package/dist/api/websocket-helpers.js +7 -4
  21. package/dist/auth/auth-entity-policy.d.ts +32 -0
  22. package/dist/auth/auth-entity-policy.d.ts.map +1 -0
  23. package/dist/auth/auth-entity-policy.js +44 -0
  24. package/dist/auth/auth-secret.d.ts +24 -0
  25. package/dist/auth/auth-secret.d.ts.map +1 -0
  26. package/dist/auth/auth-secret.js +46 -0
  27. package/dist/auth/base-url.d.ts +39 -0
  28. package/dist/auth/base-url.d.ts.map +1 -0
  29. package/dist/auth/base-url.js +84 -0
  30. package/dist/auth/better-auth-entities.js +20 -7
  31. package/dist/auth/better-auth-options.d.ts +15 -0
  32. package/dist/auth/better-auth-options.d.ts.map +1 -0
  33. package/dist/auth/better-auth-options.js +38 -0
  34. package/dist/auth/better-auth-request.d.ts +8 -1
  35. package/dist/auth/better-auth-request.d.ts.map +1 -1
  36. package/dist/auth/better-auth-request.js +26 -3
  37. package/dist/auth/plugins/entity-definitions/admin.d.ts.map +1 -1
  38. package/dist/auth/plugins/entity-definitions/admin.js +6 -1
  39. package/dist/auth/plugins/entity-definitions/anonymous.d.ts.map +1 -1
  40. package/dist/auth/plugins/entity-definitions/anonymous.js +2 -1
  41. package/dist/auth/plugins/entity-definitions/api-key.d.ts.map +1 -1
  42. package/dist/auth/plugins/entity-definitions/api-key.js +18 -2
  43. package/dist/auth/plugins/entity-definitions/audit-log.d.ts.map +1 -1
  44. package/dist/auth/plugins/entity-definitions/audit-log.js +27 -1
  45. package/dist/auth/plugins/entity-definitions/jwt.d.ts.map +1 -1
  46. package/dist/auth/plugins/entity-definitions/jwt.js +4 -2
  47. package/dist/auth/plugins/entity-definitions/organization.d.ts.map +1 -1
  48. package/dist/auth/plugins/entity-definitions/organization.js +15 -1
  49. package/dist/auth/plugins/entity-definitions/passkey.d.ts.map +1 -1
  50. package/dist/auth/plugins/entity-definitions/passkey.js +9 -1
  51. package/dist/auth/plugins/entity-definitions/phone-number.d.ts.map +1 -1
  52. package/dist/auth/plugins/entity-definitions/phone-number.js +2 -1
  53. package/dist/auth/plugins/entity-definitions/sso.d.ts.map +1 -1
  54. package/dist/auth/plugins/entity-definitions/sso.js +7 -3
  55. package/dist/auth/plugins/entity-definitions/two-factor.d.ts.map +1 -1
  56. package/dist/auth/plugins/entity-definitions/two-factor.js +8 -3
  57. package/dist/auth/verify-email-guard.d.ts +37 -0
  58. package/dist/auth/verify-email-guard.d.ts.map +1 -0
  59. package/dist/auth/verify-email-guard.js +48 -0
  60. package/dist/bin/build-config.d.ts.map +1 -1
  61. package/dist/bin/build-config.js +6 -2
  62. package/dist/bin/typecheck-web.js +1 -1
  63. package/dist/cache/decorator.d.ts +12 -0
  64. package/dist/cache/decorator.d.ts.map +1 -1
  65. package/dist/cache/decorator.js +86 -19
  66. package/dist/cache/index.d.ts +1 -1
  67. package/dist/cache/index.d.ts.map +1 -1
  68. package/dist/cache/index.js +3 -3
  69. package/dist/cache/types.d.ts +16 -0
  70. package/dist/cache/types.d.ts.map +1 -1
  71. package/dist/cli/plugin-loader.d.ts +17 -0
  72. package/dist/cli/plugin-loader.d.ts.map +1 -0
  73. package/dist/cli/plugin-loader.js +178 -0
  74. package/dist/cli/plugin.d.ts +65 -0
  75. package/dist/cli/plugin.d.ts.map +1 -0
  76. package/dist/cli/plugin.js +7 -0
  77. package/dist/code-formatting/oxfmt.js +1 -1
  78. package/dist/code-formatting/oxlint.js +1 -1
  79. package/dist/database/db.d.ts +35 -10
  80. package/dist/database/db.d.ts.map +1 -1
  81. package/dist/database/db.js +37 -4
  82. package/dist/database/puri.d.ts +8 -0
  83. package/dist/database/puri.d.ts.map +1 -1
  84. package/dist/database/puri.js +108 -171
  85. package/dist/database/puri.types.d.ts +2 -0
  86. package/dist/database/puri.types.d.ts.map +1 -1
  87. package/dist/database/puri.types.js +1 -1
  88. package/dist/database/sql-expression-brand.d.ts +10 -0
  89. package/dist/database/sql-expression-brand.d.ts.map +1 -0
  90. package/dist/database/sql-expression-brand.js +10 -0
  91. package/dist/dict/en.d.ts +1 -0
  92. package/dist/dict/en.d.ts.map +1 -1
  93. package/dist/dict/en.js +2 -1
  94. package/dist/dict/ko.d.ts +1 -0
  95. package/dist/dict/ko.d.ts.map +1 -1
  96. package/dist/dict/ko.js +2 -1
  97. package/dist/dict/sonamu-dictionary.d.ts +23 -1
  98. package/dist/dict/sonamu-dictionary.d.ts.map +1 -1
  99. package/dist/dict/sonamu-dictionary.js +187 -32
  100. package/dist/entity/entity-codegen-safety.d.ts +105 -0
  101. package/dist/entity/entity-codegen-safety.d.ts.map +1 -0
  102. package/dist/entity/entity-codegen-safety.js +70 -0
  103. package/dist/entity/entity-manager.d.ts +32 -0
  104. package/dist/entity/entity-manager.d.ts.map +1 -1
  105. package/dist/entity/entity-manager.js +21 -2
  106. package/dist/entity/entity.d.ts +5 -2
  107. package/dist/entity/entity.d.ts.map +1 -1
  108. package/dist/entity/entity.js +19 -8
  109. package/dist/env.d.ts +19 -0
  110. package/dist/env.d.ts.map +1 -1
  111. package/dist/env.js +31 -3
  112. package/dist/filter/utils.d.ts +2 -2
  113. package/dist/filter/utils.d.ts.map +1 -1
  114. package/dist/filter/utils.js +16 -11
  115. package/dist/index.d.ts +4 -3
  116. package/dist/index.d.ts.map +1 -1
  117. package/dist/index.js +7 -6
  118. package/dist/logger/configure.d.ts.map +1 -1
  119. package/dist/logger/configure.js +10 -3
  120. package/dist/migration/code-generation.d.ts.map +1 -1
  121. package/dist/migration/code-generation.js +177 -96
  122. package/dist/migration/migrator.d.ts +16 -0
  123. package/dist/migration/migrator.d.ts.map +1 -1
  124. package/dist/migration/migrator.js +28 -3
  125. package/dist/migration/slack-confirm.d.ts +6 -2
  126. package/dist/migration/slack-confirm.d.ts.map +1 -1
  127. package/dist/migration/slack-confirm.js +11 -9
  128. package/dist/ssr/errors.d.ts +13 -0
  129. package/dist/ssr/errors.d.ts.map +1 -0
  130. package/dist/ssr/errors.js +12 -0
  131. package/dist/ssr/index.d.ts +1 -0
  132. package/dist/ssr/index.d.ts.map +1 -1
  133. package/dist/ssr/index.js +5 -2
  134. package/dist/ssr/renderer.d.ts +6 -0
  135. package/dist/ssr/renderer.d.ts.map +1 -1
  136. package/dist/ssr/renderer.js +10 -1
  137. package/dist/storage/base-file.d.ts +6 -2
  138. package/dist/storage/base-file.d.ts.map +1 -1
  139. package/dist/storage/base-file.js +10 -5
  140. package/dist/storage/buffered-file.d.ts +1 -0
  141. package/dist/storage/buffered-file.d.ts.map +1 -1
  142. package/dist/storage/buffered-file.js +5 -2
  143. package/dist/storage/index.d.ts +1 -0
  144. package/dist/storage/index.d.ts.map +1 -1
  145. package/dist/storage/index.js +4 -2
  146. package/dist/storage/upload-safety.d.ts +47 -0
  147. package/dist/storage/upload-safety.d.ts.map +1 -0
  148. package/dist/storage/upload-safety.js +112 -0
  149. package/dist/stream/ws.d.ts +6 -0
  150. package/dist/stream/ws.d.ts.map +1 -1
  151. package/dist/stream/ws.js +52 -6
  152. package/dist/syncer/api-parser.js +6 -1
  153. package/dist/syncer/code-generator.d.ts +2 -0
  154. package/dist/syncer/code-generator.d.ts.map +1 -1
  155. package/dist/syncer/code-generator.js +21 -7
  156. package/dist/template/implementations/entity.template.d.ts.map +1 -1
  157. package/dist/template/implementations/entity.template.js +40 -35
  158. package/dist/template/implementations/generated.template.d.ts +10 -0
  159. package/dist/template/implementations/generated.template.d.ts.map +1 -1
  160. package/dist/template/implementations/generated.template.js +46 -7
  161. package/dist/template/implementations/generated_http.template.d.ts.map +1 -1
  162. package/dist/template/implementations/generated_http.template.js +3 -1
  163. package/dist/template/implementations/init_types.template.d.ts.map +1 -1
  164. package/dist/template/implementations/init_types.template.js +3 -8
  165. package/dist/template/implementations/model.template.d.ts.map +1 -1
  166. package/dist/template/implementations/model.template.js +9 -2
  167. package/dist/template/implementations/sd.template.d.ts +0 -1
  168. package/dist/template/implementations/sd.template.d.ts.map +1 -1
  169. package/dist/template/implementations/sd.template.js +2 -5
  170. package/dist/template/implementations/view_form.template.d.ts.map +1 -1
  171. package/dist/template/implementations/view_form.template.js +8 -6
  172. package/dist/template/implementations/view_list.template.d.ts.map +1 -1
  173. package/dist/template/implementations/view_list.template.js +8 -4
  174. package/dist/testing/dev-test-routes.d.ts.map +1 -1
  175. package/dist/testing/dev-test-routes.js +7 -4
  176. package/dist/tooling/__tests__/helpers/sonamu-test-root.js +1 -1
  177. package/dist/tooling/cdd-service.js +1 -1
  178. package/dist/tooling/index.d.ts +1 -0
  179. package/dist/tooling/index.d.ts.map +1 -1
  180. package/dist/tooling/index.js +3 -1
  181. package/dist/tsdown.api.config.js +1 -1
  182. package/dist/types/types.d.ts +81 -1
  183. package/dist/types/types.d.ts.map +1 -1
  184. package/dist/types/types.js +2 -1
  185. package/dist/ui/__tests__/ui-test-helpers.d.ts +10 -0
  186. package/dist/ui/__tests__/ui-test-helpers.d.ts.map +1 -0
  187. package/dist/ui/__tests__/ui-test-helpers.js +75 -0
  188. package/dist/ui/access-guard.d.ts +42 -0
  189. package/dist/ui/access-guard.d.ts.map +1 -0
  190. package/dist/ui/access-guard.js +94 -0
  191. package/dist/ui/api.d.ts.map +1 -1
  192. package/dist/ui/api.js +40 -95
  193. package/dist/ui/dev-host-guard.d.ts +28 -0
  194. package/dist/ui/dev-host-guard.d.ts.map +1 -0
  195. package/dist/ui/dev-host-guard.js +67 -0
  196. package/dist/ui/migrations-api.d.ts.map +1 -1
  197. package/dist/ui/migrations-api.js +72 -16
  198. package/dist/ui/open-vscode-api.d.ts +4 -0
  199. package/dist/ui/open-vscode-api.d.ts.map +1 -0
  200. package/dist/ui/open-vscode-api.js +65 -0
  201. package/dist/ui/scaffolding-api.d.ts +18 -0
  202. package/dist/ui/scaffolding-api.d.ts.map +1 -0
  203. package/dist/ui/scaffolding-api.js +111 -0
  204. package/dist/ui/ui-db-presets.d.ts +19 -0
  205. package/dist/ui/ui-db-presets.d.ts.map +1 -0
  206. package/dist/ui/ui-db-presets.js +34 -0
  207. package/dist/ui/ui-request-guard.d.ts +11 -0
  208. package/dist/ui/ui-request-guard.d.ts.map +1 -0
  209. package/dist/ui/ui-request-guard.js +38 -0
  210. package/dist/ui-web/assets/{index-QDMyy31K.js → index-BPqP-tug.js} +39 -39
  211. package/dist/ui-web/index.html +2 -1
  212. package/package.json +1 -1
  213. package/src/__tests__/env.test.ts +121 -2
  214. package/src/api/__tests__/code-converters.union.test.ts +97 -0
  215. package/src/api/__tests__/guards.test.ts +233 -0
  216. package/src/api/__tests__/sonamu.auth-secret.test.ts +77 -0
  217. package/src/api/__tests__/sonamu.dev-host-guard.test.ts +130 -0
  218. package/src/api/__tests__/sonamu.guards.test.ts +327 -0
  219. package/src/api/__tests__/sonamu.upload-limits.test.ts +156 -0
  220. package/src/api/__tests__/websocket-helpers.test.ts +101 -0
  221. package/src/api/caster.ts +7 -0
  222. package/src/api/code-converters.ts +44 -12
  223. package/src/api/config.ts +8 -0
  224. package/src/api/decorators.ts +1 -0
  225. package/src/api/guards.ts +130 -0
  226. package/src/api/sonamu.ts +195 -39
  227. package/src/api/websocket-helpers.ts +11 -6
  228. package/src/auth/__tests__/auth-entity-policy.test.ts +59 -0
  229. package/src/auth/__tests__/auth-secret.test.ts +133 -0
  230. package/src/auth/__tests__/base-url-host-poisoning.test.ts +193 -0
  231. package/src/auth/__tests__/base-url.test.ts +117 -0
  232. package/src/auth/__tests__/better-auth-options.test.ts +194 -0
  233. package/src/auth/__tests__/better-auth-request.test.ts +45 -0
  234. package/src/auth/__tests__/verify-email-guard.test.ts +228 -0
  235. package/src/auth/auth-entity-policy.ts +48 -0
  236. package/src/auth/auth-secret.ts +77 -0
  237. package/src/auth/base-url.ts +109 -0
  238. package/src/auth/better-auth-entities.ts +30 -30
  239. package/src/auth/better-auth-options.ts +39 -0
  240. package/src/auth/better-auth-request.ts +28 -1
  241. package/src/auth/plugins/entity-definitions/admin.ts +5 -0
  242. package/src/auth/plugins/entity-definitions/anonymous.ts +9 -1
  243. package/src/auth/plugins/entity-definitions/api-key.ts +53 -16
  244. package/src/auth/plugins/entity-definitions/audit-log.ts +122 -21
  245. package/src/auth/plugins/entity-definitions/jwt.ts +5 -4
  246. package/src/auth/plugins/entity-definitions/organization.ts +26 -6
  247. package/src/auth/plugins/entity-definitions/passkey.ts +8 -7
  248. package/src/auth/plugins/entity-definitions/phone-number.ts +1 -0
  249. package/src/auth/plugins/entity-definitions/sso.ts +20 -15
  250. package/src/auth/plugins/entity-definitions/two-factor.ts +21 -5
  251. package/src/auth/verify-email-guard.ts +82 -0
  252. package/src/bin/__tests__/build-web-command.test.ts +24 -0
  253. package/src/bin/build-config.ts +5 -0
  254. package/src/cache/decorator.ts +116 -21
  255. package/src/cache/index.ts +6 -1
  256. package/src/cache/types.ts +18 -0
  257. package/src/cli/plugin-loader.ts +238 -0
  258. package/src/cli/plugin.ts +82 -0
  259. package/src/database/__tests__/db.test.ts +64 -1
  260. package/src/database/__tests__/puri.test.ts +56 -0
  261. package/src/database/db.ts +56 -6
  262. package/src/database/puri.ts +135 -125
  263. package/src/database/puri.types.test-d.ts +12 -0
  264. package/src/database/puri.types.ts +3 -0
  265. package/src/database/sql-expression-brand.ts +9 -0
  266. package/src/dict/__tests__/sonamu-dictionary.test.ts +217 -0
  267. package/src/dict/en.ts +2 -0
  268. package/src/dict/ko.ts +2 -0
  269. package/src/dict/sonamu-dictionary.ts +298 -38
  270. package/src/entity/__tests__/entity-codegen-safety.test.ts +326 -0
  271. package/src/entity/entity-codegen-safety.ts +126 -0
  272. package/src/entity/entity-manager.ts +29 -1
  273. package/src/entity/entity.ts +26 -12
  274. package/src/env.ts +52 -1
  275. package/src/filter/utils.ts +19 -7
  276. package/src/index.ts +4 -3
  277. package/src/logger/__tests__/configure.test.ts +51 -1
  278. package/src/logger/configure.ts +14 -1
  279. package/src/migration/__tests__/code-generation.escape.test.ts +322 -0
  280. package/src/migration/__tests__/migrator.test.ts +76 -1
  281. package/src/migration/code-generation.ts +247 -117
  282. package/src/migration/migrator.ts +45 -4
  283. package/src/migration/slack-confirm.ts +19 -5
  284. package/src/ssr/__tests__/preload-guard.test.ts +274 -0
  285. package/src/ssr/errors.ts +11 -0
  286. package/src/ssr/index.ts +1 -0
  287. package/src/ssr/renderer.ts +12 -0
  288. package/src/storage/__tests__/upload-safety.test.ts +179 -0
  289. package/src/storage/base-file.ts +8 -4
  290. package/src/storage/buffered-file.ts +3 -1
  291. package/src/storage/index.ts +9 -0
  292. package/src/storage/upload-safety.ts +155 -0
  293. package/src/stream/__tests__/ws.test.ts +97 -0
  294. package/src/stream/ws.ts +67 -5
  295. package/src/syncer/__tests__/api-parser.readonly.test.ts +55 -0
  296. package/src/syncer/__tests__/code-generator-path-containment.test.ts +105 -0
  297. package/src/syncer/api-parser.ts +12 -0
  298. package/src/syncer/code-generator.ts +34 -6
  299. package/src/template/__tests__/generated.template.search-text.test.ts +5 -1
  300. package/src/template/__tests__/model.template.auth-entity.test.ts +42 -0
  301. package/src/template/__tests__/save-params.template.test.ts +219 -0
  302. package/src/template/implementations/entity.template.ts +53 -48
  303. package/src/template/implementations/generated.template.ts +87 -12
  304. package/src/template/implementations/generated_http.template.ts +5 -0
  305. package/src/template/implementations/init_types.template.ts +4 -22
  306. package/src/template/implementations/model.template.ts +15 -1
  307. package/src/template/implementations/sd.template.ts +4 -5
  308. package/src/template/implementations/view_form.template.ts +13 -5
  309. package/src/template/implementations/view_list.template.ts +12 -3
  310. package/src/testing/__tests__/dev-test-routes.test.ts +99 -0
  311. package/src/testing/dev-test-routes.ts +7 -3
  312. package/src/tooling/__tests__/cli-plugins.contract.test.ts +273 -0
  313. package/src/tooling/index.ts +1 -0
  314. package/src/types/types.ts +22 -1
  315. package/src/ui/__tests__/access-guard.test.ts +217 -0
  316. package/src/ui/__tests__/dev-host-guard.test.ts +161 -0
  317. package/src/ui/__tests__/fixture-api.test.ts +92 -0
  318. package/src/ui/__tests__/migrations-api.test.ts +352 -0
  319. package/src/ui/__tests__/open-vscode.test.ts +151 -0
  320. package/src/ui/__tests__/scaffolding-api.test.ts +232 -0
  321. package/src/ui/__tests__/ui-process-routes.test.ts +166 -0
  322. package/src/ui/__tests__/ui-test-helpers.ts +71 -0
  323. package/src/ui/access-guard.ts +115 -0
  324. package/src/ui/api.ts +65 -198
  325. package/src/ui/dev-host-guard.ts +109 -0
  326. package/src/ui/migrations-api.ts +130 -24
  327. package/src/ui/open-vscode-api.ts +82 -0
  328. package/src/ui/scaffolding-api.ts +177 -0
  329. package/src/ui/ui-db-presets.ts +41 -0
  330. 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
+ });
@@ -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
- export type SonamuDBConfig = Record<SonamuDBPreset, Knex.Config>;
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
- this[instanceName] = createKnexInstance(config);
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
- getDBConfig(which: DBPreset): Knex.Config {
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): Knex.Config =>
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): Knex.Config =>
362
- mergeConfigs<Knex.Config>(defaultKnexConfig, mainConfig(environment), {
411
+ const readonlyConfig = (environment: SonamuEnvironment): DatabaseConfig =>
412
+ mergeConfigs<DatabaseConfig>(defaultKnexConfig, mainConfig(environment), {
363
413
  connection: connectionFromEnv({
364
414
  baseName,
365
415
  suffix: environment,