sonamu 0.2.38 → 0.2.40

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 (265) hide show
  1. package/dist/bin/cli-wrapper.d.mts +1 -0
  2. package/dist/bin/cli-wrapper.d.ts +0 -2
  3. package/dist/bin/cli-wrapper.js +26 -23
  4. package/dist/bin/cli-wrapper.js.map +1 -1
  5. package/dist/bin/cli-wrapper.mjs +34 -0
  6. package/dist/bin/cli-wrapper.mjs.map +1 -0
  7. package/dist/bin/cli.d.mts +2 -0
  8. package/dist/bin/cli.d.ts +2 -2
  9. package/dist/bin/cli.js +856 -407
  10. package/dist/bin/cli.js.map +1 -1
  11. package/dist/bin/cli.mjs +887 -0
  12. package/dist/bin/cli.mjs.map +1 -0
  13. package/dist/chunk-BPT5ZXMX.js +7444 -0
  14. package/dist/chunk-BPT5ZXMX.js.map +1 -0
  15. package/dist/chunk-JXJTFHF7.mjs +20 -0
  16. package/dist/chunk-JXJTFHF7.mjs.map +1 -0
  17. package/dist/chunk-OTFO46SP.mjs +7443 -0
  18. package/dist/chunk-OTFO46SP.mjs.map +1 -0
  19. package/dist/index.d.mts +1419 -0
  20. package/dist/index.d.ts +1419 -19
  21. package/dist/index.js +422 -32
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +427 -0
  24. package/dist/index.mjs.map +1 -0
  25. package/package.json +1 -1
  26. package/tsup.config.js +8 -0
  27. package/dist/api/caster.d.ts +0 -10
  28. package/dist/api/caster.d.ts.map +0 -1
  29. package/dist/api/caster.js +0 -81
  30. package/dist/api/caster.js.map +0 -1
  31. package/dist/api/code-converters.d.ts +0 -22
  32. package/dist/api/code-converters.d.ts.map +0 -1
  33. package/dist/api/code-converters.js +0 -507
  34. package/dist/api/code-converters.js.map +0 -1
  35. package/dist/api/context.d.ts +0 -11
  36. package/dist/api/context.d.ts.map +0 -1
  37. package/dist/api/context.js +0 -3
  38. package/dist/api/context.js.map +0 -1
  39. package/dist/api/decorators.d.ts +0 -29
  40. package/dist/api/decorators.d.ts.map +0 -1
  41. package/dist/api/decorators.js +0 -26
  42. package/dist/api/decorators.js.map +0 -1
  43. package/dist/api/index.d.ts +0 -6
  44. package/dist/api/index.d.ts.map +0 -1
  45. package/dist/api/index.js +0 -22
  46. package/dist/api/index.js.map +0 -1
  47. package/dist/api/init.d.ts +0 -42
  48. package/dist/api/init.d.ts.map +0 -1
  49. package/dist/api/init.js +0 -175
  50. package/dist/api/init.js.map +0 -1
  51. package/dist/api/sonamu.d.ts +0 -66
  52. package/dist/api/sonamu.d.ts.map +0 -1
  53. package/dist/api/sonamu.js +0 -204
  54. package/dist/api/sonamu.js.map +0 -1
  55. package/dist/bin/cli-wrapper.d.ts.map +0 -1
  56. package/dist/bin/cli.d.ts.map +0 -1
  57. package/dist/database/_batch_update.d.ts +0 -15
  58. package/dist/database/_batch_update.d.ts.map +0 -1
  59. package/dist/database/_batch_update.js +0 -89
  60. package/dist/database/_batch_update.js.map +0 -1
  61. package/dist/database/base-model.d.ts +0 -39
  62. package/dist/database/base-model.d.ts.map +0 -1
  63. package/dist/database/base-model.js +0 -270
  64. package/dist/database/base-model.js.map +0 -1
  65. package/dist/database/db.d.ts +0 -21
  66. package/dist/database/db.d.ts.map +0 -1
  67. package/dist/database/db.js +0 -99
  68. package/dist/database/db.js.map +0 -1
  69. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts +0 -2
  70. package/dist/database/knex-plugins/knex-on-duplicate-update.d.ts.map +0 -1
  71. package/dist/database/knex-plugins/knex-on-duplicate-update.js +0 -37
  72. package/dist/database/knex-plugins/knex-on-duplicate-update.js.map +0 -1
  73. package/dist/database/upsert-builder.d.ts +0 -34
  74. package/dist/database/upsert-builder.d.ts.map +0 -1
  75. package/dist/database/upsert-builder.js +0 -240
  76. package/dist/database/upsert-builder.js.map +0 -1
  77. package/dist/entity/entity-manager.d.ts +0 -32
  78. package/dist/entity/entity-manager.d.ts.map +0 -1
  79. package/dist/entity/entity-manager.js +0 -135
  80. package/dist/entity/entity-manager.js.map +0 -1
  81. package/dist/entity/entity-utils.d.ts +0 -61
  82. package/dist/entity/entity-utils.d.ts.map +0 -1
  83. package/dist/entity/entity-utils.js +0 -124
  84. package/dist/entity/entity-utils.js.map +0 -1
  85. package/dist/entity/entity.d.ts +0 -59
  86. package/dist/entity/entity.d.ts.map +0 -1
  87. package/dist/entity/entity.js +0 -697
  88. package/dist/entity/entity.js.map +0 -1
  89. package/dist/entity/migrator.d.ts +0 -143
  90. package/dist/entity/migrator.d.ts.map +0 -1
  91. package/dist/entity/migrator.js +0 -1434
  92. package/dist/entity/migrator.js.map +0 -1
  93. package/dist/entity/smd-utils.d.ts +0 -61
  94. package/dist/entity/smd-utils.d.ts.map +0 -1
  95. package/dist/entity/smd-utils.js +0 -121
  96. package/dist/entity/smd-utils.js.map +0 -1
  97. package/dist/exceptions/error-handler.d.ts +0 -3
  98. package/dist/exceptions/error-handler.d.ts.map +0 -1
  99. package/dist/exceptions/error-handler.js +0 -34
  100. package/dist/exceptions/error-handler.js.map +0 -1
  101. package/dist/exceptions/so-exceptions.d.ts +0 -48
  102. package/dist/exceptions/so-exceptions.d.ts.map +0 -1
  103. package/dist/exceptions/so-exceptions.js +0 -105
  104. package/dist/exceptions/so-exceptions.js.map +0 -1
  105. package/dist/index.d.ts.map +0 -1
  106. package/dist/smd/entity-manager.d.ts +0 -28
  107. package/dist/smd/entity-manager.d.ts.map +0 -1
  108. package/dist/smd/entity-manager.js +0 -119
  109. package/dist/smd/entity-manager.js.map +0 -1
  110. package/dist/smd/entity.d.ts +0 -40
  111. package/dist/smd/entity.d.ts.map +0 -1
  112. package/dist/smd/entity.js +0 -430
  113. package/dist/smd/entity.js.map +0 -1
  114. package/dist/smd/migrator.d.ts +0 -109
  115. package/dist/smd/migrator.d.ts.map +0 -1
  116. package/dist/smd/migrator.js +0 -1164
  117. package/dist/smd/migrator.js.map +0 -1
  118. package/dist/smd/smd-manager.d.ts +0 -28
  119. package/dist/smd/smd-manager.d.ts.map +0 -1
  120. package/dist/smd/smd-manager.js +0 -146
  121. package/dist/smd/smd-manager.js.map +0 -1
  122. package/dist/smd/smd-utils.d.ts +0 -61
  123. package/dist/smd/smd-utils.d.ts.map +0 -1
  124. package/dist/smd/smd-utils.js +0 -121
  125. package/dist/smd/smd-utils.js.map +0 -1
  126. package/dist/smd/smd.d.ts +0 -40
  127. package/dist/smd/smd.d.ts.map +0 -1
  128. package/dist/smd/smd.js +0 -429
  129. package/dist/smd/smd.js.map +0 -1
  130. package/dist/syncer/index.d.ts +0 -2
  131. package/dist/syncer/index.d.ts.map +0 -1
  132. package/dist/syncer/index.js +0 -18
  133. package/dist/syncer/index.js.map +0 -1
  134. package/dist/syncer/syncer.d.ts +0 -108
  135. package/dist/syncer/syncer.d.ts.map +0 -1
  136. package/dist/syncer/syncer.js +0 -1119
  137. package/dist/syncer/syncer.js.map +0 -1
  138. package/dist/templates/base-template.d.ts +0 -13
  139. package/dist/templates/base-template.d.ts.map +0 -1
  140. package/dist/templates/base-template.js +0 -10
  141. package/dist/templates/base-template.js.map +0 -1
  142. package/dist/templates/entity.template.d.ts +0 -17
  143. package/dist/templates/entity.template.d.ts.map +0 -1
  144. package/dist/templates/entity.template.js +0 -78
  145. package/dist/templates/entity.template.js.map +0 -1
  146. package/dist/templates/generated.template.d.ts +0 -27
  147. package/dist/templates/generated.template.d.ts.map +0 -1
  148. package/dist/templates/generated.template.js +0 -239
  149. package/dist/templates/generated.template.js.map +0 -1
  150. package/dist/templates/generated_http.template.d.ts +0 -24
  151. package/dist/templates/generated_http.template.d.ts.map +0 -1
  152. package/dist/templates/generated_http.template.js +0 -133
  153. package/dist/templates/generated_http.template.js.map +0 -1
  154. package/dist/templates/generated_sso.template.d.ts +0 -17
  155. package/dist/templates/generated_sso.template.d.ts.map +0 -1
  156. package/dist/templates/generated_sso.template.js +0 -66
  157. package/dist/templates/generated_sso.template.js.map +0 -1
  158. package/dist/templates/index.d.ts +0 -2
  159. package/dist/templates/index.d.ts.map +0 -1
  160. package/dist/templates/index.js +0 -18
  161. package/dist/templates/index.js.map +0 -1
  162. package/dist/templates/init_enums.template.d.ts +0 -17
  163. package/dist/templates/init_enums.template.d.ts.map +0 -1
  164. package/dist/templates/init_enums.template.js +0 -54
  165. package/dist/templates/init_enums.template.js.map +0 -1
  166. package/dist/templates/init_generated.template.d.ts +0 -17
  167. package/dist/templates/init_generated.template.d.ts.map +0 -1
  168. package/dist/templates/init_generated.template.js +0 -46
  169. package/dist/templates/init_generated.template.js.map +0 -1
  170. package/dist/templates/init_types.template.d.ts +0 -17
  171. package/dist/templates/init_types.template.d.ts.map +0 -1
  172. package/dist/templates/init_types.template.js +0 -37
  173. package/dist/templates/init_types.template.js.map +0 -1
  174. package/dist/templates/model.template.d.ts +0 -17
  175. package/dist/templates/model.template.d.ts.map +0 -1
  176. package/dist/templates/model.template.js +0 -168
  177. package/dist/templates/model.template.js.map +0 -1
  178. package/dist/templates/model_test.template.d.ts +0 -17
  179. package/dist/templates/model_test.template.d.ts.map +0 -1
  180. package/dist/templates/model_test.template.js +0 -34
  181. package/dist/templates/model_test.template.js.map +0 -1
  182. package/dist/templates/service.template.d.ts +0 -28
  183. package/dist/templates/service.template.d.ts.map +0 -1
  184. package/dist/templates/service.template.js +0 -167
  185. package/dist/templates/service.template.js.map +0 -1
  186. package/dist/templates/smd.template.d.ts +0 -17
  187. package/dist/templates/smd.template.d.ts.map +0 -1
  188. package/dist/templates/smd.template.js +0 -49
  189. package/dist/templates/smd.template.js.map +0 -1
  190. package/dist/templates/view_enums_buttonset.template.d.ts +0 -17
  191. package/dist/templates/view_enums_buttonset.template.d.ts.map +0 -1
  192. package/dist/templates/view_enums_buttonset.template.js +0 -30
  193. package/dist/templates/view_enums_buttonset.template.js.map +0 -1
  194. package/dist/templates/view_enums_dropdown.template.d.ts +0 -18
  195. package/dist/templates/view_enums_dropdown.template.d.ts.map +0 -1
  196. package/dist/templates/view_enums_dropdown.template.js +0 -68
  197. package/dist/templates/view_enums_dropdown.template.js.map +0 -1
  198. package/dist/templates/view_enums_select.template.d.ts +0 -17
  199. package/dist/templates/view_enums_select.template.d.ts.map +0 -1
  200. package/dist/templates/view_enums_select.template.js +0 -54
  201. package/dist/templates/view_enums_select.template.js.map +0 -1
  202. package/dist/templates/view_form.template.d.ts +0 -74
  203. package/dist/templates/view_form.template.d.ts.map +0 -1
  204. package/dist/templates/view_form.template.js +0 -368
  205. package/dist/templates/view_form.template.js.map +0 -1
  206. package/dist/templates/view_id_all_select.template.d.ts +0 -17
  207. package/dist/templates/view_id_all_select.template.d.ts.map +0 -1
  208. package/dist/templates/view_id_all_select.template.js +0 -30
  209. package/dist/templates/view_id_all_select.template.js.map +0 -1
  210. package/dist/templates/view_id_async_select.template.d.ts +0 -17
  211. package/dist/templates/view_id_async_select.template.d.ts.map +0 -1
  212. package/dist/templates/view_id_async_select.template.js +0 -104
  213. package/dist/templates/view_id_async_select.template.js.map +0 -1
  214. package/dist/templates/view_list.template.d.ts +0 -86
  215. package/dist/templates/view_list.template.d.ts.map +0 -1
  216. package/dist/templates/view_list.template.js +0 -526
  217. package/dist/templates/view_list.template.js.map +0 -1
  218. package/dist/templates/view_list_columns.template.d.ts +0 -17
  219. package/dist/templates/view_list_columns.template.d.ts.map +0 -1
  220. package/dist/templates/view_list_columns.template.js +0 -47
  221. package/dist/templates/view_list_columns.template.js.map +0 -1
  222. package/dist/templates/view_search_input.template.d.ts +0 -17
  223. package/dist/templates/view_search_input.template.d.ts.map +0 -1
  224. package/dist/templates/view_search_input.template.js +0 -63
  225. package/dist/templates/view_search_input.template.js.map +0 -1
  226. package/dist/testing/fixture-manager.d.ts +0 -18
  227. package/dist/testing/fixture-manager.d.ts.map +0 -1
  228. package/dist/testing/fixture-manager.js +0 -238
  229. package/dist/testing/fixture-manager.js.map +0 -1
  230. package/dist/types/smd.types.d.ts +0 -741
  231. package/dist/types/smd.types.d.ts.map +0 -1
  232. package/dist/types/smd.types.js +0 -292
  233. package/dist/types/smd.types.js.map +0 -1
  234. package/dist/types/types.d.ts +0 -721
  235. package/dist/types/types.d.ts.map +0 -1
  236. package/dist/types/types.js +0 -287
  237. package/dist/types/types.js.map +0 -1
  238. package/dist/ui/index.d.ts +0 -4
  239. package/dist/ui/index.d.ts.map +0 -1
  240. package/dist/ui/index.js +0 -14
  241. package/dist/ui/index.js.map +0 -1
  242. package/dist/ui/main.d.ts +0 -4
  243. package/dist/ui/main.d.ts.map +0 -1
  244. package/dist/ui/main.js +0 -15
  245. package/dist/ui/main.js.map +0 -1
  246. package/dist/utils/controller.d.ts +0 -9
  247. package/dist/utils/controller.d.ts.map +0 -1
  248. package/dist/utils/controller.js +0 -36
  249. package/dist/utils/controller.js.map +0 -1
  250. package/dist/utils/lodash-able.d.ts +0 -2
  251. package/dist/utils/lodash-able.d.ts.map +0 -1
  252. package/dist/utils/lodash-able.js +0 -9
  253. package/dist/utils/lodash-able.js.map +0 -1
  254. package/dist/utils/model.d.ts +0 -17
  255. package/dist/utils/model.d.ts.map +0 -1
  256. package/dist/utils/model.js +0 -23
  257. package/dist/utils/model.js.map +0 -1
  258. package/dist/utils/sql-parser.d.ts +0 -4
  259. package/dist/utils/sql-parser.d.ts.map +0 -1
  260. package/dist/utils/sql-parser.js +0 -40
  261. package/dist/utils/sql-parser.js.map +0 -1
  262. package/dist/utils/utils.d.ts +0 -9
  263. package/dist/utils/utils.d.ts.map +0 -1
  264. package/dist/utils/utils.js +0 -100
  265. package/dist/utils/utils.js.map +0 -1
