-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.js
More file actions
817 lines (748 loc) · 25.7 KB
/
index.js
File metadata and controls
817 lines (748 loc) · 25.7 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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
// Copyright IBM Corp. and LoopBack contributors 2019,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
// no translation: HasMany, BelongsTo, HasOne, ReferencesMany
'use strict';
const ArtifactGenerator = require('../../lib/artifact-generator');
const debug = require('../../lib/debug')('relation-generator');
const inspect = require('util').inspect;
const path = require('path');
const chalk = require('chalk');
const utils = require('../../lib/utils');
const relationUtils = require('./utils.generator');
const BelongsToRelationGenerator = require('./belongs-to-relation.generator');
const HasManyRelationGenerator = require('./has-many-relation.generator');
const HasManyThroughRelationGenerator = require('./has-many-through-relation.generator');
const HasOneRelationGenerator = require('./has-one-relation.generator');
const ReferencesManyRelationGenerator = require('./references-many-relation.generator');
const g = require('../../lib/globalize');
const ERROR_INCORRECT_RELATION_TYPE = g.f('Incorrect relation type');
const ERROR_MODEL_DOES_NOT_EXIST = g.f('model does not exist.');
const ERROR_NO_MODELS_FOUND = g.f('No models found in');
const ERROR_REPOSITORY_DOES_NOT_EXIST = g.f(
'class does not exist. Please create repository first with "lb4 repository" command.',
);
const PROMPT_BASE_RELATION_CLASS = g.f('Please select the relation type');
const PROMPT_MESSAGE_SOURCE_MODEL = g.f('Please select source model');
const PROMPT_MESSAGE_TARGET_MODEL = g.f('Please select target model');
const PROMPT_MESSAGE_THROUGH_MODEL = g.f('Please select through model');
const PROMPT_MESSAGE_PROPERTY_NAME = g.f(
'Source property name for the relation getter (will be the relation name)',
);
const PROMPT_MESSAGE_RELATION_NAME = g.f('Relation name');
const PROMPT_MESSAGE_FOREIGN_KEY_NAME = g.f(
'Foreign key name to define on the target model',
);
const PROMPT_MESSAGE_FOREIGN_KEY_NAME_BELONGS_TO = g.f(
'Foreign key name to define on the source model',
);
const PROMPT_MESSAGE_FOREIGN_KEY_NAME_REFERENCES_MANY = g.f(
'Foreign key name to define on the source model',
);
module.exports = class RelationGenerator extends ArtifactGenerator {
constructor(args, opts) {
super(args, opts);
this.args = args;
this.opts = opts;
}
setOptions() {
return super.setOptions();
}
_setupGenerator() {
this.option('relationType', {
type: String,
required: false,
description: g.f('Relation type'),
});
this.option('sourceModel', {
type: String,
required: false,
description: g.f('Source model'),
});
this.option('destinationModel', {
type: String,
required: false,
description: g.f('Destination model'),
});
this.option('throughModel', {
type: String,
required: false,
description: g.f('Through model'),
});
this.option('sourceModelPrimaryKey', {
type: String,
required: false,
description: g.f('Primary key on source model'),
});
this.option('sourceModelPrimaryKeyType', {
type: String,
required: false,
description: g.f('Type of the primary key on source model'),
});
this.option('destinationModelPrimaryKey', {
type: String,
required: false,
description: g.f('Primary key on destination model'),
});
this.option('destinationModelPrimaryKeyType', {
type: String,
required: false,
description: g.f('Type of the primary key on destination model'),
});
this.option('sourceKeyOnThrough', {
type: String,
required: false,
description: g.f('Foreign key references source model on through model'),
});
this.option('targetKeyOnThrough', {
type: String,
required: false,
description: g.f('Foreign key references target model on through model'),
});
this.option('defaultForeignKeyName', {
type: String,
required: false,
description: g.f('default foreign key name'),
});
this.option('foreignKeyName', {
type: String,
required: false,
description: g.f(
'Destination model foreign key name (optional, provide only when there is a custom foreign key)',
),
});
this.option('relationName', {
type: String,
required: false,
description: g.f('Relation name'),
});
this.option('defaultRelationName', {
type: String,
required: false,
description: g.f('Default relation name'),
});
this.option('registerInclusionResolver', {
type: Boolean,
required: false,
description: g.f(
'Allow <sourceModel> queries to include data from related <destinationModel>',
),
});
this.artifactInfo = {
type: 'relation',
rootDir: utils.sourceRootDir,
outDir: utils.sourceRootDir,
};
// to check if model and repo exist
this.artifactInfo.modelDir = path.resolve(
this.artifactInfo.rootDir,
utils.modelsDir,
);
this.artifactInfo.repoDir = path.resolve(
this.artifactInfo.rootDir,
utils.repositoriesDir,
);
super._setupGenerator();
this._arguments = [];
this.isChecked = {
relationType: false,
sourceModel: false,
destinationModel: false,
};
}
checkLoopBackProject() {
if (this.shouldExit()) return;
return super.checkLoopBackProject();
}
_getDefaultRelationName() {
let defaultRelationName;
switch (this.artifactInfo.relationType) {
case relationUtils.relationType.belongsTo:
// this is how the belongsToAccessor generates the default relation name
defaultRelationName = this.artifactInfo.foreignKeyName.replace(
/Id$/,
'',
);
break;
case relationUtils.relationType.hasMany:
defaultRelationName = utils.pluralize(
utils.camelCase(this.artifactInfo.destinationModel),
);
break;
case relationUtils.relationType.hasManyThrough:
defaultRelationName = utils.pluralize(
utils.camelCase(this.artifactInfo.destinationModel),
);
break;
case relationUtils.relationType.hasOne:
defaultRelationName = utils.camelCase(
this.artifactInfo.destinationModel,
);
break;
case relationUtils.relationType.referencesMany:
// this is how the referencesManyAccessor generates the default relation name
defaultRelationName = this.artifactInfo.foreignKeyName.replace(
/Ids$/,
's',
);
break;
}
return defaultRelationName;
}
async _promptModelList(message, parameter, toRemove = []) {
let modelList;
try {
debug(`model list dir ${this.artifactInfo.modelDir}`);
modelList = await utils.getArtifactList(
this.artifactInfo.modelDir,
'model',
);
} catch (err) {
/* istanbul ignore next */
return this.exit(err);
}
let repoList;
try {
debug(`repository list dir ${this.artifactInfo.repoDir}`);
repoList = await utils.getArtifactList(
this.artifactInfo.repoDir,
'repository',
);
} catch (err) {
/* istanbul ignore next */
return this.exit(err);
}
if (modelList.length === 0) {
/* istanbul ignore next */
return this.exit(
new Error(
`${ERROR_NO_MODELS_FOUND} ${this.artifactInfo.modelDir}.
${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/Model-generator.html for information on how models are discovered',
)}`,
),
);
}
if (this.options[parameter]) {
this.isChecked[parameter] = true;
if (!modelList.includes(this.options[parameter])) {
/* istanbul ignore next */
return this.exit(
new Error(
`"${this.options[parameter]}" ${ERROR_MODEL_DOES_NOT_EXIST}`,
),
);
}
debug(
`${parameter} received from command line: ${this.options[parameter]}`,
);
this.artifactInfo[parameter] = this.options[parameter];
}
// remove source & target models from the selections of through model
if (toRemove.length > 0) {
const updateAry = [];
modelList.forEach(ele => {
if (!toRemove.includes(ele)) {
updateAry.push(ele);
}
});
modelList = updateAry;
}
// Prompt a user for model.
return this.prompt([
{
type: 'list',
name: parameter,
message: message,
choices: modelList,
when: !this.artifactInfo[parameter],
default: modelList[0],
},
]).then(props => {
if (this.isChecked[parameter]) return;
if (!modelList.includes(props[parameter])) {
/* istanbul ignore next */
return this.exit(
new Error(`"${props[parameter]}" ${ERROR_MODEL_DOES_NOT_EXIST}`),
);
}
// checks if the corresponding repository exists
if (!repoList.includes(props[parameter])) {
/* istanbul ignore next */
return this.exit(
new Error(
`${props[parameter]}Repository ${ERROR_REPOSITORY_DOES_NOT_EXIST}`,
),
);
}
debug(`props after ${parameter} prompt: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
return props;
});
}
// Prompt a user for Relation type
async promptRelationType() {
if (this.shouldExit()) return false;
const relationTypeChoices = Object.keys(relationUtils.relationType);
if (this.options.relationType) {
this.isChecked.relationType = true;
debug(
`Relation type received from command line: ${this.options.relationType}`,
);
if (!relationTypeChoices.includes(this.options.relationType)) {
/* istanbul ignore next */
return this.exit(new Error(ERROR_INCORRECT_RELATION_TYPE));
}
this.artifactInfo.relationType = this.options.relationType;
}
return this.prompt([
{
type: 'list',
name: 'relationType',
message: PROMPT_BASE_RELATION_CLASS,
choices: relationTypeChoices,
when: !this.artifactInfo.relationType,
validate: utils.validateClassName,
default: relationTypeChoices[0],
},
]).then(props => {
if (this.isChecked.relationType) return;
if (!relationTypeChoices.includes(props.relationType)) {
/* istanbul ignore next */
this.exit(new Error(ERROR_INCORRECT_RELATION_TYPE));
}
Object.assign(this.artifactInfo, props);
debug(`props after relation type prompt: ${inspect(props)}`);
return props;
});
}
// Get model list for source model.
async promptSourceModels() {
if (this.shouldExit()) return false;
return this._promptModelList(PROMPT_MESSAGE_SOURCE_MODEL, 'sourceModel');
}
// Get model list for target model.
async promptTargetModels() {
if (this.shouldExit()) return false;
return this._promptModelList(
PROMPT_MESSAGE_TARGET_MODEL,
'destinationModel',
);
}
// Get model list for through model.
async promptThroughModels() {
if (this.shouldExit()) return false;
if (this.artifactInfo.relationType === 'hasManyThrough') {
return this._promptModelList(
PROMPT_MESSAGE_THROUGH_MODEL,
'throughModel',
[this.artifactInfo.destinationModel, this.artifactInfo.sourceModel],
);
}
}
/**
* Prompt foreign key if not exist:
* 1. From source model get primary key. If primary key does not exist -
* error.
* 2. Get primary key type from source model.
* 3. Generate foreign key (camelCase source class Name + primary key name).
* 4. Check is foreign key exist in destination model. If not - prompt.
* Error - if type is not the same.
*
* For belongsTo and referencesMany this is getting source key not fk.
*/
async promptForeignKey() {
if (this.shouldExit()) return false;
if (this.options.sourceModelPrimaryKey) {
this.artifactInfo.sourceModelPrimaryKey =
this.options.sourceModelPrimaryKey;
}
if (this.options.sourceModelPrimaryKeyType) {
this.artifactInfo.sourceModelPrimaryKeyType =
this.options.sourceModelPrimaryKeyType;
}
if (this.options.destinationModelPrimaryKey) {
this.artifactInfo.destinationModelPrimaryKey =
this.options.destinationModelPrimaryKey;
}
if (this.options.destinationModelPrimaryKeyType) {
this.artifactInfo.destinationModelPrimaryKeyType =
this.options.destinationModelPrimaryKeyType;
}
if (!this.artifactInfo.sourceModelPrimaryKey) {
const sourceModelPK = await relationUtils.getModelPrimaryKeyProperty(
this.fs,
this.artifactInfo.modelDir,
this.artifactInfo.sourceModel,
);
if (sourceModelPK) {
this.artifactInfo.sourceModelPrimaryKey = sourceModelPK;
if (!this.artifactInfo.sourceModelPrimaryKeyType) {
const sourceModelPKType = relationUtils.getModelPropertyType(
this.artifactInfo.modelDir,
this.artifactInfo.sourceModel,
this.artifactInfo.sourceModelPrimaryKey,
);
if (sourceModelPKType) {
this.artifactInfo.sourceModelPrimaryKeyType = sourceModelPKType;
}
}
} else {
const answer = await this.prompt([
{
type: 'input',
name: 'sourceModelPrimaryKey',
message: g.f(
'What is the name of ID property of the source model?',
),
when: this.artifactInfo.sourceModelPrimaryKey === undefined,
default: 'id',
},
]);
this.artifactInfo.sourceModelPrimaryKey = answer.sourceModelPrimaryKey;
}
}
if (!this.artifactInfo.sourceModelPrimaryKeyType) {
const answer = await this.prompt([
{
type: 'list',
name: 'sourceModelPrimaryKeyType',
message: g.f('What is the type of the source model primary key?'),
choices: ['number', 'string', 'object'],
when: this.artifactInfo.sourceModelPrimaryKeyType === undefined,
default: 'number',
},
]);
this.artifactInfo.sourceModelPrimaryKeyType =
answer.sourceModelPrimaryKeyType;
}
debug(
`source model primary key and type: ${this.artifactInfo.destinationModelPrimaryKey}
${this.artifactInfo.destinationModelPrimaryKeyType}`,
);
if (!this.artifactInfo.destinationModelPrimaryKey) {
const destModelPK = await relationUtils.getModelPrimaryKeyProperty(
this.fs,
this.artifactInfo.modelDir,
this.artifactInfo.destinationModel,
);
if (destModelPK) {
this.artifactInfo.destinationModelPrimaryKey = destModelPK;
if (!this.artifactInfo.destinationModelPrimaryKeyType) {
const destModelPKType = relationUtils.getModelPropertyType(
this.artifactInfo.modelDir,
this.artifactInfo.destinationModel,
this.artifactInfo.destinationModelPrimaryKey,
);
if (destModelPKType) {
this.artifactInfo.destinationModelPrimaryKeyType = destModelPKType;
}
}
} else {
const answer = await this.prompt([
{
type: 'input',
name: 'destinationModelPrimaryKey',
message: g.f(
'What is the name of ID property of the target model?',
),
when: this.artifactInfo.destinationModelPrimaryKey === undefined,
default: 'id',
},
]);
this.artifactInfo.destinationModelPrimaryKey =
answer.destinationModelPrimaryKey;
}
}
if (!this.artifactInfo.destinationModelPrimaryKeyType) {
const answer = await this.prompt([
{
type: 'list',
name: 'destinationModelPrimaryKeyType',
message: g.f('What is the type of the target model primary key?'),
choices: ['number', 'string', 'object'],
when: this.artifactInfo.destinationModelPrimaryKeyType === undefined,
default: 'number',
},
]);
this.artifactInfo.destinationModelPrimaryKeyType =
answer.destinationModelPrimaryKeyType;
}
debug(
`destination model primary key and type: ${this.artifactInfo.destinationModelPrimaryKey}
${this.artifactInfo.destinationModelPrimaryKeyType}`,
);
// checks fks for hasManyThrough
if (this.artifactInfo.relationType === 'hasManyThrough') {
this.artifactInfo.defaultSourceKeyOnThrough =
utils.camelCase(this.artifactInfo.sourceModel) + 'Id';
this.artifactInfo.defaultTargetKeyOnThrough =
utils.camelCase(this.artifactInfo.destinationModel) + 'Id';
return this._promptKeyFromOnThroughModel();
}
if (this.options.foreignKeyName) {
debug(
`Foreign key name received from command line: ${this.options.foreignKeyName}`,
);
this.artifactInfo.foreignKeyName = this.options.foreignKeyName;
}
switch (this.artifactInfo.relationType) {
case 'belongsTo':
this.artifactInfo.defaultForeignKeyName =
utils.camelCase(this.artifactInfo.destinationModel) + 'Id';
break;
case 'referencesMany':
this.artifactInfo.defaultForeignKeyName =
utils.camelCase(this.artifactInfo.destinationModel) + 'Ids';
break;
default:
this.artifactInfo.defaultForeignKeyName =
utils.camelCase(this.artifactInfo.sourceModel) + 'Id';
break;
}
let msg;
switch (this.artifactInfo.relationType) {
case 'belongsTo':
msg = PROMPT_MESSAGE_FOREIGN_KEY_NAME_BELONGS_TO;
break;
case 'referencesMany':
msg = PROMPT_MESSAGE_FOREIGN_KEY_NAME_REFERENCES_MANY;
break;
default:
msg = PROMPT_MESSAGE_FOREIGN_KEY_NAME;
break;
}
const foreignKeyModel =
this.artifactInfo.relationType === 'belongsTo' ||
this.artifactInfo.relationType === 'referencesMany'
? this.artifactInfo.sourceModel
: this.artifactInfo.destinationModel;
const project = new relationUtils.AstLoopBackProject();
const fkFile = path.join(
this.artifactInfo.modelDir,
utils.getModelFileName(foreignKeyModel),
);
const df = project.addSourceFileAtPath(fkFile);
const cl = relationUtils.getClassObj(df, foreignKeyModel);
return this.prompt([
{
type: 'string',
name: 'foreignKeyName',
message: msg,
default: this.artifactInfo.defaultForeignKeyName,
when: !this.artifactInfo.foreignKeyName,
validate: utils.validateKeyName,
},
]).then(props => {
debug(`props after foreign key name prompt: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
this.artifactInfo.doesForeignKeyExist = relationUtils.doesPropertyExist(
cl,
this.artifactInfo.foreignKeyName,
);
// checks if it's the case that the fk already exists in source model and decorated by @belongsTo or @referencesMany, which should be aborted
if (
this.artifactInfo.doesForeignKeyExist &&
(this.artifactInfo.relationType === 'belongsTo' ||
this.artifactInfo.relationType === 'referencesMany')
) {
try {
relationUtils.doesRelationExist(cl, this.artifactInfo.foreignKeyName);
} catch (err) {
/* istanbul ignore next */
this.exit(err);
}
}
return props;
});
}
async _promptKeyFromOnThroughModel() {
if (this.options.sourceKeyOnThrough) {
this.artifactInfo.sourceKeyOnThrough = this.options.sourceKeyOnThrough;
}
if (this.shouldExit()) return false;
return this.prompt([
{
type: 'string',
name: 'sourceKeyOnThrough',
message: g.f(
`Foreign key name that references the ${chalk.yellow(
'source model',
)} to define on the through model`,
),
default: this.artifactInfo.defaultSourceKeyOnThrough,
when: !this.artifactInfo.sourceKeyOnThrough,
validate: utils.validateKeyName,
},
]).then(props => {
debug(`props after foreign key name prompt: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
return this._promptKeyToOnThroughModel();
});
}
async _promptKeyToOnThroughModel() {
if (this.shouldExit()) return false;
if (this.options.targetKeyOnThrough) {
this.artifactInfo.targetKeyOnThrough = this.options.targetKeyOnThrough;
}
return this.prompt([
{
type: 'string',
name: 'targetKeyOnThrough',
message: g.f(
`Foreign key name that references the ${chalk.yellow(
'target model',
)} to define on the through model`,
),
default: this.artifactInfo.defaultTargetKeyOnThrough,
when: !this.artifactInfo.targetKeyOnThrough,
validate: input =>
utils.validateKeyToKeyFrom(
input,
this.artifactInfo.sourceKeyOnThrough,
),
},
]).then(props => {
debug(`props after foreign key name prompt: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
});
}
async promptRelationName() {
if (this.shouldExit()) return false;
if (this.options.relationName) {
debug(
`Relation name received from command line: ${this.options.relationName}`,
);
this.artifactInfo.relationName = this.options.relationName;
}
this.artifactInfo.defaultRelationName = this._getDefaultRelationName();
// for hasMany && hasOne, the source key is the same as the relation name
const msg =
this.artifactInfo.relationType === 'belongsTo' ||
this.artifactInfo.relationType === 'referencesMany'
? PROMPT_MESSAGE_RELATION_NAME
: PROMPT_MESSAGE_PROPERTY_NAME;
return this.prompt([
{
type: 'string',
name: 'relationName',
message: msg,
when: !this.artifactInfo.relationName,
default: this.artifactInfo.defaultRelationName,
validate: inputName =>
utils.validateRelationName(
inputName,
this.artifactInfo.relationType,
this.artifactInfo.foreignKeyName,
),
},
]).then(props => {
if (
props.relationName === this.artifactInfo.foreignKeyName ||
this.artifactInfo.relationName === this.artifactInfo.foreignKeyName
) {
/* istanbul ignore next */
return this.exit(
new Error(
`relation name ${props.relationName} cannot be the same as foreign key name ${this.artifactInfo.foreignKeyName}.`,
),
);
}
debug(`props after relation name prompt: ${inspect(props)}`);
// checks if the relation name already exists
this.artifactInfo.srcRepositoryFile = path.resolve(
this.artifactInfo.repoDir,
utils.getRepositoryFileName(this.artifactInfo.sourceModel),
);
this.artifactInfo.srcRepositoryClassName =
utils.toClassName(this.artifactInfo.sourceModel) + 'Repository';
this.artifactInfo.srcRepositoryFileObj =
new relationUtils.AstLoopBackProject().addSourceFileAtPath(
this.artifactInfo.srcRepositoryFile,
);
const repoClassDeclaration =
this.artifactInfo.srcRepositoryFileObj.getClassOrThrow(
this.artifactInfo.srcRepositoryClassName,
);
// checks if the relation name already exists in repo
if (
relationUtils.doesPropertyExist(
repoClassDeclaration,
props.relationName,
)
) {
/* istanbul ignore next */
return this.exit(
new Error(
`relation ${props.relationName} already exists in the repository ${this.artifactInfo.srcRepositoryClassName}.`,
),
);
}
Object.assign(this.artifactInfo, props);
return props;
});
}
async promptRegisterInclusionResolver() {
if (this.shouldExit()) return false;
this.artifactInfo.registerInclusionResolver =
this.options.registerInclusionResolver;
const props = await this.prompt([
{
type: 'confirm',
name: 'registerInclusionResolver',
message: g.f(
'Allow %s queries to include data from related %s instances? ',
chalk.yellow(this.artifactInfo.sourceModel),
chalk.yellow(this.artifactInfo.destinationModel),
),
when: this.artifactInfo.registerInclusionResolver === undefined,
default: true,
},
]);
debug(`props after inclusion resolver promps: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
return props;
}
async scaffold() {
if (this.shouldExit()) return false;
debug('Invoke generator...');
let relationGenerator;
this.artifactInfo.name = this.artifactInfo.relationType;
switch (this.artifactInfo.relationType) {
case relationUtils.relationType.belongsTo:
relationGenerator = new BelongsToRelationGenerator(
this.args,
this.opts,
);
break;
case relationUtils.relationType.hasMany:
relationGenerator = new HasManyRelationGenerator(this.args, this.opts);
break;
case relationUtils.relationType.hasManyThrough:
relationGenerator = new HasManyThroughRelationGenerator(
this.args,
this.opts,
);
break;
case relationUtils.relationType.hasOne:
relationGenerator = new HasOneRelationGenerator(this.args, this.opts);
break;
case relationUtils.relationType.referencesMany:
relationGenerator = new ReferencesManyRelationGenerator(
this.args,
this.opts,
);
break;
}
try {
await relationGenerator.generateAll(this.artifactInfo);
} catch (error) {
/* istanbul ignore next */
this.exit(error);
}
}
async end() {
await super.end();
}
};