sasat 0.19.13 → 0.19.14

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 (56) hide show
  1. package/lib/cli/cli.js +2 -0
  2. package/lib/cli/commands/migrate.d.ts +6 -3
  3. package/lib/cli/commands/migrate.js +1 -1
  4. package/lib/db/getDbClient.d.ts +1 -1
  5. package/lib/db/sql/createTable/createTableParser.js +2 -2
  6. package/lib/generatorv2/codegen/ts/generateIDEncoder.js +2 -5
  7. package/lib/generatorv2/codegen/ts/generateMiddlewares.d.ts +2 -0
  8. package/lib/generatorv2/codegen/ts/generateMiddlewares.js +45 -0
  9. package/lib/generatorv2/codegen/ts/generateQueryResolver.js +15 -6
  10. package/lib/generatorv2/codegen/ts/generateUserDefinedCondition.js +2 -1
  11. package/lib/generatorv2/codegen/ts/mutation/makeMutationInputDecoder.js +5 -2
  12. package/lib/generatorv2/codegen/ts/tsFileNames.d.ts +1 -0
  13. package/lib/generatorv2/codegen/ts/tsFileNames.js +1 -0
  14. package/lib/generatorv2/codegen/tscodegen_v2.d.ts +1 -0
  15. package/lib/generatorv2/codegen/tscodegen_v2.js +4 -0
  16. package/lib/generatorv2/codegen_v2.d.ts +1 -0
  17. package/lib/generatorv2/codegen_v2.js +10 -0
  18. package/lib/generatorv2/nodes/entityNode.js +11 -5
  19. package/lib/generatorv2/nodes/mutationNode.d.ts +1 -0
  20. package/lib/generatorv2/parser/makeContextNodes.js +2 -2
  21. package/lib/generatorv2/parser/makeMutationNodes.js +27 -23
  22. package/lib/generatorv2/parser/makeSubscriptionNode.js +11 -19
  23. package/lib/index.d.ts +1 -0
  24. package/lib/index.js +1 -0
  25. package/lib/migration/controller.d.ts +2 -1
  26. package/lib/migration/controller.js +6 -2
  27. package/lib/migration/creators/tableCreator.d.ts +5 -11
  28. package/lib/migration/creators/tableCreator.js +6 -16
  29. package/lib/migration/data/GQLOption.d.ts +14 -18
  30. package/lib/migration/data/GQLOption.js +2 -22
  31. package/lib/migration/exec/runMigration.d.ts +2 -1
  32. package/lib/migration/exec/runMigration.js +7 -1
  33. package/lib/migration/front/tableMigrator.d.ts +5 -9
  34. package/lib/migration/front/tableMigrator.js +14 -16
  35. package/lib/migration/makeMutaion.d.ts +16 -0
  36. package/lib/migration/makeMutaion.js +29 -0
  37. package/lib/migration/makeQuery.d.ts +13 -4
  38. package/lib/migration/makeQuery.js +12 -8
  39. package/lib/migration/serializable/table.d.ts +1 -5
  40. package/lib/migration/serializable/table.js +3 -35
  41. package/lib/runtime/resolverMiddleware.d.ts +1 -1
  42. package/package.json +10 -10
  43. package/lib/generatorv2/codegen/gql/generateTypeDefs.d.ts +0 -3
  44. package/lib/generatorv2/codegen/gql/generateTypeDefs.js +0 -114
  45. package/lib/generatorv2/codegen/gql/gqlString.d.ts +0 -16
  46. package/lib/generatorv2/codegen/gql/gqlString.js +0 -43
  47. package/lib/generatorv2/codegen/gql/typeDefinition.d.ts +0 -9
  48. package/lib/generatorv2/codegen/gql/typeDefinition.js +0 -13
  49. package/lib/generatorv2/codegen/ts/relationMap/makeCondition.d.ts +0 -2
  50. package/lib/generatorv2/codegen/ts/relationMap/makeCondition.js +0 -100
  51. package/lib/generatorv2/nodes/ConditionNode.d.ts +0 -56
  52. package/lib/generatorv2/nodes/ConditionNode.js +0 -1
  53. package/lib/generatorv2/parser/makeQueryNodes.d.ts +0 -0
  54. package/lib/generatorv2/parser/makeQueryNodes.js +0 -1
  55. package/lib/runtime/dsl/query/fieldToQuery.d.ts +0 -0
  56. package/lib/runtime/dsl/query/fieldToQuery.js +0 -1
