@@ -56,6 +56,12 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
5656 ) ,
5757 default : undefined ,
5858 } ) ;
59+
60+ this . option ( 'optionalId' , {
61+ type : Boolean ,
62+ description : g . f ( 'Boolean to mark id property as optional field' ) ,
63+ default : false ,
64+ } ) ;
5965 }
6066
6167 _setupGenerator ( ) {
@@ -283,17 +289,26 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
283289 for ( let i = 0 ; i < this . discoveringModels . length ; i ++ ) {
284290 const modelInfo = this . discoveringModels [ i ] ;
285291 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- associations : this . options . relations ,
294- } ,
295- ) ,
292+ const modelDefinition = await modelMaker . discoverSingleModel (
293+ this . artifactInfo . dataSource ,
294+ modelInfo . name ,
295+ {
296+ schema : modelInfo . owner ,
297+ disableCamelCase : this . artifactInfo . disableCamelCase ,
298+ associations : this . options . relations ,
299+ } ,
296300 ) ;
301+ if ( this . options . optionalId ) {
302+ // Find id properties (can be multiple ids if using composite key)
303+ const idProperties = Object . values ( modelDefinition . properties ) . filter (
304+ property => property . id ,
305+ ) ;
306+ // Mark as not required
307+ idProperties . forEach ( property => {
308+ property . required = false ;
309+ } ) ;
310+ }
311+ this . artifactInfo . modelDefinitions . push ( modelDefinition ) ;
297312 debug ( `Discovered: ${ modelInfo . name } ` ) ;
298313 }
299314 }
0 commit comments