sonamu 0.4.13 → 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 (371) 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/bin/cli.mjs +2 -2
  47. package/dist/{chunk-DMJSNO2L.js → chunk-2WAC2GER.js} +44 -44
  48. package/dist/{chunk-DMJSNO2L.js.map → chunk-2WAC2GER.js.map} +1 -1
  49. package/dist/{chunk-NI37CY4T.mjs → chunk-C3IPIF6O.mjs} +2 -2
  50. package/dist/{chunk-DYFCACHD.js → chunk-EXHKSVTE.js} +7 -7
  51. package/dist/{chunk-QJFHDCBN.mjs → chunk-FCERKIIF.mjs} +2 -2
  52. package/dist/chunk-FCERKIIF.mjs.map +1 -0
  53. package/dist/{chunk-DDJ7T4MA.mjs → chunk-HGIBJYOU.mjs} +2 -2
  54. package/dist/{chunk-NIFOTHBW.mjs → chunk-JKSOJRQA.mjs} +2 -2
  55. package/dist/{chunk-CXAVBVKC.js → chunk-OTKKFP3Y.js} +100 -100
  56. package/dist/{chunk-J6S43O7G.js → chunk-UZ2IY5VE.js} +4 -4
  57. package/dist/database/_batch_update.d.ts +15 -0
  58. package/dist/database/_batch_update.d.ts.map +1 -0
  59. package/dist/database/_batch_update.js +2 -0
  60. package/dist/database/_batch_update.js.map +1 -0
  61. package/dist/database/base-model.d.ts +41 -0
  62. package/dist/database/base-model.d.ts.map +1 -0
  63. package/dist/database/base-model.js +2 -0
  64. package/dist/database/base-model.js.map +1 -0
  65. package/dist/database/code-generator.d.ts +13 -0
  66. package/dist/database/code-generator.d.ts.map +1 -0
  67. package/dist/database/code-generator.js +2 -0
  68. package/dist/database/code-generator.js.map +1 -0
  69. package/dist/database/db.d.ts +40 -0
  70. package/dist/database/db.d.ts.map +1 -0
  71. package/dist/database/db.js +2 -0
  72. package/dist/database/db.js.map +1 -0
  73. package/dist/database/drivers/knex/base-model.js +8 -8
  74. package/dist/database/drivers/knex/base-model.mjs +3 -3
  75. package/dist/database/drivers/kysely/base-model.js +9 -9
  76. package/dist/database/drivers/kysely/base-model.mjs +3 -3
  77. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts +2 -0
  78. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts.map +1 -0
  79. package/dist/database/knex-plugins/knex-on-duplicate-update.js +2 -0
  80. package/dist/database/knex-plugins/knex-on-duplicate-update.js.map +1 -0
  81. package/dist/database/puri-wrapper.d.ts +34 -0
  82. package/dist/database/puri-wrapper.d.ts.map +1 -0
  83. package/dist/database/puri-wrapper.js +2 -0
  84. package/dist/database/puri-wrapper.js.map +1 -0
  85. package/dist/database/puri.d.ts +83 -0
  86. package/dist/database/puri.d.ts.map +1 -0
  87. package/dist/database/puri.js +2 -0
  88. package/dist/database/puri.js.map +1 -0
  89. package/dist/database/puri.types.d.ts +60 -0
  90. package/dist/database/puri.types.d.ts.map +1 -0
  91. package/dist/database/puri.types.js +2 -0
  92. package/dist/database/puri.types.js.map +1 -0
  93. package/dist/database/transaction-context.d.ts +9 -0
  94. package/dist/database/transaction-context.d.ts.map +1 -0
  95. package/dist/database/transaction-context.js +2 -0
  96. package/dist/database/transaction-context.js.map +1 -0
  97. package/dist/database/types.d.ts +39 -0
  98. package/dist/database/types.d.ts.map +1 -0
  99. package/dist/database/types.js +2 -0
  100. package/dist/database/types.js.map +1 -0
  101. package/dist/database/upsert-builder.d.ts +34 -0
  102. package/dist/database/upsert-builder.d.ts.map +1 -0
  103. package/dist/database/upsert-builder.js +2 -0
  104. package/dist/database/upsert-builder.js.map +1 -0
  105. package/dist/entity/entity-manager.d.ts +32 -0
  106. package/dist/entity/entity-manager.d.ts.map +1 -0
  107. package/dist/entity/entity-manager.js +2 -0
  108. package/dist/entity/entity-manager.js.map +1 -0
  109. package/dist/entity/entity-utils.d.ts +61 -0
  110. package/dist/entity/entity-utils.d.ts.map +1 -0
  111. package/dist/entity/entity-utils.js +2 -0
  112. package/dist/entity/entity-utils.js.map +1 -0
  113. package/dist/entity/entity.d.ts +62 -0
  114. package/dist/entity/entity.d.ts.map +1 -0
  115. package/dist/entity/entity.js +2 -0
  116. package/dist/entity/entity.js.map +1 -0
  117. package/dist/entity/migrator.d.ts +135 -0
  118. package/dist/entity/migrator.d.ts.map +1 -0
  119. package/dist/entity/migrator.js +2 -0
  120. package/dist/entity/migrator.js.map +1 -0
  121. package/dist/exceptions/error-handler.d.ts +3 -0
  122. package/dist/exceptions/error-handler.d.ts.map +1 -0
  123. package/dist/exceptions/error-handler.js +2 -0
  124. package/dist/exceptions/error-handler.js.map +1 -0
  125. package/dist/exceptions/so-exceptions.d.ts +48 -0
  126. package/dist/exceptions/so-exceptions.d.ts.map +1 -0
  127. package/dist/exceptions/so-exceptions.js +2 -0
  128. package/dist/exceptions/so-exceptions.js.map +1 -0
  129. package/dist/file-storage/driver.d.ts +45 -0
  130. package/dist/file-storage/driver.d.ts.map +1 -0
  131. package/dist/file-storage/driver.js +2 -0
  132. package/dist/file-storage/driver.js.map +1 -0
  133. package/dist/file-storage/file-storage.d.ts +50 -0
  134. package/dist/file-storage/file-storage.d.ts.map +1 -0
  135. package/dist/file-storage/file-storage.js +2 -0
  136. package/dist/file-storage/file-storage.js.map +1 -0
  137. package/dist/index.d.ts +22 -813
  138. package/dist/index.d.ts.map +1 -0
  139. package/dist/index.js +1 -433
  140. package/dist/index.js.map +1 -1
  141. package/dist/index.mjs +3 -3
  142. package/dist/migration/code-generation.d.ts +15 -0
  143. package/dist/migration/code-generation.d.ts.map +1 -0
  144. package/dist/migration/code-generation.js +2 -0
  145. package/dist/migration/code-generation.js.map +1 -0
  146. package/dist/migration/migration-set.d.ts +17 -0
  147. package/dist/migration/migration-set.d.ts.map +1 -0
  148. package/dist/migration/migration-set.js +2 -0
  149. package/dist/migration/migration-set.js.map +1 -0
  150. package/dist/migration/migrator.d.ts +130 -0
  151. package/dist/migration/migrator.d.ts.map +1 -0
  152. package/dist/migration/migrator.js +2 -0
  153. package/dist/migration/migrator.js.map +1 -0
  154. package/dist/migration/types.d.ts +52 -0
  155. package/dist/migration/types.d.ts.map +1 -0
  156. package/dist/migration/types.js +2 -0
  157. package/dist/migration/types.js.map +1 -0
  158. package/dist/smd/smd-manager.d.ts +28 -0
  159. package/dist/smd/smd-manager.d.ts.map +1 -0
  160. package/dist/smd/smd-manager.js +2 -0
  161. package/dist/smd/smd-manager.js.map +1 -0
  162. package/dist/smd/smd.d.ts +40 -0
  163. package/dist/smd/smd.d.ts.map +1 -0
  164. package/dist/smd/smd.js +2 -0
  165. package/dist/smd/smd.js.map +1 -0
  166. package/dist/syncer/index.d.ts +2 -0
  167. package/dist/syncer/index.d.ts.map +1 -0
  168. package/dist/syncer/index.js +2 -0
  169. package/dist/syncer/index.js.map +1 -0
  170. package/dist/syncer/syncer.d.ts +127 -0
  171. package/dist/syncer/syncer.d.ts.map +1 -0
  172. package/dist/syncer/syncer.js +2 -0
  173. package/dist/syncer/syncer.js.map +1 -0
  174. package/dist/templates/base-template.d.ts +13 -0
  175. package/dist/templates/base-template.d.ts.map +1 -0
  176. package/dist/templates/base-template.js +2 -0
  177. package/dist/templates/base-template.js.map +1 -0
  178. package/dist/templates/entity.template.d.ts +17 -0
  179. package/dist/templates/entity.template.d.ts.map +1 -0
  180. package/dist/templates/entity.template.js +2 -0
  181. package/dist/templates/entity.template.js.map +1 -0
  182. package/dist/templates/generated.template.d.ts +27 -0
  183. package/dist/templates/generated.template.d.ts.map +1 -0
  184. package/dist/templates/generated.template.js +2 -0
  185. package/dist/templates/generated.template.js.map +1 -0
  186. package/dist/templates/generated_http.template.d.ts +24 -0
  187. package/dist/templates/generated_http.template.d.ts.map +1 -0
  188. package/dist/templates/generated_http.template.js +2 -0
  189. package/dist/templates/generated_http.template.js.map +1 -0
  190. package/dist/templates/generated_sso.template.d.ts +20 -0
  191. package/dist/templates/generated_sso.template.d.ts.map +1 -0
  192. package/dist/templates/generated_sso.template.js +2 -0
  193. package/dist/templates/generated_sso.template.js.map +1 -0
  194. package/dist/templates/index.d.ts +2 -0
  195. package/dist/templates/index.d.ts.map +1 -0
  196. package/dist/templates/index.js +2 -0
  197. package/dist/templates/index.js.map +1 -0
  198. package/dist/templates/init_types.template.d.ts +17 -0
  199. package/dist/templates/init_types.template.d.ts.map +1 -0
  200. package/dist/templates/init_types.template.js +2 -0
  201. package/dist/templates/init_types.template.js.map +1 -0
  202. package/dist/templates/model.template.d.ts +17 -0
  203. package/dist/templates/model.template.d.ts.map +1 -0
  204. package/dist/templates/model.template.js +2 -0
  205. package/dist/templates/model.template.js.map +1 -0
  206. package/dist/templates/model_test.template.d.ts +17 -0
  207. package/dist/templates/model_test.template.d.ts.map +1 -0
  208. package/dist/templates/model_test.template.js +2 -0
  209. package/dist/templates/model_test.template.js.map +1 -0
  210. package/dist/templates/service.template.d.ts +29 -0
  211. package/dist/templates/service.template.d.ts.map +1 -0
  212. package/dist/templates/service.template.js +2 -0
  213. package/dist/templates/service.template.js.map +1 -0
  214. package/dist/templates/view_enums_buttonset.template.d.ts +17 -0
  215. package/dist/templates/view_enums_buttonset.template.d.ts.map +1 -0
  216. package/dist/templates/view_enums_buttonset.template.js +2 -0
  217. package/dist/templates/view_enums_buttonset.template.js.map +1 -0
  218. package/dist/templates/view_enums_dropdown.template.d.ts +18 -0
  219. package/dist/templates/view_enums_dropdown.template.d.ts.map +1 -0
  220. package/dist/templates/view_enums_dropdown.template.js +2 -0
  221. package/dist/templates/view_enums_dropdown.template.js.map +1 -0
  222. package/dist/templates/view_enums_select.template.d.ts +17 -0
  223. package/dist/templates/view_enums_select.template.d.ts.map +1 -0
  224. package/dist/templates/view_enums_select.template.js +2 -0
  225. package/dist/templates/view_enums_select.template.js.map +1 -0
  226. package/dist/templates/view_form.template.d.ts +26 -0
  227. package/dist/templates/view_form.template.d.ts.map +1 -0
  228. package/dist/templates/view_form.template.js +2 -0
  229. package/dist/templates/view_form.template.js.map +1 -0
  230. package/dist/templates/view_id_all_select.template.d.ts +17 -0
  231. package/dist/templates/view_id_all_select.template.d.ts.map +1 -0
  232. package/dist/templates/view_id_all_select.template.js +2 -0
  233. package/dist/templates/view_id_all_select.template.js.map +1 -0
  234. package/dist/templates/view_id_async_select.template.d.ts +17 -0
  235. package/dist/templates/view_id_async_select.template.d.ts.map +1 -0
  236. package/dist/templates/view_id_async_select.template.js +2 -0
  237. package/dist/templates/view_id_async_select.template.js.map +1 -0
  238. package/dist/templates/view_list.template.d.ts +38 -0
  239. package/dist/templates/view_list.template.d.ts.map +1 -0
  240. package/dist/templates/view_list.template.js +2 -0
  241. package/dist/templates/view_list.template.js.map +1 -0
  242. package/dist/templates/view_list_columns.template.d.ts +17 -0
  243. package/dist/templates/view_list_columns.template.d.ts.map +1 -0
  244. package/dist/templates/view_list_columns.template.js +2 -0
  245. package/dist/templates/view_list_columns.template.js.map +1 -0
  246. package/dist/templates/view_search_input.template.d.ts +17 -0
  247. package/dist/templates/view_search_input.template.d.ts.map +1 -0
  248. package/dist/templates/view_search_input.template.js +2 -0
  249. package/dist/templates/view_search_input.template.js.map +1 -0
  250. package/dist/testing/_relation-graph.d.ts +7 -0
  251. package/dist/testing/_relation-graph.d.ts.map +1 -0
  252. package/dist/testing/_relation-graph.js +2 -0
  253. package/dist/testing/_relation-graph.js.map +1 -0
  254. package/dist/testing/fixture-manager.d.ts +35 -0
  255. package/dist/testing/fixture-manager.d.ts.map +1 -0
  256. package/dist/testing/fixture-manager.js +2 -0
  257. package/dist/testing/fixture-manager.js.map +1 -0
  258. package/dist/types/types.d.ts +609 -0
  259. package/dist/types/types.d.ts.map +1 -0
  260. package/dist/types/types.js +2 -0
  261. package/dist/types/types.js.map +1 -0
  262. package/dist/typings/knex.d.js +2 -0
  263. package/dist/typings/knex.d.js.map +1 -0
  264. package/dist/utils/async-utils.d.ts +25 -0
  265. package/dist/utils/async-utils.d.ts.map +1 -0
  266. package/dist/utils/async-utils.js +2 -0
  267. package/dist/utils/async-utils.js.map +1 -0
  268. package/dist/utils/controller.d.ts +9 -0
  269. package/dist/utils/controller.d.ts.map +1 -0
  270. package/dist/utils/controller.js +2 -0
  271. package/dist/utils/controller.js.map +1 -0
  272. package/dist/utils/fs-utils.d.ts +9 -0
  273. package/dist/utils/fs-utils.d.ts.map +1 -0
  274. package/dist/utils/fs-utils.js +2 -0
  275. package/dist/utils/fs-utils.js.map +1 -0
  276. package/dist/utils/lodash-able.d.ts +2 -0
  277. package/dist/utils/lodash-able.d.ts.map +1 -0
  278. package/dist/utils/lodash-able.js +2 -0
  279. package/dist/utils/lodash-able.js.map +1 -0
  280. package/dist/utils/model.d.ts +17 -0
  281. package/dist/utils/model.d.ts.map +1 -0
  282. package/dist/utils/model.js +2 -0
  283. package/dist/utils/model.js.map +1 -0
  284. package/dist/utils/sql-parser.d.ts +4 -0
  285. package/dist/utils/sql-parser.d.ts.map +1 -0
  286. package/dist/utils/sql-parser.js +2 -0
  287. package/dist/utils/sql-parser.js.map +1 -0
  288. package/dist/utils/utils.d.ts +9 -0
  289. package/dist/utils/utils.d.ts.map +1 -0
  290. package/dist/utils/utils.js +2 -0
  291. package/dist/utils/utils.js.map +1 -0
  292. package/dist/utils/zod-error.d.ts +8 -0
  293. package/dist/utils/zod-error.d.ts.map +1 -0
  294. package/dist/utils/zod-error.js +2 -0
  295. package/dist/utils/zod-error.js.map +1 -0
  296. package/nodemon.json +6 -0
  297. package/package.json +29 -44
  298. package/src/api/base-frame.ts +3 -4
  299. package/src/api/caster.ts +22 -23
  300. package/src/api/code-converters.ts +170 -134
  301. package/src/api/context.ts +13 -6
  302. package/src/api/decorators.ts +146 -20
  303. package/src/api/index.ts +2 -0
  304. package/src/api/sonamu.ts +374 -165
  305. package/src/bin/build-config.ts +5 -0
  306. package/src/bin/cli-wrapper.ts +29 -40
  307. package/src/bin/cli.ts +132 -190
  308. package/src/database/_batch_update.ts +10 -15
  309. package/src/database/base-model.ts +300 -216
  310. package/src/database/db.ts +191 -21
  311. package/src/database/{drivers/knex/plugins → knex-plugins}/knex-on-duplicate-update.ts +1 -1
  312. package/src/database/puri-wrapper.ts +129 -0
  313. package/src/database/puri.ts +808 -0
  314. package/src/database/puri.types.ts +222 -0
  315. package/src/database/transaction-context.ts +18 -0
  316. package/src/database/upsert-builder.ts +32 -35
  317. package/src/entity/entity-manager.ts +7 -15
  318. package/src/entity/entity.ts +9 -31
  319. package/src/entity/migrator-/354/235/264/354/202/254/352/260/224/354/226/264/354/232/224.md +1 -0
  320. package/src/file-storage/driver.ts +121 -0
  321. package/src/file-storage/file-storage.ts +100 -0
  322. package/src/index.ts +14 -11
  323. package/src/migration/code-generation.ts +777 -0
  324. package/src/migration/migration-set.ts +453 -0
  325. package/src/migration/migrator.ts +823 -0
  326. package/src/migration/types.ts +53 -0
  327. package/src/shared/web.shared.ts.txt +33 -2
  328. package/src/syncer/syncer.ts +294 -127
  329. package/src/templates/generated.template.ts +13 -1
  330. package/src/templates/generated_http.template.ts +15 -12
  331. package/src/templates/generated_sso.template.ts +50 -2
  332. package/src/templates/model.template.ts +138 -2
  333. package/src/templates/service.template.ts +0 -1
  334. package/src/templates/view_form.template.ts +11 -7
  335. package/src/templates/view_list.template.ts +12 -4
  336. package/src/testing/fixture-manager.ts +229 -174
  337. package/src/types/types.ts +102 -14
  338. package/src/utils/async-utils.ts +64 -0
  339. package/src/utils/fs-utils.ts +17 -0
  340. package/src/utils/model.ts +0 -2
  341. package/src/utils/utils.ts +14 -58
  342. package/src/utils/zod-error.ts +12 -176
  343. package/tsconfig.json +2 -0
  344. package/tsup.config.js +4 -2
  345. package/.pnp.cjs +0 -14363
  346. package/.pnp.loader.mjs +0 -2047
  347. package/.vscode/extensions.json +0 -6
  348. package/.vscode/settings.json +0 -9
  349. package/.yarnrc.yml +0 -5
  350. package/dist/chunk-QJFHDCBN.mjs.map +0 -1
  351. package/src/database/base-model.abstract.ts +0 -97
  352. package/src/database/db.abstract.ts +0 -75
  353. package/src/database/drivers/knex/base-model.ts +0 -55
  354. package/src/database/drivers/knex/client.ts +0 -209
  355. package/src/database/drivers/knex/db.ts +0 -232
  356. package/src/database/drivers/knex/generator.ts +0 -659
  357. package/src/database/drivers/kysely/base-model.ts +0 -89
  358. package/src/database/drivers/kysely/client.ts +0 -309
  359. package/src/database/drivers/kysely/db.ts +0 -238
  360. package/src/database/drivers/kysely/generator.ts +0 -714
  361. package/src/database/types.ts +0 -118
  362. package/src/entity/migrator.ts +0 -1400
  363. package/src/smd/smd-manager.ts +0 -139
  364. package/src/smd/smd.ts +0 -571
  365. package/src/templates/kysely_types.template.ts +0 -205
  366. /package/dist/{chunk-NI37CY4T.mjs.map → chunk-C3IPIF6O.mjs.map} +0 -0
  367. /package/dist/{chunk-DYFCACHD.js.map → chunk-EXHKSVTE.js.map} +0 -0
  368. /package/dist/{chunk-DDJ7T4MA.mjs.map → chunk-HGIBJYOU.mjs.map} +0 -0
  369. /package/dist/{chunk-NIFOTHBW.mjs.map → chunk-JKSOJRQA.mjs.map} +0 -0
  370. /package/dist/{chunk-CXAVBVKC.js.map → chunk-OTKKFP3Y.js.map} +0 -0
  371. /package/dist/{chunk-J6S43O7G.js.map → chunk-UZ2IY5VE.js.map} +0 -0
