sonamu 0.0.42 → 0.1.2

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 (208) hide show
  1. package/.pnp.cjs +1963 -951
  2. package/.pnp.loader.mjs +1816 -54
  3. package/.yarnrc.yml +1 -1
  4. package/dist/api/code-converters.d.ts +3 -3
  5. package/dist/api/code-converters.d.ts.map +1 -1
  6. package/dist/api/code-converters.js +5 -15
  7. package/dist/api/code-converters.js.map +1 -1
  8. package/dist/api/context.d.ts +1 -1
  9. package/dist/api/context.d.ts.map +1 -1
  10. package/dist/api/decorators.d.ts +3 -3
  11. package/dist/api/decorators.d.ts.map +1 -1
  12. package/dist/api/sonamu.d.ts +3 -3
  13. package/dist/api/sonamu.d.ts.map +1 -1
  14. package/dist/api/sonamu.js +6 -6
  15. package/dist/api/sonamu.js.map +1 -1
  16. package/dist/bin/cli.js +132 -33
  17. package/dist/bin/cli.js.map +1 -1
  18. package/dist/database/db.d.ts +2 -2
  19. package/dist/database/db.d.ts.map +1 -1
  20. package/dist/database/db.js +1 -1
  21. package/dist/database/db.js.map +1 -1
  22. package/dist/database/upsert-builder.d.ts +2 -2
  23. package/dist/database/upsert-builder.d.ts.map +1 -1
  24. package/dist/database/upsert-builder.js +10 -8
  25. package/dist/database/upsert-builder.js.map +1 -1
  26. package/dist/entity/entity-manager.d.ts +29 -0
  27. package/dist/entity/entity-manager.d.ts.map +1 -0
  28. package/dist/entity/entity-manager.js +128 -0
  29. package/dist/entity/entity-manager.js.map +1 -0
  30. package/dist/entity/entity-utils.d.ts +61 -0
  31. package/dist/entity/entity-utils.d.ts.map +1 -0
  32. package/dist/entity/entity-utils.js +121 -0
  33. package/dist/entity/entity-utils.js.map +1 -0
  34. package/dist/entity/entity.d.ts +54 -0
  35. package/dist/entity/entity.d.ts.map +1 -0
  36. package/dist/entity/entity.js +596 -0
  37. package/dist/entity/entity.js.map +1 -0
  38. package/dist/entity/migrator.d.ts +143 -0
  39. package/dist/entity/migrator.d.ts.map +1 -0
  40. package/dist/entity/migrator.js +1385 -0
  41. package/dist/entity/migrator.js.map +1 -0
  42. package/dist/entity/smd-utils.d.ts +61 -0
  43. package/dist/entity/smd-utils.d.ts.map +1 -0
  44. package/dist/entity/smd-utils.js +121 -0
  45. package/dist/entity/smd-utils.js.map +1 -0
  46. package/dist/index.d.ts +3 -3
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +3 -3
  49. package/dist/index.js.map +1 -1
  50. package/dist/smd/entity-manager.d.ts +28 -0
  51. package/dist/smd/entity-manager.d.ts.map +1 -0
  52. package/dist/smd/entity-manager.js +119 -0
  53. package/dist/smd/entity-manager.js.map +1 -0
  54. package/dist/smd/entity.d.ts +40 -0
  55. package/dist/smd/entity.d.ts.map +1 -0
  56. package/dist/smd/entity.js +430 -0
  57. package/dist/smd/entity.js.map +1 -0
  58. package/dist/smd/migrator.d.ts +2 -2
  59. package/dist/smd/migrator.d.ts.map +1 -1
  60. package/dist/smd/migrator.js +5 -5
  61. package/dist/smd/migrator.js.map +1 -1
  62. package/dist/smd/smd-manager.d.ts +3 -3
  63. package/dist/smd/smd-manager.d.ts.map +1 -1
  64. package/dist/smd/smd-manager.js +2 -2
  65. package/dist/smd/smd-manager.js.map +1 -1
  66. package/dist/smd/smd-utils.d.ts +3 -3
  67. package/dist/smd/smd-utils.d.ts.map +1 -1
  68. package/dist/smd/smd.d.ts +5 -6
  69. package/dist/smd/smd.d.ts.map +1 -1
  70. package/dist/smd/smd.js +3 -3
  71. package/dist/smd/smd.js.map +1 -1
  72. package/dist/syncer/syncer.d.ts +15 -11
  73. package/dist/syncer/syncer.d.ts.map +1 -1
  74. package/dist/syncer/syncer.js +134 -74
  75. package/dist/syncer/syncer.js.map +1 -1
  76. package/dist/templates/base-template.d.ts +2 -2
  77. package/dist/templates/base-template.d.ts.map +1 -1
  78. package/dist/templates/entity.template.d.ts +17 -0
  79. package/dist/templates/entity.template.d.ts.map +1 -0
  80. package/dist/templates/entity.template.js +46 -0
  81. package/dist/templates/entity.template.js.map +1 -0
  82. package/dist/templates/generated.template.d.ts +11 -7
  83. package/dist/templates/generated.template.d.ts.map +1 -1
  84. package/dist/templates/generated.template.js +72 -43
  85. package/dist/templates/generated.template.js.map +1 -1
  86. package/dist/templates/generated_http.template.d.ts +3 -3
  87. package/dist/templates/generated_http.template.d.ts.map +1 -1
  88. package/dist/templates/generated_http.template.js +3 -3
  89. package/dist/templates/generated_http.template.js.map +1 -1
  90. package/dist/templates/init_enums.template.d.ts +2 -2
  91. package/dist/templates/init_enums.template.d.ts.map +1 -1
  92. package/dist/templates/init_enums.template.js +2 -2
  93. package/dist/templates/init_enums.template.js.map +1 -1
  94. package/dist/templates/init_generated.template.d.ts +3 -3
  95. package/dist/templates/init_generated.template.d.ts.map +1 -1
  96. package/dist/templates/init_generated.template.js +13 -14
  97. package/dist/templates/init_generated.template.js.map +1 -1
  98. package/dist/templates/init_types.template.d.ts +3 -3
  99. package/dist/templates/init_types.template.d.ts.map +1 -1
  100. package/dist/templates/init_types.template.js +10 -10
  101. package/dist/templates/init_types.template.js.map +1 -1
  102. package/dist/templates/model.template.d.ts +3 -3
  103. package/dist/templates/model.template.d.ts.map +1 -1
  104. package/dist/templates/model.template.js +28 -28
  105. package/dist/templates/model.template.js.map +1 -1
  106. package/dist/templates/model_test.template.d.ts +3 -3
  107. package/dist/templates/model_test.template.d.ts.map +1 -1
  108. package/dist/templates/model_test.template.js +4 -4
  109. package/dist/templates/model_test.template.js.map +1 -1
  110. package/dist/templates/service.template.d.ts +3 -3
  111. package/dist/templates/service.template.d.ts.map +1 -1
  112. package/dist/templates/service.template.js +3 -3
  113. package/dist/templates/service.template.js.map +1 -1
  114. package/dist/templates/smd.template.d.ts +2 -2
  115. package/dist/templates/smd.template.d.ts.map +1 -1
  116. package/dist/templates/smd.template.js +2 -2
  117. package/dist/templates/smd.template.js.map +1 -1
  118. package/dist/templates/view_enums_buttonset.template.d.ts +3 -3
  119. package/dist/templates/view_enums_buttonset.template.d.ts.map +1 -1
  120. package/dist/templates/view_enums_buttonset.template.js +4 -4
  121. package/dist/templates/view_enums_buttonset.template.js.map +1 -1
  122. package/dist/templates/view_enums_dropdown.template.d.ts +3 -3
  123. package/dist/templates/view_enums_dropdown.template.d.ts.map +1 -1
  124. package/dist/templates/view_enums_dropdown.template.js +3 -3
  125. package/dist/templates/view_enums_dropdown.template.js.map +1 -1
  126. package/dist/templates/view_enums_select.template.d.ts +3 -3
  127. package/dist/templates/view_enums_select.template.d.ts.map +1 -1
  128. package/dist/templates/view_enums_select.template.js +3 -3
  129. package/dist/templates/view_enums_select.template.js.map +1 -1
  130. package/dist/templates/view_form.template.d.ts +25 -29
  131. package/dist/templates/view_form.template.d.ts.map +1 -1
  132. package/dist/templates/view_form.template.js +19 -19
  133. package/dist/templates/view_form.template.js.map +1 -1
  134. package/dist/templates/view_id_all_select.template.d.ts +3 -3
  135. package/dist/templates/view_id_all_select.template.d.ts.map +1 -1
  136. package/dist/templates/view_id_all_select.template.js +4 -4
  137. package/dist/templates/view_id_all_select.template.js.map +1 -1
  138. package/dist/templates/view_id_async_select.template.d.ts +3 -3
  139. package/dist/templates/view_id_async_select.template.d.ts.map +1 -1
  140. package/dist/templates/view_id_async_select.template.js +6 -6
  141. package/dist/templates/view_id_async_select.template.js.map +1 -1
  142. package/dist/templates/view_list.template.d.ts +30 -34
  143. package/dist/templates/view_list.template.d.ts.map +1 -1
  144. package/dist/templates/view_list.template.js +40 -40
  145. package/dist/templates/view_list.template.js.map +1 -1
  146. package/dist/templates/view_list_columns.template.d.ts +3 -3
  147. package/dist/templates/view_list_columns.template.d.ts.map +1 -1
  148. package/dist/templates/view_list_columns.template.js +3 -3
  149. package/dist/templates/view_list_columns.template.js.map +1 -1
  150. package/dist/templates/view_search_input.template.d.ts +3 -3
  151. package/dist/templates/view_search_input.template.d.ts.map +1 -1
  152. package/dist/templates/view_search_input.template.js +3 -3
  153. package/dist/templates/view_search_input.template.js.map +1 -1
  154. package/dist/testing/fixture-manager.d.ts +2 -2
  155. package/dist/testing/fixture-manager.d.ts.map +1 -1
  156. package/dist/testing/fixture-manager.js +18 -16
  157. package/dist/testing/fixture-manager.js.map +1 -1
  158. package/dist/types/smd.types.d.ts +741 -0
  159. package/dist/types/smd.types.d.ts.map +1 -0
  160. package/dist/types/smd.types.js +292 -0
  161. package/dist/types/smd.types.js.map +1 -0
  162. package/dist/types/types.d.ts +187 -190
  163. package/dist/types/types.d.ts.map +1 -1
  164. package/dist/types/types.js +22 -30
  165. package/dist/types/types.js.map +1 -1
  166. package/dist/utils/model.d.ts +2 -2
  167. package/dist/utils/model.d.ts.map +1 -1
  168. package/dist/utils/utils.d.ts +1 -0
  169. package/dist/utils/utils.d.ts.map +1 -1
  170. package/dist/utils/utils.js +6 -2
  171. package/dist/utils/utils.js.map +1 -1
  172. package/package.json +11 -8
  173. package/src/api/code-converters.ts +9 -17
  174. package/src/api/sonamu.ts +10 -6
  175. package/src/bin/cli.ts +156 -27
  176. package/src/database/upsert-builder.ts +2 -2
  177. package/src/entity/entity-manager.ts +150 -0
  178. package/src/{smd/smd-utils.ts → entity/entity-utils.ts} +3 -3
  179. package/src/entity/entity.ts +774 -0
  180. package/src/{smd → entity}/migrator.ts +426 -106
  181. package/src/index.ts +3 -3
  182. package/src/smd/smd-manager.ts +3 -13
  183. package/src/smd/smd.ts +13 -10
  184. package/src/syncer/syncer.ts +125 -73
  185. package/src/templates/base-template.ts +2 -2
  186. package/src/templates/entity.template.ts +50 -0
  187. package/src/templates/generated.template.ts +93 -57
  188. package/src/templates/generated_http.template.ts +4 -4
  189. package/src/templates/init_types.template.ts +11 -11
  190. package/src/templates/model.template.ts +29 -29
  191. package/src/templates/model_test.template.ts +5 -5
  192. package/src/templates/service.template.ts +4 -4
  193. package/src/templates/view_enums_buttonset.template.ts +5 -5
  194. package/src/templates/view_enums_dropdown.template.ts +4 -4
  195. package/src/templates/view_enums_select.template.ts +8 -4
  196. package/src/templates/view_form.template.ts +21 -21
  197. package/src/templates/view_id_all_select.template.ts +5 -5
  198. package/src/templates/view_id_async_select.template.ts +9 -7
  199. package/src/templates/view_list.template.ts +54 -44
  200. package/src/templates/view_list_columns.template.ts +4 -4
  201. package/src/templates/view_search_input.template.ts +4 -4
  202. package/src/testing/fixture-manager.ts +12 -12
  203. package/src/types/types.ts +59 -39
  204. package/src/utils/utils.ts +4 -0
  205. package/tsconfig.json +4 -1
  206. package/src/templates/init_enums.template.ts +0 -71
  207. package/src/templates/init_generated.template.ts +0 -51
  208. package/src/templates/smd.template.ts +0 -53
