sonamu 0.2.28 → 0.2.29

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 (50) hide show
  1. package/dist/bin/cli.js +8 -3
  2. package/dist/bin/cli.js.map +1 -1
  3. package/dist/database/upsert-builder.d.ts.map +1 -1
  4. package/dist/database/upsert-builder.js +4 -2
  5. package/dist/database/upsert-builder.js.map +1 -1
  6. package/dist/entity/entity-manager.d.ts.map +1 -1
  7. package/dist/entity/entity-manager.js +4 -0
  8. package/dist/entity/entity-manager.js.map +1 -1
  9. package/dist/entity/migrator.d.ts.map +1 -1
  10. package/dist/entity/migrator.js +26 -16
  11. package/dist/entity/migrator.js.map +1 -1
  12. package/dist/syncer/syncer.d.ts.map +1 -1
  13. package/dist/syncer/syncer.js +3 -0
  14. package/dist/syncer/syncer.js.map +1 -1
  15. package/dist/templates/entity.template.d.ts.map +1 -1
  16. package/dist/templates/entity.template.js +3 -1
  17. package/dist/templates/entity.template.js.map +1 -1
  18. package/dist/templates/generated.template.d.ts.map +1 -1
  19. package/dist/templates/generated.template.js +3 -1
  20. package/dist/templates/generated.template.js.map +1 -1
  21. package/dist/templates/generated_http.template.d.ts.map +1 -1
  22. package/dist/templates/generated_http.template.js +2 -1
  23. package/dist/templates/generated_http.template.js.map +1 -1
  24. package/dist/templates/generated_sso.template.d.ts.map +1 -1
  25. package/dist/templates/generated_sso.template.js +3 -1
  26. package/dist/templates/generated_sso.template.js.map +1 -1
  27. package/dist/templates/init_types.template.d.ts.map +1 -1
  28. package/dist/templates/init_types.template.js +3 -1
  29. package/dist/templates/init_types.template.js.map +1 -1
  30. package/dist/templates/model.template.d.ts.map +1 -1
  31. package/dist/templates/model.template.js +3 -1
  32. package/dist/templates/model.template.js.map +1 -1
  33. package/dist/templates/model_test.template.d.ts.map +1 -1
  34. package/dist/templates/model_test.template.js +3 -1
  35. package/dist/templates/model_test.template.js.map +1 -1
  36. package/dist/testing/fixture-manager.js +1 -1
  37. package/package.json +1 -1
  38. package/src/bin/cli.ts +13 -3
  39. package/src/database/upsert-builder.ts +4 -1
  40. package/src/entity/entity-manager.ts +8 -0
  41. package/src/entity/migrator.ts +30 -22
  42. package/src/syncer/syncer.ts +6 -0
  43. package/src/templates/entity.template.ts +4 -1
  44. package/src/templates/generated.template.ts +4 -1
  45. package/src/templates/generated_http.template.ts +3 -1
  46. package/src/templates/generated_sso.template.ts +4 -1
  47. package/src/templates/init_types.template.ts +4 -1
  48. package/src/templates/model.template.ts +4 -1
  49. package/src/templates/model_test.template.ts +4 -1
  50. package/src/testing/fixture-manager.ts +1 -1
@@ -57,7 +57,6 @@ import { EntityManager } from "./entity-manager";
57
57
  import { Entity } from "./entity";
58
58
  import { Sonamu } from "../api";
59
59
  import { ServiceUnavailableException } from "../exceptions/so-exceptions";
60
- import { nonNullable } from "../utils/utils";
61
60
 
62
61
  type MigratorMode = "dev" | "deploy";
