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