@@ -1,42 +0,0 @@
1
- /// <reference types="node" />
2
- import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
- import { IncomingMessage, Server, ServerResponse } from "http";
4
- import { Context } from "./context";
5
- import { Syncer } from "../syncer/syncer";
6
- import { SonamuDBConfig } from "../database/db";
7
- export declare type SonamuConfig = {
8
- api: {
9
- dir: string;
10
- };
11
- sync: {
12
- targets: string[];
13
- };
14
- route: {
15
- prefix: string;
16
- };
17
- };
18
- declare type SonamuFastifyConfig = {
19
- contextProvider: (defaultContext: Pick<Context, "headers" | "reply">, request: FastifyRequest, reply: FastifyReply) => Context;
20
- };
21
- declare class SonamuClass {
22
- isInitialized: boolean;
23
- private _apiRootPath;
24
- set apiRootPath(apiRootPath: string);
25
- get apiRootPath(): string;
26
- get appRootPath(): string;
27
- private _dbConfig;
28
- set dbConfig(dbConfig: SonamuDBConfig);
29
- get dbConfig(): SonamuDBConfig;
30
- private _syncer;
31
- set syncer(syncer: Syncer);
32
- get syncer(): Syncer;
33
- private _config;
34
- set config(config: SonamuConfig);
35
- get config(): SonamuConfig;
36
- init(): Promise<void>;
37
- withFastify(server: FastifyInstance<Server, IncomingMessage, ServerResponse>, config: SonamuFastifyConfig): Promise<void>;
38
- destroy(): Promise<void>;
39
- }
40
- export declare const Sonamu: SonamuClass;
41
- export {};
42
- //# sourceMappingURL=init.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/api/init.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAG/D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAM,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAMpD,oBAAY,YAAY,GAAG;IACzB,GAAG,EAAE;QACH,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,CAAC;IACF,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH,CAAC;AACF,aAAK,mBAAmB,GAAG;IACzB,eAAe,EAAE,CACf,cAAc,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,EAClD,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,YAAY,KAChB,OAAO,CAAC;CACd,CAAC;AACF,cAAM,WAAW;IACR,aAAa,EAAE,OAAO,CAAS;IAEtC,OAAO,CAAC,YAAY,CAAuB;IAC3C,IAAI,WAAW,CAAC,WAAW,EAAE,MAAM,EAElC;IACD,IAAI,WAAW,IAAI,MAAM,CAKxB;IACD,IAAI,WAAW,IAAI,MAAM,CAExB;IAED,OAAO,CAAC,SAAS,CAA+B;IAChD,IAAI,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAEpC;IACD,IAAI,QAAQ,IAAI,cAAc,CAK7B;IAED,OAAO,CAAC,OAAO,CAAuB;IACtC,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAExB;IACD,IAAI,MAAM,IAAI,MAAM,CAKnB;IAED,OAAO,CAAC,OAAO,CAA6B;IAC5C,IAAI,MAAM,CAAC,MAAM,EAAE,YAAY,EAE9B;IACD,IAAI,MAAM,IAAI,YAAY,CAKzB;IAEK,IAAI;IAsCJ,WAAW,CACf,MAAM,EAAE,eAAe,CAAC,MAAM,EAAE,eAAe,EAAE,cAAc,CAAC,EAChE,MAAM,EAAE,mBAAmB;IAiFvB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/B;AACD,eAAO,MAAM,MAAM,aAAoB,CAAC"}
package/dist/api/init.js DELETED
@@ -1,175 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Sonamu = void 0;
16
- const chalk_1 = __importDefault(require("chalk"));
17
- const zod_1 = require("zod");
18
- const code_converters_1 = require("./code-converters");
19
- const so_exceptions_1 = require("../exceptions/so-exceptions");
20
- const smd_manager_1 = require("../smd/smd-manager");
21
- const caster_1 = require("../api/caster");
22
- const types_1 = require("../types/types");
23
- const syncer_1 = require("../syncer/syncer");
24
- const controller_1 = require("../utils/controller");
25
- const db_1 = require("../database/db");
26
- const base_model_1 = require("../database/base-model");
27
- const utils_1 = require("../utils/utils");
28
- const path_1 = __importDefault(require("path"));
29
- const fs_1 = require("fs");
30
- class SonamuClass {
31
- constructor() {
32
- this.isInitialized = false;
33
- this._apiRootPath = null;
34
- this._dbConfig = null;
35
- this._syncer = null;
36
- this._config = null;
37
- }
38
- set apiRootPath(apiRootPath) {
39
- this._apiRootPath = apiRootPath;
40
- }
41
- get apiRootPath() {
42
- if (this._apiRootPath === null) {
43
- throw new Error("Sonamu has not been initialized");
44
- }
45
- return this._apiRootPath;
46
- }
47
- get appRootPath() {
48
- return this.apiRootPath.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep);
49
- }
50
- set dbConfig(dbConfig) {
51
- this._dbConfig = dbConfig;
52
- }
53
- get dbConfig() {
54
- if (this._dbConfig === null) {
55
- throw new Error("Sonamu has not been initialized");
56
- }
57
- return this._dbConfig;
58
- }
59
- set syncer(syncer) {
60
- this._syncer = syncer;
61
- }
62
- get syncer() {
63
- if (this._syncer === null) {
64
- throw new Error("Sonamu has not been initialized");
65
- }
66
- return this._syncer;
67
- }
68
- set config(config) {
69
- this._config = config;
70
- }
71
- get config() {
72
- if (this._config === null) {
73
- throw new Error("Sonamu has not been initialized");
74
- }
75
- return this._config;
76
- }
77
- init() {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- if (this.isInitialized) {
80
- return;
81
- }
82
- this.apiRootPath = yield (0, utils_1.findApiRootPath)();
83
- const configPath = path_1.default.join(this.apiRootPath, "sonamu.config.json");
84
- if ((0, fs_1.existsSync)(configPath) === false) {
85
- throw new Error(`Cannot find sonamu.config.json in ${configPath}`);
86
- }
87
- this.config = JSON.parse((0, fs_1.readFileSync)(configPath).toString());
88
- // DB 로드
89
- this.dbConfig = yield db_1.DB.readKnexfile();
90
- console.log(chalk_1.default.green("DB Config Loaded!"));
91
- // SMD 로드
92
- yield smd_manager_1.SMDManager.autoload();
93
- // Syncer & Autoload: SMD / Models / Types / APIs
94
- console.time(chalk_1.default.cyan("autoload&sync:"));
95
- yield smd_manager_1.SMDManager.autoload();
96
- this.syncer = new syncer_1.Syncer();
97
- yield this.syncer.autoloadModels();
98
- yield this.syncer.autoloadTypes();
99
- yield this.syncer.autoloadApis();
100
- if ((0, controller_1.isLocal)()) {
101
- yield this.syncer.sync();
102
- }
103
- console.timeEnd(chalk_1.default.cyan("autoload&sync:"));
104
- this.isInitialized = true;
105
- });
106
- }
107
- withFastify(server, config) {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- if (this.isInitialized === false) {
110
- yield this.init();
111
- }
112
- // 전체 라우팅 리스트
113
- server.get(`${this.config.route.prefix}/routes`, (_request, _reply) => __awaiter(this, void 0, void 0, function* () {
114
- return this.syncer.apis;
115
- }));
116
- // Healthcheck API
117
- server.get(`${this.config.route.prefix}/healthcheck`, (_request, _reply) => __awaiter(this, void 0, void 0, function* () {
118
- return "ok";
119
- }));
120
- // API 라우팅 등록
121
- this.syncer.apis.map((api) => {
122
- // model
123
- if (this.syncer.models[api.modelName] === undefined) {
124
- throw new Error(`정의되지 않은 모델에 접근 ${api.modelName}`);
125
- }
126
- const model = this.syncer.models[api.modelName];
127
- // 파라미터 정보로 zod 스키마 빌드
128
- const ReqType = (0, code_converters_1.getZodObjectFromApi)(api, this.syncer.types);
129
- // route
130
- server.route({
131
- method: api.options.httpMethod,
132
- url: this.config.route.prefix + api.path,
133
- handler: (request, reply) => __awaiter(this, void 0, void 0, function* () {
134
- var _a, _b;
135
- const which = api.options.httpMethod === "GET" ? "query" : "body";
136
- let reqBody;
137
- try {
138
- reqBody = (0, caster_1.fastifyCaster)(ReqType).parse((_a = request[which]) !== null && _a !== void 0 ? _a : {});
139
- }
140
- catch (e) {
141
- if (e instanceof zod_1.ZodError) {
142
- // TODO: BadRequest 에러 핸들링 (ZodError issues를 humanize하여 출력하는 로직 필요)
143
- throw new so_exceptions_1.BadRequestException(`${e.issues[0].message}`, e.errors);
144
- }
145
- else {
146
- throw e;
147
- }
148
- }
149
- const result = yield model[api.methodName].apply(model, api.parameters.map((param) => {
150
- // Context 인젝션
151
- if (types_1.ApiParamType.isContext(param.type)) {
152
- return config.contextProvider({
153
- headers: request.headers,
154
- reply,
155
- }, request, reply);
156
- }
157
- else {
158
- return reqBody[param.name];
159
- }
160
- }));
161
- reply.type((_b = api.options.contentType) !== null && _b !== void 0 ? _b : "application/json");
162
- return result;
163
- }),
164
- }); // END server.route
165
- });
166
- });
167
- }
168
- destroy() {
169
- return __awaiter(this, void 0, void 0, function* () {
170
- yield base_model_1.BaseModel.destroy();
171
- });
172
- }
173
- }
174
- exports.Sonamu = new SonamuClass();
175
- //# sourceMappingURL=init.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/api/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,kDAA0B;AAG1B,6BAA+B;AAC/B,uDAAwD;AAExD,+DAAkE;AAClE,oDAAgD;AAChD,0CAA8C;AAC9C,0CAA8C;AAC9C,6CAA0C;AAC1C,oDAA8C;AAC9C,uCAAoD;AACpD,uDAAmD;AACnD,0CAAiD;AACjD,gDAAwB;AACxB,2BAA8C;AAoB9C,MAAM,WAAW;IAAjB;QACS,kBAAa,GAAY,KAAK,CAAC;QAE9B,iBAAY,GAAkB,IAAI,CAAC;QAcnC,cAAS,GAA0B,IAAI,CAAC;QAW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AAW,mCAAI,kBAAkB,CAAC,CAAC;wBAC1D,OAAO,MAAM,CAAC;oBAChB,CAAC,CAAA;iBACF,CAAC,CAAC,CAAC,mBAAmB;YACzB,CAAC,CAAC,CAAC;QACL,CAAC;KAAA;IAEK,OAAO;;YACX,MAAM,sBAAS,CAAC,OAAO,EAAE,CAAC;QAC5B,CAAC;KAAA;CACF;AACY,QAAA,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC"}
@@ -1,66 +0,0 @@
1
- /// <reference types="node" />
2
- import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
- import { IncomingMessage, Server, ServerResponse } from "http";
4
- import { Context } from "./context";
5
- import { ApiParam, ApiParamType } from "../types/types";
6
- import { Syncer } from "../syncer/syncer";
7
- import { SonamuDBConfig } from "../database/db";
8
- import { ApiDecoratorOptions } from "./decorators";
9
- export type SonamuConfig = {
10
- api: {
11
- dir: string;
12
- };
13
- sync: {
14
- targets: string[];
15
- };
16
- route: {
17
- prefix: string;
18
- };
19
- };
20
- type SonamuFastifyConfig = {
21
- contextProvider: (defaultContext: Pick<Context, "headers" | "reply">, request: FastifyRequest, reply: FastifyReply) => Context;
22
- guardHandler: (guard: string, request: FastifyRequest, api: {
23
- typeParameters: ApiParamType.TypeParam[];
24
- parameters: ApiParam[];
25
- returnType: ApiParamType;
26
- modelName: string;
27
- methodName: string;
28
- path: string;
29
- options: ApiDecoratorOptions;
30
- }) => void;
31
- cache?: {
32
- get: (key: string) => Promise<unknown | null>;
33
- put: (key: string, value: unknown, ttl?: number) => Promise<void>;
34
- resolveKey: (path: string, reqBody: {
35
- [key: string]: unknown;
36
- }) => {
37
- cache: false;
38
- } | {
39
- cache: true;
40
- key: string;
41
- ttl?: number;
42
- };
43
- };
44
- };
45
- declare class SonamuClass {
46
- isInitialized: boolean;
47
- private _apiRootPath;
48
- set apiRootPath(apiRootPath: string);
49
- get apiRootPath(): string;
50
- get appRootPath(): string;
51
- private _dbConfig;
52
- set dbConfig(dbConfig: SonamuDBConfig);
53
- get dbConfig(): SonamuDBConfig;
54
- private _syncer;
55
- set syncer(syncer: Syncer);
56
- get syncer(): Syncer;
57
- private _config;
58
- set config(config: SonamuConfig);
59
- get config(): SonamuConfig;
60
- init(doSilent?: boolean, enableSync?: boolean, apiRootPath?: string): Promise<void>;
61
- withFastify(server: FastifyInstance<Server, IncomingMessage, ServerResponse>, config: SonamuFastifyConfig): Promise<void>;
62
- destroy(): Promise<void>;
63
- }
64
- export declare const Sonamu: SonamuClass;
65
- export {};
66
- //# sourceMappingURL=sonamu.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sonamu.d.ts","sourceRoot":"","sources":["../../src/api/sonamu.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAG/D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAM,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAKpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,MAAM,MAAM,YAAY,GAAG;IACzB,GAAG,EAAE;QACH,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,CAAC;IACF,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH,CAAC;AACF,KAAK,mBAAmB,GAAG;IACzB,eAAe,EAAE,CACf,cAAc,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,EAClD,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,YAAY,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AuB;IACtC,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAExB;IACD,IAAI,MAAM,IAAI,MAAM,CAKnB;IAED,OAAO,CAAC,OAAO,CAA6B;IAC5C,IAAI,MAAM,CAAC,MAAM,EAAE,YAAY,EAE9B;IACD,IAAI,MAAM,IAAI,YAAY,CAKzB;IAEK,IAAI,CACR,QAAQ,GAAE,OAAe,EACzB,UAAU,GAAE,OAAc,EAC1B,WAAW,CAAC,EAAE,MAAM;IA6ChB,WAAW,CACf,MAAM,EAAE,eAAe,CAAC,MAAM,EAAE,eAAe,EAAE,cAAc,CAAC,EAChE,MAAM,EAAE,mBAAmB;IAkHvB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/B;AACD,eAAO,MAAM,MAAM,aAAoB,CAAC"}
@@ -1,204 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Sonamu = void 0;
16
- const chalk_1 = __importDefault(require("chalk"));
17
- const zod_1 = require("zod");
18
- const code_converters_1 = require("./code-converters");
19
- const so_exceptions_1 = require("../exceptions/so-exceptions");
20
- const entity_manager_1 = require("../entity/entity-manager");
21
- const caster_1 = require("./caster");
22
- const types_1 = require("../types/types");
23
- const syncer_1 = require("../syncer/syncer");
24
- const controller_1 = require("../utils/controller");
25
- const db_1 = require("../database/db");
26
- const base_model_1 = require("../database/base-model");
27
- const utils_1 = require("../utils/utils");
28
- const path_1 = __importDefault(require("path"));
29
- const fs_extra_1 = __importDefault(require("fs-extra"));
30
- class SonamuClass {
31
- constructor() {
32
- this.isInitialized = false;
33
- this._apiRootPath = null;
34
- this._dbConfig = null;
35
- this._syncer = null;
36
- this._config = null;
37
- }
38
- set apiRootPath(apiRootPath) {
39
- this._apiRootPath = apiRootPath;
40
- }
41
- get apiRootPath() {
42
- if (this._apiRootPath === null) {
43
- throw new Error("Sonamu has not been initialized");
44
- }
45
- return this._apiRootPath;
46
- }
47
- get appRootPath() {
48
- return this.apiRootPath.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep);
49
- }
50
- set dbConfig(dbConfig) {
51
- this._dbConfig = dbConfig;
52
- }
53
- get dbConfig() {
54
- if (this._dbConfig === null) {
55
- throw new Error("Sonamu has not been initialized");
56
- }
57
- return this._dbConfig;
58
- }
59
- set syncer(syncer) {
60
- this._syncer = syncer;
61
- }
62
- get syncer() {
63
- if (this._syncer === null) {
64
- throw new Error("Sonamu has not been initialized");
65
- }
66
- return this._syncer;
67
- }
68
- set config(config) {
69
- this._config = config;
70
- }
71
- get config() {
72
- if (this._config === null) {
73
- throw new Error("Sonamu has not been initialized");
74
- }
75
- return this._config;
76
- }
77
- init(doSilent = false, enableSync = true, apiRootPath) {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- if (this.isInitialized) {
80
- return;
81
- }
82
- !doSilent && console.time(chalk_1.default.cyan("Sonamu.init"));
83
- this.apiRootPath = apiRootPath !== null && apiRootPath !== void 0 ? apiRootPath : (yield (0, utils_1.findApiRootPath)());
84
- const configPath = path_1.default.join(this.apiRootPath, "sonamu.config.json");
85
- if (fs_extra_1.default.existsSync(configPath) === false) {
86
- throw new Error(`Cannot find sonamu.config.json in ${configPath}`);
87
- }
88
- this.config = JSON.parse(fs_extra_1.default.readFileSync(configPath).toString());
89
- // DB 로드
90
- this.dbConfig = yield db_1.DB.readKnexfile();
91
- !doSilent && console.log(chalk_1.default.green("DB Config Loaded!"));
92
- // Entity 로드
93
- yield entity_manager_1.EntityManager.autoload(doSilent);
94
- // Syncer
95
- this.syncer = new syncer_1.Syncer();
96
- // Autoload: Models / Types / APIs
97
- yield this.syncer.autoloadModels();
98
- yield this.syncer.autoloadTypes();
99
- yield this.syncer.autoloadApis();
100
- if ((0, controller_1.isLocal)() && !(0, controller_1.isTest)() && enableSync) {
101
- yield this.syncer.sync();
102
- fetch("http://127.0.0.1:57001/api/reload", {
103
- method: "GET",
104
- }).catch((e) => console.log(chalk_1.default.dim(`Failed to reload Sonamu UI: ${e.message}`)));
105
- }
106
- this.isInitialized = true;
107
- !doSilent && console.timeEnd(chalk_1.default.cyan("Sonamu.init"));
108
- });
109
- }
110
- withFastify(server, config) {
111
- return __awaiter(this, void 0, void 0, function* () {
112
- if (this.isInitialized === false) {
113
- yield this.init();
114
- }
115
- // 전체 라우팅 리스트
116
- server.get(`${this.config.route.prefix}/routes`, (_request, _reply) => __awaiter(this, void 0, void 0, function* () {
117
- return this.syncer.apis;
118
- }));
119
- // Healthcheck API
120
- server.get(`${this.config.route.prefix}/healthcheck`, (_request, _reply) => __awaiter(this, void 0, void 0, function* () {
121
- return "ok";
122
- }));
123
- // API 라우팅 등록
124
- this.syncer.apis.map((api) => {
125
- // model
126
- if (this.syncer.models[api.modelName] === undefined) {
127
- throw new Error(`정의되지 않은 모델에 접근 ${api.modelName}`);
128
- }
129
- const model = this.syncer.models[api.modelName];
130
- // 파라미터 정보로 zod 스키마 빌드
131
- const ReqType = (0, code_converters_1.getZodObjectFromApi)(api, this.syncer.types);
132
- // route
133
- server.route({
134
- method: api.options.httpMethod,
135
- url: this.config.route.prefix + api.path,
136
- handler: (request, reply) => __awaiter(this, void 0, void 0, function* () {
137
- var _a, _b, _c, _d;
138
- ((_a = api.options.guards) !== null && _a !== void 0 ? _a : []).every((guard) => config.guardHandler(guard, request, api));
139
- // request 파싱
140
- const which = api.options.httpMethod === "GET" ? "query" : "body";
141
- let reqBody;
142
- try {
143
- reqBody = (0, caster_1.fastifyCaster)(ReqType).parse((_b = request[which]) !== null && _b !== void 0 ? _b : {});
144
- }
145
- catch (e) {
146
- if (e instanceof zod_1.ZodError) {
147
- // TODO: BadRequest 에러 핸들링 (ZodError issues를 humanize하여 출력하는 로직 필요)
148
- throw new so_exceptions_1.BadRequestException(`${e.issues[0].message}`, e.errors);
149
- }
150
- else {
151
- throw e;
152
- }
153
- }
154
- // Content-Type
155
- reply.type((_c = api.options.contentType) !== null && _c !== void 0 ? _c : "application/json");
156
- // 캐시
157
- const { cacheKey, cacheTtl, cachedData } = yield (() => __awaiter(this, void 0, void 0, function* () {
158
- if (config.cache) {
159
- const cacheKeyRes = config.cache.resolveKey(api.path, reqBody);
160
- if (cacheKeyRes.cache === false) {
161
- return { cacheKey: null, cachedData: null };
162
- }
163
- const cacheKey = cacheKeyRes.key;
164
- const cacheTtl = cacheKeyRes.ttl;
165
- const cachedData = yield config.cache.get(cacheKey);
166
- return { cacheKey, cacheTtl, cachedData };
167
- }
168
- return { cacheKey: null, cachedData: null };
169
- }))();
170
- if (cachedData !== null) {
171
- return cachedData;
172
- }
173
- // 결과
174
- const result = yield model[api.methodName].apply(model, api.parameters.map((param) => {
175
- // Context 인젝션
176
- if (types_1.ApiParamType.isContext(param.type)) {
177
- return config.contextProvider({
178
- headers: request.headers,
179
- reply,
180
- }, request, reply);
181
- }
182
- else {
183
- return reqBody[param.name];
184
- }
185
- }));
186
- reply.type((_d = api.options.contentType) !== null && _d !== void 0 ? _d : "application/json");
187
- // 캐시 키 있는 경우 갱신 후 저장
188
- if (config.cache && cacheKey) {
189
- yield config.cache.put(cacheKey, result, cacheTtl);
190
- }
191
- return result;
192
- }),
193
- }); // END server.route
194
- });
195
- });
196
- }
197
- destroy() {
198
- return __awaiter(this, void 0, void 0, function* () {
199
- yield base_model_1.BaseModel.destroy();
200
- });
201
- }
202
- }
203
- exports.Sonamu = new SonamuClass();
204
-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+0,0 @@
1
- {"version":3,"file":"cli-wrapper.d.ts","sourceRoot":"","sources":["../../src/bin/cli-wrapper.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/bin/cli.ts"],"names":[],"mappings":""}
@@ -1,15 +0,0 @@
1
- import { Knex } from "knex";
2
- export type RowWithId<Id extends string> = {
3
- [key in Id]: any;
4
- } & Record<string, any>;
5
- /**
6
- * Batch update rows in a table. Technically its a patch since it only updates the specified columns. Any omitted columns will not be affected
7
- * @param knex
8
- * @param tableName
9
- * @param id
10
- * @param rows
11
- * @param chunkSize
12
- * @param trx
13
- */
14
- export declare function batchUpdate<Id extends string>(knex: Knex, tableName: string, id: Id, rows: RowWithId<Id>[], chunkSize?: number, trx?: Knex.Transaction | null): Promise<void>;
15
- //# sourceMappingURL=_batch_update.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_batch_update.d.ts","sourceRoot":"","sources":["../../src/database/_batch_update.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,SAAS,CAAC,EAAE,SAAS,MAAM,IAAI;KACxC,GAAG,IAAI,EAAE,GAAG,GAAG;CACjB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAExB;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,EAAE,SAAS,MAAM,EACjD,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,MAAM,EACjB,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,SAAS,CAAC,EAAE,CAAC,EAAE,EACrB,SAAS,SAAK,EACd,GAAG,GAAE,IAAI,CAAC,WAAW,GAAG,IAAW,iBA0BpC"}
@@ -1,89 +0,0 @@
1
- "use strict";
2
- /*
3
- 아래의 링크에서 참고해서 가져온 소스코드
4
- https://github.com/knex/knex/issues/5716
5
- */
6
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
- return new (P || (P = Promise))(function (resolve, reject) {
9
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
- step((generator = generator.apply(thisArg, _arguments || [])).next());
13
- });
14
- };
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.batchUpdate = void 0;
17
- /**
18
- * Batch update rows in a table. Technically its a patch since it only updates the specified columns. Any omitted columns will not be affected
19
- * @param knex
20
- * @param tableName
21
- * @param id
22
- * @param rows
23
- * @param chunkSize
24
- * @param trx
25
- */
26
- function batchUpdate(knex, tableName, id, rows, chunkSize = 50, trx = null) {
27
- return __awaiter(this, void 0, void 0, function* () {
28
- const chunks = [];
29
- for (let i = 0; i < rows.length; i += chunkSize) {
30
- chunks.push(rows.slice(i, i + chunkSize));
31
- }
32
- const executeUpdate = (chunk, transaction) => __awaiter(this, void 0, void 0, function* () {
33
- const sql = generateBatchUpdateSQL(knex, tableName, chunk, id);
34
- return knex.raw(sql).transacting(transaction);
35
- });
36
- if (trx) {
37
- for (const chunk of chunks) {
38
- yield executeUpdate(chunk, trx);
39
- }
40
- }
41
- else {
42
- yield knex.transaction((newTrx) => __awaiter(this, void 0, void 0, function* () {
43
- for (const chunk of chunks) {
44
- yield executeUpdate(chunk, newTrx);
45
- }
46
- }));
47
- }
48
- });
49
- }
50
- exports.batchUpdate = batchUpdate;
51
- /**
52
- * Generate a set of unique keys in a data array
53
- *
54
- * Example:
55
- * [ { a: 1, b: 2 }, { a: 3, c: 4 } ] => Set([ "a", "b", "c" ])
56
- * @param data
57
- */
58
- function generateKeySetFromData(data) {
59
- const keySet = new Set();
60
- for (const row of data) {
61
- for (const key of Object.keys(row)) {
62
- keySet.add(key);
63
- }
64
- }
65
- return keySet;
66
- }
67
- function generateBatchUpdateSQL(knex, tableName, data, identifier) {
68
- const keySet = generateKeySetFromData(data);
69
- const bindings = [];
70
- const cases = [];
71
- for (const key of keySet) {
72
- if (key === identifier)
73
- continue;
74
- const rows = [];
75
- for (const row of data) {
76
- if (Object.hasOwnProperty.call(row, key)) {
77
- rows.push(`WHEN \`${identifier}\` = ? THEN ?`);
78
- bindings.push(row[identifier], row[key]);
79
- }
80
- }
81
- const whenThen = rows.join(" ");
82
- cases.push(`\`${key}\` = CASE ${whenThen} ELSE \`${key}\` END`);
83
- }
84
- const whereInIds = data.map((row) => row[identifier]);
85
- const whereInPlaceholders = whereInIds.map(() => "?").join(", ");
86
- const sql = knex.raw(`UPDATE \`${tableName}\` SET ${cases.join(", ")} WHERE ${identifier} IN (${whereInPlaceholders})`, [...bindings, ...whereInIds]);
87
- return sql.toString();
88
- }
89
- //# sourceMappingURL=_batch_update.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_batch_update.js","sourceRoot":"","sources":["../../src/database/_batch_update.ts"],"names":[],"mappings":";AAAA;;;EAGE;;;;;;;;;;;;AAQF;;;;;;;;GAQG;AACH,SAAsB,WAAW,CAC/B,IAAU,EACV,SAAiB,EACjB,EAAM,EACN,IAAqB,EACrB,SAAS,GAAG,EAAE,EACd,MAA+B,IAAI;;QAEnC,MAAM,MAAM,GAAsB,EAAE,CAAC;QACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,SAAS,EAAE;YAC/C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;SAC3C;QAED,MAAM,aAAa,GAAG,CACpB,KAAsB,EACtB,WAA6B,EAC7B,EAAE;YACF,MAAM,GAAG,GAAG,sBAAsB,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YAC/D,OAAO,IA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