@@ -6,7 +6,10 @@ import {
66 getGeneratedFileHeader ,
77 getPrimaryKeyInfo ,
88 getScalarFields ,
9+ getSelectableScalarFields ,
910 getTableNames ,
11+ getWritableFieldNames ,
12+ resolveInnerInputType ,
1013 ucFirst ,
1114 lcFirst ,
1215 getCreateInputTypeName ,
@@ -150,8 +153,8 @@ function buildFieldSchemaObject(table: CleanTable): t.ObjectExpression {
150153 ) ;
151154}
152155
153- function buildSelectObject ( table : CleanTable ) : t . ObjectExpression {
154- const fields = getScalarFields ( table ) ;
156+ function buildSelectObject ( table : CleanTable , typeRegistry ?: TypeRegistry ) : t . ObjectExpression {
157+ const fields = getSelectableScalarFields ( table , typeRegistry ) ;
155158 return t . objectExpression (
156159 fields . map ( ( f ) =>
157160 t . objectProperty ( t . identifier ( f . name ) , t . booleanLiteral ( true ) ) ,
@@ -305,9 +308,9 @@ function buildSubcommandSwitch(
305308 return t . switchStatement ( t . identifier ( 'subcommand' ) , cases ) ;
306309}
307310
308- function buildListHandler ( table : CleanTable , targetName ?: string ) : t . FunctionDeclaration {
311+ function buildListHandler ( table : CleanTable , targetName ?: string , typeRegistry ?: TypeRegistry ) : t . FunctionDeclaration {
309312 const { singularName } = getTableNames ( table ) ;
310- const selectObj = buildSelectObject ( table ) ;
313+ const selectObj = buildSelectObject ( table , typeRegistry ) ;
311314
312315 const tryBody : t . Statement [ ] = [
313316 buildGetClientStatement ( targetName ) ,
@@ -349,11 +352,11 @@ function buildListHandler(table: CleanTable, targetName?: string): t.FunctionDec
349352 ) ;
350353}
351354
352- function buildGetHandler ( table : CleanTable , targetName ?: string ) : t . FunctionDeclaration {
355+ function buildGetHandler ( table : CleanTable , targetName ?: string , typeRegistry ?: TypeRegistry ) : t . FunctionDeclaration {
353356 const { singularName } = getTableNames ( table ) ;
354357 const pkFields = getPrimaryKeyInfo ( table ) ;
355358 const pk = pkFields [ 0 ] ;
356- const selectObj = buildSelectObject ( table ) ;
359+ const selectObj = buildSelectObject ( table , typeRegistry ) ;
357360
358361 const promptQuestion = t . objectExpression ( [
359362 t . objectProperty ( t . identifier ( 'type' ) , t . stringLiteral ( 'text' ) ) ,
@@ -423,38 +426,6 @@ function buildGetHandler(table: CleanTable, targetName?: string): t.FunctionDecl
423426 ) ;
424427}
425428
426- /**
427- * Get the set of field names that have defaults in the create input type.
428- * Looks up the CreateXInput -> inner input type (e.g. DatabaseInput) in the
429- * TypeRegistry and checks each field's defaultValue from introspection.
430- */
431- /**
432- * Resolve the inner input type from a CreateXInput or UpdateXInput type.
433- * The CreateXInput has an inner field (e.g. "database" of type DatabaseInput)
434- * that contains the actual field definitions.
435- */
436- export function resolveInnerInputType (
437- inputTypeName : string ,
438- typeRegistry : TypeRegistry ,
439- ) : { name : string ; fields : Set < string > } | null {
440- const inputType = typeRegistry . get ( inputTypeName ) ;
441- if ( ! inputType ?. inputFields ) return null ;
442-
443- for ( const inputField of inputType . inputFields ) {
444- const innerTypeName = inputField . type . name
445- || inputField . type . ofType ?. name
446- || inputField . type . ofType ?. ofType ?. name ;
447- if ( ! innerTypeName ) continue ;
448-
449- const innerType = typeRegistry . get ( innerTypeName ) ;
450- if ( ! innerType ?. inputFields ) continue ;
451-
452- const fields = new Set ( innerType . inputFields . map ( ( f ) => f . name ) ) ;
453- return { name : innerTypeName , fields } ;
454- }
455- return null ;
456- }
457-
458429export function getFieldsWithDefaults (
459430 table : CleanTable ,
460431 typeRegistry ?: TypeRegistry ,
@@ -481,22 +452,6 @@ export function getFieldsWithDefaults(
481452 return fieldsWithDefaults ;
482453}
483454
484- /**
485- * Get the set of field names that actually exist in the create/update input type.
486- * Fields not in this set (e.g. computed fields like searchTsvRank, hashUuid)
487- * should be excluded from the data object in create/update handlers.
488- */
489- function getWritableFieldNames (
490- table : CleanTable ,
491- typeRegistry ?: TypeRegistry ,
492- ) : Set < string > | null {
493- if ( ! typeRegistry ) return null ;
494-
495- const createInputTypeName = getCreateInputTypeName ( table ) ;
496- const resolved = resolveInnerInputType ( createInputTypeName , typeRegistry ) ;
497- return resolved ?. fields ?? null ;
498- }
499-
500455function buildMutationHandler (
501456 table : CleanTable ,
502457 operation : 'create' | 'update' | 'delete' ,
@@ -589,7 +544,7 @@ function buildMutationHandler(
589544 ? t . objectExpression ( [
590545 t . objectProperty ( t . identifier ( pk . name ) , t . booleanLiteral ( true ) ) ,
591546 ] )
592- : buildSelectObject ( table ) ;
547+ : buildSelectObject ( table , typeRegistry ) ;
593548
594549 let ormArgs : t . ObjectExpression ;
595550
@@ -1013,8 +968,8 @@ export function generateTableCommand(table: CleanTable, options?: TableCommandOp
1013968
1014969 const tn = options ?. targetName ;
1015970 const ormTypes = { createInputTypeName, patchTypeName, innerFieldName } ;
1016- statements . push ( buildListHandler ( table , tn ) ) ;
1017- if ( hasGet ) statements . push ( buildGetHandler ( table , tn ) ) ;
971+ statements . push ( buildListHandler ( table , tn , options ?. typeRegistry ) ) ;
972+ if ( hasGet ) statements . push ( buildGetHandler ( table , tn , options ?. typeRegistry ) ) ;
1018973 statements . push ( buildMutationHandler ( table , 'create' , tn , options ?. typeRegistry , ormTypes ) ) ;
1019974 if ( hasUpdate ) statements . push ( buildMutationHandler ( table , 'update' , tn , options ?. typeRegistry , ormTypes ) ) ;
1020975 if ( hasDelete ) statements . push ( buildMutationHandler ( table , 'delete' , tn , options ?. typeRegistry , ormTypes ) ) ;
0 commit comments