@constructive-io/graphql-codegen 2.23.3 → 2.24.1
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.
- package/README.md +147 -2
- package/cli/codegen/babel-ast.d.ts +53 -0
- package/cli/codegen/babel-ast.js +160 -0
- package/cli/codegen/barrel.d.ts +7 -2
- package/cli/codegen/barrel.js +193 -102
- package/cli/codegen/client.js +61 -0
- package/cli/codegen/custom-mutations.d.ts +2 -12
- package/cli/codegen/custom-mutations.js +116 -124
- package/cli/codegen/custom-queries.d.ts +2 -10
- package/cli/codegen/custom-queries.js +236 -335
- package/cli/codegen/gql-ast.js +22 -1
- package/cli/codegen/index.d.ts +3 -0
- package/cli/codegen/index.js +73 -3
- package/cli/codegen/invalidation.d.ts +20 -0
- package/cli/codegen/invalidation.js +327 -0
- package/cli/codegen/mutation-keys.d.ts +24 -0
- package/cli/codegen/mutation-keys.js +247 -0
- package/cli/codegen/mutations.d.ts +5 -19
- package/cli/codegen/mutations.js +385 -383
- package/cli/codegen/orm/barrel.d.ts +1 -1
- package/cli/codegen/orm/barrel.js +42 -10
- package/cli/codegen/orm/client-generator.d.ts +1 -19
- package/cli/codegen/orm/client-generator.js +108 -77
- package/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
- package/cli/codegen/orm/custom-ops-generator.js +192 -235
- package/cli/codegen/orm/input-types-generator.d.ts +13 -1
- package/cli/codegen/orm/input-types-generator.js +425 -147
- package/cli/codegen/orm/model-generator.d.ts +1 -19
- package/cli/codegen/orm/model-generator.js +229 -234
- package/cli/codegen/queries.d.ts +4 -12
- package/cli/codegen/queries.js +660 -390
- package/cli/codegen/query-keys.d.ts +15 -0
- package/cli/codegen/query-keys.js +477 -0
- package/cli/codegen/scalars.js +1 -0
- package/cli/codegen/schema-types-generator.d.ts +15 -10
- package/cli/codegen/schema-types-generator.js +87 -175
- package/cli/codegen/type-resolver.d.ts +1 -30
- package/cli/codegen/type-resolver.js +0 -53
- package/cli/codegen/types.d.ts +1 -1
- package/cli/codegen/types.js +76 -21
- package/cli/codegen/utils.d.ts +6 -0
- package/cli/codegen/utils.js +19 -0
- package/esm/cli/codegen/babel-ast.d.ts +53 -0
- package/esm/cli/codegen/babel-ast.js +111 -0
- package/esm/cli/codegen/barrel.d.ts +7 -2
- package/esm/cli/codegen/barrel.js +161 -103
- package/esm/cli/codegen/client.js +61 -0
- package/esm/cli/codegen/custom-mutations.d.ts +2 -12
- package/esm/cli/codegen/custom-mutations.js +83 -124
- package/esm/cli/codegen/custom-queries.d.ts +2 -10
- package/esm/cli/codegen/custom-queries.js +204 -336
- package/esm/cli/codegen/gql-ast.js +23 -2
- package/esm/cli/codegen/index.d.ts +3 -0
- package/esm/cli/codegen/index.js +69 -2
- package/esm/cli/codegen/invalidation.d.ts +20 -0
- package/esm/cli/codegen/invalidation.js +291 -0
- package/esm/cli/codegen/mutation-keys.d.ts +24 -0
- package/esm/cli/codegen/mutation-keys.js +211 -0
- package/esm/cli/codegen/mutations.d.ts +5 -19
- package/esm/cli/codegen/mutations.js +353 -384
- package/esm/cli/codegen/orm/barrel.d.ts +1 -1
- package/esm/cli/codegen/orm/barrel.js +10 -11
- package/esm/cli/codegen/orm/client-generator.d.ts +1 -19
- package/esm/cli/codegen/orm/client-generator.js +76 -78
- package/esm/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
- package/esm/cli/codegen/orm/custom-ops-generator.js +160 -236
- package/esm/cli/codegen/orm/input-types-generator.d.ts +13 -1
- package/esm/cli/codegen/orm/input-types-generator.js +393 -148
- package/esm/cli/codegen/orm/model-generator.d.ts +1 -19
- package/esm/cli/codegen/orm/model-generator.js +197 -235
- package/esm/cli/codegen/queries.d.ts +4 -12
- package/esm/cli/codegen/queries.js +628 -391
- package/esm/cli/codegen/query-keys.d.ts +15 -0
- package/esm/cli/codegen/query-keys.js +441 -0
- package/esm/cli/codegen/scalars.js +1 -0
- package/esm/cli/codegen/schema-types-generator.d.ts +15 -10
- package/esm/cli/codegen/schema-types-generator.js +54 -175
- package/esm/cli/codegen/type-resolver.d.ts +1 -30
- package/esm/cli/codegen/type-resolver.js +0 -49
- package/esm/cli/codegen/types.d.ts +1 -1
- package/esm/cli/codegen/types.js +44 -22
- package/esm/cli/codegen/utils.d.ts +6 -0
- package/esm/cli/codegen/utils.js +18 -0
- package/esm/types/config.d.ts +75 -0
- package/esm/types/config.js +18 -0
- package/package.json +6 -4
- package/types/config.d.ts +75 -0
- package/types/config.js +19 -1
- package/cli/codegen/ts-ast.d.ts +0 -124
- package/cli/codegen/ts-ast.js +0 -280
- package/esm/cli/codegen/ts-ast.d.ts +0 -124
- package/esm/cli/codegen/ts-ast.js +0 -260
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Model class generator for ORM client
|
|
2
|
+
* Model class generator for ORM client (Babel AST-based)
|
|
3
3
|
*
|
|
4
4
|
* Generates per-table model classes with findMany, findFirst, create, update, delete methods.
|
|
5
|
-
*
|
|
6
|
-
* Example output:
|
|
7
|
-
* ```typescript
|
|
8
|
-
* export class UserModel {
|
|
9
|
-
* constructor(private client: OrmClient) {}
|
|
10
|
-
*
|
|
11
|
-
* findMany<S extends UserSelect>(args?: FindManyArgs<S, UserWhereInput, UserOrderBy>) {
|
|
12
|
-
* return new QueryBuilder<...>({ ... });
|
|
13
|
-
* }
|
|
14
|
-
* // ...
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
5
|
*/
|
|
18
6
|
import type { CleanTable } from '../../../types/schema';
|
|
19
7
|
export interface GeneratedModelFile {
|
|
@@ -22,11 +10,5 @@ export interface GeneratedModelFile {
|
|
|
22
10
|
modelName: string;
|
|
23
11
|
tableName: string;
|
|
24
12
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Generate a model class file for a table
|
|
27
|
-
*/
|
|
28
13
|
export declare function generateModelFile(table: CleanTable, _useSharedTypes: boolean): GeneratedModelFile;
|
|
29
|
-
/**
|
|
30
|
-
* Generate all model files for a list of tables
|
|
31
|
-
*/
|
|
32
14
|
export declare function generateAllModelFiles(tables: CleanTable[], useSharedTypes: boolean): GeneratedModelFile[];
|
|
@@ -1,17 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { generateCode } from '../babel-ast';
|
|
3
|
+
import { getTableNames, getOrderByTypeName, getFilterTypeName, lcFirst, getGeneratedFileHeader, } from '../utils';
|
|
4
|
+
function createImportDeclaration(moduleSpecifier, namedImports, typeOnly = false) {
|
|
5
|
+
const specifiers = namedImports.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name)));
|
|
6
|
+
const decl = t.importDeclaration(specifiers, t.stringLiteral(moduleSpecifier));
|
|
7
|
+
decl.importKind = typeOnly ? 'type' : 'value';
|
|
8
|
+
return decl;
|
|
9
|
+
}
|
|
10
|
+
function buildMethodBody(builderFn, args, operation, typeName, fieldName) {
|
|
11
|
+
const destructureDecl = t.variableDeclaration('const', [
|
|
12
|
+
t.variableDeclarator(t.objectPattern([
|
|
13
|
+
t.objectProperty(t.identifier('document'), t.identifier('document'), false, true),
|
|
14
|
+
t.objectProperty(t.identifier('variables'), t.identifier('variables'), false, true),
|
|
15
|
+
]), t.callExpression(t.identifier(builderFn), args)),
|
|
16
|
+
]);
|
|
17
|
+
const returnStmt = t.returnStatement(t.newExpression(t.identifier('QueryBuilder'), [
|
|
18
|
+
t.objectExpression([
|
|
19
|
+
t.objectProperty(t.identifier('client'), t.memberExpression(t.thisExpression(), t.identifier('client'))),
|
|
20
|
+
t.objectProperty(t.identifier('operation'), t.stringLiteral(operation)),
|
|
21
|
+
t.objectProperty(t.identifier('operationName'), t.stringLiteral(typeName)),
|
|
22
|
+
t.objectProperty(t.identifier('fieldName'), t.stringLiteral(fieldName)),
|
|
23
|
+
t.objectProperty(t.identifier('document'), t.identifier('document'), false, true),
|
|
24
|
+
t.objectProperty(t.identifier('variables'), t.identifier('variables'), false, true),
|
|
25
|
+
]),
|
|
26
|
+
]));
|
|
27
|
+
return [destructureDecl, returnStmt];
|
|
28
|
+
}
|
|
29
|
+
function createClassMethod(name, typeParameters, params, returnType, body) {
|
|
30
|
+
const method = t.classMethod('method', t.identifier(name), params, t.blockStatement(body));
|
|
31
|
+
method.typeParameters = typeParameters;
|
|
32
|
+
method.returnType = returnType;
|
|
33
|
+
return method;
|
|
34
|
+
}
|
|
35
|
+
function createConstTypeParam(constraintTypeName) {
|
|
36
|
+
const param = t.tsTypeParameter(t.tsTypeReference(t.identifier(constraintTypeName)), null, 'S');
|
|
37
|
+
param.const = true;
|
|
38
|
+
return t.tsTypeParameterDeclaration([param]);
|
|
39
|
+
}
|
|
6
40
|
export function generateModelFile(table, _useSharedTypes) {
|
|
7
|
-
const project = createProject();
|
|
8
41
|
const { typeName, singularName, pluralName } = getTableNames(table);
|
|
9
42
|
const modelName = `${typeName}Model`;
|
|
10
|
-
// Avoid "index.ts" which clashes with barrel file
|
|
11
43
|
const baseFileName = lcFirst(typeName);
|
|
12
44
|
const fileName = baseFileName === 'index' ? `${baseFileName}Model.ts` : `${baseFileName}.ts`;
|
|
13
45
|
const entityLower = singularName;
|
|
14
|
-
// Type names for this entity - use inflection from table metadata
|
|
15
46
|
const selectTypeName = `${typeName}Select`;
|
|
16
47
|
const relationTypeName = `${typeName}WithRelations`;
|
|
17
48
|
const whereTypeName = getFilterTypeName(table);
|
|
@@ -20,244 +51,175 @@ export function generateModelFile(table, _useSharedTypes) {
|
|
|
20
51
|
const updateInputTypeName = `Update${typeName}Input`;
|
|
21
52
|
const deleteInputTypeName = `Delete${typeName}Input`;
|
|
22
53
|
const patchTypeName = `${typeName}Patch`;
|
|
23
|
-
const createDataType = `${createInputTypeName}['${singularName}']`;
|
|
24
|
-
// Query names from PostGraphile
|
|
25
54
|
const pluralQueryName = table.query?.all ?? pluralName;
|
|
26
55
|
const createMutationName = table.query?.create ?? `create${typeName}`;
|
|
27
56
|
const updateMutationName = table.query?.update;
|
|
28
57
|
const deleteMutationName = table.query?.delete;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
'buildCreateDocument',
|
|
45
|
-
'buildUpdateDocument',
|
|
46
|
-
'buildDeleteDocument',
|
|
47
|
-
],
|
|
48
|
-
}),
|
|
49
|
-
createImport({
|
|
50
|
-
moduleSpecifier: '../select-types',
|
|
51
|
-
typeOnlyNamedImports: [
|
|
52
|
-
'ConnectionResult',
|
|
53
|
-
'FindManyArgs',
|
|
54
|
-
'FindFirstArgs',
|
|
55
|
-
'CreateArgs',
|
|
56
|
-
'UpdateArgs',
|
|
57
|
-
'DeleteArgs',
|
|
58
|
-
'InferSelectResult',
|
|
59
|
-
],
|
|
60
|
-
}),
|
|
61
|
-
]);
|
|
62
|
-
// Build complete set of input-types imports
|
|
63
|
-
// Select types are now centralized in input-types.ts with relations included
|
|
64
|
-
const inputTypeImports = new Set([
|
|
65
|
-
typeName,
|
|
66
|
-
relationTypeName,
|
|
67
|
-
selectTypeName,
|
|
68
|
-
whereTypeName,
|
|
69
|
-
orderByTypeName,
|
|
70
|
-
createInputTypeName,
|
|
71
|
-
updateInputTypeName,
|
|
72
|
-
patchTypeName,
|
|
73
|
-
]);
|
|
74
|
-
// Add single combined import from input-types
|
|
75
|
-
sourceFile.addImportDeclaration(createImport({
|
|
76
|
-
moduleSpecifier: '../input-types',
|
|
77
|
-
typeOnlyNamedImports: Array.from(inputTypeImports),
|
|
78
|
-
}));
|
|
79
|
-
// Add Model class
|
|
80
|
-
sourceFile.addStatements('\n// ============================================================================');
|
|
81
|
-
sourceFile.addStatements('// Model Class');
|
|
82
|
-
sourceFile.addStatements('// ============================================================================\n');
|
|
83
|
-
// Generate the model class
|
|
84
|
-
const classDeclaration = sourceFile.addClass({
|
|
85
|
-
name: modelName,
|
|
86
|
-
isExported: true,
|
|
87
|
-
});
|
|
58
|
+
const statements = [];
|
|
59
|
+
statements.push(createImportDeclaration('../client', ['OrmClient']));
|
|
60
|
+
statements.push(createImportDeclaration('../query-builder', [
|
|
61
|
+
'QueryBuilder', 'buildFindManyDocument', 'buildFindFirstDocument',
|
|
62
|
+
'buildCreateDocument', 'buildUpdateDocument', 'buildDeleteDocument',
|
|
63
|
+
]));
|
|
64
|
+
statements.push(createImportDeclaration('../select-types', [
|
|
65
|
+
'ConnectionResult', 'FindManyArgs', 'FindFirstArgs', 'CreateArgs',
|
|
66
|
+
'UpdateArgs', 'DeleteArgs', 'InferSelectResult',
|
|
67
|
+
], true));
|
|
68
|
+
statements.push(createImportDeclaration('../input-types', [
|
|
69
|
+
typeName, relationTypeName, selectTypeName, whereTypeName, orderByTypeName,
|
|
70
|
+
createInputTypeName, updateInputTypeName, patchTypeName,
|
|
71
|
+
], true));
|
|
72
|
+
const classBody = [];
|
|
88
73
|
// Constructor
|
|
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
|
-
return new QueryBuilder({
|
|
183
|
-
client: this.client,
|
|
184
|
-
operation: 'mutation',
|
|
185
|
-
operationName: '${typeName}',
|
|
186
|
-
fieldName: '${createMutationName}',
|
|
187
|
-
document,
|
|
188
|
-
variables,
|
|
189
|
-
});`,
|
|
190
|
-
});
|
|
191
|
-
// update method (if available) - uses const generic for proper literal type inference
|
|
74
|
+
const constructorParam = t.identifier('client');
|
|
75
|
+
constructorParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('OrmClient')));
|
|
76
|
+
const paramProp = t.tsParameterProperty(constructorParam);
|
|
77
|
+
paramProp.accessibility = 'private';
|
|
78
|
+
classBody.push(t.classMethod('constructor', t.identifier('constructor'), [paramProp], t.blockStatement([])));
|
|
79
|
+
// findMany method
|
|
80
|
+
const findManyParam = t.identifier('args');
|
|
81
|
+
findManyParam.optional = true;
|
|
82
|
+
findManyParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('FindManyArgs'), t.tsTypeParameterInstantiation([
|
|
83
|
+
t.tsTypeReference(t.identifier('S')),
|
|
84
|
+
t.tsTypeReference(t.identifier(whereTypeName)),
|
|
85
|
+
t.tsTypeReference(t.identifier(orderByTypeName)),
|
|
86
|
+
])));
|
|
87
|
+
const findManyReturnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('QueryBuilder'), t.tsTypeParameterInstantiation([
|
|
88
|
+
t.tsTypeLiteral([
|
|
89
|
+
t.tsPropertySignature(t.identifier(pluralQueryName), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ConnectionResult'), t.tsTypeParameterInstantiation([
|
|
90
|
+
t.tsTypeReference(t.identifier('InferSelectResult'), t.tsTypeParameterInstantiation([
|
|
91
|
+
t.tsTypeReference(t.identifier(relationTypeName)),
|
|
92
|
+
t.tsTypeReference(t.identifier('S')),
|
|
93
|
+
])),
|
|
94
|
+
])))),
|
|
95
|
+
]),
|
|
96
|
+
])));
|
|
97
|
+
const findManyArgs = [
|
|
98
|
+
t.stringLiteral(typeName),
|
|
99
|
+
t.stringLiteral(pluralQueryName),
|
|
100
|
+
t.optionalMemberExpression(t.identifier('args'), t.identifier('select'), false, true),
|
|
101
|
+
t.objectExpression([
|
|
102
|
+
t.objectProperty(t.identifier('where'), t.optionalMemberExpression(t.identifier('args'), t.identifier('where'), false, true)),
|
|
103
|
+
t.objectProperty(t.identifier('orderBy'), t.tsAsExpression(t.optionalMemberExpression(t.identifier('args'), t.identifier('orderBy'), false, true), t.tsUnionType([t.tsArrayType(t.tsStringKeyword()), t.tsUndefinedKeyword()]))),
|
|
104
|
+
t.objectProperty(t.identifier('first'), t.optionalMemberExpression(t.identifier('args'), t.identifier('first'), false, true)),
|
|
105
|
+
t.objectProperty(t.identifier('last'), t.optionalMemberExpression(t.identifier('args'), t.identifier('last'), false, true)),
|
|
106
|
+
t.objectProperty(t.identifier('after'), t.optionalMemberExpression(t.identifier('args'), t.identifier('after'), false, true)),
|
|
107
|
+
t.objectProperty(t.identifier('before'), t.optionalMemberExpression(t.identifier('args'), t.identifier('before'), false, true)),
|
|
108
|
+
t.objectProperty(t.identifier('offset'), t.optionalMemberExpression(t.identifier('args'), t.identifier('offset'), false, true)),
|
|
109
|
+
]),
|
|
110
|
+
t.stringLiteral(whereTypeName),
|
|
111
|
+
t.stringLiteral(orderByTypeName),
|
|
112
|
+
];
|
|
113
|
+
classBody.push(createClassMethod('findMany', createConstTypeParam(selectTypeName), [findManyParam], findManyReturnType, buildMethodBody('buildFindManyDocument', findManyArgs, 'query', typeName, pluralQueryName)));
|
|
114
|
+
// findFirst method
|
|
115
|
+
const findFirstParam = t.identifier('args');
|
|
116
|
+
findFirstParam.optional = true;
|
|
117
|
+
findFirstParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('FindFirstArgs'), t.tsTypeParameterInstantiation([
|
|
118
|
+
t.tsTypeReference(t.identifier('S')),
|
|
119
|
+
t.tsTypeReference(t.identifier(whereTypeName)),
|
|
120
|
+
])));
|
|
121
|
+
const findFirstReturnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('QueryBuilder'), t.tsTypeParameterInstantiation([
|
|
122
|
+
t.tsTypeLiteral([
|
|
123
|
+
t.tsPropertySignature(t.identifier(pluralQueryName), t.tsTypeAnnotation(t.tsTypeLiteral([
|
|
124
|
+
t.tsPropertySignature(t.identifier('nodes'), t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('InferSelectResult'), t.tsTypeParameterInstantiation([
|
|
125
|
+
t.tsTypeReference(t.identifier(relationTypeName)),
|
|
126
|
+
t.tsTypeReference(t.identifier('S')),
|
|
127
|
+
]))))),
|
|
128
|
+
]))),
|
|
129
|
+
]),
|
|
130
|
+
])));
|
|
131
|
+
const findFirstArgs = [
|
|
132
|
+
t.stringLiteral(typeName),
|
|
133
|
+
t.stringLiteral(pluralQueryName),
|
|
134
|
+
t.optionalMemberExpression(t.identifier('args'), t.identifier('select'), false, true),
|
|
135
|
+
t.objectExpression([
|
|
136
|
+
t.objectProperty(t.identifier('where'), t.optionalMemberExpression(t.identifier('args'), t.identifier('where'), false, true)),
|
|
137
|
+
]),
|
|
138
|
+
t.stringLiteral(whereTypeName),
|
|
139
|
+
];
|
|
140
|
+
classBody.push(createClassMethod('findFirst', createConstTypeParam(selectTypeName), [findFirstParam], findFirstReturnType, buildMethodBody('buildFindFirstDocument', findFirstArgs, 'query', typeName, pluralQueryName)));
|
|
141
|
+
// create method
|
|
142
|
+
const createParam = t.identifier('args');
|
|
143
|
+
createParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CreateArgs'), t.tsTypeParameterInstantiation([
|
|
144
|
+
t.tsTypeReference(t.identifier('S')),
|
|
145
|
+
t.tsIndexedAccessType(t.tsTypeReference(t.identifier(createInputTypeName)), t.tsLiteralType(t.stringLiteral(singularName))),
|
|
146
|
+
])));
|
|
147
|
+
const createReturnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('QueryBuilder'), t.tsTypeParameterInstantiation([
|
|
148
|
+
t.tsTypeLiteral([
|
|
149
|
+
t.tsPropertySignature(t.identifier(createMutationName), t.tsTypeAnnotation(t.tsTypeLiteral([
|
|
150
|
+
t.tsPropertySignature(t.identifier(entityLower), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('InferSelectResult'), t.tsTypeParameterInstantiation([
|
|
151
|
+
t.tsTypeReference(t.identifier(relationTypeName)),
|
|
152
|
+
t.tsTypeReference(t.identifier('S')),
|
|
153
|
+
])))),
|
|
154
|
+
]))),
|
|
155
|
+
]),
|
|
156
|
+
])));
|
|
157
|
+
const createArgs = [
|
|
158
|
+
t.stringLiteral(typeName),
|
|
159
|
+
t.stringLiteral(createMutationName),
|
|
160
|
+
t.stringLiteral(entityLower),
|
|
161
|
+
t.memberExpression(t.identifier('args'), t.identifier('select')),
|
|
162
|
+
t.memberExpression(t.identifier('args'), t.identifier('data')),
|
|
163
|
+
t.stringLiteral(createInputTypeName),
|
|
164
|
+
];
|
|
165
|
+
classBody.push(createClassMethod('create', createConstTypeParam(selectTypeName), [createParam], createReturnType, buildMethodBody('buildCreateDocument', createArgs, 'mutation', typeName, createMutationName)));
|
|
166
|
+
// update method (if available)
|
|
192
167
|
if (updateMutationName) {
|
|
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
|
-
});
|
|
168
|
+
const updateParam = t.identifier('args');
|
|
169
|
+
updateParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UpdateArgs'), t.tsTypeParameterInstantiation([
|
|
170
|
+
t.tsTypeReference(t.identifier('S')),
|
|
171
|
+
t.tsTypeLiteral([t.tsPropertySignature(t.identifier('id'), t.tsTypeAnnotation(t.tsStringKeyword()))]),
|
|
172
|
+
t.tsTypeReference(t.identifier(patchTypeName)),
|
|
173
|
+
])));
|
|
174
|
+
const updateReturnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('QueryBuilder'), t.tsTypeParameterInstantiation([
|
|
175
|
+
t.tsTypeLiteral([
|
|
176
|
+
t.tsPropertySignature(t.identifier(updateMutationName), t.tsTypeAnnotation(t.tsTypeLiteral([
|
|
177
|
+
t.tsPropertySignature(t.identifier(entityLower), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('InferSelectResult'), t.tsTypeParameterInstantiation([
|
|
178
|
+
t.tsTypeReference(t.identifier(relationTypeName)),
|
|
179
|
+
t.tsTypeReference(t.identifier('S')),
|
|
180
|
+
])))),
|
|
181
|
+
]))),
|
|
182
|
+
]),
|
|
183
|
+
])));
|
|
184
|
+
const updateArgs = [
|
|
185
|
+
t.stringLiteral(typeName),
|
|
186
|
+
t.stringLiteral(updateMutationName),
|
|
187
|
+
t.stringLiteral(entityLower),
|
|
188
|
+
t.memberExpression(t.identifier('args'), t.identifier('select')),
|
|
189
|
+
t.memberExpression(t.identifier('args'), t.identifier('where')),
|
|
190
|
+
t.memberExpression(t.identifier('args'), t.identifier('data')),
|
|
191
|
+
t.stringLiteral(updateInputTypeName),
|
|
192
|
+
];
|
|
193
|
+
classBody.push(createClassMethod('update', createConstTypeParam(selectTypeName), [updateParam], updateReturnType, buildMethodBody('buildUpdateDocument', updateArgs, 'mutation', typeName, updateMutationName)));
|
|
221
194
|
}
|
|
222
195
|
// delete method (if available)
|
|
223
196
|
if (deleteMutationName) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
operationName: '${typeName}',
|
|
244
|
-
fieldName: '${deleteMutationName}',
|
|
245
|
-
document,
|
|
246
|
-
variables,
|
|
247
|
-
});`,
|
|
248
|
-
});
|
|
197
|
+
const deleteParam = t.identifier('args');
|
|
198
|
+
deleteParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('DeleteArgs'), t.tsTypeParameterInstantiation([
|
|
199
|
+
t.tsTypeLiteral([t.tsPropertySignature(t.identifier('id'), t.tsTypeAnnotation(t.tsStringKeyword()))]),
|
|
200
|
+
])));
|
|
201
|
+
const deleteReturnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('QueryBuilder'), t.tsTypeParameterInstantiation([
|
|
202
|
+
t.tsTypeLiteral([
|
|
203
|
+
t.tsPropertySignature(t.identifier(deleteMutationName), t.tsTypeAnnotation(t.tsTypeLiteral([
|
|
204
|
+
t.tsPropertySignature(t.identifier(entityLower), t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('id'), t.tsTypeAnnotation(t.tsStringKeyword()))]))),
|
|
205
|
+
]))),
|
|
206
|
+
]),
|
|
207
|
+
])));
|
|
208
|
+
const deleteArgs = [
|
|
209
|
+
t.stringLiteral(typeName),
|
|
210
|
+
t.stringLiteral(deleteMutationName),
|
|
211
|
+
t.stringLiteral(entityLower),
|
|
212
|
+
t.memberExpression(t.identifier('args'), t.identifier('where')),
|
|
213
|
+
t.stringLiteral(deleteInputTypeName),
|
|
214
|
+
];
|
|
215
|
+
classBody.push(createClassMethod('delete', null, [deleteParam], deleteReturnType, buildMethodBody('buildDeleteDocument', deleteArgs, 'mutation', typeName, deleteMutationName)));
|
|
249
216
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
};
|
|
217
|
+
const classDecl = t.classDeclaration(t.identifier(modelName), null, t.classBody(classBody));
|
|
218
|
+
statements.push(t.exportNamedDeclaration(classDecl));
|
|
219
|
+
const header = getGeneratedFileHeader(`${typeName} model for ORM client`);
|
|
220
|
+
const code = generateCode(statements);
|
|
221
|
+
return { fileName, content: header + '\n' + code, modelName, tableName: table.name };
|
|
256
222
|
}
|
|
257
|
-
// Select types with relations are now generated in input-types.ts
|
|
258
|
-
/**
|
|
259
|
-
* Generate all model files for a list of tables
|
|
260
|
-
*/
|
|
261
223
|
export function generateAllModelFiles(tables, useSharedTypes) {
|
|
262
224
|
return tables.map((table) => generateModelFile(table, useSharedTypes));
|
|
263
225
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Query hook generators using AST-based code generation
|
|
2
|
+
* Query hook generators using Babel AST-based code generation
|
|
3
3
|
*
|
|
4
4
|
* Output structure:
|
|
5
5
|
* queries/
|
|
@@ -12,18 +12,10 @@ export interface GeneratedQueryFile {
|
|
|
12
12
|
content: string;
|
|
13
13
|
}
|
|
14
14
|
export interface QueryGeneratorOptions {
|
|
15
|
-
/** Whether to generate React Query hooks (default: true for backwards compatibility) */
|
|
16
15
|
reactQueryEnabled?: boolean;
|
|
16
|
+
useCentralizedKeys?: boolean;
|
|
17
|
+
hasRelationships?: boolean;
|
|
17
18
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Generate list query hook file content using AST
|
|
20
|
-
*/
|
|
21
19
|
export declare function generateListQueryHook(table: CleanTable, options?: QueryGeneratorOptions): GeneratedQueryFile;
|
|
22
|
-
|
|
23
|
-
* Generate single item query hook file content using AST
|
|
24
|
-
*/
|
|
25
|
-
export declare function generateSingleQueryHook(table: CleanTable, options?: QueryGeneratorOptions): GeneratedQueryFile;
|
|
26
|
-
/**
|
|
27
|
-
* Generate all query hook files for all tables
|
|
28
|
-
*/
|
|
20
|
+
export declare function generateSingleQueryHook(table: CleanTable, options?: QueryGeneratorOptions): GeneratedQueryFile | null;
|
|
29
21
|
export declare function generateAllQueryHooks(tables: CleanTable[], options?: QueryGeneratorOptions): GeneratedQueryFile[];
|