sonamu 0.4.14 → 0.5.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 (348) hide show
  1. package/.swcrc +15 -0
  2. package/dist/api/base-frame.d.ts +8 -0
  3. package/dist/api/base-frame.d.ts.map +1 -0
  4. package/dist/api/base-frame.js +2 -0
  5. package/dist/api/base-frame.js.map +1 -0
  6. package/dist/api/caster.d.ts +5 -0
  7. package/dist/api/caster.d.ts.map +1 -0
  8. package/dist/api/caster.js +2 -0
  9. package/dist/api/caster.js.map +1 -0
  10. package/dist/api/code-converters.d.ts +23 -0
  11. package/dist/api/code-converters.d.ts.map +1 -0
  12. package/dist/api/code-converters.js +2 -0
  13. package/dist/api/code-converters.js.map +1 -0
  14. package/dist/api/context.d.ts +16 -0
  15. package/dist/api/context.d.ts.map +1 -0
  16. package/dist/api/context.js +2 -0
  17. package/dist/api/context.js.map +1 -0
  18. package/dist/api/decorators.d.ts +50 -0
  19. package/dist/api/decorators.d.ts.map +1 -0
  20. package/dist/api/decorators.js +2 -0
  21. package/dist/api/decorators.js.map +1 -0
  22. package/dist/api/index.d.ts +8 -0
  23. package/dist/api/index.d.ts.map +1 -0
  24. package/dist/api/index.js +2 -0
  25. package/dist/api/index.js.map +1 -0
  26. package/dist/api/sonamu.d.ts +83 -0
  27. package/dist/api/sonamu.d.ts.map +1 -0
  28. package/dist/api/sonamu.js +2 -0
  29. package/dist/api/sonamu.js.map +1 -0
  30. package/dist/api/sonamu.types.d.ts +30 -0
  31. package/dist/api/sonamu.types.d.ts.map +1 -0
  32. package/dist/api/sonamu.types.js +2 -0
  33. package/dist/api/sonamu.types.js.map +1 -0
  34. package/dist/bin/build-config.d.ts +5 -0
  35. package/dist/bin/build-config.d.ts.map +1 -0
  36. package/dist/bin/build-config.js +2 -0
  37. package/dist/bin/build-config.js.map +1 -0
  38. package/dist/bin/cli-wrapper.d.ts +2 -0
  39. package/dist/bin/cli-wrapper.d.ts.map +1 -0
  40. package/dist/bin/cli-wrapper.js +1 -38
  41. package/dist/bin/cli-wrapper.js.map +1 -1
  42. package/dist/bin/cli.d.ts +2 -2
  43. package/dist/bin/cli.d.ts.map +1 -0
  44. package/dist/bin/cli.js +1 -903
  45. package/dist/bin/cli.js.map +1 -1
  46. package/dist/database/_batch_update.d.ts +15 -0
  47. package/dist/database/_batch_update.d.ts.map +1 -0
  48. package/dist/database/_batch_update.js +2 -0
  49. package/dist/database/_batch_update.js.map +1 -0
  50. package/dist/database/base-model.d.ts +41 -0
  51. package/dist/database/base-model.d.ts.map +1 -0
  52. package/dist/database/base-model.js +2 -0
  53. package/dist/database/base-model.js.map +1 -0
  54. package/dist/database/code-generator.d.ts +13 -0
  55. package/dist/database/code-generator.d.ts.map +1 -0
  56. package/dist/database/code-generator.js +2 -0
  57. package/dist/database/code-generator.js.map +1 -0
  58. package/dist/database/db.d.ts +40 -0
  59. package/dist/database/db.d.ts.map +1 -0
  60. package/dist/database/db.js +2 -0
  61. package/dist/database/db.js.map +1 -0
  62. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts +2 -0
  63. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts.map +1 -0
  64. package/dist/database/knex-plugins/knex-on-duplicate-update.js +2 -0
  65. package/dist/database/knex-plugins/knex-on-duplicate-update.js.map +1 -0
  66. package/dist/database/puri-wrapper.d.ts +34 -0
  67. package/dist/database/puri-wrapper.d.ts.map +1 -0
  68. package/dist/database/puri-wrapper.js +2 -0
  69. package/dist/database/puri-wrapper.js.map +1 -0
  70. package/dist/database/puri.d.ts +83 -0
  71. package/dist/database/puri.d.ts.map +1 -0
  72. package/dist/database/puri.js +2 -0
  73. package/dist/database/puri.js.map +1 -0
  74. package/dist/database/puri.types.d.ts +60 -0
  75. package/dist/database/puri.types.d.ts.map +1 -0
  76. package/dist/database/puri.types.js +2 -0
  77. package/dist/database/puri.types.js.map +1 -0
  78. package/dist/database/transaction-context.d.ts +9 -0
  79. package/dist/database/transaction-context.d.ts.map +1 -0
  80. package/dist/database/transaction-context.js +2 -0
  81. package/dist/database/transaction-context.js.map +1 -0
  82. package/dist/database/types.d.ts +39 -0
  83. package/dist/database/types.d.ts.map +1 -0
  84. package/dist/database/types.js +2 -0
  85. package/dist/database/types.js.map +1 -0
  86. package/dist/database/upsert-builder.d.ts +34 -0
  87. package/dist/database/upsert-builder.d.ts.map +1 -0
  88. package/dist/database/upsert-builder.js +2 -0
  89. package/dist/database/upsert-builder.js.map +1 -0
  90. package/dist/entity/entity-manager.d.ts +32 -0
  91. package/dist/entity/entity-manager.d.ts.map +1 -0
  92. package/dist/entity/entity-manager.js +2 -0
  93. package/dist/entity/entity-manager.js.map +1 -0
  94. package/dist/entity/entity-utils.d.ts +61 -0
  95. package/dist/entity/entity-utils.d.ts.map +1 -0
  96. package/dist/entity/entity-utils.js +2 -0
  97. package/dist/entity/entity-utils.js.map +1 -0
  98. package/dist/entity/entity.d.ts +62 -0
  99. package/dist/entity/entity.d.ts.map +1 -0
  100. package/dist/entity/entity.js +2 -0
  101. package/dist/entity/entity.js.map +1 -0
  102. package/dist/entity/migrator.d.ts +135 -0
  103. package/dist/entity/migrator.d.ts.map +1 -0
  104. package/dist/entity/migrator.js +2 -0
  105. package/dist/entity/migrator.js.map +1 -0
  106. package/dist/exceptions/error-handler.d.ts +3 -0
  107. package/dist/exceptions/error-handler.d.ts.map +1 -0
  108. package/dist/exceptions/error-handler.js +2 -0
  109. package/dist/exceptions/error-handler.js.map +1 -0
  110. package/dist/exceptions/so-exceptions.d.ts +48 -0
  111. package/dist/exceptions/so-exceptions.d.ts.map +1 -0
  112. package/dist/exceptions/so-exceptions.js +2 -0
  113. package/dist/exceptions/so-exceptions.js.map +1 -0
  114. package/dist/file-storage/driver.d.ts +45 -0
  115. package/dist/file-storage/driver.d.ts.map +1 -0
  116. package/dist/file-storage/driver.js +2 -0
  117. package/dist/file-storage/driver.js.map +1 -0
  118. package/dist/file-storage/file-storage.d.ts +50 -0
  119. package/dist/file-storage/file-storage.d.ts.map +1 -0
  120. package/dist/file-storage/file-storage.js +2 -0
  121. package/dist/file-storage/file-storage.js.map +1 -0
  122. package/dist/index.d.ts +22 -813
  123. package/dist/index.d.ts.map +1 -0
  124. package/dist/index.js +1 -433
  125. package/dist/index.js.map +1 -1
  126. package/dist/migration/code-generation.d.ts +15 -0
  127. package/dist/migration/code-generation.d.ts.map +1 -0
  128. package/dist/migration/code-generation.js +2 -0
  129. package/dist/migration/code-generation.js.map +1 -0
  130. package/dist/migration/migration-set.d.ts +17 -0
  131. package/dist/migration/migration-set.d.ts.map +1 -0
  132. package/dist/migration/migration-set.js +2 -0
  133. package/dist/migration/migration-set.js.map +1 -0
  134. package/dist/migration/migrator.d.ts +130 -0
  135. package/dist/migration/migrator.d.ts.map +1 -0
  136. package/dist/migration/migrator.js +2 -0
  137. package/dist/migration/migrator.js.map +1 -0
  138. package/dist/migration/types.d.ts +52 -0
  139. package/dist/migration/types.d.ts.map +1 -0
  140. package/dist/migration/types.js +2 -0
  141. package/dist/migration/types.js.map +1 -0
  142. package/dist/smd/smd-manager.d.ts +28 -0
  143. package/dist/smd/smd-manager.d.ts.map +1 -0
  144. package/dist/smd/smd-manager.js +2 -0
  145. package/dist/smd/smd-manager.js.map +1 -0
  146. package/dist/smd/smd.d.ts +40 -0
  147. package/dist/smd/smd.d.ts.map +1 -0
  148. package/dist/smd/smd.js +2 -0
  149. package/dist/smd/smd.js.map +1 -0
  150. package/dist/syncer/index.d.ts +2 -0
  151. package/dist/syncer/index.d.ts.map +1 -0
  152. package/dist/syncer/index.js +2 -0
  153. package/dist/syncer/index.js.map +1 -0
  154. package/dist/syncer/syncer.d.ts +127 -0
  155. package/dist/syncer/syncer.d.ts.map +1 -0
  156. package/dist/syncer/syncer.js +2 -0
  157. package/dist/syncer/syncer.js.map +1 -0
  158. package/dist/templates/base-template.d.ts +13 -0
  159. package/dist/templates/base-template.d.ts.map +1 -0
  160. package/dist/templates/base-template.js +2 -0
  161. package/dist/templates/base-template.js.map +1 -0
  162. package/dist/templates/entity.template.d.ts +17 -0
  163. package/dist/templates/entity.template.d.ts.map +1 -0
  164. package/dist/templates/entity.template.js +2 -0
  165. package/dist/templates/entity.template.js.map +1 -0
  166. package/dist/templates/generated.template.d.ts +27 -0
  167. package/dist/templates/generated.template.d.ts.map +1 -0
  168. package/dist/templates/generated.template.js +2 -0
  169. package/dist/templates/generated.template.js.map +1 -0
  170. package/dist/templates/generated_http.template.d.ts +24 -0
  171. package/dist/templates/generated_http.template.d.ts.map +1 -0
  172. package/dist/templates/generated_http.template.js +2 -0
  173. package/dist/templates/generated_http.template.js.map +1 -0
  174. package/dist/templates/generated_sso.template.d.ts +20 -0
  175. package/dist/templates/generated_sso.template.d.ts.map +1 -0
  176. package/dist/templates/generated_sso.template.js +2 -0
  177. package/dist/templates/generated_sso.template.js.map +1 -0
  178. package/dist/templates/index.d.ts +2 -0
  179. package/dist/templates/index.d.ts.map +1 -0
  180. package/dist/templates/index.js +2 -0
  181. package/dist/templates/index.js.map +1 -0
  182. package/dist/templates/init_types.template.d.ts +17 -0
  183. package/dist/templates/init_types.template.d.ts.map +1 -0
  184. package/dist/templates/init_types.template.js +2 -0
  185. package/dist/templates/init_types.template.js.map +1 -0
  186. package/dist/templates/model.template.d.ts +17 -0
  187. package/dist/templates/model.template.d.ts.map +1 -0
  188. package/dist/templates/model.template.js +2 -0
  189. package/dist/templates/model.template.js.map +1 -0
  190. package/dist/templates/model_test.template.d.ts +17 -0
  191. package/dist/templates/model_test.template.d.ts.map +1 -0
  192. package/dist/templates/model_test.template.js +2 -0
  193. package/dist/templates/model_test.template.js.map +1 -0
  194. package/dist/templates/service.template.d.ts +29 -0
  195. package/dist/templates/service.template.d.ts.map +1 -0
  196. package/dist/templates/service.template.js +2 -0
  197. package/dist/templates/service.template.js.map +1 -0
  198. package/dist/templates/view_enums_buttonset.template.d.ts +17 -0
  199. package/dist/templates/view_enums_buttonset.template.d.ts.map +1 -0
  200. package/dist/templates/view_enums_buttonset.template.js +2 -0
  201. package/dist/templates/view_enums_buttonset.template.js.map +1 -0
  202. package/dist/templates/view_enums_dropdown.template.d.ts +18 -0
  203. package/dist/templates/view_enums_dropdown.template.d.ts.map +1 -0
  204. package/dist/templates/view_enums_dropdown.template.js +2 -0
  205. package/dist/templates/view_enums_dropdown.template.js.map +1 -0
  206. package/dist/templates/view_enums_select.template.d.ts +17 -0
  207. package/dist/templates/view_enums_select.template.d.ts.map +1 -0
  208. package/dist/templates/view_enums_select.template.js +2 -0
  209. package/dist/templates/view_enums_select.template.js.map +1 -0
  210. package/dist/templates/view_form.template.d.ts +26 -0
  211. package/dist/templates/view_form.template.d.ts.map +1 -0
  212. package/dist/templates/view_form.template.js +2 -0
  213. package/dist/templates/view_form.template.js.map +1 -0
  214. package/dist/templates/view_id_all_select.template.d.ts +17 -0
  215. package/dist/templates/view_id_all_select.template.d.ts.map +1 -0
  216. package/dist/templates/view_id_all_select.template.js +2 -0
  217. package/dist/templates/view_id_all_select.template.js.map +1 -0
  218. package/dist/templates/view_id_async_select.template.d.ts +17 -0
  219. package/dist/templates/view_id_async_select.template.d.ts.map +1 -0
  220. package/dist/templates/view_id_async_select.template.js +2 -0
  221. package/dist/templates/view_id_async_select.template.js.map +1 -0
  222. package/dist/templates/view_list.template.d.ts +38 -0
  223. package/dist/templates/view_list.template.d.ts.map +1 -0
  224. package/dist/templates/view_list.template.js +2 -0
  225. package/dist/templates/view_list.template.js.map +1 -0
  226. package/dist/templates/view_list_columns.template.d.ts +17 -0
  227. package/dist/templates/view_list_columns.template.d.ts.map +1 -0
  228. package/dist/templates/view_list_columns.template.js +2 -0
  229. package/dist/templates/view_list_columns.template.js.map +1 -0
  230. package/dist/templates/view_search_input.template.d.ts +17 -0
  231. package/dist/templates/view_search_input.template.d.ts.map +1 -0
  232. package/dist/templates/view_search_input.template.js +2 -0
  233. package/dist/templates/view_search_input.template.js.map +1 -0
  234. package/dist/testing/_relation-graph.d.ts +7 -0
  235. package/dist/testing/_relation-graph.d.ts.map +1 -0
  236. package/dist/testing/_relation-graph.js +2 -0
  237. package/dist/testing/_relation-graph.js.map +1 -0
  238. package/dist/testing/fixture-manager.d.ts +35 -0
  239. package/dist/testing/fixture-manager.d.ts.map +1 -0
  240. package/dist/testing/fixture-manager.js +2 -0
  241. package/dist/testing/fixture-manager.js.map +1 -0
  242. package/dist/types/types.d.ts +609 -0
  243. package/dist/types/types.d.ts.map +1 -0
  244. package/dist/types/types.js +2 -0
  245. package/dist/types/types.js.map +1 -0
  246. package/dist/typings/knex.d.js +2 -0
  247. package/dist/typings/knex.d.js.map +1 -0
  248. package/dist/utils/async-utils.d.ts +25 -0
  249. package/dist/utils/async-utils.d.ts.map +1 -0
  250. package/dist/utils/async-utils.js +2 -0
  251. package/dist/utils/async-utils.js.map +1 -0
  252. package/dist/utils/controller.d.ts +9 -0
  253. package/dist/utils/controller.d.ts.map +1 -0
  254. package/dist/utils/controller.js +2 -0
  255. package/dist/utils/controller.js.map +1 -0
  256. package/dist/utils/fs-utils.d.ts +9 -0
  257. package/dist/utils/fs-utils.d.ts.map +1 -0
  258. package/dist/utils/fs-utils.js +2 -0
  259. package/dist/utils/fs-utils.js.map +1 -0
  260. package/dist/utils/lodash-able.d.ts +2 -0
  261. package/dist/utils/lodash-able.d.ts.map +1 -0
  262. package/dist/utils/lodash-able.js +2 -0
  263. package/dist/utils/lodash-able.js.map +1 -0
  264. package/dist/utils/model.d.ts +17 -0
  265. package/dist/utils/model.d.ts.map +1 -0
  266. package/dist/utils/model.js +2 -0
  267. package/dist/utils/model.js.map +1 -0
  268. package/dist/utils/sql-parser.d.ts +4 -0
  269. package/dist/utils/sql-parser.d.ts.map +1 -0
  270. package/dist/utils/sql-parser.js +2 -0
  271. package/dist/utils/sql-parser.js.map +1 -0
  272. package/dist/utils/utils.d.ts +9 -0
  273. package/dist/utils/utils.d.ts.map +1 -0
  274. package/dist/utils/utils.js +2 -0
  275. package/dist/utils/utils.js.map +1 -0
  276. package/dist/utils/zod-error.d.ts +8 -0
  277. package/dist/utils/zod-error.d.ts.map +1 -0
  278. package/dist/utils/zod-error.js +2 -0
  279. package/dist/utils/zod-error.js.map +1 -0
  280. package/nodemon.json +6 -0
  281. package/package.json +29 -44
  282. package/src/api/base-frame.ts +3 -4
  283. package/src/api/caster.ts +22 -23
  284. package/src/api/code-converters.ts +170 -134
  285. package/src/api/context.ts +13 -6
  286. package/src/api/decorators.ts +146 -20
  287. package/src/api/index.ts +2 -0
  288. package/src/api/sonamu.ts +374 -165
  289. package/src/bin/build-config.ts +5 -0
  290. package/src/bin/cli-wrapper.ts +29 -40
  291. package/src/bin/cli.ts +132 -190
  292. package/src/database/_batch_update.ts +10 -15
  293. package/src/database/base-model.ts +300 -216
  294. package/src/database/db.ts +191 -21
  295. package/src/database/{drivers/knex/plugins → knex-plugins}/knex-on-duplicate-update.ts +1 -1
  296. package/src/database/puri-wrapper.ts +129 -0
  297. package/src/database/puri.ts +808 -0
  298. package/src/database/puri.types.ts +222 -0
  299. package/src/database/transaction-context.ts +18 -0
  300. package/src/database/upsert-builder.ts +32 -35
  301. package/src/entity/entity-manager.ts +7 -15
  302. package/src/entity/entity.ts +9 -31
  303. package/src/entity/migrator-/354/235/264/354/202/254/352/260/224/354/226/264/354/232/224.md +1 -0
  304. package/src/file-storage/driver.ts +121 -0
  305. package/src/file-storage/file-storage.ts +100 -0
  306. package/src/index.ts +14 -11
  307. package/src/migration/code-generation.ts +777 -0
  308. package/src/migration/migration-set.ts +453 -0
  309. package/src/migration/migrator.ts +823 -0
  310. package/src/migration/types.ts +53 -0
  311. package/src/shared/web.shared.ts.txt +33 -2
  312. package/src/syncer/syncer.ts +294 -127
  313. package/src/templates/generated.template.ts +13 -1
  314. package/src/templates/generated_http.template.ts +15 -12
  315. package/src/templates/generated_sso.template.ts +50 -2
  316. package/src/templates/model.template.ts +138 -2
  317. package/src/templates/service.template.ts +0 -1
  318. package/src/templates/view_form.template.ts +11 -7
  319. package/src/templates/view_list.template.ts +12 -4
  320. package/src/testing/fixture-manager.ts +229 -174
  321. package/src/types/types.ts +102 -14
  322. package/src/utils/async-utils.ts +64 -0
  323. package/src/utils/fs-utils.ts +17 -0
  324. package/src/utils/model.ts +0 -2
  325. package/src/utils/utils.ts +14 -58
  326. package/src/utils/zod-error.ts +12 -176
  327. package/tsconfig.json +2 -0
  328. package/tsup.config.js +4 -2
  329. package/.pnp.cjs +0 -14363
  330. package/.pnp.loader.mjs +0 -2047
  331. package/.vscode/extensions.json +0 -6
  332. package/.vscode/settings.json +0 -9
  333. package/.yarnrc.yml +0 -5
  334. package/src/database/base-model.abstract.ts +0 -97
  335. package/src/database/db.abstract.ts +0 -75
  336. package/src/database/drivers/knex/base-model.ts +0 -55
  337. package/src/database/drivers/knex/client.ts +0 -209
  338. package/src/database/drivers/knex/db.ts +0 -232
  339. package/src/database/drivers/knex/generator.ts +0 -659
  340. package/src/database/drivers/kysely/base-model.ts +0 -89
  341. package/src/database/drivers/kysely/client.ts +0 -309
  342. package/src/database/drivers/kysely/db.ts +0 -238
  343. package/src/database/drivers/kysely/generator.ts +0 -714
  344. package/src/database/types.ts +0 -118
  345. package/src/entity/migrator.ts +0 -1400
  346. package/src/smd/smd-manager.ts +0 -139
  347. package/src/smd/smd.ts +0 -571
  348. package/src/templates/kysely_types.template.ts +0 -205
