-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathhas-one-relation.generator.js
More file actions
221 lines (192 loc) · 7.14 KB
/
has-one-relation.generator.js
File metadata and controls
221 lines (192 loc) · 7.14 KB
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
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const path = require('path');
const BaseRelationGenerator = require('./base-relation.generator');
const relationUtils = require('./utils.generator');
const utils = require('../../lib/utils');
const CONTROLLER_TEMPLATE_PATH_HAS_ONE =
'controller-relation-template-has-one.ts.ejs';
module.exports = class HasOneRelationGenerator extends BaseRelationGenerator {
constructor(args, opts) {
super(args, opts);
}
async generateControllers(options) {
this.artifactInfo.sourceModelClassName = options.sourceModel;
this.artifactInfo.targetModelClassName = options.destinationModel;
this.artifactInfo.sourceRepositoryClassName =
this.artifactInfo.sourceModelClassName + 'Repository';
this.artifactInfo.controllerClassName =
this.artifactInfo.sourceModelClassName +
this.artifactInfo.targetModelClassName +
'Controller';
this.artifactInfo.paramSourceRepository = utils.camelCase(
this.artifactInfo.sourceModelClassName + 'Repository',
);
this.artifactInfo.sourceModelName = utils.toFileName(options.sourceModel);
this.artifactInfo.sourceModelPath = utils.pluralize(
this.artifactInfo.sourceModelName,
);
this.artifactInfo.targetModelName = utils.toFileName(
options.destinationModel,
);
this.artifactInfo.targetModelPath = this.artifactInfo.targetModelName;
this.artifactInfo.targetModelRequestBody = utils.camelCase(
this.artifactInfo.targetModelName,
);
this.artifactInfo.relationPropertyName = options.relationName;
this.artifactInfo.sourceModelPrimaryKey = options.sourceModelPrimaryKey;
this.artifactInfo.sourceModelPrimaryKeyType =
options.sourceModelPrimaryKeyType;
this.artifactInfo.targetModelPrimaryKey =
options.destinationModelPrimaryKey;
this.artifactInfo.foreignKeyName = options.foreignKeyName;
const source = this.templatePath(CONTROLLER_TEMPLATE_PATH_HAS_ONE);
this.artifactInfo.name =
options.sourceModel + '-' + options.destinationModel;
this.artifactInfo.outFile =
utils.toFileName(this.artifactInfo.name) + '.controller.ts';
const dest = this.destinationPath(
path.join(this.artifactInfo.outDir, this.artifactInfo.outFile),
);
this.copyTemplatedFiles(source, dest, this.artifactInfo);
await relationUtils.addExportController(
this,
path.resolve(this.artifactInfo.outDir, 'index.ts'),
this.artifactInfo.controllerClassName,
utils.toFileName(this.artifactInfo.name) + '.controller',
);
}
async generateModels(options) {
// for repo to generate relation name
this.artifactInfo.relationName = options.relationName;
const modelDir = this.artifactInfo.modelDir;
const sourceModel = options.sourceModel;
const targetModel = options.destinationModel;
const relationType = options.relationType;
const relationName = options.relationName;
const fktype = options.sourceModelPrimaryKeyType;
const isForeignKeyExist = options.doesForeignKeyExist;
const foreignKeyName = options.foreignKeyName;
const isDefaultForeignKey =
foreignKeyName === utils.camelCase(options.sourceModel) + 'Id';
let modelProperty;
const project = new relationUtils.AstLoopBackProject();
const sourceFile = relationUtils.addFileToProject(
project,
modelDir,
sourceModel,
);
const sourceClass = relationUtils.getClassObj(sourceFile, sourceModel);
relationUtils.doesRelationExist(sourceClass, relationName, {
force: this.options.force,
});
modelProperty = this.getHasOne(
targetModel,
relationName,
isDefaultForeignKey,
foreignKeyName,
);
relationUtils.addProperty(sourceClass, modelProperty);
const imports = relationUtils.getRequiredImports(
targetModel,
relationType,
sourceModel,
);
relationUtils.addRequiredImports(sourceFile, imports);
await sourceFile.save();
const targetFile = relationUtils.addFileToProject(
project,
modelDir,
targetModel,
);
const targetClass = relationUtils.getClassObj(targetFile, targetModel);
if (isForeignKeyExist) {
const existingFK = targetClass.getProperty(foreignKeyName);
if (
existingFK &&
!relationUtils.isValidPropertyType(targetClass, foreignKeyName, fktype)
) {
existingFK.remove();
const newFK = relationUtils.addForeignKey(foreignKeyName, fktype);
relationUtils.addProperty(targetClass, newFK);
targetClass.formatText();
await targetFile.save();
}
}
if (isForeignKeyExist) {
if (
!relationUtils.isValidPropertyType(targetClass, foreignKeyName, fktype)
) {
throw new Error('foreignKey Type Error');
}
} else {
modelProperty = relationUtils.addForeignKey(foreignKeyName, fktype);
relationUtils.addProperty(targetClass, modelProperty);
targetClass.formatText();
await targetFile.save();
}
}
getHasOne(className, relationName, isDefaultForeignKey, foreignKeyName) {
let relationDecorator = [
{
name: 'hasOne',
arguments: [`() => ${className}, {keyTo: '${foreignKeyName}'}`],
},
];
if (isDefaultForeignKey) {
relationDecorator = [
{
name: 'hasOne',
arguments: [`() => ${className}`],
},
];
}
return {
decorators: relationDecorator,
name: relationName,
type: className,
};
}
_getRepositoryRequiredImports(dstModelClassName, dstRepositoryClassName) {
const importsArray = super._getRepositoryRequiredImports(
dstModelClassName,
dstRepositoryClassName,
this.artifactInfo.srcModelClass,
);
importsArray.push({
name: 'HasOneRepositoryFactory',
module: '@loopback/repository',
});
return importsArray;
}
_getRepositoryRelationPropertyName() {
return this.artifactInfo.relationName;
}
_getRepositoryRelationPropertyType() {
return `HasOneRepositoryFactory<${utils.toClassName(
this.artifactInfo.dstModelClass,
)}, typeof ${utils.toClassName(
this.artifactInfo.srcModelClass,
)}.prototype.${this.artifactInfo.srcModelPrimaryKey}>`;
}
_addCreatorToRepositoryConstructor(classConstructor) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
const statement =
`this.${relationPropertyName} = ` +
`this.createHasOneRepositoryFactoryFor('${relationPropertyName}', ` +
`${utils.camelCase(this.artifactInfo.dstRepositoryClassName)}Getter);`;
classConstructor.insertStatements(1, statement);
}
_registerInclusionResolverForRelation(classConstructor, options) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
if (options.registerInclusionResolver) {
const statement =
`this.registerInclusionResolver(` +
`'${relationPropertyName}', this.${relationPropertyName}.inclusionResolver);`;
classConstructor.insertStatements(2, statement);
}
}
};