Skip to content

Commit 1a51f32

Browse files
awaissaeedforenaxachrinza
authored andcommitted
fix(cli): look for all id properties including composite keys
Signed-off-by: Awais Saeed <[email protected]>
1 parent 497b21d commit 1a51f32

File tree

1 file changed

+18
-21
lines changed
  • packages/cli/generators/discover

1 file changed

+18
-21
lines changed

packages/cli/generators/discover/index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -283,30 +283,27 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
283283
for (let i = 0; i < this.discoveringModels.length; i++) {
284284
const modelInfo = this.discoveringModels[i];
285285
debug(`Discovering: ${modelInfo.name}...`);
286-
this.artifactInfo.modelDefinitions.push(
287-
await modelMaker.discoverSingleModel(
288-
this.artifactInfo.dataSource,
289-
modelInfo.name,
290-
{
291-
schema: modelInfo.owner,
292-
disableCamelCase: this.artifactInfo.disableCamelCase,
293-
},
294-
),
286+
const modelDefinition = await modelMaker.discoverSingleModel(
287+
this.artifactInfo.dataSource,
288+
modelInfo.name,
289+
{
290+
schema: modelInfo.owner,
291+
disableCamelCase: this.artifactInfo.disableCamelCase,
292+
},
295293
);
294+
if (this.options.optionalId) {
295+
// Find id properties (can be multiple ids if using composite key)
296+
const idProperties = Object.values(modelDefinition.properties).filter(
297+
property => property.id,
298+
);
299+
// Mark as not required
300+
idProperties.forEach(property => {
301+
property.required = false;
302+
});
303+
}
304+
this.artifactInfo.modelDefinitions.push(modelDefinition);
296305
debug(`Discovered: ${modelInfo.name}`);
297306
}
298-
299-
if (this.options.optionalId) {
300-
// Find id property
301-
const idProperty = Object.entries(
302-
this.artifactInfo.modelDefinitions[0].properties,
303-
).find(x => x[1].id === 1)[0];
304-
305-
// Mark as not required
306-
this.artifactInfo.modelDefinitions[0].properties[
307-
idProperty
308-
].required = false;
309-
}
310307
}
311308

312309
/**

0 commit comments

Comments
 (0)