@@ -1,53 +1,42 @@
1
1
  #!/usr/bin/env ts-node
2
2
 
3
- import { spawnSync } from "child_process";
4
- import { extname, resolve } from "path";
5
- import { existsSync, readFileSync } from "fs";
6
-
7
- const cjsPath = resolve(__dirname, "bin/cli.js");
8
- const esmPath = resolve(__dirname, "bin/cli.mjs");
9
-
10
- const isESM = () => {
11
- const packageJsonPath = resolve(process.cwd(), "package.json");
12
- const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
13
-
14
- // package.json에 "type": "module" 설정 확인
15
- if (packageJson.type === "module") {
16
- return true;
17
- }
18
-
19
- // 환경 변수에서 ESM 여부 확인
20
- if (process.env.USE_ESM === "true") {
21
- return true;
22
- }
23
-
24
- // package.json에 "type": "module" 설정
25
- if (existsSync(packageJsonPath)) {
26
- const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
27
- return packageJson.type === "module";
28
- }
29
-
30
- // main 필드가 .mjs로 끝나는지 확인
31
- if (packageJson.main && extname(packageJson.main) === ".mjs") {
32
- return true;
3
+ import { spawnSync, execSync } from "child_process";
4
+ import { resolve } from "path";
5
+ import { existsSync } from "fs";
6
+ import chalk from "chalk";
7
+ import { SWC_BUILD_COMMAND } from "./build-config";
8
+
9
+ const scriptPath = resolve(__dirname, "cli.js");
10
+ const args = process.argv.slice(2);
11
+
12
+ // build 명령어는 dist 없이도 실행 가능하도록 cli.ts 외부에서 처리(Sonamu.init에서 dist 필요)
13
+ function build() {
14
+ try {
15
+ execSync(SWC_BUILD_COMMAND, { cwd: process.cwd(), stdio: "inherit" });
16
+ } catch (error) {
17
+ console.error(chalk.red("Build failed."), error);
18
+ process.exit(1);
33
19
  }
20
+ }
34
21
 
35
- return false;
36
- };
22
+ if (args[0] === "build") {
23
+ console.log(chalk.blue("Building the project..."));
24
+ build();
25
+ console.log(chalk.green("Build completed successfully."));
26
+ process.exit(0);
27
+ }
37
28
 
38
- const scriptPath = isESM() ? esmPath : cjsPath;
29
+ if (args[0] === "dev:serve") {
30
+ build();
31
+ }
39
32
 
40
33
  if (!existsSync(scriptPath)) {
41
34
  console.error(`Error: Script not found at ${scriptPath}`);
42
35
  process.exit(1);
43
36
  }
44
37
 
45
- const result = spawnSync(
46
- process.execPath,
47
- [scriptPath, ...process.argv.slice(2)],
48
- {
49
- stdio: "inherit",
50
- }
51
- );
38
+ const result = spawnSync(process.execPath, [scriptPath, ...args], {
39
+ stdio: "inherit",
40
+ });
52
41
 
53
42
  process.exit(result.status ?? 1);
package/src/bin/cli.ts CHANGED
@@ -8,25 +8,15 @@ dotenv.config();
8
8
  import path from "path";
9
9
  import { tsicli } from "tsicli";
10
10
  import { execSync } from "child_process";
11
- import fs from "fs-extra";
12
- import inflection from "inflection";
13
- import prettier from "prettier";
11
+ import { mkdir, readFile, readdir, writeFile } from "fs/promises";
12
+ import { exists } from "../utils/fs-utils";
14
13
  import process from "process";
15
- import _ from "lodash";
16
14
  import { Sonamu } from "../api";
15
+ import knex, { Knex } from "knex";
17
16
  import { EntityManager } from "../entity/entity-manager";
18
- import { Migrator } from "../entity/migrator";
17
+ import { Migrator } from "../migration/migrator";
19
18
  import { FixtureManager } from "../testing/fixture-manager";
20
- import { SMDManager } from "../smd/smd-manager";
21
- import { DB } from "../database/db";
22
- import {
23
- KnexConfig,
24
- KyselyConfig,
25
- SonamuKnexDBConfig,
26
- SonamuKyselyDBConfig,
27
- } from "../database/types";
28
- import { KnexClient } from "../database/drivers/knex/client";
29
- import { KyselyClient } from "../database/drivers/kysely/client";
19
+ import { SWC_BUILD_COMMAND } from "./build-config";
30
20
 
31
21
  let migrator: Migrator;
32
22
 
@@ -64,7 +54,8 @@ async function bootstrap() {
64
54
  ["scaffold", "view_list", "#entityId"],
65
55
  ["scaffold", "view_form", "#entityId"],
66
56
  ["ui"],
67
- ["smd_migration"],
57
+ ["dev:serve"],
58
+ ["serve"],
68
59
  ],
69
60
  runners: {
70
61
  migrate_run,
@@ -83,7 +74,8 @@ async function bootstrap() {
83
74
  ui,
84
75
  // scaffold_view_list,
85
76
  // scaffold_view_form,
86
- smd_migration,
77
+ "dev:serve": dev_serve,
78
+ serve,
87
79
  },
88
80
  });
