/
TypeORMConverter.ts
476 lines (410 loc) · 13.7 KB
/
TypeORMConverter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import {
GraphQLBoolean,
GraphQLEnumType,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLScalarType,
GraphQLString
} from 'graphql';
import { GraphQLISODateTime } from 'type-graphql';
import { EntityMetadata } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import { UniqueMetadata } from 'typeorm/metadata/UniqueMetadata';
import { getMetadataStorage } from '../metadata';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { GraphQLJSONObject } = require('graphql-type-json');
const SYSTEM_FIELDS = [
'createdAt',
'createdById',
'updatedAt',
'updatedById',
'deletedAt',
'deletedById'
];
function uniquesForEntity(entity: EntityMetadata): string[] {
return entity.uniques.reduce<string[]>(
(arr, unique: UniqueMetadata) => {
return [...arr, ...unique.columns.map((col: ColumnMetadata) => col.propertyName)];
},
[] as string[]
);
}
export function filenameToImportPath(filename: string): string {
return filename.replace(/\.(j|t)s$/, '').replace(/\\/g, '/');
}
export function extractEnumObject(column: ColumnMetadata): GraphQLEnumType {
return getMetadataStorage().getEnum(column.entityMetadata.name, column.propertyName);
}
export function columnToGraphQLType(column: ColumnMetadata): GraphQLScalarType | GraphQLEnumType {
// Check to see if this column is an enum and return that
if (column.enum) {
return extractEnumObject(column);
} else if (column.propertyName.match(/Id$/)) {
return GraphQLID;
}
// Some types have a name attribute
const type = (column.type as any).name ? (column.type as any).name : column.type;
switch (type) {
// case String:
case 'String':
case 'varchar':
case 'text':
case 'uuid':
return GraphQLString;
// case Boolean:
// case 'Boolean':
case 'boolean':
case 'bool':
return GraphQLBoolean;
// case Number:
// case 'Number':
case 'float':
case 'float4':
case 'float8':
case 'smallmoney':
case 'money':
case 'double':
case 'dec':
case 'decimal':
case 'fixed':
case 'numeric':
case 'real':
case 'double precision':
return GraphQLFloat;
case 'int':
case 'smallint':
case 'mediumint':
case 'bigint':
case 'integer':
case 'int2':
case 'int4':
case 'int8':
return GraphQLInt;
// case Date:
// case 'Date':
// case 'timestamp':
case 'datetime':
return GraphQLISODateTime;
case 'json':
case 'jsonb':
case 'varying character': // HACK: allows us to generate proper types for sqlite (mock db)
return GraphQLJSONObject;
default:
if (type instanceof GraphQLScalarType) {
return type;
}
throw new Error(`convertToGraphQLType: Unexpected type: ${type}`);
}
}
export function columnTypeToGraphQLDataType(column: ColumnMetadata): string {
const graphQLType = columnToGraphQLType(column);
// Sometimes we want to return the full blow GraphQL data type, but sometimes we want to return
// the more readable name. Ex:
// GraphQLInt -> Int
// GraphQLJSONObject -> GraphQLJSONObject
switch (graphQLType) {
case GraphQLJSONObject:
return 'GraphQLJSONObject';
default:
return graphQLType.name;
}
}
// const ID_TYPE = 'ID';
export function columnToTypeScriptType(column: ColumnMetadata): string {
if (column.isPrimary) {
return 'string'; // TODO: should this be ID_TYPE?
} else if (column.enum) {
return extractEnumObject(column).name;
} else {
const graphqlType = columnTypeToGraphQLDataType(column);
const typeMap: any = {
Boolean: 'boolean',
DateTime: 'Date',
Float: 'number',
GraphQLJSONObject: 'JSON',
ID: 'string', // TODO: should this be ID_TYPE?
Int: 'number',
String: 'string'
};
return typeMap[graphqlType] || 'string';
}
}
export function generateEnumMapImports(): string[] {
const imports: string[] = [];
const enumMap = getMetadataStorage().enumMap;
Object.keys(enumMap).forEach((tableName: string) => {
Object.keys(enumMap[tableName]).forEach((columnName: string) => {
const enumColumn = enumMap[tableName][columnName];
const filename = filenameToImportPath(enumColumn.filename);
imports.push(`import { ${enumColumn.name} } from '${filename}'
`);
});
});
const classMap = getMetadataStorage().classMap;
Object.keys(classMap).forEach((tableName: string) => {
const classObj = classMap[tableName];
const filename = filenameToImportPath(classObj.filename);
// Need to ts-ignore here for when we export compiled code
// otherwise, it says we can't find a declaration file for this from the compiled code
imports.push('// @ts-ignore\n');
imports.push(`import { ${classObj.name} } from '${filename}'
`);
});
return imports;
}
export function entityToWhereUniqueInput(entity: EntityMetadata): string {
const uniques = uniquesForEntity(entity);
const numUniques = entity.columns.reduce<number>((num, column: ColumnMetadata) => {
if (uniques.includes(column.propertyName) || column.isPrimary) {
num++;
}
return num;
}, 0);
// If there is only one unique field, it should not be nullable
const uniqueFieldsAreNullable = numUniques > 1;
let fieldsTemplate = '';
entity.columns.forEach((column: ColumnMetadata) => {
if (uniques.includes(column.propertyName) || column.isPrimary) {
const nullable = uniqueFieldsAreNullable ? ', { nullable: true }' : '';
const graphQLDataType = columnTypeToGraphQLDataType(column);
const tsType = columnToTypeScriptType(column);
fieldsTemplate += `
@TypeGraphQLField(() => ${graphQLDataType}${nullable})
${column.propertyName}?: ${tsType};
`;
}
});
const template = `
@TypeGraphQLInputType()
export class ${entity.name}WhereUniqueInput {
${fieldsTemplate}
}
`;
return template;
}
export function entityToCreateInput(entity: EntityMetadata): string {
let fieldTemplates = '';
entity.columns.forEach((column: ColumnMetadata) => {
if (
!column.isPrimary &&
!column.isCreateDate &&
!column.isGenerated &&
!column.isUpdateDate &&
!column.isVersion &&
!SYSTEM_FIELDS.includes(column.propertyName)
) {
const graphQLDataType = columnTypeToGraphQLDataType(column);
const nullable = column.isNullable ? '{ nullable: true }' : '';
const tsRequired = column.isNullable ? '?' : '!';
const tsType = columnToTypeScriptType(column);
// we need to know what the graphql type is and what the tsType is
// for enums
if (
column.enum ||
column.type === 'json' ||
column.type === 'jsonb' ||
column.type === 'varying character'
) {
fieldTemplates += `
@TypeGraphQLField(() => ${graphQLDataType}, ${nullable})
${column.propertyName}${tsRequired}: ${tsType};
`;
} else {
fieldTemplates += `
@TypeGraphQLField(${nullable})
${column.propertyName}${tsRequired}: ${tsType};
`;
}
}
});
return `
@TypeGraphQLInputType()
export class ${entity.name}CreateInput {
${fieldTemplates}
}
`;
}
export function entityToUpdateInput(entity: EntityMetadata): string {
let fieldTemplates = '';
entity.columns.forEach((column: ColumnMetadata) => {
if (
!column.isPrimary &&
!column.isCreateDate &&
!column.isGenerated &&
!column.isUpdateDate &&
!column.isVersion &&
!SYSTEM_FIELDS.includes(column.propertyName)
) {
// TODO: also don't allow updated foreign key fields
// Example: photo.userId: String
const graphQLDataType = columnTypeToGraphQLDataType(column);
const tsType = columnToTypeScriptType(column);
if (
column.enum ||
column.type === 'json' ||
column.type === 'jsonb' ||
column.type === 'varying character'
) {
fieldTemplates += `
@TypeGraphQLField(() => ${graphQLDataType}, { nullable: true })
${column.propertyName}?: ${tsType};
`;
} else {
fieldTemplates += `
@TypeGraphQLField({ nullable: true })
${column.propertyName}?: ${tsType};
`;
}
}
});
return `
@TypeGraphQLInputType()
export class ${entity.name}UpdateInput {
${fieldTemplates}
}
`;
}
// Constructs required arguments needed when doing an update
export function entityToUpdateInputArgs(entity: EntityMetadata): string {
return `
@ArgsType()
export class ${entity.name}UpdateArgs {
@TypeGraphQLField() data!: ${entity.name}UpdateInput;
@TypeGraphQLField() where!: ${entity.name}WhereUniqueInput;
}
`;
}
function columnToTypes(column: ColumnMetadata) {
const graphqlType = columnToGraphQLType(column);
const tsType = columnToTypeScriptType(column);
return { graphqlType, tsType };
}
export function entityToWhereInput(entity: EntityMetadata): string {
let fieldTemplates = '';
entity.columns.forEach((column: ColumnMetadata) => {
// Don't allow filtering on these fields
if (column.isPrimary || column.isVersion || SYSTEM_FIELDS.includes(column.propertyName)) {
return;
}
const { graphqlType, tsType } = columnToTypes(column);
const graphQLDataType = columnTypeToGraphQLDataType(column);
// TODO: for foreign key fields, only allow the same filters as ID below
// Example: photo.userId: String
if (column.isPrimary || graphqlType === GraphQLID) {
fieldTemplates += `
@TypeGraphQLField(() => ${graphQLDataType},{ nullable: true })
${column.propertyName}_eq?: string;
@TypeGraphQLField(() => [${graphQLDataType}], { nullable: true })
${column.propertyName}_in?: string[];
`;
} else if (graphqlType === GraphQLString) {
// TODO: do we need NOT?
// `${column.propertyName}_not`
fieldTemplates += `
@TypeGraphQLField({ nullable: true })
${column.propertyName}_eq?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_contains?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_startsWith?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_endsWith?: ${tsType};
@TypeGraphQLField(() => [${graphQLDataType}], { nullable: true })
${column.propertyName}_in?: ${tsType}[];
`;
} else if (graphqlType === GraphQLFloat || graphqlType === GraphQLInt) {
fieldTemplates += `
@TypeGraphQLField({ nullable: true })
${column.propertyName}_eq?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_gt?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_gte?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_lt?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_lte?: ${tsType};
@TypeGraphQLField(() => [${graphQLDataType}], { nullable: true })
${column.propertyName}_in?: ${tsType}[];
`;
} else if (graphqlType === GraphQLISODateTime) {
fieldTemplates += `
@TypeGraphQLField({ nullable: true })
${column.propertyName}_gt?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_gte?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_lt?: ${tsType};
@TypeGraphQLField({ nullable: true })
${column.propertyName}_lte?: ${tsType};
`;
} else if (
column.type !== 'json' &&
column.type !== 'jsonb' &&
column.type !== 'varying character'
) {
// @@@ dcaddigan not sure what it means to search by JSONObjects
// future release?
// Enums will fall through here
fieldTemplates += `
@TypeGraphQLField(() => ${graphQLDataType}, { nullable: true })
${column.propertyName}_eq?: ${graphQLDataType};
@TypeGraphQLField(() => [${graphQLDataType}], { nullable: true })
${column.propertyName}_in?: ${graphQLDataType}[];
`;
}
});
return `
@TypeGraphQLInputType()
export class ${entity.name}WhereInput extends BaseWhereInput {
${fieldTemplates}
}
`;
}
export function entityToWhereArgs(entity: EntityMetadata): string {
return `
@ArgsType()
export class ${entity.name}WhereArgs extends PaginationArgs {
@TypeGraphQLField(() => ${entity.name}WhereInput, { nullable: true })
where?: ${entity.name}WhereInput;
@TypeGraphQLField(() => ${entity.name}OrderByEnum, { nullable: true })
orderBy?: ${entity.name}OrderByEnum;
}
`;
}
// Note: it would be great to inject a single `Arg` with the [entity.nameCreateInput] array arg,
// but that is not allowed by TypeGraphQL
export function entityToCreateManyArgs(entity: EntityMetadata): string {
return `
@ArgsType()
export class ${entity.name}CreateManyArgs {
@TypeGraphQLField(() => [${entity.name}CreateInput])
data!: ${entity.name}CreateInput[];
}
`;
}
export function entityToOrderByEnum(entity: EntityMetadata): string {
const ORDER_BY_BLACKLIST = ['createdById', 'updatedById', 'deletedById'];
let fieldsTemplate = '';
entity.columns.forEach((column: ColumnMetadata) => {
if (
!column.isPrimary &&
!column.isVersion &&
!ORDER_BY_BLACKLIST.includes(column.propertyName)
) {
fieldsTemplate += `
${column.propertyName}_ASC = '${column.propertyName}_ASC',
${column.propertyName}_DESC = '${column.propertyName}_DESC',
`;
}
});
return `
export enum ${entity.name}OrderByEnum {
${fieldsTemplate}
}
registerEnumType(${entity.name}OrderByEnum, {
name: '${entity.name}OrderByInput'
});
`;
}