63
62
  export type MigratorOptions = {
@@ -104,7 +103,12 @@ export class Migrator {
104
103
  if (
105
104
  (dbConfig.fixture_local.connection as Knex.MySql2ConnectionConfig)
106
105
  .host !==
107
- (dbConfig.fixture_remote.connection as Knex.MySql2ConnectionConfig).host
106
+ (dbConfig.fixture_remote.connection as Knex.MySql2ConnectionConfig)
107
+ .host ||
108
+ (dbConfig.fixture_local.connection as Knex.MySql2ConnectionConfig)
109
+ .database !==
110
+ (dbConfig.fixture_remote.connection as Knex.MySql2ConnectionConfig)
111
+ .database
108
112
  ) {
109
113
  const fixtureRemoteDB = knex(dbConfig.fixture_remote);
110
114
  applyDBs.push(fixtureRemoteDB);
@@ -190,7 +194,7 @@ export class Migrator {
190
194
  if (onlyTs.length > 0) {
191
195
  console.debug({ onlyTs });
192
196
  throw new ServiceUnavailableException(
193
- `There is an un-compiled TS migration files.\nPlease run the dev:serve\n\n${onlyTs
197
+ `There is an un-compiled TS migration files.\nPlease compile them first.\n\n${onlyTs
194
198
  .map((f) => f.name)
195
199
  .join("\n")}`
196
200
  );
@@ -302,23 +306,27 @@ export class Migrator {
302
306
  applied: string[];
303
307
  }[]
304
308
  > {
305
- // get connections
306
- const conns = (
307
- await Promise.all(
308
- targets.map(async (target) => {
309
- const knexOptions =
310
- Sonamu.dbConfig[target as keyof typeof Sonamu.dbConfig];
311
- if (knexOptions === undefined) {
312
- return null;
313
- }
309
+ // get uniq knex configs
310
+ const configs = uniqBy(
311
+ targets
312
+ .map((target) => ({
313
+ connKey: target,
314
+ options: Sonamu.dbConfig[target as keyof typeof Sonamu.dbConfig],
315
+ }))
316
+ .filter((c) => c.options !== undefined),
317
+ ({ options }) =>
318
+ `${(options.connection as Knex.MySql2ConnectionConfig).host}:${
319
+ (options.connection as Knex.MySql2ConnectionConfig).port ?? 3306
320
+ }/${(options.connection as Knex.MySql2ConnectionConfig).database}`
321
+ );
314
322
 
315
- return {
316
- connKey: target,
317
- knex: knex(knexOptions),
318
- };
319
- })
320
- )
321
- ).filter(nonNullable);
323
+ // get connections
324
+ const conns = await Promise.all(
325
+ configs.map(async (config) => ({
326
+ connKey: config.connKey,
327
+ knex: knex(config.options),
328
+ }))
329
+ );
322
330
 
323
331
  // action
324
332
  const result = await (async () => {
@@ -629,8 +637,8 @@ export class Migrator {
629
637
 
630
638
  // 기존 ShadowDB 리셋
631
639
  console.log(chalk.magenta(`${shadowDatabase} 리셋`));
632
- await tdb.raw(`DROP DATABASE IF EXISTS ${shadowDatabase};`);
633
- await tdb.raw(`CREATE DATABASE ${shadowDatabase};`);
640
+ await tdb.raw(`DROP DATABASE IF EXISTS \`${shadowDatabase}\`;`);
641
+ await tdb.raw(`CREATE DATABASE \`${shadowDatabase}\`;`);
634
642
 
635
643
  // ShadowDB 테이블 + 데이터 생성
636
644
  console.log(chalk.magenta(`${shadowDatabase} 데이터베이스 생성`));
@@ -660,7 +668,7 @@ export class Migrator {
660
668
 
661
669
  // 생성한 Shadow DB 삭제
662
670
  console.log(chalk.magenta(`${shadowDatabase} 삭제`));
663
- await sdb.raw(`DROP DATABASE IF EXISTS ${shadowDatabase};`);
671
+ await sdb.raw(`DROP DATABASE IF EXISTS \`${shadowDatabase}\`;`);
664
672
 
665
673
  return [
666
674
  {
@@ -1332,6 +1332,12 @@ export class Syncer {
1332
1332
  table?: string,
1333
1333
  title?: string
1334
1334
  ) {
1335
+ if (/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(entityId) === false) {
1336
+ throw new BadRequestException(
1337
+ "entityId는 자바스크립트 변수명 규칙을 따라야 합니다."
1338
+ );
1339
+ }
1340
+
1335
1341
  await this.generateTemplate("entity", {
1336
1342
  entityId,
1337
1343
  parentId,
@@ -1,6 +1,7 @@
1
1
  import { TemplateOptions } from "../types/types";
2
2
  import { EntityManager, EntityNamesRecord } from "../entity/entity-manager";
3
3
  import { Template } from "./base-template";
4
+ import { Sonamu } from "../api";
4
5
 
5
6
  export class Template__entity extends Template {
6
7
  constructor() {
@@ -8,8 +9,10 @@ export class Template__entity extends Template {
8
9
  }
9
10
 
10
11
  getTargetAndPath(names: EntityNamesRecord, parentNames?: EntityNamesRecord) {
12
+ const { dir } = Sonamu.config.api;
13
+
11
14
  return {
12
- target: "api/src/application",
15
+ target: `${dir}/src/application`,
13
16
  path: `${(parentNames ?? names).fs}/${names.fs}.entity.json`,
14
17
  };
15
18
  }
@@ -6,6 +6,7 @@ import { EntityPropNode } from "../types/types";
6
6
  import { propNodeToZodTypeDef, zodTypeToZodCode } from "../api/code-converters";
7
7
  import { Template } from "./base-template";
8
8
  import { nonNullable } from "../utils/utils";
9
+ import { Sonamu } from "../api";
9
10
 
10
11
  export type SourceCode = {
11
12
  label: string;
@@ -18,8 +19,10 @@ export class Template__generated extends Template {
18
19
  }
19
20
 
20
21
  getTargetAndPath() {
22
+ const { dir } = Sonamu.config.api;
23
+
21
24
  return {
22
- target: "api/src/application",
25
+ target: `${dir}/src/application`,
23
26
  path: `sonamu.generated.ts`,
24
27
  };
25
28
  }
@@ -13,8 +13,10 @@ export class Template__generated_http extends Template {
13
13
  }
14
14
 
15
15
  getTargetAndPath() {
16
+ const { dir } = Sonamu.config.api;
17
+
16
18
  return {
17
- target: "api/src/application",
19
+ target: `${dir}/src/application`,
18
20
  path: `sonamu.generated.http`,
19
21
  };
20
22
  }
@@ -5,6 +5,7 @@ import { camelize } from "inflection";
5
5
  import { SourceCode } from "./generated.template";
6
6
  import { uniq } from "lodash";
7
7
  import { nonNullable } from "../utils/utils";
8
+ import { Sonamu } from "../api";
8
9
 
9
10
  export class Template__generated_sso extends Template {
10
11
  constructor() {
@@ -12,8 +13,10 @@ export class Template__generated_sso extends Template {
12
13
  }
13
14
 
14
15
  getTargetAndPath() {
16
+ const { dir } = Sonamu.config.api;
17
+
15
18
  return {
16
- target: "api/src/application",
19
+ target: `${dir}/src/application`,
17
20
  path: `sonamu.generated.sso.ts`,
18
21
  };
19
22
  }
@@ -1,6 +1,7 @@
1
1
  import { TemplateOptions } from "../types/types";
2
2
  import { EntityManager, EntityNamesRecord } from "../entity/entity-manager";
3
3
  import { Template } from "./base-template";
4
+ import { Sonamu } from "../api";
4
5
 
5
6
  export class Template__init_types extends Template {
6
7
  constructor() {
@@ -8,8 +9,10 @@ export class Template__init_types extends Template {
8
9
  }
9
10
 
10
11
  getTargetAndPath(names: EntityNamesRecord) {
12
+ const { dir } = Sonamu.config.api;
13
+
11
14
  return {
12
- target: "api/src/application",
15
+ target: `${dir}/src/application`,
13
16
  path: `${names.fs}/${names.fs}.types.ts`,
14
17
  };
15
18
  }
@@ -2,6 +2,7 @@ import { RenderingNode, TemplateOptions } from "../types/types";
2
2
  import { EntityManager, EntityNamesRecord } from "../entity/entity-manager";
3
3
  import { Template } from "./base-template";
4
4
  import { Template__view_list } from "./view_list.template";
5
+ import { Sonamu } from "../api";
5
6
 
6
7
  export class Template__model extends Template {
7
8
  constructor() {
@@ -9,8 +10,10 @@ export class Template__model extends Template {
9
10
  }
10
11
 
11
12
  getTargetAndPath(names: EntityNamesRecord) {
13
+ const { dir } = Sonamu.config.api;
14
+
12
15
  return {
13
- target: "api/src/application",
16
+ target: `${dir}/src/application`,
14
17
  path: `${names.fs}/${names.fs}.model.ts`,
15
18
  };
16
19
  }
@@ -1,6 +1,7 @@
1
1
  import { TemplateOptions } from "../types/types";
2
2
  import { EntityManager, EntityNamesRecord } from "../entity/entity-manager";
3
3
  import { Template } from "./base-template";
4
+ import { Sonamu } from "../api";
4
5
 
5
6
  export class Template__model_test extends Template {
6
7
  constructor() {
@@ -8,8 +9,10 @@ export class Template__model_test extends Template {
8
9
  }
9
10
 
10
11
  getTargetAndPath(names: EntityNamesRecord) {
12
+ const { dir } = Sonamu.config.api;
13
+
11
14
  return {
12
- target: "api/src/application",
15
+ target: `${dir}/src/application`,
13
16
  path: `${names.fs}/${names.fs}.model.test.ts`,
14
17
  };
15
18
  }
@@ -150,7 +150,7 @@ export class FixtureManagerClass {
150
150
  )
151
151
  .into(tableName);
152
152
  console.log("OK");
153
- await transaction.raw(`SET FOREIGN_KEY_CHECKS = 0`);
153
+ await transaction.raw(`SET FOREIGN_KEY_CHECKS = 1`);
154
154
  });
155
155
  }
156
156
  })