89
81
  }
@@ -91,12 +83,73 @@ bootstrap().finally(async () => {
91
83
  if (migrator) {
92
84
  await migrator.destroy();
93
85
  }
94
- await FixtureManager.destory();
86
+ await FixtureManager.destroy();
95
87
 
96
88
  /* Global End */
97
89
  console.log(chalk.bgBlue(`END ${new Date()}\n`));
98
90
  });
99
91
 
92
+ async function dev_serve() {
93
+ const nodemon = await import("nodemon");
94
+
95
+ const nodemonConfig = await (async () => {
96
+ const projectNodemonPath = path.join(Sonamu.apiRootPath, "nodemon.json");
97
+ const hasProjectNodemon = await exists(projectNodemonPath);
98
+
99
+ if (hasProjectNodemon) {
100
+ return JSON.parse(await readFile(projectNodemonPath, "utf8"));
101
+ }
102
+
103
+ return {
104
+ watch: ["src/index.ts"],
105
+ ignore: ["dist/**", "**/*.js", "**/*.d.ts"],
106
+ exec: [
107
+ SWC_BUILD_COMMAND,
108
+ "node -r source-map-support/register -r dotenv/config dist/index.js",
109
+ ].join(" && "),
110
+ };
111
+ })();
112
+
113
+ nodemon.default(nodemonConfig);
114
+
115
+ // 프로세스 종료 처리
116
+ const cleanup = async () => {
117
+ await Sonamu.server?.close();
118
+ process.exit(0);
119
+ };
120
+
121
+ process.on("SIGINT", cleanup);
122
+ process.on("SIGTERM", cleanup);
123
+ process.on("SIGUSR2", cleanup);
124
+ }
125
+
126
+ async function serve() {
127
+ const distIndexPath = path.join(Sonamu.apiRootPath, "dist", "index.js");
128
+
129
+ if (!(await exists(distIndexPath))) {
130
+ console.log(
131
+ chalk.red("dist/index.js not found. Please build your project first.")
132
+ );
133
+ console.log(chalk.blue("Run: yarn sonamu build"));
134
+ return;
135
+ }
136
+
137
+ const { spawn } = await import("child_process");
138
+ const serverProcess = spawn(
139
+ "node",
140
+ ["-r", "source-map-support/register", "-r", "dotenv/config", distIndexPath],
141
+ {
142
+ cwd: Sonamu.apiRootPath,
143
+ stdio: "inherit",
144
+ }
145
+ );
146
+
147
+ process.on("SIGINT", () => {
148
+ serverProcess.kill("SIGTERM");
149
+ process.exit(0);
150
+ });
151
+ }
152
+
100
153
  async function setupMigrator() {
101
154
  // migrator
102
155
  migrator = new Migrator({
@@ -149,97 +202,93 @@ async function migrate_reset() {
149
202
  }
150
203
 
151
204
  async function fixture_init() {
152
- const _db = DB.getClient("development_master");
153
- const srcConn = _db.connectionInfo;
154
-
205
+ const srcConfig = Sonamu.dbConfig.development_master;
155
206
  const targets = [
156
207
  {
157
208
  label: "(REMOTE) Fixture DB",
158
- connKey: "fixture_remote",
209
+ config: Sonamu.dbConfig.fixture_remote,
159
210
  },
160
211
  {
161
212
  label: "(LOCAL) Fixture DB",
162
- connKey: "fixture_local",
213
+ config: Sonamu.dbConfig.fixture_local,
214
+ toSkip: (() => {
215
+ const remoteConn = Sonamu.dbConfig.fixture_remote
216
+ .connection as Knex.ConnectionConfig;
217
+ const localConn = Sonamu.dbConfig.fixture_local
218
+ .connection as Knex.ConnectionConfig;
219
+ return (
220
+ remoteConn.host === localConn.host &&
221
+ remoteConn.database === localConn.database
222
+ );
223
+ })(),
163
224
  },
164
225
  {
165
226
  label: "(LOCAL) Testing DB",
166
- connKey: "test",
227
+ config: Sonamu.dbConfig.test,
167
228
  },
168
229
  ] as {
169
230
  label: string;
170
- connKey: keyof SonamuKnexDBConfig | keyof SonamuKyselyDBConfig;
231
+ config: Knex.Config;
232
+ toSkip?: boolean;
171
233
  }[];
172
234
 
173
235
  // 1. 기준DB 스키마를 덤프
174
236
  console.log("DUMP...");
175
237
  const dumpFilename = `/tmp/sonamu-fixture-init-${Date.now()}.sql`;
238
+ const srcConn = srcConfig.connection as Knex.ConnectionConfig;
176
239
  const migrationsDump = `/tmp/sonamu-fixture-init-migrations-${Date.now()}.sql`;
177
240
  execSync(
178
- `mysqldump -h${srcConn.host} -P${srcConn.port} -u${srcConn.user} -p${srcConn.password} --single-transaction -d --no-create-db --triggers ${srcConn.database} > ${dumpFilename}`
241
+ `mysqldump -h${srcConn.host} -u${srcConn.user} -p${srcConn.password} --single-transaction -d --no-create-db --triggers ${srcConn.database} > ${dumpFilename}`
179
242
  );
180
-
181
- // 2. 마이그레이션 테이블이 존재하면 덤프
182
- const dbClient = DB.baseConfig!.client;
183
- const migrationTable = DB.migrationTable;
184
- const [migrations] = await _db.raw<{ count: number }>(
185
- "SELECT COUNT(*) as count FROM information_schema.tables WHERE table_schema = ? AND table_name = ?",
186
- [srcConn.database, migrationTable]
243
+ const _db = knex(srcConfig);
244
+ const [[migrations]] = await _db.raw(
245
+ "SELECT COUNT(*) as count FROM information_schema.tables WHERE table_schema = ? AND table_name = 'knex_migrations'",
246
+ [srcConn.database]
187
247
  );
188
248
  if (migrations.count > 0) {
189
249
  execSync(
190
- `mysqldump -h${srcConn.host} -P${srcConn.port} -u${srcConn.user} -p${srcConn.password} --single-transaction --no-create-db --triggers ${srcConn.database} ${migrationTable} ${migrationTable}_lock > ${migrationsDump}`
250
+ `mysqldump -h${srcConn.host} -u${srcConn.user} -p${srcConn.password} --single-transaction --no-create-db --triggers ${srcConn.database} knex_migrations knex_migrations_lock > ${migrationsDump}`
191
251
  );
192
252
  }
193
253
 
194
254
  // 2. 대상DB 각각에 대하여 존재여부 확인 후 붓기
195
- for await (const { label, connKey } of targets) {
196
- const config = DB.connectionInfo[connKey];
197
-
198
- if (
199
- label === "(LOCAL) Fixture DB" &&
200
- targets.find(
201
- (t) =>
202
- t.label === "(REMOTE) Fixture DB" &&
203
- DB.connectionInfo[t.connKey].host === config.host &&
204
- DB.connectionInfo[t.connKey].database === config.database
205
- )
206
- ) {
255
+ for await (const { label, config, toSkip } of targets) {
256
+ const conn = config.connection as Knex.ConnectionConfig;
257
+
258
+ if (toSkip === true) {
207
259
  console.log(chalk.red(`${label}: Skipped!`));
208
260
  continue;
209
261
  }
210
262
 
211
- const db = (() => {
212
- if (dbClient === "knex") {
213
- const config = _.cloneDeep(DB.fullConfig[connKey]) as KnexConfig;
214
- config.connection.database = undefined;
215
- return new KnexClient(config);
216
- } else {
217
- const config = _.cloneDeep(DB.fullConfig[connKey]) as KyselyConfig;
218
- config.database = undefined;
219
- return new KyselyClient(config);
220
- }
221
- })();
222
-
223
- const [row] = await db.raw(`SHOW DATABASES LIKE "${config.database}"`);
263
+ const db = knex({
264
+ ...config,
265
+ connection: {
266
+ ...((config.connection ?? {}) as Knex.ConnectionConfig),
267
+ database: undefined,
268
+ },
269
+ });
270
+ const [[row]] = await db.raw(`SHOW DATABASES LIKE "${conn.database}"`);
224
271
  if (row) {
225
272
  console.log(
226
- chalk.yellow(`${label}: Database "${config.database}" Already exists`)
273
+ chalk.yellow(`${label}: Database "${conn.database}" Already exists`)
227
274
  );
228
275
  await db.destroy();
229
276
  continue;
230
277
  }
231
278
 
232
279
  console.log(`SYNC to ${label}...`);
233
- const mysqlCmd = `mysql -h${config.host} -P${srcConn.port} -u${config.user} -p${config.password}`;
234
- execSync(`${mysqlCmd} -e 'DROP DATABASE IF EXISTS \`${config.database}\`'`);
235
- execSync(`${mysqlCmd} -e 'CREATE DATABASE \`${config.database}\`'`);
236
- execSync(`${mysqlCmd} ${config.database} < ${dumpFilename}`);
237
- if (fs.existsSync(migrationsDump)) {
238
- execSync(`${mysqlCmd} ${config.database} < ${migrationsDump}`);
280
+ const mysqlCmd = `mysql -h${conn.host} -u${conn.user} -p${conn.password}`;
281
+ execSync(`${mysqlCmd} -e 'DROP DATABASE IF EXISTS \`${conn.database}\`'`);
282
+ execSync(`${mysqlCmd} -e 'CREATE DATABASE \`${conn.database}\`'`);
283
+ execSync(`${mysqlCmd} ${conn.database} < ${dumpFilename}`);
284
+ if (await exists(migrationsDump)) {
285
+ execSync(`${mysqlCmd} ${conn.database} < ${migrationsDump}`);
239
286
  }
240
287
 
241
288
  await db.destroy();
242
289
  }
290
+
291
+ await _db.destroy();
243
292
  }
244
293
 
245
294
  async function fixture_import(entityId: string, recordIds: number[]) {
@@ -257,11 +306,11 @@ async function fixture_sync() {
257
306
 
258
307
  async function stub_practice(name: string) {
259
308
  const practiceDir = path.join(Sonamu.apiRootPath, "src", "practices");
260
- const fileNames = fs.readdirSync(practiceDir);
309
+ const fileNames = await readdir(practiceDir);
261
310
 
262
- const maxSeqNo = (() => {
263
- if (fs.existsSync(practiceDir) === false) {
264
- fs.mkdirSync(practiceDir, { recursive: true });
311
+ const maxSeqNo = await (async () => {
312
+ if (!(await exists(practiceDir))) {
313
+ await mkdir(practiceDir, { recursive: true });
265
314
  }
266
315
 
267
316
  const filteredSeqs = fileNames
@@ -299,7 +348,7 @@ async function stub_practice(name: string) {
299
348
  `await BaseModel.destroy();`,
300
349
  `});`,
301
350
  ].join("\n");
302
- fs.writeFileSync(dstPath, code);
351
+ await writeFile(dstPath, code);
303
352
 
304
353
  execSync(`code ${dstPath}`);
305
354
 
@@ -329,10 +378,20 @@ async function scaffold_model_test(entityId: string) {
329
378
 
330
379
  async function ui() {
331
380
  try {
381
+ type StartServersOptions = {
382
+ projectName: string;
383
+ apiRootPath: string;
384
+ port: number;
385
+ };
332
386
  const sonamuUI: {
333
- startServers: (appRootPath: string) => void;
387
+ startServers: (options: StartServersOptions) => void;
334
388
  } = await import("@sonamu-kit/ui" as string);
335
- sonamuUI.startServers(Sonamu.apiRootPath);
389
+ sonamuUI.startServers({
390
+ projectName:
391
+ Sonamu.config.projectName ?? path.basename(Sonamu.apiRootPath),
392
+ apiRootPath: Sonamu.apiRootPath,
393
+ port: Sonamu.config.ui?.port ?? 57000,
394
+ });
336
395
  } catch (e: unknown) {
337
396
  if (e instanceof Error && e.message.includes("isn't declared")) {
338
397
  console.log(`You need to install ${chalk.blue(`@sonamu-kit/ui`)} first.`);
@@ -341,120 +400,3 @@ async function ui() {
341
400
  throw e;
342
401
  }
343
402
  }
344
-
345
- async function smd_migration() {
346
- await SMDManager.autoload();
347
- const smdIds = SMDManager.getAllIds();
348
-
349
- function enumLabelsToEntityEnums(
350
- entityId: string,
351
- enumLabels: {
352
- [enumName: string]: { [name: string]: { ko: string } };
353
- }
354
- ): { [enumName: string]: { [name: string]: string } } {
355
- return Object.fromEntries(
356
- Object.entries(enumLabels).map(([enumLabelName, enumLabel]) => {
357
- const enumName =
358
- entityId + inflection.camelize(enumLabelName.toLowerCase(), false);
359
- return [
360
- enumName,
361
- Object.fromEntries(
362
- Object.entries(enumLabel).map(([name, { ko }]) => [name, ko])
363
- ),
364
- ];
365
- })
366
- );
367
- }
368
- for await (const smdId of smdIds) {
369
- const smd = SMDManager.get(smdId);
370
- const entityJson = {
371
- id: smd.id,
372
- ...(smd.parentId && { parentId: smd.parentId }),
373
- title: smd.title,
374
- props: smd.props,
375
- indexes: smd.indexes,
376
- subsets: smd.subsets,
377
- enums: enumLabelsToEntityEnums(smd.id, smd.enumLabels),
378
- };
379
-
380
- const parentNames = SMDManager.getNamesFromId(smd.parentId ?? smd.id);
381
- const names = SMDManager.getNamesFromId(smd.id);
382
- const dstPath = path.join(
383
- Sonamu.apiRootPath,
384
- "src",
385
- "application",
386
- parentNames.fs,
387
- `${names.fs}.entity.json`
388
- );
389
-
390
- const formatted = await prettier.format(JSON.stringify(entityJson), {
391
- parser: "json",
392
- });
393
- fs.writeFileSync(dstPath, formatted);
394
- console.log(chalk.blue(`CREATED: ${dstPath}`));
395
-
396
- // smd.ts, enums.ts, genereated.ts 삭제 (트랜스파일 된 js파일도 삭제)
397
- const srcSmdPath = path.join(
398
- Sonamu.apiRootPath,
399
- "src",
400
- "application",
401
- parentNames.fs,
402
- `${names.fs}.smd.ts`
403
- );
404
- const dstSmdPath = srcSmdPath
405
- .replace("/src/", "/dist/")
406
- .replace(/\.ts$/, ".js");
407
- const srcEnumsPath = path.join(
408
- Sonamu.apiRootPath,
409
- "src",
410
- "application",
411
- parentNames.fs,
412
- `${names.fs}.enums.ts`
413
- );
414
- const dstEnumsPath = srcEnumsPath
415
- .replace("/src/", "/dist/")
416
- .replace(/\.ts$/, ".js");
417
- const srcGeneratedPath = path.join(
418
- Sonamu.apiRootPath,
419
- "src",
420
- "application",
421
- parentNames.fs,
422
- `${names.fs}.generated.ts`
423
- );
424
- const dstGeneratedPath = srcGeneratedPath
425
- .replace("/src/", "/dist/")
426
- .replace(/\.ts$/, ".js");
427
-
428
- [
429
- srcSmdPath,
430
- dstSmdPath,
431
- srcEnumsPath,
432
- dstEnumsPath,
433
- ...Sonamu.config.sync.targets.map((target) =>
434
- srcEnumsPath
435
- .replace(Sonamu.apiRootPath, path.join(Sonamu.appRootPath, target))
436
- .replace("/src/application/", "/src/services/")
437
- ),
438
- srcGeneratedPath,
439
- dstGeneratedPath,
440
- ].map((p) => {
441
- if (fs.existsSync(p) === false) {
442
- console.log(chalk.yellow(`NOT FOUND: ${p}`));
443
- return;
444
- }
445
- fs.unlinkSync(p);
446
- console.log(chalk.red(`DELETED: ${p}`));
447
- });
448
- }
449
-
450
- // Entity로 reload
451
- console.log("Entity로 다시 로드합니다.");
452
- EntityManager.isAutoloaded = false;
453
- await EntityManager.autoload();
454
-
455
- // Entity를 통해 generated.ts 재생성
456
- const entityIds = EntityManager.getAllParentIds();
457
- for await (const entityId of entityIds) {
458
- await Sonamu.syncer.generateTemplate("generated", { entityId });
459
- }
460
- }
@@ -4,11 +4,6 @@
4
4
  */
5
5
 
6
6
  import { Knex } from "knex";
7
- import { DB } from "./db";
8
- import { KnexClient } from "./drivers/knex/client";
9
- import { KyselyClient } from "./drivers/kysely/client";
10
- import { Transaction } from "kysely";
11
- import { Database } from "./types";
12
7
 
13
8
  export type RowWithId<Id extends string> = {
14
9
  [key in Id]: any;
@@ -24,12 +19,12 @@ export type RowWithId<Id extends string> = {
24
19
  * @param trx
25
20
  */
26
21
  export async function batchUpdate<Id extends string>(
27
- db: KnexClient | KyselyClient,
22
+ knex: Knex,
28
23
  tableName: string,
29
24
  ids: Id[],
30
25
  rows: RowWithId<Id>[],
31
26
  chunkSize = 50,
32
- trx: Knex.Transaction | Transaction<Database> | null = null
27
+ trx: Knex.Transaction | null = null
33
28
  ) {
34
29
  const chunks: RowWithId<Id>[][] = [];
35
30
  for (let i = 0; i < rows.length; i += chunkSize) {
@@ -38,18 +33,18 @@ export async function batchUpdate<Id extends string>(
38
33
 
39
34
  const executeUpdate = async (
40
35
  chunk: RowWithId<Id>[],
41
- transaction: KyselyClient | KnexClient
36
+ transaction: Knex.Transaction
42
37
  ) => {
43
- const sql = generateBatchUpdateSQL(db, tableName, chunk, ids);
44
- return transaction.raw(sql);
38
+ const sql = generateBatchUpdateSQL(knex, tableName, chunk, ids);
39
+ return knex.raw(sql).transacting(transaction);
45
40
  };
46
41
 
47
42
  if (trx) {
48
43
  for (const chunk of chunks) {
49
- await executeUpdate(chunk, DB.toClient(trx));
44
+ await executeUpdate(chunk, trx);
50
45
  }
51
46
  } else {
52
- await db.trx(async (newTrx) => {
47
+ await knex.transaction(async (newTrx) => {
53
48
  for (const chunk of chunks) {
54
49
  await executeUpdate(chunk, newTrx);
55
50
  }
@@ -75,7 +70,7 @@ function generateKeySetFromData(data: Record<string, any>[]) {
75
70
  }
76
71
 
77
72
  function generateBatchUpdateSQL<Id extends string>(
78
- db: KnexClient | KyselyClient,
73
+ db: Knex,
79
74
  tableName: string,
80
75
  data: Record<string, any>[],
81
76
  identifiers: Id[]
@@ -117,10 +112,10 @@ function generateBatchUpdateSQL<Id extends string>(
117
112
  data.map((row) => row[col])
118
113
  );
119
114
 
120
- const sql = db.createRawQuery(
115
+ const sql = db.raw(
121
116
  `UPDATE \`${tableName}\` SET ${cases.join(", ")} WHERE ${whereInClauses}`,
122
117
  [...bindings, ...whereInBindings]
123
118
  );
124
119
 
125
- return sql;
120
+ return sql.toQuery();
126
121
  }