@@ -1,97 +0,0 @@
1
- import { DateTime } from "luxon";
2
- import _ from "lodash";
3
- import { Knex } from "knex";
4
- import { RawBuilder } from "kysely";
5
- import { BaseListParams } from "../utils/model";
6
- import { DBPreset, DatabaseDriver, DriverSpec } from "./types";
7
- import { SubsetQuery } from "../types/types";
8
-
9
- export abstract class BaseModelAbstract<D extends DatabaseDriver> {
10
- public modelName: string = "Unknown";
11
-
12
- abstract runSubsetQuery<T extends BaseListParams, U extends string>(options: {
13
- params: T;
14
- baseTable?: string;
15
- subset: U;
16
- subsetQuery: SubsetQuery;
17
- build: (buildParams: {
18
- qb: DriverSpec[D]["queryBuilder"];
19
- db: DriverSpec[D]["adapter"];
20
- select: SubsetQuery["select"];
21
- joins: SubsetQuery["joins"];
22
- virtual: string[];
23
- }) => any;
24
- debug?: boolean | "list" | "count";
25
- db?: DriverSpec[D]["adapter"];
26
- optimizeCountQuery?: boolean;
27
- }): Promise<{
28
- rows: any[];
29
- total?: number;
30
- subsetQuery: SubsetQuery;
31
- qb: DriverSpec[D]["queryBuilder"];
32
- }>;
33
- abstract getDB(which: DBPreset): DriverSpec[D]["adapter"];
34
- abstract destroy(): Promise<void>;
35
- abstract useLoaders(
36
- db: DriverSpec[D]["adapter"],
37
- rows: any[],
38
- loaders: SubsetQuery["loaders"]
39
- ): Promise<any[]>;
40
- abstract getJoinClause(
41
- db: DriverSpec[D]["adapter"],
42
- join: SubsetQuery["joins"][number]
43
- ): string | Knex.Raw<any> | RawBuilder<unknown>;
44
-
45
- myNow(timestamp?: number): string {
46
- const dt: DateTime =
47
- timestamp === undefined
48
- ? DateTime.local()
49
- : DateTime.fromSeconds(timestamp);
50
- return dt.toFormat("yyyy-MM-dd HH:mm:ss");
51
- }
52
-
53
- hydrate<T>(rows: T[]): T[] {
54
- return rows.map((row: any) => {
55
- // nullable relation인 경우 관련된 필드가 전부 null로 생성되는 것 방지하는 코드
56
- const nestedKeys = Object.keys(row).filter((key) => key.includes("__"));
57
- const groups = _.groupBy(nestedKeys, (key) => key.split("__")[0]);
58
- const nullKeys = Object.keys(groups).filter(
59
- (key) =>
60
- groups[key].length > 1 &&
61
- groups[key].every((field) => row[field] === null)
62
- );
63
-
64
- const hydrated = Object.keys(row).reduce((r, field) => {
65
- if (!field.includes("__")) {
66
- if (Array.isArray(row[field]) && _.isObject(row[field][0])) {
67
- r[field] = this.hydrate(row[field]);
68
- return r;
69
- } else {
70
- r[field] = row[field];
71
- return r;
72
- }
73
- }
74
-
75
- const parts = field.split("__");
76
- const objPath =
77
- parts[0] +
78
- parts
79
- .slice(1)
80
- .map((part) => `[${part}]`)
81
- .join("");
82
- _.set(
83
- r,
84
- objPath,
85
- row[field] && Array.isArray(row[field]) && _.isObject(row[field][0])
86
- ? this.hydrate(row[field])
87
- : row[field]
88
- );
89
-
90
- return r;
91
- }, {} as any);
92
- nullKeys.map((nullKey) => (hydrated[nullKey] = null));
93
-
94
- return hydrated;
95
- });
96
- }
97
- }
@@ -1,75 +0,0 @@
1
- import { Knex } from "knex";
2
- import { Kysely } from "kysely";
3
- import path from "path";
4
- import { KnexClient } from "./drivers/knex/client";
5
- import { KyselyClient } from "./drivers/kysely/client";
6
- import {
7
- SonamuDBConfig,
8
- DBPreset,
9
- Database,
10
- SonamuDBBaseConfig,
11
- KnexConfig,
12
- } from "./types";
13
-
14
- // db.ts에 포함시킬 경우 순환참조 발생
15
-
16
- export abstract class DBClass {
17
- public _fullConfig?: SonamuDBConfig;
18
- set fullConfig(config: SonamuDBConfig) {
19
- this._fullConfig = config;
20
- }
21
- get fullConfig() {
22
- if (!this._fullConfig) {
23
- throw new Error("FixtureManager has not been initialized");
24
- }
25
- return this._fullConfig;
26
- }
27
-
28
- abstract tdb: KnexClient | KyselyClient;
29
- abstract fdb: KnexClient | KyselyClient;
30
-
31
- abstract testInit(): Promise<void>;
32
- abstract getDB(which: DBPreset): Knex | Kysely<Database>;
33
- abstract destroy(): Promise<void>;
34
- abstract raw(db: Knex | Kysely<Database>, query: string): any;
35
-
36
- async getBaseConfig(rootPath: string): Promise<SonamuDBBaseConfig> {
37
- const baseConfigPath = path.join(rootPath, "/dist/configs/db.js");
38
- const module = await import(baseConfigPath);
39
- const config = module.default?.default ?? module.default ?? module;
40
- return config;
41
- }
42
-
43
- getCurrentConfig(which?: DBPreset) {
44
- switch (process.env.NODE_ENV ?? "development") {
45
- case "development":
46
- case "staging":
47
- return which === "w"
48
- ? this.fullConfig["development_master"]
49
- : this.fullConfig["development_slave"] ??
50
- this.fullConfig["development_master"];
51
- break;
52
- case "production":
53
- return which === "w"
54
- ? this.fullConfig["production_master"]
55
- : this.fullConfig["production_slave"] ??
56
- this.fullConfig["production_master"];
57
- break;
58
- case "test":
59
- return this.fullConfig["test"];
60
- break;
61
- default:
62
- throw new Error(
63
- `현재 ENV ${process.env.NODE_ENV}에는 설정 가능한 DB설정이 없습니다.`
64
- );
65
- }
66
- }
67
-
68
- toClient(db: Knex | Kysely<Database>): KnexClient | KyselyClient {
69
- if (db instanceof Kysely) {
70
- return new KyselyClient(this.getCurrentConfig(), db);
71
- } else {
72
- return new KnexClient(this.getCurrentConfig() as KnexConfig, db);
73
- }
74
- }
75
- }
@@ -1,55 +0,0 @@
1
- // base-model.knex.ts
2
- import { Knex } from "knex";
3
- import { SubsetQuery, isCustomJoinClause } from "../../../types/types";
4
- import { BaseModelClassAbstract } from "../../base-model";
5
- import { DB } from "../../db";
6
- import { KnexClient } from "./client";
7
- import { DBPreset } from "../../types";
8
- import { UpsertBuilder } from "../../upsert-builder";
9
-
10
- export class BaseModelClass extends BaseModelClassAbstract<"knex"> {
11
- getDB(which: DBPreset): Knex {
12
- return DB.getDB(which) as Knex;
13
- }
14
-
15
- async destroy(): Promise<void> {
16
- return DB.destroy();
17
- }
18
-
19
- getUpsertBuilder() {
20
- return new UpsertBuilder<"knex">();
21
- }
22
-
23
- protected applyJoins(
24
- clonedQb: KnexClient,
25
- joins: SubsetQuery["joins"]
26
- ): KnexClient {
27
- for (const join of joins) {
28
- if (isCustomJoinClause(join)) {
29
- if (join.join === "inner") {
30
- clonedQb.qb = clonedQb.qb.innerJoin(
31
- `${join.table} as ${join.as}`,
32
- join.custom as any
33
- );
34
- } else {
35
- clonedQb.qb = clonedQb.qb.leftJoin(
36
- `${join.table} as ${join.as}`,
37
- join.custom as any
38
- );
39
- }
40
- } else {
41
- if (join.join === "inner") {
42
- clonedQb.innerJoin(`${join.table} as ${join.as}`, join.from, join.to);
43
- } else if (join.join === "outer") {
44
- clonedQb.leftJoin(`${join.table} as ${join.as}`, join.from, join.to);
45
- }
46
- }
47
- }
48
- return clonedQb;
49
- }
50
-
51
- protected async executeCountQuery(qb: Knex.QueryBuilder): Promise<number> {
52
- const result = await qb.clear("select").count("* as total").first();
53
- return Number(result?.total) ?? 0;
54
- }
55
- }
@@ -1,209 +0,0 @@
1
- import knex, { Knex } from "knex";
2
- import { DatabaseClient, KnexConfig, WhereClause } from "../../types";
3
- import { asArray } from "../../../utils/model";
4
- import _ from "lodash";
5
- import { KnexGenerator } from "./generator";
6
-
7
- // 확장된 Transaction 타입 정의
8
- export type ExtendedKnexTrx = Knex.Transaction & DatabaseClient<"knex">;
9
-
10
- export class KnexClient implements DatabaseClient<"knex"> {
11
- private knex: Knex;
12
- generator: KnexGenerator = new KnexGenerator();
13
-
14
- get connectionInfo() {
15
- return {
16
- host: this.knex.client.config.connection?.host ?? "localhost",
17
- port: this.knex.client.config.connection?.port ?? 3306,
18
- database: this.knex.client.config.connection?.database ?? "",
19
- user: this.knex.client.config.connection?.user ?? "",
20
- password: this.knex.client.config.connection?.password ?? "",
21
- };
22
- }
23
-
24
- private _qb?: Knex.QueryBuilder;
25
- set qb(qb: Knex.QueryBuilder) {
26
- this._qb = qb;
27
- }
28
- get qb() {
29
- if (!this._qb) {
30
- throw new Error("QueryBuilder is not initialized");
31
- }
32
- return this._qb;
33
- }
34
-
35
- get sql() {
36
- return this.qb.toQuery();
37
- }
38
-
39
- constructor(
40
- private config: KnexConfig,
41
- _knex?: Knex
42
- ) {
43
- this.knex = _knex ?? knex(this.config);
44
- }
45
-
46
- from(table: string): KnexClient {
47
- this.qb = this.knex.from(table);
48
- return this;
49
- }
50
-
51
- innerJoin(table: string, k1: string, k2: string) {
52
- this.qb = this.qb.innerJoin(table, k1, k2);
53
- return this;
54
- }
55
-
56
- leftJoin(table: string, k1: string, k2: string) {
57
- this.qb = this.qb.leftJoin(table, k1, k2);
58
- return this;
59
- }
60
-
61
- clearSelect() {
62
- this.qb = this.qb.clearSelect();
63
- return this;
64
- }
65
-
66
- select(columns: string | string[]) {
67
- this.qb = this.qb.select(asArray(columns));
68
- return this;
69
- }
70
-
71
- selectAll() {
72
- this.qb = this.qb.select("*");
73
- return this;
74
- }
75
-
76
- where(ops: WhereClause | WhereClause[]) {
77
- if (typeof ops[0] === "string") {
78
- ops = [ops as WhereClause];
79
- }
80
- for (const [lhs, op, rhs] of asArray(ops)) {
81
- this.qb = this.qb.where(lhs, op, rhs);
82
- }
83
- return this;
84
- }
85
-
86
- orWhere(ops: WhereClause | WhereClause[]) {
87
- this.qb = this.qb.orWhere((qb) => {
88
- for (const [lhs, op, rhs] of asArray(ops)) {
89
- qb.andWhere(lhs, op, rhs);
90
- }
91
- });
92
- return this;
93
- }
94
-
95
- async insert(table: string, data: any[]) {
96
- await this.knex(table).insert(data);
97
- }
98
-
99
- async upsert(table: string, data: any[]) {
100
- const q = this.knex(table).insert(data);
101
- const updateFields = Array.isArray(data) ? Object.keys(data[0]) : data;
102
- await q.onDuplicateUpdate.apply(q, updateFields);
103
- }
104
-
105
- limit(limit: number) {
106
- this.qb = this.qb.limit(limit);
107
- return this;
108
- }
109
-
110
- offset(offset: number) {
111
- this.qb = this.qb.offset(offset);
112
- return this;
113
- }
114
-
115
- count(column: string, alias?: string) {
116
- this.qb = this.qb.count(alias ? `${column} as ${alias}` : column);
117
- return this;
118
- }
119
-
120
- distinct(column: string) {
121
- this.qb = this.qb.distinct(column);
122
- return this;
123
- }
124
-
125
- first() {
126
- this.qb = this.qb.limit(1);
127
- return this;
128
- }
129
-
130
- async execute(trx?: ExtendedKnexTrx): Promise<any[]> {
131
- if (trx) {
132
- return this.qb.transacting(trx);
133
- }
134
- return this.qb;
135
- }
136
-
137
- async pluck(column: string): Promise<any[]> {
138
- return this.qb.pluck(column);
139
- }
140
-
141
- createRawQuery(query: string, bindings?: any[]) {
142
- if (bindings?.length) {
143
- return this.knex.raw(query, bindings).toQuery();
144
- }
145
- return this.knex.raw(query).toQuery();
146
- }
147
-
148
- async raw<R>(query: string, bindings?: any[]): Promise<R[]> {
149
- if (bindings?.length) {
150
- return (await this.knex.raw(query, bindings))[0];
151
- }
152
- return (await this.knex.raw(query))[0];
153
- }
154
-
155
- async truncate(table: string) {
156
- await this.knex(table).truncate();
157
- }
158
-
159
- trx(callback: (trx: KnexClient) => Promise<any>) {
160
- return this.knex.transaction((trx) =>
161
- callback(new KnexClient(this.config, trx))
162
- );
163
- }
164
-
165
- destroy() {
166
- return this.knex.destroy();
167
- }
168
-
169
- clearQueryParts(parts: ("order" | "offset" | "limit")[]) {
170
- this.qb = parts.reduce((acc, part) => acc.clear(part), this.qb.clone());
171
- return this;
172
- }
173
-
174
- clone() {
175
- const client = new KnexClient(this.config);
176
- client.qb = this.qb.clone();
177
- return client;
178
- }
179
-
180
- // Migrator
181
-
182
- async getMigrations() {
183
- const [, result] = (await this.knex.migrate.list()) as [
184
- unknown,
185
- {
186
- file: string;
187
- directory: string;
188
- }[],
189
- ];
190
-
191
- return result.map((r) => r.file.replace(".js", ""));
192
- }
193
-
194
- async status() {
195
- return this.knex.migrate.status();
196
- }
197
-
198
- async migrate() {
199
- return this.knex.migrate.latest();
200
- }
201
-
202
- async rollback() {
203
- return this.knex.migrate.rollback();
204
- }
205
-
206
- async rollbackAll() {
207
- return this.knex.migrate.rollback(undefined, true);
208
- }
209
- }
@@ -1,232 +0,0 @@
1
- import _ from "lodash";
2
- import { DBPreset, KnexBaseConfig, SonamuKnexDBConfig } from "../../types";
3
- import knex, { Knex } from "knex";
4
- import { KnexClient } from "./client";
5
- import { DBClass } from "../../db.abstract";
6
- import { attachOnDuplicateUpdate } from "./plugins/knex-on-duplicate-update";
7
- import { KnexGenerator } from "./generator";
8
-
9
- export class DBKnexClass extends DBClass {
10
- public migrationTable = "knex_migrations";
11
- public generator: KnexGenerator = new KnexGenerator();
12
- public baseConfig?: KnexBaseConfig;
13
-
14
- public declare _fullConfig?: SonamuKnexDBConfig;
15
- set fullConfig(config: SonamuKnexDBConfig) {
16
- this._fullConfig = config;
17
- }
18
- get fullConfig() {
19
- if (!this._fullConfig) {
20
- throw new Error("DB Config has not been initialized");
21
- }
22
- return this._fullConfig;
23
- }
24
-
25
- private wdb?: Knex;
26
- private rdb?: Knex;
27
-
28
- private _tdb: KnexClient | null = null;
29
- set tdb(tdb: KnexClient) {
30
- this._tdb = tdb;
31
- }
32
- get tdb(): KnexClient {
33
- if (this._tdb === null) {
34
- throw new Error("tdb has not been initialized");
35
- }
36
- return this._tdb;
37
- }
38
-
39
- private _fdb: KnexClient | null = null;
40
- set fdb(fdb: KnexClient) {
41
- this._fdb = fdb;
42
- }
43
- get fdb(): KnexClient {
44
- if (this._fdb === null) {
45
- throw new Error("fdb has not been initialized");
46
- }
47
- return this._fdb;
48
- }
49
-
50
- get connectionInfo() {
51
- return _.mapValues(this.fullConfig, ({ connection }) => ({
52
- host: connection.host ?? "localhost",
53
- port: connection.port ?? 3306,
54
- database: connection.database,
55
- user: connection.user,
56
- password: connection.password,
57
- }));
58
- }
59
-
60
- constructor() {
61
- super();
62
- attachOnDuplicateUpdate();
63
- }
64
-
65
- init(config: KnexBaseConfig) {
66
- this.baseConfig = config;
67
- this.fullConfig = this.generateDBConfig(config);
68
- }
69
-
70
- async testInit() {
71
- if (this._tdb !== null) {
72
- return;
73
- }
74
-
75
- if (this.fullConfig.test && this.fullConfig.production_master) {
76
- const tConn = this.connectionInfo.test;
77
- const pConn = this.connectionInfo.production_master;
78
-
79
- if (
80
- `${tConn.host ?? "localhost"}:${tConn.port ?? 3306}/${
81
- tConn.database
82
- }` ===
83
- `${pConn.host ?? "localhost"}:${pConn.port ?? 3306}/${pConn.database}`
84
- ) {
85
- throw new Error(
86
- `테스트DB와 프로덕션DB에 동일한 데이터베이스가 사용되었습니다.`
87
- );
88
- }
89
- }
90
-
91
- this.tdb = new KnexClient(this.fullConfig.test);
92
- this.fdb = new KnexClient(this.fullConfig.fixture_local);
93
- }
94
-
95
- getDB(which: DBPreset) {
96
- const instanceName = which === "w" ? "wdb" : "rdb";
97
-
98
- if (!this[instanceName]) {
99
- const config = this.getCurrentConfig(which);
100
- this[instanceName] = knex(config);
101
- }
102
-
103
- return this[instanceName]!;
104
- }
105
-
106
- getClient(mode: keyof SonamuKnexDBConfig) {
107
- return new KnexClient(this.fullConfig[mode]);
108
- }
109
-
110
- async destroy(): Promise<void> {
111
- if (this.wdb !== undefined) {
112
- await this.wdb.destroy();
113
- this.wdb = undefined;
114
- }
115
- if (this.rdb !== undefined) {
116
- await this.rdb.destroy();
117
- this.rdb = undefined;
118
- }
119
- }
120
-
121
- async testDestroy() {
122
- if (this._tdb) {
123
- await this._tdb.destroy();
124
- this._tdb = null;
125
- }
126
- if (this._fdb) {
127
- await this._fdb.destroy();
128
- this._fdb = null;
129
- }
130
- }
131
-
132
- raw(db: Knex, query: string) {
133
- return db.raw(query);
134
- }
135
-
136
- public generateDBConfig(config: KnexBaseConfig): SonamuKnexDBConfig {
137
- const defaultKnexConfig = _.merge(
138
- {
139
- client: "mysql2",
140
- pool: {
141
- min: 1,
142
- max: 5,
143
- },
144
- migrations: {
145
- extension: "js",
146
- directory: "./dist/migrations",
147
- },
148
- connection: {
149
- host: "localhost",
150
- port: 3306,
151
- database: config.database,
152
- },
153
- },
154
- config.defaultOptions
155
- );
156
-
157
- // 로컬 환경 설정
158
- const test = _.merge(
159
- {},
160
- defaultKnexConfig,
161
- {
162
- connection: {
163
- database: `${config.database}_test`,
164
- },
165
- },
166
- config.environments?.test
167
- );
168
-
169
- const fixture_local = _.merge({}, defaultKnexConfig, {
170
- connection: {
171
- database: `${config.database}_fixture`,
172
- },
173
- });
174
-
175
- // 개발 환경 설정
176
- const devMasterOptions = config.environments?.development;
177
- const devSlaveOptions = config.environments?.development_slave;
178
- const development_master = _.merge({}, defaultKnexConfig, devMasterOptions);
179
- const development_slave = _.merge(
180
- {},
181
- defaultKnexConfig,
182
- devMasterOptions,
183
- devSlaveOptions
184
- );
185
- const fixture_remote = _.merge({}, defaultKnexConfig, devMasterOptions, {
186
- connection: {
187
- database: `${config.database}_fixture`,
188
- },
189
- });
190
-
191
- // 프로덕션 환경 설정
192
- const prodMasterOptions = config.environments?.production ?? {};
193
- const prodSlaveOptions = config.environments?.production_slave ?? {};
194
- const production_master = _.merge({}, defaultKnexConfig, prodMasterOptions);
195
- const production_slave = _.merge(
196
- {},
197
- defaultKnexConfig,
198
- prodMasterOptions,
199
- prodSlaveOptions
200
- );
201
-
202
- return {
203
- test,
204
- fixture_local,
205
- fixture_remote,
206
- development_master,
207
- development_slave,
208
- production_master,
209
- production_slave,
210
- };
211
- }
212
-
213
- /**
214
- * keys에 해당하는 설정들을 중복없이 가져옵니다. (host/port/database가 같은 설정은 중복으로 처리합니다.)
215
- */
216
- getUniqueConfigs(keys: (keyof SonamuKnexDBConfig)[]) {
217
- const targets = keys.map((key) => ({
218
- connKey: key,
219
- options: this.fullConfig[key as keyof SonamuKnexDBConfig],
220
- }));
221
-
222
- return _.uniqBy(targets, ({ options }) => {
223
- const conn = options.connection as Knex.ConnectionConfig & {
224
- port?: number;
225
- };
226
-
227
- return `${conn.host ?? "localhost"}:${conn.port ?? 3306}/${
228
- conn.database
229
- }`;
230
- });
231
- }
232
- }