@@ -4,40 +4,36 @@ export interface GqlFromContextParam {
4
4
  column: string;
5
5
  contextName?: string;
6
6
  }
7
- type Enabled = {
8
- enabled: boolean;
9
- };
10
7
  export type MutationOption = {
11
8
  noReFetch: boolean;
12
9
  subscription: boolean;
13
10
  subscriptionFilter: string[];
14
11
  };
12
+ export type GQLMutation = {
13
+ type: 'create' | 'update' | 'delete';
14
+ noReFetch: boolean;
15
+ middlewares: string[];
16
+ contextFields: GqlFromContextParam[];
17
+ subscription: {
18
+ enabled: boolean;
19
+ subscriptionFilter: string[];
20
+ };
21
+ };
15
22
  export type GQLQuery = {
16
23
  type: 'single' | 'list-all' | 'list-paging';
17
24
  name: string;
18
25
  conditions: QueryConditionNode[];
26
+ middlewares: string[];
19
27
  } | {
20
28
  type: 'primary';
21
29
  name?: never;
22
30
  conditions: never[];
31
+ middlewares: string[];
23
32
  };
24
33
  export interface GQLOption {
25
34
  enabled: boolean;
26
35
  queries: GQLQuery[];
27
- mutation: {
28
- create: MutationOption & Enabled;
29
- update: MutationOption & Enabled;
30
- delete: Omit<MutationOption, 'noReFetch'> & Enabled;
31
- fromContextColumns: GqlFromContextParam[];
32
- };
36
+ mutations: GQLMutation[];
33
37
  }
34
- export declare const defaultMutationOption: {
35
- enabled: boolean;
36
- noReFetch: boolean;
37
- subscription: boolean;
38
- subscriptionFilter: never[];
39
- };
40
- export declare const getDefaultGqlOption: () => GQLOption;
41
- export declare const updateMutationOption: (option: GQLOption, mutation: Partial<GQLOption['mutation']>) => GQLOption;
38
+ export declare const defaultGQLOption: () => GQLOption;
42
39
  export declare const getArgs: (query: GQLQuery, entity: EntityNode) => ArgQueryConditionValue[];
