sonamu 0.6.0 → 0.7.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 (406) hide show
  1. package/.swcrc.project-default +18 -0
  2. package/bin/cli.js +24 -0
  3. package/dist/ai/agents/agent.d.ts +11 -0
  4. package/dist/ai/agents/agent.d.ts.map +1 -0
  5. package/dist/ai/agents/agent.js +65 -0
  6. package/dist/ai/agents/index.d.ts +3 -0
  7. package/dist/ai/agents/index.d.ts.map +1 -0
  8. package/dist/ai/agents/index.js +4 -0
  9. package/dist/ai/agents/types.d.ts +43 -0
  10. package/dist/ai/agents/types.d.ts.map +1 -0
  11. package/dist/ai/agents/types.js +3 -0
  12. package/dist/ai/index.d.ts +2 -0
  13. package/dist/ai/index.d.ts.map +1 -0
  14. package/dist/ai/index.js +3 -0
  15. package/dist/ai/providers/rtzr/api.d.ts +22 -0
  16. package/dist/ai/providers/rtzr/api.d.ts.map +1 -0
  17. package/dist/ai/providers/rtzr/api.js +28 -0
  18. package/dist/ai/providers/rtzr/error.d.ts +18 -0
  19. package/dist/ai/providers/rtzr/error.d.ts.map +1 -0
  20. package/dist/ai/providers/rtzr/error.js +29 -0
  21. package/dist/ai/providers/rtzr/index.d.ts +5 -0
  22. package/dist/ai/providers/rtzr/index.d.ts.map +1 -0
  23. package/dist/ai/providers/rtzr/index.js +6 -0
  24. package/dist/ai/providers/rtzr/model.d.ts +52 -0
  25. package/dist/ai/providers/rtzr/model.d.ts.map +1 -0
  26. package/dist/ai/providers/rtzr/model.js +137 -0
  27. package/dist/ai/providers/rtzr/options.d.ts +7 -0
  28. package/dist/ai/providers/rtzr/options.d.ts.map +1 -0
  29. package/dist/ai/providers/rtzr/options.js +47 -0
  30. package/dist/ai/providers/rtzr/provider.d.ts +18 -0
  31. package/dist/ai/providers/rtzr/provider.d.ts.map +1 -0
  32. package/dist/ai/providers/rtzr/provider.js +54 -0
  33. package/dist/ai/providers/rtzr/utils.d.ts +19 -0
  34. package/dist/ai/providers/rtzr/utils.d.ts.map +1 -0
  35. package/dist/ai/providers/rtzr/utils.js +88 -0
  36. package/dist/api/base-frame.d.ts +2 -2
  37. package/dist/api/base-frame.d.ts.map +1 -1
  38. package/dist/api/base-frame.js +2 -1
  39. package/dist/api/caster.d.ts.map +1 -1
  40. package/dist/api/caster.js +6 -1
  41. package/dist/api/code-converters.d.ts +58 -14
  42. package/dist/api/code-converters.d.ts.map +1 -1
  43. package/dist/api/code-converters.js +178 -409
  44. package/dist/api/config.d.ts +27 -13
  45. package/dist/api/config.d.ts.map +1 -1
  46. package/dist/api/config.js +19 -26
  47. package/dist/api/context.d.ts +4 -3
  48. package/dist/api/context.d.ts.map +1 -1
  49. package/dist/api/context.js +1 -1
  50. package/dist/api/decorators.d.ts +20 -6
  51. package/dist/api/decorators.d.ts.map +1 -1
  52. package/dist/api/decorators.js +111 -18
  53. package/dist/api/index.d.ts +2 -2
  54. package/dist/api/index.d.ts.map +1 -1
  55. package/dist/api/index.js +3 -3
  56. package/dist/api/sonamu.d.ts +7 -7
  57. package/dist/api/sonamu.d.ts.map +1 -1
  58. package/dist/api/sonamu.js +83 -51
  59. package/dist/api/validator.d.ts +6 -0
  60. package/dist/api/validator.d.ts.map +1 -0
  61. package/dist/api/validator.js +81 -0
  62. package/dist/bin/build-config.d.ts +5 -1
  63. package/dist/bin/build-config.d.ts.map +1 -1
  64. package/dist/bin/build-config.js +5 -2
  65. package/dist/bin/cli.js +165 -64
  66. package/dist/bin/loader-register.d.ts +2 -0
  67. package/dist/bin/loader-register.d.ts.map +1 -0
  68. package/dist/bin/loader-register.js +34 -0
  69. package/dist/database/_batch_update.d.ts +5 -3
  70. package/dist/database/_batch_update.d.ts.map +1 -1
  71. package/dist/database/_batch_update.js +30 -13
  72. package/dist/database/base-model.d.ts +96 -10
  73. package/dist/database/base-model.d.ts.map +1 -1
  74. package/dist/database/base-model.js +232 -89
  75. package/dist/database/base-model.types.d.ts +93 -0
  76. package/dist/database/base-model.types.d.ts.map +1 -0
  77. package/dist/database/base-model.types.js +10 -0
  78. package/dist/database/code-generator.d.ts +1 -1
  79. package/dist/database/code-generator.d.ts.map +1 -1
  80. package/dist/database/code-generator.js +11 -10
  81. package/dist/database/db.d.ts +5 -6
  82. package/dist/database/db.d.ts.map +1 -1
  83. package/dist/database/db.js +22 -25
  84. package/dist/database/puri-subset.test-d.js +81 -0
  85. package/dist/database/puri-subset.types.d.ts +123 -0
  86. package/dist/database/puri-subset.types.d.ts.map +1 -0
  87. package/dist/database/puri-subset.types.js +16 -0
  88. package/dist/database/puri-wrapper.d.ts +13 -11
  89. package/dist/database/puri-wrapper.d.ts.map +1 -1
  90. package/dist/database/puri-wrapper.js +2 -2
  91. package/dist/database/puri.d.ts +25 -14
  92. package/dist/database/puri.d.ts.map +1 -1
  93. package/dist/database/puri.js +83 -21
  94. package/dist/database/puri.types.d.ts +21 -7
  95. package/dist/database/puri.types.d.ts.map +1 -1
  96. package/dist/database/puri.types.js +4 -1
  97. package/dist/database/transaction-context.d.ts +1 -1
  98. package/dist/database/transaction-context.d.ts.map +1 -1
  99. package/dist/database/transaction-context.js +1 -1
  100. package/dist/database/upsert-builder.d.ts +9 -3
  101. package/dist/database/upsert-builder.d.ts.map +1 -1
  102. package/dist/database/upsert-builder.js +228 -78
  103. package/dist/entity/entity-manager.d.ts +165 -2
  104. package/dist/entity/entity-manager.d.ts.map +1 -1
  105. package/dist/entity/entity-manager.js +26 -10
  106. package/dist/entity/entity.d.ts +5 -3
  107. package/dist/entity/entity.d.ts.map +1 -1
  108. package/dist/entity/entity.js +153 -54
  109. package/dist/exceptions/error-handler.d.ts +1 -1
  110. package/dist/exceptions/error-handler.d.ts.map +1 -1
  111. package/dist/exceptions/error-handler.js +1 -1
  112. package/dist/exceptions/so-exceptions.d.ts +1 -1
  113. package/dist/exceptions/so-exceptions.d.ts.map +1 -1
  114. package/dist/exceptions/so-exceptions.js +1 -1
  115. package/dist/file-storage/driver.d.ts +1 -1
  116. package/dist/file-storage/driver.d.ts.map +1 -1
  117. package/dist/file-storage/driver.js +1 -1
  118. package/dist/file-storage/file-storage.js +2 -2
  119. package/dist/index.d.ts +18 -11
  120. package/dist/index.d.ts.map +1 -1
  121. package/dist/index.js +19 -13
  122. package/dist/migration/code-generation.d.ts +1 -1
  123. package/dist/migration/code-generation.d.ts.map +1 -1
  124. package/dist/migration/code-generation.js +123 -67
  125. package/dist/migration/migration-set.d.ts +2 -10
  126. package/dist/migration/migration-set.d.ts.map +1 -1
  127. package/dist/migration/migration-set.js +67 -218
  128. package/dist/migration/migrator.d.ts +24 -73
  129. package/dist/migration/migrator.d.ts.map +1 -1
  130. package/dist/migration/migrator.js +121 -301
  131. package/dist/migration/postgresql-schema-reader.d.ts +51 -0
  132. package/dist/migration/postgresql-schema-reader.d.ts.map +1 -0
  133. package/dist/migration/postgresql-schema-reader.js +245 -0
  134. package/dist/migration/types.d.ts +6 -38
  135. package/dist/migration/types.d.ts.map +1 -1
  136. package/dist/migration/types.js +1 -1
  137. package/dist/naite/messaging-types.d.ts +43 -0
  138. package/dist/naite/messaging-types.d.ts.map +1 -0
  139. package/dist/naite/messaging-types.js +7 -0
  140. package/dist/naite/naite-reporter.d.ts +41 -0
  141. package/dist/naite/naite-reporter.d.ts.map +1 -0
  142. package/dist/naite/naite-reporter.js +102 -0
  143. package/dist/naite/naite.d.ts +91 -8
  144. package/dist/naite/naite.d.ts.map +1 -1
  145. package/dist/naite/naite.js +285 -41
  146. package/dist/stream/sse.d.ts +2 -2
  147. package/dist/stream/sse.d.ts.map +1 -1
  148. package/dist/stream/sse.js +1 -1
  149. package/dist/syncer/api-parser.d.ts +3 -13
  150. package/dist/syncer/api-parser.d.ts.map +1 -1
  151. package/dist/syncer/api-parser.js +67 -56
  152. package/dist/syncer/checksum.d.ts +2 -2
  153. package/dist/syncer/checksum.d.ts.map +1 -1
  154. package/dist/syncer/checksum.js +11 -11
  155. package/dist/syncer/code-generator.d.ts +3 -3
  156. package/dist/syncer/code-generator.d.ts.map +1 -1
  157. package/dist/syncer/code-generator.js +37 -17
  158. package/dist/syncer/entity-operations.d.ts +2 -2
  159. package/dist/syncer/entity-operations.d.ts.map +1 -1
  160. package/dist/syncer/entity-operations.js +9 -8
  161. package/dist/syncer/file-patterns.d.ts +1 -1
  162. package/dist/syncer/file-patterns.d.ts.map +1 -1
  163. package/dist/syncer/file-patterns.js +1 -1
  164. package/dist/syncer/index.d.ts +4 -4
  165. package/dist/syncer/index.d.ts.map +1 -1
  166. package/dist/syncer/index.js +5 -5
  167. package/dist/syncer/module-loader.d.ts +4 -4
  168. package/dist/syncer/module-loader.d.ts.map +1 -1
  169. package/dist/syncer/module-loader.js +17 -12
  170. package/dist/syncer/syncer.d.ts +31 -24
  171. package/dist/syncer/syncer.d.ts.map +1 -1
  172. package/dist/syncer/syncer.js +92 -45
  173. package/dist/template/entity-converter.d.ts +1 -1
  174. package/dist/template/entity-converter.d.ts.map +1 -1
  175. package/dist/template/entity-converter.js +15 -8
  176. package/dist/template/helpers.d.ts +2 -2
  177. package/dist/template/helpers.d.ts.map +1 -1
  178. package/dist/template/helpers.js +3 -3
  179. package/dist/template/implementations/entity.template.d.ts +2 -2
  180. package/dist/template/implementations/entity.template.d.ts.map +1 -1
  181. package/dist/template/implementations/entity.template.js +4 -5
  182. package/dist/template/implementations/generated.template.d.ts +2 -3
  183. package/dist/template/implementations/generated.template.d.ts.map +1 -1
  184. package/dist/template/implementations/generated.template.js +46 -29
  185. package/dist/template/implementations/generated_http.template.d.ts +2 -3
  186. package/dist/template/implementations/generated_http.template.d.ts.map +1 -1
  187. package/dist/template/implementations/generated_http.template.js +9 -9
  188. package/dist/template/implementations/generated_sso.template.d.ts +3 -4
  189. package/dist/template/implementations/generated_sso.template.d.ts.map +1 -1
  190. package/dist/template/implementations/generated_sso.template.js +54 -25
  191. package/dist/template/implementations/init_types.template.d.ts +2 -2
  192. package/dist/template/implementations/init_types.template.d.ts.map +1 -1
  193. package/dist/template/implementations/init_types.template.js +2 -2
  194. package/dist/template/implementations/model.template.d.ts +2 -2
  195. package/dist/template/implementations/model.template.d.ts.map +1 -1
  196. package/dist/template/implementations/model.template.js +47 -37
  197. package/dist/template/implementations/model_test.template.d.ts +2 -2
  198. package/dist/template/implementations/model_test.template.d.ts.map +1 -1
  199. package/dist/template/implementations/model_test.template.js +2 -2
  200. package/dist/template/implementations/service.template.d.ts +4 -4
  201. package/dist/template/implementations/service.template.d.ts.map +1 -1
  202. package/dist/template/implementations/service.template.js +24 -16
  203. package/dist/template/implementations/view_enums_buttonset.template.d.ts +2 -2
  204. package/dist/template/implementations/view_enums_buttonset.template.d.ts.map +1 -1
  205. package/dist/template/implementations/view_enums_buttonset.template.js +1 -1
  206. package/dist/template/implementations/view_enums_dropdown.template.d.ts +2 -2
  207. package/dist/template/implementations/view_enums_dropdown.template.d.ts.map +1 -1
  208. package/dist/template/implementations/view_enums_dropdown.template.js +2 -2
  209. package/dist/template/implementations/view_enums_select.template.d.ts +2 -2
  210. package/dist/template/implementations/view_enums_select.template.d.ts.map +1 -1
  211. package/dist/template/implementations/view_enums_select.template.js +2 -2
  212. package/dist/template/implementations/view_form.template.d.ts +2 -2
  213. package/dist/template/implementations/view_form.template.d.ts.map +1 -1
  214. package/dist/template/implementations/view_form.template.js +4 -4
  215. package/dist/template/implementations/view_id_all_select.template.d.ts +2 -2
  216. package/dist/template/implementations/view_id_all_select.template.d.ts.map +1 -1
  217. package/dist/template/implementations/view_id_all_select.template.js +1 -1
  218. package/dist/template/implementations/view_id_async_select.template.d.ts +2 -2
  219. package/dist/template/implementations/view_id_async_select.template.d.ts.map +1 -1
  220. package/dist/template/implementations/view_id_async_select.template.js +1 -1
  221. package/dist/template/implementations/view_list.template.d.ts +2 -2
  222. package/dist/template/implementations/view_list.template.d.ts.map +1 -1
  223. package/dist/template/implementations/view_list.template.js +29 -19
  224. package/dist/template/implementations/view_list_columns.template.d.ts +3 -3
  225. package/dist/template/implementations/view_list_columns.template.d.ts.map +1 -1
  226. package/dist/template/implementations/view_list_columns.template.js +1 -1
  227. package/dist/template/implementations/view_search_input.template.d.ts +2 -2
  228. package/dist/template/implementations/view_search_input.template.d.ts.map +1 -1
  229. package/dist/template/implementations/view_search_input.template.js +1 -1
  230. package/dist/template/index.d.ts +4 -2
  231. package/dist/template/index.d.ts.map +1 -1
  232. package/dist/template/index.js +5 -3
  233. package/dist/template/template-manager.d.ts +56 -0
  234. package/dist/template/template-manager.d.ts.map +1 -0
  235. package/dist/template/template-manager.js +125 -0
  236. package/dist/template/template-types.d.ts +16 -0
  237. package/dist/template/template-types.d.ts.map +1 -0
  238. package/dist/template/template-types.js +7 -0
  239. package/dist/template/template.d.ts +12 -2
  240. package/dist/template/template.d.ts.map +1 -1
  241. package/dist/template/template.js +19 -6
  242. package/dist/template/zod-converter.d.ts +40 -7
  243. package/dist/template/zod-converter.d.ts.map +1 -1
  244. package/dist/template/zod-converter.js +341 -58
  245. package/dist/testing/_relation-graph.d.ts +1 -1
  246. package/dist/testing/_relation-graph.d.ts.map +1 -1
  247. package/dist/testing/_relation-graph.js +12 -3
  248. package/dist/testing/fixture-manager.d.ts +42 -11
  249. package/dist/testing/fixture-manager.d.ts.map +1 -1
  250. package/dist/testing/fixture-manager.js +338 -236
  251. package/dist/types/types.d.ts +709 -104
  252. package/dist/types/types.d.ts.map +1 -1
  253. package/dist/types/types.js +309 -52
  254. package/dist/typings/knex.d.js +2 -2
  255. package/dist/utils/async-utils.d.ts.map +1 -1
  256. package/dist/utils/async-utils.js +3 -3
  257. package/dist/utils/console-util.js +1 -1
  258. package/dist/utils/controller.d.ts +1 -0
  259. package/dist/utils/controller.d.ts.map +1 -1
  260. package/dist/utils/controller.js +4 -1
  261. package/dist/utils/esm-utils.d.ts +0 -6
  262. package/dist/utils/esm-utils.d.ts.map +1 -1
  263. package/dist/utils/esm-utils.js +2 -9
  264. package/dist/utils/formatter.d.ts +3 -0
  265. package/dist/utils/formatter.d.ts.map +1 -0
  266. package/dist/utils/formatter.js +110 -0
  267. package/dist/utils/fs-utils.d.ts +1 -1
  268. package/dist/utils/fs-utils.d.ts.map +1 -1
  269. package/dist/utils/fs-utils.js +1 -1
  270. package/dist/utils/lodash-able.d.ts.map +1 -1
  271. package/dist/utils/lodash-able.js +1 -1
  272. package/dist/utils/object-utils.d.ts +44 -0
  273. package/dist/utils/object-utils.d.ts.map +1 -0
  274. package/dist/utils/object-utils.js +191 -0
  275. package/dist/utils/path-utils.d.ts +1 -1
  276. package/dist/utils/path-utils.d.ts.map +1 -1
  277. package/dist/utils/path-utils.js +3 -3
  278. package/dist/utils/process-utils.js +1 -1
  279. package/dist/utils/sql-parser.d.ts +5 -1
  280. package/dist/utils/sql-parser.d.ts.map +1 -1
  281. package/dist/utils/sql-parser.js +14 -3
  282. package/dist/utils/type-utils.d.ts +23 -0
  283. package/dist/utils/type-utils.d.ts.map +1 -0
  284. package/dist/utils/type-utils.js +45 -0
  285. package/dist/utils/utils.d.ts +7 -1
  286. package/dist/utils/utils.d.ts.map +1 -1
  287. package/dist/utils/utils.js +44 -5
  288. package/dist/utils/zod-error.d.ts +1 -1
  289. package/dist/utils/zod-error.d.ts.map +1 -1
  290. package/dist/utils/zod-error.js +1 -1
  291. package/package.json +54 -29
  292. package/src/ai/agents/agent.ts +87 -0
  293. package/src/ai/agents/index.ts +2 -0
  294. package/src/ai/agents/types.ts +47 -0
  295. package/src/ai/index.ts +1 -0
  296. package/src/ai/providers/rtzr/api.ts +37 -0
  297. package/src/ai/providers/rtzr/error.ts +34 -0
  298. package/src/ai/providers/rtzr/index.ts +4 -0
  299. package/src/ai/providers/rtzr/model.ts +201 -0
  300. package/src/ai/providers/rtzr/options.ts +49 -0
  301. package/src/ai/providers/rtzr/provider.ts +91 -0
  302. package/src/ai/providers/rtzr/utils.ts +127 -0
  303. package/src/api/base-frame.ts +4 -2
  304. package/src/api/caster.ts +17 -23
  305. package/src/api/code-converters.ts +176 -533
  306. package/src/api/config.ts +39 -56
  307. package/src/api/context.ts +7 -18
  308. package/src/api/decorators.ts +175 -46
  309. package/src/api/index.ts +2 -2
  310. package/src/api/sonamu.ts +133 -124
  311. package/src/api/validator.ts +83 -0
  312. package/src/bin/build-config.ts +7 -1
  313. package/src/bin/cli.ts +192 -110
  314. package/src/bin/loader-register.ts +38 -0
  315. package/src/database/_batch_update.ts +46 -31
  316. package/src/database/base-model.ts +390 -182
  317. package/src/database/base-model.types.ts +155 -0
  318. package/src/database/code-generator.ts +13 -32
  319. package/src/database/db.ts +36 -50
  320. package/src/database/puri-subset.test-d.ts +471 -0
  321. package/src/database/puri-subset.types.ts +195 -0
  322. package/src/database/puri-wrapper.ts +58 -67
  323. package/src/database/puri.ts +182 -126
  324. package/src/database/puri.types.ts +64 -31
  325. package/src/database/transaction-context.ts +1 -1
  326. package/src/database/upsert-builder.ts +262 -132
  327. package/src/entity/entity-manager.ts +36 -28
  328. package/src/entity/entity.ts +330 -249
  329. package/src/exceptions/error-handler.ts +3 -3
  330. package/src/exceptions/so-exceptions.ts +11 -11
  331. package/src/file-storage/driver.ts +5 -5
  332. package/src/file-storage/file-storage.ts +2 -2
  333. package/src/index.ts +18 -12
  334. package/src/migration/code-generation.ts +185 -172
  335. package/src/migration/migration-set.ts +80 -293
  336. package/src/migration/migrator.ts +182 -425
  337. package/src/migration/mysql-schema-reader.ts.txt +272 -0
  338. package/src/migration/postgresql-schema-reader.ts +310 -0
  339. package/src/migration/types.ts +6 -39
  340. package/src/naite/messaging-types.ts +51 -0
  341. package/src/naite/naite-reporter.ts +128 -0
  342. package/src/naite/naite.ts +378 -33
  343. package/src/shared/web.shared.ts.txt +20 -24
  344. package/src/stream/sse.ts +5 -5
  345. package/src/syncer/api-parser.ts +52 -69
  346. package/src/syncer/checksum.ts +25 -37
  347. package/src/syncer/code-generator.ts +58 -62
  348. package/src/syncer/entity-operations.ts +12 -15
  349. package/src/syncer/file-patterns.ts +2 -2
  350. package/src/syncer/index.ts +4 -4
  351. package/src/syncer/module-loader.ts +28 -25
  352. package/src/syncer/syncer.ts +155 -162
  353. package/src/template/entity-converter.ts +18 -27
  354. package/src/template/helpers.ts +8 -11
  355. package/src/template/implementations/entity.template.ts +6 -6
  356. package/src/template/implementations/generated.template.ts +99 -99
  357. package/src/template/implementations/generated_http.template.ts +21 -54
  358. package/src/template/implementations/generated_sso.template.ts +78 -65
  359. package/src/template/implementations/init_types.template.ts +4 -6
  360. package/src/template/implementations/model.template.ts +47 -38
  361. package/src/template/implementations/model_test.template.ts +3 -3
  362. package/src/template/implementations/service.template.ts +56 -80
  363. package/src/template/implementations/view_enums_buttonset.template.ts +2 -2
  364. package/src/template/implementations/view_enums_dropdown.template.ts +4 -4
  365. package/src/template/implementations/view_enums_select.template.ts +3 -3
  366. package/src/template/implementations/view_form.template.ts +34 -75
  367. package/src/template/implementations/view_id_all_select.template.ts +2 -2
  368. package/src/template/implementations/view_id_async_select.template.ts +9 -23
  369. package/src/template/implementations/view_list.template.ts +54 -95
  370. package/src/template/implementations/view_list_columns.template.ts +4 -10
  371. package/src/template/implementations/view_search_input.template.ts +2 -2
  372. package/src/template/index.ts +4 -2
  373. package/src/template/template-manager.ts +166 -0
  374. package/src/template/template-types.ts +16 -0
  375. package/src/template/template.ts +29 -10
  376. package/src/template/zod-converter.ts +407 -101
  377. package/src/testing/_relation-graph.ts +18 -11
  378. package/src/testing/fixture-manager.ts +468 -362
  379. package/src/types/types.ts +516 -248
  380. package/src/typings/knex.d.ts +7 -9
  381. package/src/utils/async-utils.ts +8 -12
  382. package/src/utils/console-util.ts +1 -1
  383. package/src/utils/controller.ts +3 -0
  384. package/src/utils/esm-utils.ts +8 -18
  385. package/src/utils/formatter.ts +109 -0
  386. package/src/utils/fs-utils.ts +1 -1
  387. package/src/utils/lodash-able.ts +1 -4
  388. package/src/utils/object-utils.ts +217 -0
  389. package/src/utils/path-utils.ts +3 -6
  390. package/src/utils/process-utils.ts +1 -1
  391. package/src/utils/sql-parser.ts +23 -5
  392. package/src/utils/type-utils.ts +83 -0
  393. package/src/utils/utils.ts +58 -9
  394. package/src/utils/zod-error.ts +3 -3
  395. package/dist/bin/cli-wrapper.d.ts +0 -3
  396. package/dist/bin/cli-wrapper.d.ts.map +0 -1
  397. package/dist/bin/cli-wrapper.js +0 -72
  398. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts +0 -2
  399. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts.map +0 -1
  400. package/dist/database/knex-plugins/knex-on-duplicate-update.js +0 -39
  401. package/dist/entity/entity-utils.d.ts +0 -61
  402. package/dist/entity/entity-utils.d.ts.map +0 -1
  403. package/dist/entity/entity-utils.js +0 -210
  404. package/src/bin/cli-wrapper.ts +0 -82
  405. package/src/database/knex-plugins/knex-on-duplicate-update.ts +0 -45
  406. package/src/entity/entity-utils.ts +0 -291