package/src/bin/cli.ts CHANGED
@@ -7,14 +7,24 @@ dotenv.config();
7
7
 
8
8
  import path from "path";
9
9
  import { BaseModel } from "../database/base-model";
10
- import { SMDManager } from "../smd/smd-manager";
11
- import { Migrator } from "../smd/migrator";
10
+ import { EntityManager } from "../entity/entity-manager";
11
+ import { Migrator } from "../entity/migrator";
12
12
  import { FixtureManager } from "../testing/fixture-manager";
13
13
  import { tsicli } from "tsicli";
14
14
  import { execSync } from "child_process";
15
- import { existsSync, mkdirSync, readdirSync, writeFileSync } from "fs";
15
+ import {
16
+ existsSync,
17
+ mkdirSync,
18
+ readdirSync,
19
+ unlinkSync,
20
+ writeFileSync,
21
+ } from "fs";
16
22
  import { Sonamu } from "../api";
17
23
  import knex, { Knex } from "knex";
24
+ import { camelize } from "inflection";
25
+ import prettier from "prettier";
26
+ import { SMDManager } from "../smd/smd-manager";
27
+ import process from "process";
18
28
 
19
29
  let migrator: Migrator;
20
30
 
@@ -23,13 +33,13 @@ async function bootstrap() {
23
33
 
24
34
  await tsicli(process.argv, {
25
35
  types: {
26
- "#smdId": {
36
+ "#entityId": {
27
37
  type: "autocomplete",
28
- name: "#smdId",
29
- message: "Please input #smdId",
30
- choices: SMDManager.getAllParentIds().map((smdId) => ({
31
- title: smdId,
32
- value: smdId,
38
+ name: "#entityId",
39
+ message: "Please input #entityId",
40
+ choices: EntityManager.getAllParentIds().map((entityId) => ({
41
+ title: entityId,
42
+ value: entityId,
33
43
  })),
34
44
  },
35
45
  "#recordIds": "number[]",
@@ -37,7 +47,7 @@ async function bootstrap() {
37
47
  },
38
48
  args: [
39
49
  ["fixture", "init"],
40
- ["fixture", "import", "#smdId", "#recordIds"],
50
+ ["fixture", "import", "#entityId", "#recordIds"],
41
51
  ["fixture", "sync"],
42
52
  ["migrate", "run"],
43
53
  ["migrate", "check"],
@@ -45,12 +55,13 @@ async function bootstrap() {
45
55
  ["migrate", "reset"],
46
56
  ["migrate", "clear"],
47
57
  ["stub", "practice", "#name"],
48
- ["stub", "smd", "#name"],
49
- ["scaffold", "model", "#smdId"],
50
- ["scaffold", "model_test", "#smdId"],
51
- ["scaffold", "view_list", "#smdId"],
52
- ["scaffold", "view_form", "#smdId"],
58
+ ["stub", "entity", "#name"],
59
+ ["scaffold", "model", "#entityId"],
60
+ ["scaffold", "model_test", "#entityId"],
61
+ ["scaffold", "view_list", "#entityId"],
62
+ ["scaffold", "view_form", "#entityId"],
53
63
  ["ui"],
64
+ ["smd_migration"],
54
65
  ],
55
66
  runners: {
56
67
  migrate_run,
@@ -62,12 +73,13 @@ async function bootstrap() {
62
73
  fixture_import,
63
74
  fixture_sync,
64
75
  stub_practice,
65
- stub_smd,
76
+ stub_entity,
66
77
  scaffold_model,
67
78
  scaffold_model_test,
68
79
  ui,
69
80
  // scaffold_view_list,
70
81
  // scaffold_view_form,
82
+ smd_migration,
71
83
  },
72
84
  });
73
85
  }
@@ -199,10 +211,10 @@ async function fixture_init() {
199
211
  }
200
212
  }
201
213
 
202
- async function fixture_import(smdId: string, recordIds: number[]) {
214
+ async function fixture_import(entityId: string, recordIds: number[]) {
203
215
  await setupFixtureManager();
204
216
 
205
- await FixtureManager.importFixture(smdId, recordIds);
217
+ await FixtureManager.importFixture(entityId, recordIds);
206
218
  await FixtureManager.sync();
207
219
  }
208
220
 
@@ -267,27 +279,27 @@ async function stub_practice(name: string) {
267
279
  execSync(`echo "${runCode}" | pbcopy`);
268
280
  }
269
281
 
270
- async function stub_smd(name: string) {
271
- await Sonamu.syncer.generateTemplate("smd", {
272
- smdId: name,
273
- });
282
+ async function stub_entity(entityId: string) {
283
+ await Sonamu.syncer.createEntity(entityId);
274
284
  }
275
285
 
276
- async function scaffold_model(smdId: string) {
286
+ async function scaffold_model(entityId: string) {
277
287
  await Sonamu.syncer.generateTemplate("model", {
278
- smdId,
288
+ entityId,
279
289
  });
280
290
  }
281
291
 
282
- async function scaffold_model_test(smdId: string) {
292
+ async function scaffold_model_test(entityId: string) {
283
293
  await Sonamu.syncer.generateTemplate("model_test", {
284
- smdId,
294
+ entityId,
285
295
  });
286
296
  }
287
297
 
288
298
  async function ui() {
289
299
  try {
290
- const sonamuUI = await import("@sonamu-kit/ui" as any);
300
+ const sonamuUI: {
301
+ startServers: (appRootPath: string) => void;
302
+ } = await import("@sonamu-kit/ui" as string);
291
303
  sonamuUI.startServers(Sonamu.appRootPath);
292
304
  } catch (e: unknown) {
293
305
  if (e instanceof Error && e.message.includes("isn't declared")) {
@@ -297,3 +309,120 @@ async function ui() {
297
309
  throw e;
298
310
  }
299
311
  }
312
+
313
+ async function smd_migration() {
314
+ await SMDManager.autoload();
315
+ const smdIds = SMDManager.getAllIds();
316
+
317
+ function enumLabelsToEntityEnums(
318
+ entityId: string,
319
+ enumLabels: {
320
+ [enumName: string]: { [name: string]: { ko: string } };
321
+ }
322
+ ): { [enumName: string]: { [name: string]: string } } {
323
+ return Object.fromEntries(
324
+ Object.entries(enumLabels).map(([enumLabelName, enumLabel]) => {
325
+ const enumName =
326
+ entityId + camelize(enumLabelName.toLowerCase(), false);
327
+ return [
328
+ enumName,
329
+ Object.fromEntries(
330
+ Object.entries(enumLabel).map(([name, { ko }]) => [name, ko])
331
+ ),
332
+ ];
333
+ })
334
+ );
335
+ }
336
+ for await (const smdId of smdIds) {
337
+ const smd = SMDManager.get(smdId);
338
+ const entityJson = {
339
+ id: smd.id,
340
+ ...(smd.parentId ? { parentId: smd.parentId } : {}),
341
+ title: smd.title,
342
+ props: smd.props,
343
+ indexes: smd.indexes,
344
+ subsets: smd.subsets,
345
+ enums: enumLabelsToEntityEnums(smd.id, smd.enumLabels),
346
+ };
347
+
348
+ const parentNames = SMDManager.getNamesFromId(smd.parentId ?? smd.id);
349
+ const names = SMDManager.getNamesFromId(smd.id);
350
+ const dstPath = path.join(
351
+ Sonamu.apiRootPath,
352
+ "src",
353
+ "application",
354
+ parentNames.fs,
355
+ `${names.fs}.entity.json`
356
+ );
357
+
358
+ const formatted = prettier.format(JSON.stringify(entityJson), {
359
+ parser: "json",
360
+ });
361
+ writeFileSync(dstPath, formatted);
362
+ console.log(chalk.blue(`CREATED: ${dstPath}`));
363
+
364
+ // smd.ts, enums.ts, genereated.ts 삭제 (트랜스파일 된 js파일도 삭제)
365
+ const srcSmdPath = path.join(
366
+ Sonamu.apiRootPath,
367
+ "src",
368
+ "application",
369
+ parentNames.fs,
370
+ `${names.fs}.smd.ts`
371
+ );
372
+ const dstSmdPath = srcSmdPath
373
+ .replace("/src/", "/dist/")
374
+ .replace(/\.ts$/, ".js");
375
+ const srcEnumsPath = path.join(
376
+ Sonamu.apiRootPath,
377
+ "src",
378
+ "application",
379
+ parentNames.fs,
380
+ `${names.fs}.enums.ts`
381
+ );
382
+ const dstEnumsPath = srcEnumsPath
383
+ .replace("/src/", "/dist/")
384
+ .replace(/\.ts$/, ".js");
385
+ const srcGeneratedPath = path.join(
386
+ Sonamu.apiRootPath,
387
+ "src",
388
+ "application",
389
+ parentNames.fs,
390
+ `${names.fs}.generated.ts`
391
+ );
392
+ const dstGeneratedPath = srcGeneratedPath
393
+ .replace("/src/", "/dist/")
394
+ .replace(/\.ts$/, ".js");
395
+
396
+ [
397
+ srcSmdPath,
398
+ dstSmdPath,
399
+ srcEnumsPath,
400
+ dstEnumsPath,
401
+ ...Sonamu.config.sync.targets.map((target) =>
402
+ srcEnumsPath
403
+ .replace(Sonamu.apiRootPath, path.join(Sonamu.appRootPath, target))
404
+ .replace("/src/application/", "/src/services/")
405
+ ),
406
+ srcGeneratedPath,
407
+ dstGeneratedPath,
408
+ ].map((p) => {
409
+ if (existsSync(p) === false) {
410
+ console.log(chalk.yellow(`NOT FOUND: ${p}`));
411
+ return;
412
+ }
413
+ unlinkSync(p);
414
+ console.log(chalk.red(`DELETED: ${p}`));
415
+ });
416
+ }
417
+
418
+ // Entity로 reload
419
+ console.log("Entity로 다시 로드합니다.");
420
+ EntityManager.isAutoloaded = false;
421
+ await EntityManager.autoload();
422
+
423
+ // Entity를 통해 generated.ts 재생성
424
+ const entityIds = EntityManager.getAllParentIds();
425
+ for await (const entityId of entityIds) {
426
+ await Sonamu.syncer.generateTemplate("generated", { entityId });
427
+ }
428
+ }
@@ -1,7 +1,7 @@
1
1
  import { v4 as uuidv4 } from "uuid";
2
2
  import _, { chunk, defaults, groupBy } from "lodash";
3
3
  import { Knex } from "knex";
4
- import { SMDManager } from "../smd/smd-manager";
4
+ import { EntityManager } from "../entity/entity-manager";
5
5
 
6
6
  type TableData = {
7
7
  references: Set<string>;
@@ -34,7 +34,7 @@ export class UpsertBuilder {
34
34
  if (table === undefined) {
35
35
  const tableSpec = (() => {
36
36
  try {
37
- return SMDManager.getTableSpec(tableName);
37
+ return EntityManager.getTableSpec(tableName);
38
38
  } catch {
39
39
  return null;
40
40
  }
@@ -0,0 +1,150 @@
1
+ import chalk from "chalk";
2
+ import { glob } from "glob";
3
+ import { dasherize, underscore, pluralize, camelize } from "inflection";
4
+ import _ from "lodash";
5
+ import path from "path";
6
+ import { Entity } from "./entity";
7
+ import { EntityJson } from "../types/types";
8
+ import { Sonamu } from "../api/sonamu";
9
+ import { readFileSync } from "fs";
10
+
11
+ export type EntityNamesRecord = Record<
12
+ | "fs"
13
+ | "fsPlural"
14
+ | "camel"
15
+ | "camelPlural"
16
+ | "capital"
17
+ | "capitalPlural"
18
+ | "upper"
19
+ | "constant",
20
+ string
21
+ >;
22
+ type TableSpec = {
23
+ name: string;
24
+ uniqueColumns: string[];
25
+ };
26
+ class EntityManagerClass {
27
+ private entities: Map<string, Entity> = new Map();
28
+ public modulePaths: Map<string, string> = new Map();
29
+ private tableSpecs: Map<string, TableSpec> = new Map();
30
+ public isAutoloaded: boolean = false;
31
+
32
+ // 경로 전달받아 모든 entity.json 파일 로드
33
+ async autoload(doSilent: boolean = false) {
34
+ if (this.isAutoloaded) {
35
+ return;
36
+ }
37
+ const pathPattern = path.join(
38
+ Sonamu.apiRootPath,
39
+ "/src/application/**/*.entity.json"
40
+ );
41
+ !doSilent && console.log(chalk.yellow(`autoload ${pathPattern}`));
42
+
43
+ return new Promise((resolve) => {
44
+ glob(path.resolve(pathPattern!), (_err, files) => {
45
+ Promise.all(
46
+ files.map(async (file) => {
47
+ this.register(JSON.parse(readFileSync(file).toString()));
48
+ })
49
+ ).then(() => {
50
+ resolve("ok");
51
+ this.isAutoloaded = true;
52
+ });
53
+ });
54
+ });
55
+ }
56
+
57
+ async reload(doSilent: boolean = false) {
58
+ this.entities.clear();
59
+ this.modulePaths.clear();
60
+ this.tableSpecs.clear();
61
+ this.isAutoloaded = false;
62
+
63
+ return this.autoload(doSilent);
64
+ }
65
+
66
+ register(json: EntityJson): void {
67
+ const entity = new Entity(json);
68
+ this.entities.set(json.id, entity);
69
+ }
70
+
71
+ get(entityId: string): Entity {
72
+ const entity = this.entities.get(entityId);
73
+ if (entity === undefined) {
74
+ throw new Error(`존재하지 않는 Entity 요청 ${entityId}`);
75
+ }
76
+
77
+ return entity;
78
+ }
79
+
80
+ exists(entityId: string): boolean {
81
+ const entity = this.entities.get(entityId);
82
+ return entity !== undefined;
83
+ }
84
+
85
+ getAllIds(): string[] {
86
+ return Array.from(EntityManager.entities.keys());
87
+ }
88
+
89
+ getAllParentIds(): string[] {
90
+ return this.getAllIds().filter((entityId) => {
91
+ const entity = this.get(entityId);
92
+ return entity.parentId === undefined;
93
+ });
94
+ }
95
+
96
+ getChildrenIds(parentId: string): string[] {
97
+ return this.getAllIds().filter((entityId) => {
98
+ const entity = this.get(entityId);
99
+ return entity.parentId === parentId;
100
+ });
101
+ }
102
+
103
+ setModulePath(key: string, modulePath: string): void {
104
+ // console.debug(chalk.cyan(`setModulePath :: ${key} :: ${modulePath}`));
105
+ this.modulePaths.set(key, modulePath);
106
+ }
107
+
108
+ getModulePath(key: string): string {
109
+ const modulePath = this.modulePaths.get(key);
110
+ if (modulePath === undefined) {
111
+ throw new Error(`존재하지 않는 모듈 패스 요청 ${key}`);
112
+ }
113
+
114
+ return modulePath;
115
+ }
116
+
117
+ setTableSpec(tableSpec: TableSpec) {
118
+ this.tableSpecs.set(tableSpec.name, tableSpec);
119
+ }
120
+
121
+ getTableSpec(key: string): TableSpec {
122
+ const tableSpec = this.tableSpecs.get(key);
123
+ if (tableSpec === undefined) {
124
+ throw new Error(`존재하지 않는 테이블 스펙 요청 ${key}`);
125
+ }
126
+
127
+ return tableSpec;
128
+ }
129
+
130
+ getNamesFromId(entityId: string): EntityNamesRecord {
131
+ // entityId가 단복수 동형 단어인 경우 List 붙여서 생성
132
+ const pluralized =
133
+ pluralize(entityId) === entityId
134
+ ? `${entityId}List`
135
+ : pluralize(entityId);
136
+
137
+ return {
138
+ fs: dasherize(underscore(entityId)).toLowerCase(),
139
+ fsPlural: dasherize(underscore(pluralized)).toLowerCase(),
140
+ camel: camelize(entityId, true),
141
+ camelPlural: camelize(pluralized, true),
142
+ capital: entityId,
143
+ capitalPlural: pluralized,
144
+ upper: entityId.toUpperCase(),
145
+ constant: underscore(entityId).toUpperCase(),
146
+ };
147
+ }
148
+ }
149
+
150
+ export const EntityManager = new EntityManagerClass();
@@ -15,7 +15,7 @@ import {
15
15
  JsonProp,
16
16
  ManyToManyRelationProp,
17
17
  OneToOneRelationProp,
18
- SMDIndex,
18
+ EntityIndex,
19
19
  StringProp,
20
20
  TextProp,
21
21
  TimeProp,
@@ -273,14 +273,14 @@ export const i = {
273
273
  unique,
274
274
  };
275
275
 
276
- function index(columns: string | string[]): SMDIndex {
276
+ function index(columns: string | string[]): EntityIndex {
277
277
  return {
278
278
  type: "index",
279
279
  columns: asArray(columns),
280
280
  };
281
281
  }
282
282
 
283
- function unique(columns: string | string[]): SMDIndex {
283
+ function unique(columns: string | string[]): EntityIndex {
284
284
  return {
285
285
  type: "unique",
286
286
  columns: asArray(columns),