43
- export {};
@@ -1,28 +1,8 @@
1
- export const defaultMutationOption = {
2
- enabled: false,
3
- noReFetch: false,
4
- subscription: false,
5
- subscriptionFilter: [],
6
- };
7
- export const getDefaultGqlOption = () => ({
1
+ export const defaultGQLOption = () => ({
8
2
  enabled: false,
9
3
  queries: [],
10
- mutation: {
11
- create: defaultMutationOption,
12
- update: defaultMutationOption,
13
- delete: defaultMutationOption,
14
- fromContextColumns: [],
15
- },
4
+ mutations: [],
16
5
  });
17
- export const updateMutationOption = (option, mutation) => {
18
- return {
19
- ...option,
20
- mutation: {
21
- ...option.mutation,
22
- ...mutation,
23
- },
24
- };
25
- };
26
6
  export const getArgs = (query, entity) => {
27
7
  if (query.type === 'primary')
28
8
  return entity.fields
@@ -1,3 +1,4 @@
1
1
  import { StoreMigrator } from '../front/storeMigrator.js';
2
2
  import { Direction } from './getCurrentMigration.js';
3
- export declare const runMigration: (store: StoreMigrator, migrationName: string, direction: Direction) => Promise<void>;
3
+ import { MigrateCommandOption } from '../../cli/commands/migrate.js';
4
+ export declare const runMigration: (store: StoreMigrator, migrationName: string, direction: Direction, options: MigrateCommandOption) => Promise<void>;
@@ -1,9 +1,15 @@
1
1
  import { getDbClient } from '../../db/getDbClient.js';
2
2
  import { Console } from '../../cli/console.js';
3
3
  import { config } from '../../config/config.js';
4
- export const runMigration = async (store, migrationName, direction) => {
4
+ export const runMigration = async (store, migrationName, direction, options) => {
5
5
  const sqls = store.getSql();
6
6
  store.resetQueue();
7
+ if (!options.silent) {
8
+ sqls.forEach(Console.log);
9
+ }
10
+ if (options.dry) {
11
+ return;
12
+ }
7
13
  const transaction = await getDbClient().transaction();
8
14
  try {
9
15
  for (const sql of sqls) {
@@ -1,7 +1,7 @@
1
1
  import { StoreMigrator } from './storeMigrator.js';
2
2
  import { Table, TableHandler } from '../serializable/table.js';
3
3
  import { Reference, SerializedColumn, SerializedNormalColumn } from '../serialized/serializedColumn.js';
4
- import { GqlFromContextParam, GQLOption, MutationOption } from '../data/GQLOption.js';
4
+ import { GQLMutation, GQLOption, GQLQuery } from '../data/GQLOption.js';
5
5
  import { BaseColumn } from '../serializable/column.js';
6
6
  import { SerializedTable } from '../serialized/serializedStore.js';
7
7
  import { DBType } from '../column/columnTypes.js';
@@ -14,12 +14,10 @@ export interface MigrationTable extends Table {
14
14
  addForeignKey(reference: Reference): MigrationTable;
15
15
  changeColumnType(columnName: string, type: DBType): MigrationTable;
16
16
  setDefault(columnName: string, value: string | number | null): MigrationTable;
17
- setGQLCreate(enabled: boolean, options?: Partial<MutationOption>): MigrationTable;
18
- setGQLUpdate(enabled: boolean, options?: Partial<MutationOption>): MigrationTable;
19
- setGQLDelete(enabled: boolean, options?: Partial<Omit<MutationOption, 'noReFetch'>>): MigrationTable;
20
- setGQLContextColumn(columns: GqlFromContextParam[]): MigrationTable;
21
17
  enableGQL(): MigrationTable;
22
18
  setGQLOption(option: GQLOption): MigrationTable;
19
+ addGQLQuery(...queries: GQLQuery[]): MigrationTable;
20
+ addGQLMutation(...mutations: GQLMutation[]): MigrationTable;
23
21
  }
24
22
  export declare class TableMigrator implements MigrationTable {
25
23
  private table;
@@ -36,15 +34,13 @@ export declare class TableMigrator implements MigrationTable {
36
34
  removeIndex(...columns: string[]): MigrationTable;
37
35
  addColumn(column: SerializedNormalColumn): MigrationTable;
38
36
  dropColumn(columnName: string): MigrationTable;
39
- setGQLCreate(enabled: boolean, options?: Partial<MutationOption>): MigrationTable;
40
- setGQLUpdate(enabled: boolean, options?: Partial<MutationOption>): MigrationTable;
41
37
  enableGQL(): MigrationTable;
42
38
  setGQLOption(option: GQLOption): MigrationTable;
43
- setGQLDelete(enabled: boolean, options?: Partial<Omit<MutationOption, 'noReFetch'>>): MigrationTable;
44
- setGQLContextColumn(columns: GqlFromContextParam[]): MigrationTable;
45
39
  addForeignKey(reference: Reference): MigrationTable;
46
40
  changeColumnType(columnName: string, type: DBType): MigrationTable;
47
41
  protected tableExists(tableName: string): true;
48
42
  setDefault(columnName: string, value: string | number | null): MigrationTable;
49
43
  get gqlOption(): GQLOption;
44
+ addGQLQuery(...queries: GQLQuery[]): MigrationTable;
45
+ addGQLMutation(...mutations: GQLMutation[]): MigrationTable;
50
46
  }
@@ -51,14 +51,6 @@ export class TableMigrator {
51
51
  this.store.addQuery(SqlCreator.dropColumn(this.tableName, columnName));
52
52
  return this;
53
53
  }
54
- setGQLCreate(enabled, options) {
55
- this.table.setGQLCreate(enabled, options);
56
- return this;
57
- }
58
- setGQLUpdate(enabled, options) {
59
- this.table.setGQLUpdate(enabled, options);
60
- return this;
61
- }
62
54
  enableGQL() {
63
55
  this.table.setGQLOption({
64
56
  ...this.table.gqlOption,
@@ -70,14 +62,6 @@ export class TableMigrator {
70
62
  this.table.setGQLOption(option);
71
63
  return this;
72
64
  }
73
- setGQLDelete(enabled, options) {
74
- this.table.setGQLDelete(enabled, options);
75
- return this;
76
- }
77
- setGQLContextColumn(columns) {
78
- this.table.setGQLContextColumn(columns);
79
- return this;
80
- }
81
65
  addForeignKey(reference) {
82
66
  this.tableExists(reference.parentTable);
83
67
  this.table.addForeignKey(reference);
@@ -116,4 +100,18 @@ export class TableMigrator {
116
100
  get gqlOption() {
117
101
  return this.table.gqlOption;
118
102
  }
103
+ addGQLQuery(...queries) {
104
+ this.table.setGQLOption({
105
+ ...this.table.gqlOption,
106
+ queries: [...this.table.gqlOption.queries, ...queries],
107
+ });
108
+ return this;
109
+ }
110
+ addGQLMutation(...mutations) {
111
+ this.table.setGQLOption({
112
+ ...this.table.gqlOption,
113
+ mutations: [...this.table.gqlOption.mutations, ...mutations],
114
+ });
115
+ return this;
116
+ }
119
117
  }
@@ -0,0 +1,16 @@
1
+ import { GqlFromContextParam, GQLMutation } from './data/GQLOption.js';
2
+ type Option = {
3
+ noRefetch?: boolean;
4
+ middlewares?: string[];
5
+ contextFields?: GqlFromContextParam[];
6
+ subscription?: {
7
+ enabled: boolean;
8
+ subscriptionFilter?: string[];
9
+ } | boolean;
10
+ };
11
+ export declare const Mutations: {
12
+ create: (options?: Option) => GQLMutation;
13
+ update: (options?: Option) => GQLMutation;
14
+ delete: (options?: Omit<Option, 'noRefetch'>) => GQLMutation;
15
+ };
16
+ export {};
@@ -0,0 +1,29 @@
1
+ const formatSubscription = (subscription) => {
2
+ if (subscription === undefined || subscription === false) {
3
+ return {
4
+ enabled: false,
5
+ subscriptionFilter: [],
6
+ };
7
+ }
8
+ if (subscription === true)
9
+ return {
10
+ enabled: true,
11
+ subscriptionFilter: [],
12
+ };
13
+ return {
14
+ enabled: subscription.enabled,
15
+ subscriptionFilter: subscription.subscriptionFilter || [],
16
+ };
17
+ };
18
+ const formatOptions = (type, option) => ({
19
+ type,
20
+ noReFetch: option?.noRefetch || false,
21
+ middlewares: option?.middlewares || [],
22
+ contextFields: option?.contextFields || [],
23
+ subscription: formatSubscription(option?.subscription),
24
+ });
25
+ export const Mutations = {
26
+ create: (options) => formatOptions('create', options),
27
+ update: (options) => formatOptions('update', options),
28
+ delete: (options) => formatOptions('delete', options),
29
+ };
@@ -1,8 +1,17 @@
1
1
  import { QueryConditionNode } from '../generatorv2/nodes/QueryConditionNode.js';
2
2
  import { GQLQuery } from './data/GQLOption.js';
3
3
  export declare const Queries: {
4
- single: (name: string, conditions?: QueryConditionNode[]) => GQLQuery;
5
- listAll: (name: string, conditions?: QueryConditionNode[]) => GQLQuery;
6
- paging: (name: string, conditions?: QueryConditionNode[]) => GQLQuery;
7
- primary: GQLQuery;
4
+ single: (name: string, options?: {
5
+ conditions?: QueryConditionNode[];
6
+ middlewares?: string[];
7
+ }) => GQLQuery;
8
+ listAll: (name: string, options?: {
9
+ conditions?: QueryConditionNode[];
10
+ middlewares?: string[];
11
+ }) => GQLQuery;
12
+ paging: (name: string, options?: {
13
+ conditions?: QueryConditionNode[];
14
+ middlewares?: string[];
15
+ }) => GQLQuery;
16
+ primary: (middlewares?: string[]) => GQLQuery;
8
17
  };
@@ -1,25 +1,29 @@
1
- const single = (name, conditions) => ({
1
+ const single = (name, options) => ({
2
2
  type: 'single',
3
3
  name,
4
- conditions: conditions || [],
4
+ conditions: options?.conditions || [],
5
+ middlewares: options?.middlewares || [],
5
6
  });
6
- const listAll = (name, conditions) => ({
7
+ const listAll = (name, options) => ({
7
8
  type: 'list-all',
8
9
  name,
9
- conditions: conditions || [],
10
+ conditions: options?.conditions || [],
11
+ middlewares: options?.middlewares || [],
10
12
  });
11
- const paging = (name, conditions) => ({
13
+ const paging = (name, options) => ({
12
14
  type: 'list-paging',
13
15
  name,
14
- conditions: conditions || [],
16
+ conditions: options?.conditions || [],
17
+ middlewares: options?.middlewares || [],
15
18
  });
16
- const primary = () => ({
19
+ const primary = (middlewares = []) => ({
17
20
  type: 'primary',
18
21
  conditions: [],
22
+ middlewares,
19
23
  });
20
24
  export const Queries = {
21
25
  single,
22
26
  listAll,
23
27
  paging,
24
- primary: primary(),
28
+ primary,
25
29
  };
@@ -3,7 +3,7 @@ import { SerializedTable } from '../serialized/serializedStore.js';
3
3
  import { BaseColumn, ReferenceColumn } from './column.js';
4
4
  import { Reference, SerializedColumn } from '../serialized/serializedColumn.js';
5
5
  import { DBIndex } from '../data/index.js';
6
- import { GqlFromContextParam, GQLOption, MutationOption } from '../data/GQLOption.js';
6
+ import { GQLOption } from '../data/GQLOption.js';
7
7
  import { EntityName } from '../../generatorv2/nodes/entityName.js';
8
8
  import { DataStore } from '../dataStore.js';
9
9
  import { DBColumnTypes } from '../column/columnTypes.js';
@@ -43,11 +43,7 @@ export declare class TableHandler implements Table {
43
43
  hasColumn(columnName: string): boolean;
44
44
  isColumnPrimary(columnName: string): boolean;
45
45
  getEntityName(): EntityName;
46
- setGQLCreate(enabled: boolean, options?: Partial<MutationOption>): void;
47
- setGQLUpdate(enabled: boolean, options?: Partial<MutationOption>): void;
48
46
  setGQLOption(option: Partial<GQLOption>): void;
49
- setGQLDelete(enabled: boolean, options?: Partial<Omit<MutationOption, 'noReFetch'>>): void;
50
- setGQLContextColumn(columns: GqlFromContextParam[]): void;
51
47
  getReferenceColumns(): ReferenceColumn[];
52
48
  addForeignKey(reference: Reference): void;
53
49
  changeType(columnName: string, type: DBColumnTypes): void;
@@ -3,7 +3,7 @@ import { SqlString } from '../../runtime/sql/sqlString.js';
3
3
  import { SasatError } from '../../error.js';
4
4
  import { referenceToSql, } from '../serialized/serializedColumn.js';
5
5
  import { DBIndex } from '../data/index.js';
6
- import { defaultMutationOption, getDefaultGqlOption, updateMutationOption, } from '../data/GQLOption.js';
6
+ import { defaultGQLOption } from '../data/GQLOption.js';
7
7
  import { assembleColumn } from '../functions/assembleColumn.js';
8
8
  import { EntityName } from '../../generatorv2/nodes/entityName.js';
9
9
  export class TableHandler {
@@ -24,13 +24,13 @@ export class TableHandler {
24
24
  }
25
25
  constructor(table, store) {
26
26
  this.store = store;
27
- this._gqlOption = getDefaultGqlOption();
27
+ this._gqlOption = defaultGQLOption();
28
28
  this.tableName = table.tableName;
29
29
  this.primaryKey = table.primaryKey || [];
30
30
  this.uniqueKeys = table.uniqueKeys || [];
31
31
  this.indexes =
32
32
  table.indexes?.map(it => new DBIndex(this.tableName, it.columns)) || [];
33
- this._gqlOption = table.gqlOption || getDefaultGqlOption();
33
+ this._gqlOption = table.gqlOption || defaultGQLOption();
34
34
  this._columns = (table.columns || []).map(it => assembleColumn(it, this));
35
35
  this._virtualRelations = table.virtualRelations || [];
36
36
  }
@@ -133,41 +133,9 @@ export class TableHandler {
133
133
  getEntityName() {
134
134
  return EntityName.fromTableName(this.tableName);
135
135
  }
136
- setGQLCreate(enabled, options) {
137
- this._gqlOption = updateMutationOption(this._gqlOption, {
138
- create: {
139
- ...defaultMutationOption,
140
- ...(options || defaultMutationOption),
141
- enabled,
142
- },
143
- });
144
- }
145
- setGQLUpdate(enabled, options) {
146
- this._gqlOption = updateMutationOption(this._gqlOption, {
147
- update: {
148
- ...defaultMutationOption,
149
- ...(options || defaultMutationOption),
150
- enabled,
151
- },
152
- });
153
- }
154
136
  setGQLOption(option) {
155
137
  this._gqlOption = { ...this.gqlOption, ...option };
156
138
  }
157
- setGQLDelete(enabled, options) {
158
- this._gqlOption = updateMutationOption(this._gqlOption, {
159
- delete: {
160
- ...defaultMutationOption,
161
- ...(options || defaultMutationOption),
162
- enabled,
163
- },
164
- });
165
- }
166
- setGQLContextColumn(columns) {
167
- this._gqlOption = updateMutationOption(this._gqlOption, {
168
- fromContextColumns: columns,
169
- });
170
- }
171
139
  getReferenceColumns() {
172
140
  return this.columns.filter(it => it.isReference());
173
141
  }
@@ -1,2 +1,2 @@
1
1
  import { ResolverArgs } from './makeResolver.js';
2
- export type ResolverMiddleware<Context, RequiredParams = unknown, IncomingParams = RequiredParams> = (args: ResolverArgs<Context, IncomingParams | RequiredParams>) => ResolverArgs<Context, RequiredParams | IncomingParams>;
2
+ export type ResolverMiddleware<Context, RequiredParams = any, IncomingParams = RequiredParams> = (args: ResolverArgs<Context, IncomingParams | RequiredParams>) => ResolverArgs<Context, RequiredParams | IncomingParams>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasat",
3
- "version": "0.19.13",
3
+ "version": "0.19.14",
4
4
  "repository": "https://github.com/nin138/sasat.git",
5
5
  "author": "nin138 <ninian138@gmail.com>",
6
6
  "license": "MIT",
@@ -35,31 +35,31 @@
35
35
  "graphql-subscriptions": "^2.0.0",
36
36
  "hashids": "^2.2.10",
37
37
  "js-yaml": "^4.1.0",
38
- "mysql2": "^2.3.3",
38
+ "mysql2": "^3.1.2",
39
39
  "pluralize": "^8.0.0",
40
40
  "prettier": "^2.7.1",
41
41
  "sqlstring": "^2.3.3",
42
- "typescript": "^4.8.2"
42
+ "typescript": "^4.9.5"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@apollo/server": "^4.3.0",
46
46
  "@types/fs-extra": "11.0.1",
47
- "@types/jest": "29.2.5",
47
+ "@types/jest": "29.4.0",
48
48
  "@types/js-yaml": "4.0.5",
49
- "@types/node": "18.11.18",
49
+ "@types/node": "18.13.0",
50
50
  "@types/pluralize": "0.0.29",
51
51
  "@types/prettier": "2.7.2",
52
52
  "@types/sqlstring": "2.3.0",
53
- "@typescript-eslint/eslint-plugin": "5.48.2",
54
- "@typescript-eslint/parser": "5.48.2",
53
+ "@typescript-eslint/eslint-plugin": "5.51.0",
54
+ "@typescript-eslint/parser": "5.51.0",
55
55
  "env-cmd": "10.1.0",
56
- "eslint": "8.32.0",
56
+ "eslint": "8.33.0",
57
57
  "eslint-config-prettier": "8.6.0",
58
58
  "eslint-plugin-import": "^2.26.0",
59
59
  "eslint-plugin-prettier": "4.2.1",
60
60
  "husky": "8.0.3",
61
- "jest": "29.3.1",
62
- "lint-staged": "13.1.0",
61
+ "jest": "29.4.2",
62
+ "lint-staged": "13.1.1",
63
63
  "sasat": "link:./lib",
64
64
  "ts-jest": "29.0.5",
65
65
  "ts-node": "10.9.1",
@@ -1,3 +0,0 @@
1
- import { TsFile } from '../../../tsg/index.js';
2
- import { RootNode } from '../../nodes/rootNode.js';
3
- export declare const generateTypeDefs: (root: RootNode) => TsFile;
@@ -1,114 +0,0 @@
1
- import { TsFile, tsg } from '../../../tsg/index.js';
2
- import { nonNullableFilter } from '../../../util/type.js';
3
- import { GQLString, makeGQLType } from './gqlString.js';
4
- import { typeFieldDefinitionToTsg } from './typeDefinition.js';
5
- import { EntityName } from '../../nodes/entityName.js';
6
- import { getArgs } from '../../../migration/data/GQLOption.js';
7
- export const generateTypeDefs = (root) => {
8
- const types = [
9
- ...root.entities.map(makeEntityType),
10
- makeQuery(root),
11
- makeMutation(root.mutations),
12
- makeSubscription(root.subscriptions.filter(it => it.gqlEnabled)),
13
- ].filter(nonNullableFilter);
14
- const inputs = [
15
- tsg.propertyAssign('PagingOption', tsg.object(tsg.propertyAssign('numberOfItem', typeFieldDefinitionToTsg({ return: 'Int!' })), tsg.propertyAssign('offset', typeFieldDefinitionToTsg({ return: 'Int' })), tsg.propertyAssign('order', typeFieldDefinitionToTsg({ return: 'String' })), tsg.propertyAssign('asc', typeFieldDefinitionToTsg({ return: 'Boolean' })))),
16
- ...root.entities.map(makeCreateInput),
17
- ...root.entities.map(makeUpdateInput),
18
- ].filter(nonNullableFilter);
19
- return new TsFile(tsg
20
- .variable('const', tsg.identifier('typeDefs'), tsg.object(...types))
21
- .export(), tsg
22
- .variable('const', tsg.identifier('inputs'), tsg.object(...inputs))
23
- .export()).disableEsLint();
24
- };
25
- const makeEntityType = (node) => {
26
- if (!node.gqlEnabled)
27
- return null;
28
- return tsg.propertyAssign(node.name.name, tsg.object(...node.fields
29
- .filter(it => it.isGQLOpen)
30
- .map(it => {
31
- return tsg.propertyAssign(it.fieldName, typeFieldDefinitionToTsg({
32
- return: makeGQLType(it.gqlType, it.isNullable, it.isArray),
33
- }));
34
- }), ...node.references
35
- .filter(it => it.isGQLOpen)
36
- .map(it => {
37
- return tsg.propertyAssign(it.fieldName, typeFieldDefinitionToTsg({
38
- return: makeGQLType(EntityName.fromTableName(it.parentTableName).name, it.isNullable, it.isArray),
39
- }));
40
- }), ...node.referencedBy
41
- .filter(it => it.isGQLOpen)
42
- .map(it => {
43
- return tsg.propertyAssign(it.fieldName, typeFieldDefinitionToTsg({
44
- return: makeGQLType(EntityName.fromTableName(it.childTable).name, it.isNullable, it.isArray),
45
- }));
46
- })));
47
- };
48
- const makeInput = (inputName, fields) => {
49
- return tsg.propertyAssign(inputName, tsg.object(...fields
50
- .filter(it => it.isGQLOpen)
51
- .map(it => tsg.propertyAssign(it.fieldName, typeFieldDefinitionToTsg({
52
- return: makeGQLType(it.gqlType, it.isNullable, it.isArray),
53
- })))));
54
- };
55
- const makeCreateInput = (node) => {
56
- if (!node.gqlEnabled || !node.creatable.gqlEnabled)
57
- return null;
58
- return makeInput(node.name.createInputName(), node.creatable.fields);
59
- };
60
- const makeUpdateInput = (node) => {
61
- if (!node.gqlEnabled || !node.updateInput.gqlEnabled)
62
- return null;
63
- return makeInput(node.name.updateInputName(), node.updateInput.fields);
64
- };
65
- const makeQueryTypeDef = (entity, query) => {
66
- const args = getArgs(query, entity);
67
- return tsg.propertyAssign(query.type === 'primary' ? entity.name.lowerCase() : query.name, typeFieldDefinitionToTsg({
68
- return: GQLString.type({
69
- typeName: entity.name.name,
70
- entity: true,
71
- array: query.type === 'list-paging' || query.type === 'list-all',
72
- nullable: query.type !== 'list-paging' && query.type !== 'list-all',
73
- }),
74
- args: args.map(it => ({
75
- name: it.name,
76
- type: it.type + '!',
77
- })),
78
- }));
79
- };
80
- const makeQueryProperties = (root) => {
81
- return root.entities.flatMap(entity => entity.queries.map(it => makeQueryTypeDef(entity, it)));
82
- };
83
- const makeQuery = (root) => {
84
- const properties = makeQueryProperties(root);
85
- if (properties.length === 0)
86
- return null;
87
- return tsg.propertyAssign('Query', tsg.object(...properties));
88
- };
89
- const makeMutation = (mutations) => {
90
- if (mutations.length === 0)
91
- return null;
92
- return tsg.propertyAssign('Mutation', tsg.object(...mutations.map(mutation => {
93
- return tsg.propertyAssign(mutation.mutationName, typeFieldDefinitionToTsg({
94
- return: GQLString.type(mutation.returnType),
95
- args: mutation.args.map(arg => ({
96
- name: arg.name,
97
- type: GQLString.type(arg.type),
98
- })),
99
- }));
100
- })));
101
- };
102
- const makeSubscription = (subscriptions) => {
103
- if (subscriptions.length === 0)
104
- return null;
105
- return tsg.propertyAssign('Subscription', tsg.object(...subscriptions.map(subscription => {
106
- return tsg.propertyAssign(subscription.subscriptionName, typeFieldDefinitionToTsg({
107
- return: GQLString.type(subscription.returnType),
108
- args: subscription.args.map(arg => ({
109
- name: arg.name,
110
- type: GQLString.type(arg.type),
111
- })),
112
- }));
113
- })));
114
- };
@@ -1,16 +0,0 @@
1
- import { ArgNode, TypeNode } from '../../nodes/typeNode.js';
2
- import { FieldNode, ReferencedNode, ReferenceNode } from '../../nodes/entityNode.js';
3
- import { MutationNode } from '../../nodes/mutationNode.js';
4
- import { SubscriptionNode } from '../../nodes/subscriptionNode.js';
5
- import { QueryNode } from '../../nodes/queryNode.js';
6
- export declare const GQLString: {
7
- args: (args: ArgNode[]) => string;
8
- field: (field: FieldNode) => string;
9
- referenceField: (ref: ReferenceNode) => string;
10
- referencedField: (ref: ReferencedNode) => string;
11
- query: (node: QueryNode) => string;
12
- mutation: (node: MutationNode) => string;
13
- subscription: (node: SubscriptionNode) => string;
14
- type: (node: TypeNode) => string;
15
- };
16
- export declare const makeGQLType: (typeName: string, isNullable: boolean, isArray: boolean) => string;
@@ -1,43 +0,0 @@
1
- import { EntityName } from '../../nodes/entityName.js';
2
- export const GQLString = {
3
- args: (args) => {
4
- if (args.length === 0)
5
- return '';
6
- return `(${args
7
- .map(arg => `${arg.name}: ${GQLString.type(arg.type)}`)
8
- .join(',')})`;
9
- },
10
- field: (field) => {
11
- return `${field.fieldName}: ${fieldGqlType(field)}`;
12
- },
13
- referenceField: (ref) => {
14
- return `${ref.fieldName}: ${makeGQLType(EntityName.fromTableName(ref.parentTableName).name, ref.isNullable, ref.isArray)}`;
15
- },
16
- referencedField: (ref) => {
17
- return `${ref.fieldName}: ${makeGQLType(EntityName.fromTableName(ref.childTable).name, ref.isNullable, ref.isArray)}`;
18
- },
19
- query: (node) => {
20
- return `${node.queryName}${GQLString.args(node.args)}: ${GQLString.type(node.returnType)}`;
21
- },
22
- mutation: (node) => {
23
- return `${node.mutationName}${GQLString.args(node.args)}: ${GQLString.type(node.returnType)}`;
24
- },
25
- subscription: (node) => {
26
- return `${node.subscriptionName}${GQLString.args(node.args)}: ${GQLString.type(node.returnType)}`;
27
- },
28
- type: (node) => {
29
- const type = node.nullable ? node.typeName : node.typeName + '!';
30
- if (node.array)
31
- return `[${type}]!`;
32
- return type;
33
- },
34
- };
35
- const fieldGqlType = (field) => {
36
- return makeGQLType(field.gqlType, field.isNullable, field.isArray);
37
- };
38
- export const makeGQLType = (typeName, isNullable, isArray) => {
39
- const type = isNullable ? typeName : typeName + '!';
40
- if (isArray)
41
- return `[${type}]!`;
42
- return type;
43
- };
@@ -1,9 +0,0 @@
1
- import { ObjectLiteral } from '../../../tsg/index.js';
2
- export declare type TypeFieldDefinition = {
3
- return: string;
4
- args?: {
5
- name: string;
6
- type: string;
7
- }[];
8
- };
9
- export declare const typeFieldDefinitionToTsg: (def: TypeFieldDefinition) => ObjectLiteral;
@@ -1,13 +0,0 @@
1
- import { tsg } from '../../../tsg/index.js';
2
- import { nonNullableFilter } from '../../../util/type.js';
3
- export const typeFieldDefinitionToTsg = (def) => {
4
- const properties = [
5
- tsg.propertyAssign('return', tsg.string(def.return)),
6
- def.args
7
- ? tsg.propertyAssign('args', tsg.array(def.args.map(it => {
8
- return tsg.object(tsg.propertyAssign('name', tsg.string(it.name)), tsg.propertyAssign('type', tsg.string(it.type)));
9
- })))
10
- : null,
11
- ];
12
- return tsg.object(...properties.filter(nonNullableFilter));
13
- };
@@ -1,2 +0,0 @@
1
- import { EntityNode, ReferencedNode, ReferenceNode } from '../../../nodes/entityNode.js';
2
- export declare const makeCondition: (node: EntityNode, ref: ReferenceNode | ReferencedNode) => import("../../../../tsg/index.js").PropertyAssignment;