@@ -1,13 +1,15 @@
1
- import * as _ from "lodash-es";
2
- import prettier from "prettier";
3
1
  import equal from "fast-deep-equal";
4
- import {
2
+ import { alphabetical, diff, fork, omit } from "radashi";
3
+ import { Naite } from "..";
4
+ import type {
5
5
  GenMigrationCode,
6
6
  MigrationColumn,
7
7
  MigrationForeign,
8
8
  MigrationIndex,
9
9
  MigrationSet,
10
10
  } from "../types/types";
11
+ import { formatCode } from "../utils/formatter";
12
+ import { differenceWith, intersectionBy } from "../utils/utils";
11
13
 
12
14
  /**
13
15
  * 테이블 생성하는 케이스 - 컬럼/인덱스 생성
@@ -15,12 +17,12 @@ import {
15
17
  async function generateCreateCode_ColumnAndIndexes(
16
18
  table: string,
17
19
  columns: MigrationColumn[],
18
- indexes: MigrationIndex[]
20
+ indexes: MigrationIndex[],
19
21
  ): Promise<GenMigrationCode> {
20
22
  // fulltext index 분리
21
- const [ngramIndexes, standardIndexes] = _.partition(
23
+ const [ngramIndexes, standardIndexes] = fork(
22
24
  indexes,
23
- (i) => i.type === "fulltext" && i.parser === "ngram"
25
+ (i) => i.type === "fulltext" && i.parser === "ngram",
24
26
  );
25
27
 
26
28
  // 컬럼, 인덱스 처리
@@ -47,9 +49,7 @@ async function generateCreateCode_ColumnAndIndexes(
47
49
  table,
48
50
  type: "normal",
49
51
  title: `create__${table}`,
50
- formatted: await prettier.format(lines.join("\n"), {
51
- parser: "typescript",
52
- }),
52
+ formatted: formatCode(lines.join("\n"), "typescript", `src/migration/${table}.ts`),
53
53
  };
54
54
  }
55
55
 
@@ -63,38 +63,49 @@ function genColumnDefinitions(columns: MigrationColumn[]): string[] {
63
63
  return `table.increments().primary();`;
64
64
  }
65
65
 
66
- // FIXME: float(M,D) deprecated -> decimal(M,D) 이용하도록 하고, float/double 처리 추가
67
- if (column.type === "float" || column.type === "decimal") {
68
- chains.push(
69
- `${column.type}('${column.name}', ${column.precision}, ${column.scale})`
70
- );
66
+ // 배열 타입 처리
67
+ if (column.type.endsWith("[]")) {
68
+ const elementType = column.type.slice(0, -2); // "integer[]" -> "integer"
69
+ const pgType = getPgArrayType(column, elementType);
70
+ chains.push(`specificType('${column.name}', '${pgType}')`);
71
+ } else if (column.type === "numberOrNumeric") {
72
+ // number
73
+ if (column.numberType === "real") {
74
+ chains.push(`float('${column.name}')`);
75
+ } else if (column.numberType === "double precision") {
76
+ chains.push(`double('${column.name}')`);
77
+ } else if ((column.numberType ?? "numeric") === "numeric") {
78
+ chains.push(`decimal('${column.name}', ${column.precision}, ${column.scale})`);
79
+ }
80
+ } else if (column.type === "string") {
81
+ // string
82
+ if (column.length !== undefined) {
83
+ chains.push(`string('${column.name}', ${column.length})`);
84
+ } else {
85
+ chains.push(`text('${column.name}')`);
86
+ }
87
+ } else if (column.type === "date") {
88
+ // date
89
+ chains.push(`timestamp('${column.name}', { useTz: true })`);
90
+ } else if (column.type === "json") {
91
+ // json
92
+ chains.push(`jsonb('${column.name}')`);
71
93
  } else {
72
94
  // type, length
73
- let columnType = column.type;
74
95
  let extraType: string | undefined;
75
- if (columnType.includes("text") && columnType !== "text") {
76
- extraType = columnType;
77
- columnType = "text";
78
- }
79
96
  chains.push(
80
- `${columnType}('${column.name}'${
97
+ `${column.type}('${column.name}'${
81
98
  column.length ? `, ${column.length}` : ""
82
- }${extraType ? `, '${extraType}'` : ""})`
99
+ }${extraType ? `, '${extraType}'` : ""})`,
83
100
  );
84
101
  }
85
- if (column.unsigned) {
86
- chains.push("unsigned()");
87
- }
88
102
 
89
103
  // nullable
90
104
  chains.push(column.nullable ? "nullable()" : "notNullable()");
91
105
 
92
106
  // defaultTo
93
107
  if (column.defaultTo !== undefined) {
94
- if (
95
- typeof column.defaultTo === "string" &&
96
- column.defaultTo.startsWith(`"`)
97
- ) {
108
+ if (typeof column.defaultTo === "string" && column.defaultTo.startsWith(`"`)) {
98
109
  chains.push(`defaultTo(${column.defaultTo})`);
99
110
  } else {
100
111
  chains.push(`defaultTo(knex.raw('${column.defaultTo}'))`);
@@ -105,6 +116,25 @@ function genColumnDefinitions(columns: MigrationColumn[]): string[] {
105
116
  });
106
117
  }
107
118
 
119
+ function getPgArrayType(column: MigrationColumn, elementType: string): string {
120
+ if (elementType === "numberOrNumeric") {
121
+ if (column.numberType === "real") return "real[]";
122
+ if (column.numberType === "double precision") return "double precision[]";
123
+ return `numeric(${column.precision}, ${column.scale})[]`;
124
+ }
125
+ if (elementType === "string") {
126
+ return column.length ? `varchar(${column.length})[]` : "text[]";
127
+ }
128
+ if (elementType === "date") return "timestamptz[]";
129
+ if (elementType === "integer") return "integer[]";
130
+ if (elementType === "bigInteger") return "bigint[]";
131
+ if (elementType === "boolean") return "boolean[]";
132
+ if (elementType === "uuid") return "uuid[]";
133
+ if (elementType === "enum") return "text[]";
134
+
135
+ throw new Error(`Unknown array element type: ${elementType}`);
136
+ }
137
+
108
138
  /**
109
139
  * 개별 인덱스 정의 생성
110
140
  */
@@ -118,7 +148,7 @@ function genIndexDefinition(index: MigrationIndex, table: string) {
118
148
  if (index.type === "fulltext" && index.parser === "ngram") {
119
149
  const indexName = `${table}_${index.columns.join("_")}_index`;
120
150
  return `await knex.raw(\`ALTER TABLE ${table} ADD FULLTEXT INDEX ${indexName} (${index.columns.join(
121
- ", "
151
+ ", ",
122
152
  )}) WITH PARSER ngram\`);`;
123
153
  }
124
154
 
@@ -132,7 +162,7 @@ function genIndexDefinition(index: MigrationIndex, table: string) {
132
162
  */
133
163
  async function generateCreateCode_Foreign(
134
164
  table: string,
135
- foreigns: MigrationForeign[]
165
+ foreigns: MigrationForeign[],
136
166
  ): Promise<GenMigrationCode[]> {
137
167
  if (foreigns.length === 0) {
138
168
  return [];
@@ -162,17 +192,13 @@ async function generateCreateCode_Foreign(
162
192
  "}",
163
193
  ];
164
194
 
165
- const foreignKeysString = foreigns
166
- .map((foreign) => foreign.columns.join("_"))
167
- .join("_");
195
+ const foreignKeysString = foreigns.map((foreign) => foreign.columns.join("_")).join("_");
168
196
  return [
169
197
  {
170
198
  table,
171
199
  type: "foreign",
172
200
  title: `foreign__${table}__${foreignKeysString}`,
173
- formatted: await prettier.format(lines.join("\n"), {
174
- parser: "typescript",
175
- }),
201
+ formatted: formatCode(lines.join("\n"), "typescript", `src/migration/${table}.ts`),
176
202
  },
177
203
  ];
178
204
  }
@@ -182,7 +208,7 @@ async function generateCreateCode_Foreign(
182
208
  */
183
209
  function genForeignDefinitions(
184
210
  table: string,
185
- foreigns: MigrationForeign[]
211
+ foreigns: MigrationForeign[],
186
212
  ): { up: string[]; down: string[] } {
187
213
  return foreigns.reduce(
188
214
  (r, foreign) => {
@@ -193,7 +219,7 @@ function genForeignDefinitions(
193
219
  `table.foreign('${foreign.columns.join(",")}')
194
220
  .references('${foreign.to}')
195
221
  .onUpdate('${foreign.onUpdate}')
196
- .onDelete('${foreign.onDelete}')`
222
+ .onDelete('${foreign.onDelete}')`,
197
223
  );
198
224
  r.down.push(`table.dropForeign([${columnsStringQuote}])`);
199
225
  return r;
@@ -201,7 +227,7 @@ function genForeignDefinitions(
201
227
  {
202
228
  up: [] as string[],
203
229
  down: [] as string[],
204
- }
230
+ },
205
231
  );
206
232
  }
207
233
 
@@ -214,7 +240,7 @@ async function generateAlterCode_ColumnAndIndexes(
214
240
  entityIndexes: MigrationIndex[],
215
241
  dbColumns: MigrationColumn[],
216
242
  dbIndexes: MigrationIndex[],
217
- dbForeigns: MigrationForeign[]
243
+ dbForeigns: MigrationForeign[],
218
244
  ): Promise<GenMigrationCode[]> {
219
245
  /*
220
246
  세부 비교 후 다른점 찾아서 코드 생성
@@ -236,26 +262,53 @@ async function generateAlterCode_ColumnAndIndexes(
236
262
  alterColumnsTo,
237
263
  entityColumns,
238
264
  table,
239
- dbForeigns
265
+ dbForeigns,
240
266
  );
241
267
 
242
268
  // 인덱스의 add, drop 여부 확인
243
269
  const alterIndexesTo = getAlterIndexesTo(entityIndexes, dbIndexes);
244
270
 
245
271
  // fulltext index 분리
246
- const [ngramIndexes, standardIndexes] = _.partition(
272
+ const [ngramIndexes, standardIndexes] = fork(
247
273
  alterIndexesTo.add,
248
- (i) => i.type === "fulltext" && i.parser === "ngram"
274
+ (i) => i.type === "fulltext" && i.parser === "ngram",
249
275
  );
250
276
 
251
277
  // 인덱스가 삭제되는 경우, 컬럼과 같이 삭제된 케이스에는 drop에서 제외해야함!
252
278
  const indexNeedsToDrop = alterIndexesTo.drop.filter(
253
279
  (index) =>
254
280
  index.columns.every((colName) =>
255
- alterColumnsTo.drop.map((col) => col.name).includes(colName)
256
- ) === false
281
+ alterColumnsTo.drop.map((col) => col.name).includes(colName),
282
+ ) === false,
257
283
  );
258
284
 
285
+ // 빈 코드 생성 방지
286
+ if (
287
+ alterColumnLinesTo.add.up.length === 0 &&
288
+ alterColumnLinesTo.drop.up.length === 0 &&
289
+ alterColumnLinesTo.alter.up.length === 0 &&
290
+ standardIndexes.length === 0 &&
291
+ indexNeedsToDrop.length === 0
292
+ ) {
293
+ Naite.t("migrator:generateAlterCode_ColumnAndIndexes:emptyCodeGenerationError", {
294
+ entityColumns,
295
+ dbColumns,
296
+ entityIndexes,
297
+ dbIndexes,
298
+ });
299
+ // throw new Error("컬럼/인덱스 변경 코드 생성 오류");
300
+ }
301
+ Naite.t("migrator:generateAlterCode_ColumnAndIndexes:debug", {
302
+ "alterColumnsTo.add.length": alterColumnsTo.add.length,
303
+ "alterColumnsTo.drop.length": alterColumnsTo.drop.length,
304
+ "alterColumnsTo.alter.length": alterColumnsTo.alter.length,
305
+ "alterIndexesTo.add.length": alterIndexesTo.add.length,
306
+ "alterIndexesTo.drop.length": alterIndexesTo.drop.length,
307
+ "standardIndexes.length": standardIndexes.length,
308
+ "indexNeedsToDrop.length": indexNeedsToDrop.length,
309
+ });
310
+ // Naite.t("migrator:generateAlterCode_ColumnAndIndexes:alterColumnsTo", alterColumnsTo);
311
+
259
312
  const lines: string[] = [
260
313
  'import { Knex } from "knex";',
261
314
  "",
@@ -285,8 +338,8 @@ async function generateAlterCode_ColumnAndIndexes(
285
338
  .filter(
286
339
  (index) =>
287
340
  index.columns.every((colName) =>
288
- alterColumnsTo.add.map((col) => col.name).includes(colName)
289
- ) === false
341
+ alterColumnsTo.add.map((col) => col.name).includes(colName),
342
+ ) === false,
290
343
  )
291
344
  .map(genIndexDropDefinition),
292
345
  ...indexNeedsToDrop.map((index) => genIndexDefinition(index, table)),
@@ -294,10 +347,7 @@ async function generateAlterCode_ColumnAndIndexes(
294
347
  "}",
295
348
  ];
296
349
 
297
- const formatted = await prettier.format(lines.join("\n"), {
298
- parser: "typescript",
299
- });
300
-
350
+ const formatted = formatCode(lines.join("\n"), "typescript", `src/migration/${table}.ts`);
301
351
  const title = [
302
352
  "alter",
303
353
  table,
@@ -325,10 +375,7 @@ async function generateAlterCode_ColumnAndIndexes(
325
375
  /**
326
376
  * 각 컬럼 이름 기준으로 add, drop, alter 여부 확인
327
377
  */
328
- function getAlterColumnsTo(
329
- entityColumns: MigrationColumn[],
330
- dbColumns: MigrationColumn[]
331
- ) {
378
+ function getAlterColumnsTo(entityColumns: MigrationColumn[], dbColumns: MigrationColumn[]) {
332
379
  const columnsTo = {
333
380
  add: [] as MigrationColumn[],
334
381
  drop: [] as MigrationColumn[],
@@ -337,8 +384,8 @@ function getAlterColumnsTo(
337
384
 
338
385
  // 컬럼명 기준 비교
339
386
  const extraColumns = {
340
- db: _.differenceBy(dbColumns, entityColumns, (col) => col.name),
341
- entity: _.differenceBy(entityColumns, dbColumns, (col) => col.name),
387
+ db: diff(dbColumns, entityColumns, (col) => col.name),
388
+ entity: diff(entityColumns, dbColumns, (col) => col.name),
342
389
  };
343
390
  if (extraColumns.entity.length > 0) {
344
391
  columnsTo.add = columnsTo.add.concat(extraColumns.entity);
@@ -348,19 +395,9 @@ function getAlterColumnsTo(
348
395
  }
349
396
 
350
397
  // 동일 컬럼명의 세부 필드 비교
351
- const sameDbColumns = _.intersectionBy(
352
- dbColumns,
353
- entityColumns,
354
- (col) => col.name
355
- );
356
- const sameMdColumns = _.intersectionBy(
357
- entityColumns,
358
- dbColumns,
359
- (col) => col.name
360
- );
361
- columnsTo.alter = _.differenceWith(sameDbColumns, sameMdColumns, (a, b) =>
362
- equal(a, b)
363
- );
398
+ const sameDbColumns = intersectionBy(dbColumns, entityColumns, (col) => col.name);
399
+ const sameMdColumns = intersectionBy(entityColumns, dbColumns, (col) => col.name);
400
+ columnsTo.alter = differenceWith(sameDbColumns, sameMdColumns, (a, b) => equal(a, b));
364
401
 
365
402
  return columnsTo;
366
403
  }
@@ -372,9 +409,9 @@ function getAlterColumnLinesTo(
372
409
  columnsTo: ReturnType<typeof getAlterColumnsTo>,
373
410
  entityColumns: MigrationColumn[],
374
411
  table: string,
375
- dbForeigns: MigrationForeign[]
412
+ dbForeigns: MigrationForeign[],
376
413
  ) {
377
- let linesTo = {
414
+ const linesTo = {
378
415
  add: {
379
416
  up: [] as string[],
380
417
  down: [] as string[],
@@ -393,16 +430,14 @@ function getAlterColumnLinesTo(
393
430
  up: ["// add", ...genColumnDefinitions(columnsTo.add)],
394
431
  down: [
395
432
  "// rollback - add",
396
- `table.dropColumns(${columnsTo.add
397
- .map((col) => `'${col.name}'`)
398
- .join(", ")})`,
433
+ `table.dropColumns(${columnsTo.add.map((col) => `'${col.name}'`).join(", ")})`,
399
434
  ],
400
435
  };
401
436
 
402
437
  // drop할 컬럼에 걸린 FK 찾기
403
438
  const dropColumnNames = columnsTo.drop.map((col) => col.name);
404
439
  const fkToDropBeforeColumn = dbForeigns.filter((fk) =>
405
- fk.columns.some((col) => dropColumnNames.includes(col))
440
+ fk.columns.some((col) => dropColumnNames.includes(col)),
406
441
  );
407
442
 
408
443
  const dropFkLines = fkToDropBeforeColumn.map((fk) => {
@@ -418,46 +453,40 @@ function getAlterColumnLinesTo(
418
453
  ? ["// drop foreign keys on columns to be dropped", ...dropFkLines]
419
454
  : []),
420
455
  "// drop columns",
421
- `table.dropColumns(${columnsTo.drop
422
- .map((col) => `'${col.name}'`)
423
- .join(", ")})`,
456
+ `table.dropColumns(${columnsTo.drop.map((col) => `'${col.name}'`).join(", ")})`,
424
457
  ],
425
458
  down: [
426
459
  "// rollback - drop columns",
427
460
  ...genColumnDefinitions(columnsTo.drop),
428
- ...(restoreFkLines.length > 0
429
- ? ["// restore foreign keys", ...restoreFkLines]
430
- : []),
461
+ ...(restoreFkLines.length > 0 ? ["// restore foreign keys", ...restoreFkLines] : []),
431
462
  ],
432
463
  };
433
464
  linesTo.alter = columnsTo.alter.reduce(
434
465
  (r, dbColumn) => {
435
- const entityColumn = entityColumns.find(
436
- (col) => col.name == dbColumn.name
437
- );
466
+ const entityColumn = entityColumns.find((col) => col.name === dbColumn.name);
438
467
  if (entityColumn === undefined) {
439
468
  return r;
440
469
  }
441
470
 
442
471
  // 컬럼 변경사항
443
- const columnDiffUp = _.difference(
472
+ const columnDiffUp = diff(
444
473
  genColumnDefinitions([entityColumn]),
445
- genColumnDefinitions([dbColumn])
474
+ genColumnDefinitions([dbColumn]),
446
475
  );
447
- const columnDiffDown = _.difference(
476
+ const columnDiffDown = diff(
448
477
  genColumnDefinitions([dbColumn]),
449
- genColumnDefinitions([entityColumn])
478
+ genColumnDefinitions([entityColumn]),
450
479
  );
451
480
  if (columnDiffUp.length > 0) {
452
481
  r.up = [
453
482
  ...r.up,
454
483
  "// alter column",
455
- ...columnDiffUp.map((l) => l.replace(";", "") + ".alter();"),
484
+ ...columnDiffUp.map((l) => `${l.replace(";", "")}.alter();`),
456
485
  ];
457
486
  r.down = [
458
487
  ...r.down,
459
488
  "// rollback - alter column",
460
- ...columnDiffDown.map((l) => l.replace(";", "") + ".alter();"),
489
+ ...columnDiffDown.map((l) => `${l.replace(";", "")}.alter();`),
461
490
  ];
462
491
  }
463
492
 
@@ -466,7 +495,7 @@ function getAlterColumnLinesTo(
466
495
  {
467
496
  up: [] as string[],
468
497
  down: [] as string[],
469
- }
498
+ },
470
499
  );
471
500
 
472
501
  return linesTo;
@@ -475,22 +504,15 @@ function getAlterColumnLinesTo(
475
504
  /**
476
505
  * 인덱스의 add, drop 여부 확인
477
506
  */
478
- function getAlterIndexesTo(
479
- entityIndexes: MigrationIndex[],
480
- dbIndexes: MigrationIndex[]
481
- ) {
507
+ function getAlterIndexesTo(entityIndexes: MigrationIndex[], dbIndexes: MigrationIndex[]) {
482
508
  // 인덱스 비교
483
- let indexesTo = {
509
+ const indexesTo = {
484
510
  add: [] as MigrationIndex[],
485
511
  drop: [] as MigrationIndex[],
486
512
  };
487
513
  const extraIndexes = {
488
- db: _.differenceBy(dbIndexes, entityIndexes, (col) =>
489
- [col.type, col.columns.join("-")].join("//")
490
- ),
491
- entity: _.differenceBy(entityIndexes, dbIndexes, (col) =>
492
- [col.type, col.columns.join("-")].join("//")
493
- ),
514
+ db: diff(dbIndexes, entityIndexes, (col) => [col.type, col.columns.join("-")].join("//")),
515
+ entity: diff(entityIndexes, dbIndexes, (col) => [col.type, col.columns.join("-")].join("//")),
494
516
  };
495
517
  if (extraIndexes.entity.length > 0) {
496
518
  indexesTo.add = indexesTo.add.concat(extraIndexes.entity);
@@ -524,7 +546,7 @@ async function generateAlterCode_Foreigns(
524
546
  table: string,
525
547
  entityForeigns: MigrationForeign[],
526
548
  dbForeigns: MigrationForeign[],
527
- droppingColumns: MigrationColumn[] = []
549
+ droppingColumns: MigrationColumn[] = [],
528
550
  ): Promise<GenMigrationCode[]> {
529
551
  // console.log({ entityForeigns, dbForeigns });
530
552
 
@@ -537,9 +559,7 @@ async function generateAlterCode_Foreigns(
537
559
 
538
560
  const fkTo = entityForeigns.reduce(
539
561
  (result, entityF) => {
540
- const matchingDbF = dbForeigns.find(
541
- (dbF) => getKey(entityF) === getKey(dbF)
542
- );
562
+ const matchingDbF = dbForeigns.find((dbF) => getKey(entityF) === getKey(dbF));
543
563
  if (!matchingDbF) {
544
564
  result.add.push(entityF);
545
565
  return result;
@@ -557,20 +577,16 @@ async function generateAlterCode_Foreigns(
557
577
  drop: [] as MigrationForeign[],
558
578
  alterSrc: [] as MigrationForeign[],
559
579
  alterDst: [] as MigrationForeign[],
560
- }
580
+ },
561
581
  );
562
582
 
563
583
  // dbForeigns에는 있지만 entityForeigns에는 없는 경우 (삭제된 FK)
564
584
  // 단, 삭제될 컬럼의 FK는 제외 (generateAlterCode_ColumnAndIndexes에서 처리)
565
585
  dbForeigns.forEach((dbF) => {
566
- const matchingEntityF = entityForeigns.find(
567
- (entityF) => getKey(entityF) === getKey(dbF)
568
- );
586
+ const matchingEntityF = entityForeigns.find((entityF) => getKey(entityF) === getKey(dbF));
569
587
  if (!matchingEntityF) {
570
588
  // 이 FK의 컬럼이 삭제될 컬럼 목록에 있는지 확인
571
- const isColumnDropping = dbF.columns.some((col) =>
572
- droppingColumnNames.includes(col)
573
- );
589
+ const isColumnDropping = dbF.columns.some((col) => droppingColumnNames.includes(col));
574
590
  // 컬럼이 삭제되지 않는 경우에만 FK drop 목록에 추가
575
591
  if (!isColumnDropping) {
576
592
  fkTo.drop.push(dbF);
@@ -586,13 +602,25 @@ async function generateAlterCode_Foreigns(
586
602
  };
587
603
 
588
604
  // drop fk columns인 경우(생성될 코드 없는 경우) 패스
589
- const hasLines = Object.values(linesTo).some(
590
- (l) => l.up.length > 0 || l.down.length > 0
591
- );
605
+ const hasLines = Object.values(linesTo).some((l) => l.up.length > 0 || l.down.length > 0);
592
606
  if (!hasLines) {
593
607
  return [];
594
608
  }
595
609
 
610
+ if (
611
+ linesTo.add.up.length === 0 &&
612
+ linesTo.drop.up.length === 0 &&
613
+ linesTo.alterSrc.up.length === 0 &&
614
+ linesTo.alterDst.up.length === 0
615
+ ) {
616
+ Naite.t("migrator:generateAlterCode_Foreigns:fkChangeCodeGenerationError", {
617
+ table,
618
+ entityForeigns,
619
+ dbForeigns,
620
+ });
621
+ throw new Error("FK 변경 코드 생성 오류");
622
+ }
623
+
596
624
  const lines: string[] = [
597
625
  'import { Knex } from "knex";',
598
626
  "",
@@ -615,10 +643,7 @@ async function generateAlterCode_Foreigns(
615
643
  "}",
616
644
  ];
617
645
 
618
- const formatted = await prettier.format(lines.join("\n"), {
619
- parser: "typescript",
620
- });
621
-
646
+ const formatted = formatCode(lines.join("\n"), "typescript", `src/migration/${table}.ts`);
622
647
  const title = [
623
648
  "alter",
624
649
  table,
@@ -641,14 +666,12 @@ async function generateAlterCode_Foreigns(
641
666
  * @param entitySet
642
667
  * @returns CREATE 마이그레이션 코드
643
668
  */
644
- export async function generateCreateCode(
645
- entitySet: MigrationSet
646
- ): Promise<GenMigrationCode[]> {
669
+ export async function generateCreateCode(entitySet: MigrationSet): Promise<GenMigrationCode[]> {
647
670
  return [
648
671
  await generateCreateCode_ColumnAndIndexes(
649
672
  entitySet.table,
650
673
  entitySet.columns,
651
- entitySet.indexes
674
+ entitySet.indexes,
652
675
  ),
653
676
  ...(await generateCreateCode_Foreign(entitySet.table, entitySet.foreigns)),
654
677
  ];
@@ -662,38 +685,32 @@ export async function generateCreateCode(
662
685
  */
663
686
  export async function generateAlterCode(
664
687
  entitySet: MigrationSet,
665
- dbSet: MigrationSet
688
+ dbSet: MigrationSet,
666
689
  ): Promise<GenMigrationCode[]> {
667
690
  const replaceColumnDefaultTo = (col: MigrationColumn) => {
668
691
  // float인 경우 기본값을 0으로 지정하는 경우 "0.00"으로 변환되는 케이스 대응
669
- if (
670
- col.type === "float" &&
671
- col.defaultTo &&
672
- String(col.defaultTo).includes('"') === false
673
- ) {
674
- col.defaultTo = `"${Number(col.defaultTo).toFixed(col.scale ?? 2)}"`;
675
- }
676
- // string인 경우 기본값이 스트링인 경우 대응
677
- if (col.type === "string" && col.defaultTo === "") {
678
- col.defaultTo = '""';
679
- }
680
- // boolean인 경우 기본값 정규화 (MySQL에서는 TINYINT(1)로 저장되므로 0 또는 1로 정규화)
681
- // TODO: db.ts에 typeCase 설정 확인하여 처리하도록 수정 필요
682
- if (col.type === "boolean" && col.defaultTo !== undefined) {
683
- if (col.defaultTo === "0" || col.defaultTo.toLowerCase() === "false") {
684
- col.defaultTo = "0";
685
- } else if (col.defaultTo === "1" || col.defaultTo.toLowerCase() === "true") {
686
- col.defaultTo = "1";
687
- }
688
- }
692
+ // if (col.type === "float" && col.defaultTo && String(col.defaultTo).includes('"') === false) {
693
+ // col.defaultTo = `"${Number(col.defaultTo).toFixed(col.scale ?? 2)}"`;
694
+ // }
695
+ // // string인 경우 기본값이 빈 스트링인 경우 대응
696
+ // if (col.type === "string" && col.defaultTo === "") {
697
+ // col.defaultTo = '""';
698
+ // }
699
+ // // boolean인 경우 기본값 정규화 (MySQL에서는 TINYINT(1)로 저장되므로 0 또는 1로 정규화)
700
+ // // TODO: db.ts에 typeCase 설정 확인하여 처리하도록 수정 필요
701
+ // if (col.type === "boolean" && col.defaultTo !== undefined) {
702
+ // if (col.defaultTo === "0" || col.defaultTo.toLowerCase() === "false") {
703
+ // col.defaultTo = "0";
704
+ // } else if (col.defaultTo === "1" || col.defaultTo.toLowerCase() === "true") {
705
+ // col.defaultTo = "1";
706
+ // }
707
+ // }
708
+
709
+ // FIXME: 일단 MySQL 상황에서 발생했던 이슈의 workaround 이므로 Pg에서 재확인 후 대응 추가
689
710
  return col;
690
711
  };
691
- const entityColumns = _.sortBy(entitySet.columns, (a) => a.name).map(
692
- replaceColumnDefaultTo
693
- );
694
- const dbColumns = _.sortBy(dbSet.columns, (a) => a.name).map(
695
- replaceColumnDefaultTo
696
- );
712
+ const entityColumns = alphabetical(entitySet.columns, (a) => a.name).map(replaceColumnDefaultTo);
713
+ const dbColumns = alphabetical(dbSet.columns, (a) => a.name).map(replaceColumnDefaultTo);
697
714
 
698
715
  /* 디버깅용 코드, 특정 컬럼에서 불일치 발생할 때 확인
699
716
  const entityColumn = entitySet.columns.find(
@@ -705,11 +722,11 @@ export async function generateAlterCode(
705
722
  console.debug({ entityColumn, dbColumn });
706
723
  */
707
724
 
708
- const entityIndexes = _.sortBy(entitySet.indexes, (a) =>
709
- [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-")
725
+ const entityIndexes = alphabetical(entitySet.indexes, (a) =>
726
+ [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-"),
710
727
  );
711
- const dbIndexes = _.sortBy(dbSet.indexes, (a) =>
712
- [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-")
728
+ const dbIndexes = alphabetical(dbSet.indexes, (a) =>
729
+ [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-"),
713
730
  );
714
731
 
715
732
  const replaceNoActionOnMySQL = (f: MigrationForeign) => {
@@ -722,27 +739,23 @@ export async function generateAlterCode(
722
739
  };
723
740
  };
724
741
 
725
- const entityForeigns = _.sortBy(entitySet.foreigns, (a) =>
726
- [a.to, ...a.columns].join("-")
727
- ).map((f) => replaceNoActionOnMySQL(f));
728
- const dbForeigns = _.sortBy(dbSet.foreigns, (a) =>
729
- [a.to, ...a.columns].join("-")
742
+ const entityForeigns = alphabetical(entitySet.foreigns, (a) =>
743
+ [a.to, ...a.columns].join("-"),
730
744
  ).map((f) => replaceNoActionOnMySQL(f));
745
+ const dbForeigns = alphabetical(dbSet.foreigns, (a) => [a.to, ...a.columns].join("-")).map((f) =>
746
+ replaceNoActionOnMySQL(f),
747
+ );
731
748
 
732
749
  // 삭제될 컬럼 목록 계산
733
- const droppingColumns = _.differenceBy(
734
- dbColumns,
735
- entityColumns,
736
- (col) => col.name
737
- );
750
+ const droppingColumns = diff(dbColumns, entityColumns, (col) => col.name);
738
751
 
739
752
  const alterCodes: (GenMigrationCode | GenMigrationCode[] | null)[] = [];
740
753
 
741
754
  // 1. columnsAndIndexes 처리
742
755
  const isEqualColumns = equal(entityColumns, dbColumns);
743
756
  const isEqualIndexes = equal(
744
- entityIndexes.map((index) => _.omit(index, ["parser"])),
745
- dbIndexes
757
+ entityIndexes.map((index) => omit(index, ["parser"])),
758
+ dbIndexes,
746
759
  );
747
760
  if (!isEqualColumns || !isEqualIndexes) {
748
761
  alterCodes.push(
@@ -752,8 +765,8 @@ export async function generateAlterCode(
752
765
  entityIndexes,
753
766
  dbColumns,
754
767
  dbIndexes,
755
- dbSet.foreigns
756
- )
768
+ dbSet.foreigns,
769
+ ),
757
770
  );
758
771
  }
759
772
 
@@ -764,8 +777,8 @@ export async function generateAlterCode(
764
777
  entitySet.table,
765
778
  entityForeigns,
766
779
  dbForeigns,
767
- droppingColumns
768
- )
780
+ droppingColumns,
781
+ ),
769
782
  );
770
783
  }
771
784