11import type { ZModelServices } from '@zenstackhq/language' ;
22import colors from 'colors' ;
3- import {
4- isEnum ,
5- type DataField ,
6- type DataModel ,
7- type Enum ,
8- type Model ,
9- } from '@zenstackhq/language/ast' ;
3+ import { isEnum , type DataField , type DataModel , type Enum , type Model } from '@zenstackhq/language/ast' ;
104import {
115 DataFieldAttributeFactory ,
126 DataFieldFactory ,
@@ -79,8 +73,12 @@ export function syncEnums({
7973 // For providers that don't support native enums (e.g., SQLite), carry over
8074 // enum declarations from the existing schema as-is by deep-cloning the AST nodes.
8175 // A dummy buildReference is used since we don't need cross-reference resolution.
82- const dummyBuildReference = ( _node : AstNode , _property : string , _refNode : CstNode | undefined , refText : string ) : Reference < AstNode > =>
83- ( { $refText : refText } ) as Reference < AstNode > ;
76+ const dummyBuildReference = (
77+ _node : AstNode ,
78+ _property : string ,
79+ _refNode : CstNode | undefined ,
80+ refText : string ,
81+ ) : Reference < AstNode > => ( { $refText : refText } ) as Reference < AstNode > ;
8482
8583 oldModel . declarations
8684 . filter ( ( d ) => isEnum ( d ) )
@@ -181,7 +179,6 @@ export function syncTable({
181179 }
182180
183181 table . columns . forEach ( ( column ) => {
184-
185182 const { name, modified } = resolveNameCasing ( options . fieldCasing , column . name ) ;
186183
187184 const builtinType = provider . getBuiltinType ( column . datatype ) ;
@@ -259,9 +256,10 @@ export function syncTable({
259256 b . setDecl ( uniqueAttribute ) ;
260257 // Only add map if the unique constraint name differs from default patterns
261258 // Default patterns: TableName_columnName_key (Prisma) or just columnName (MySQL)
262- const isDefaultName = ! column . unique_name
263- || column . unique_name === `${ table . name } _${ column . name } _key`
264- || column . unique_name === column . name ;
259+ const isDefaultName =
260+ ! column . unique_name ||
261+ column . unique_name === `${ table . name } _${ column . name } _key` ||
262+ column . unique_name === column . name ;
265263 if ( ! isDefaultName ) {
266264 b . addArg ( ( ab ) => ab . StringLiteral . setValue ( column . unique_name ! ) , 'map' ) ;
267265 }
@@ -296,9 +294,7 @@ export function syncTable({
296294 ) ;
297295 }
298296
299- const hasUniqueConstraint =
300- table . columns . some ( ( c ) => c . unique || c . pk ) ||
301- table . indexes . some ( ( i ) => i . unique ) ;
297+ const hasUniqueConstraint = table . columns . some ( ( c ) => c . unique || c . pk ) || table . indexes . some ( ( i ) => i . unique ) ;
302298 if ( ! hasUniqueConstraint ) {
303299 modelFactory . addAttribute ( ( a ) => a . setDecl ( getAttributeRef ( '@@ignore' , services ) ) ) ;
304300 modelFactory . addComment (
@@ -342,8 +338,7 @@ export function syncTable({
342338 return ;
343339 }
344340
345- modelFactory . addAttribute ( ( builder ) =>
346- {
341+ modelFactory . addAttribute ( ( builder ) => {
347342 const attr = builder
348343 . setDecl ( index . unique ? modelUniqueAttribute : modelindexAttribute )
349344 . addArg ( ( argBuilder ) => {
@@ -364,16 +359,14 @@ export function syncTable({
364359 return arrayExpr ;
365360 } ) ;
366361
367- const suffix = index . unique ? '_key' : '_idx' ;
362+ const suffix = index . unique ? '_key' : '_idx' ;
368363
369- if ( index . name !== `${ table . name } _${ index . columns . map ( c => c . name ) . join ( '_' ) } ${ suffix } ` ) {
370- attr . addArg ( ( argBuilder ) => argBuilder . StringLiteral . setValue ( index . name ) , 'map' ) ;
371- }
372-
373- return attr
374- }
364+ if ( index . name !== `${ table . name } _${ index . columns . map ( ( c ) => c . name ) . join ( '_' ) } ${ suffix } ` ) {
365+ attr . addArg ( ( argBuilder ) => argBuilder . StringLiteral . setValue ( index . name ) , 'map' ) ;
366+ }
375367
376- ) ;
368+ return attr ;
369+ } ) ;
377370 } ) ;
378371 if ( table . schema && table . schema !== '' && table . schema !== defaultSchema ) {
379372 modelFactory . addAttribute ( ( b ) =>
@@ -450,7 +443,9 @@ export function syncRelation({
450443
451444 // Derive a relation field name from the FK scalar field: if the field ends with "Id",
452445 // strip the suffix and use the remainder (e.g., "authorId" -> "author").
453- const sourceNameFromReference = firstSourceField . name . toLowerCase ( ) . endsWith ( 'id' ) ? `${ resolveNameCasing ( options . fieldCasing , firstSourceField . name . slice ( 0 , - 2 ) ) . name } ${ relation . type === 'many' ? 's' : '' } ` : undefined ;
446+ const sourceNameFromReference = firstSourceField . name . toLowerCase ( ) . endsWith ( 'id' )
447+ ? `${ resolveNameCasing ( options . fieldCasing , firstSourceField . name . slice ( 0 , - 2 ) ) . name } ${ relation . type === 'many' ? 's' : '' } `
448+ : undefined ;
454449
455450 // Check if the derived name would clash with an existing field
456451 const sourceFieldFromReference = sourceModel . fields . find ( ( f ) => f . name === sourceNameFromReference ) ;
@@ -462,7 +457,7 @@ export function syncRelation({
462457 options . fieldCasing ,
463458 similarRelations > 0
464459 ? `${ fieldPrefix } ${ lowerCaseFirst ( sourceModel . name ) } _${ firstColumn } `
465- : `${ ( ! sourceFieldFromReference ? sourceNameFromReference : undefined ) || lowerCaseFirst ( resolveNameCasing ( options . fieldCasing , targetModel . name ) . name ) } ${ relation . type === 'many' ? 's' : '' } ` ,
460+ : `${ ( ! sourceFieldFromReference ? sourceNameFromReference : undefined ) || lowerCaseFirst ( resolveNameCasing ( options . fieldCasing , targetModel . name ) . name ) } ${ relation . type === 'many' ? 's' : '' } ` ,
466461 ) ;
467462
468463 if ( sourceModel . fields . find ( ( f ) => f . name === sourceFieldName ) ) {
@@ -525,7 +520,8 @@ export function syncRelation({
525520
526521 // Check if the FK constraint name differs from the default pattern
527522 const defaultFkName = `${ relation . table } _${ relation . columns . join ( '_' ) } _fkey` ;
528- if ( relation . fk_name && relation . fk_name !== defaultFkName ) ab . addArg ( ( ab ) => ab . StringLiteral . setValue ( relation . fk_name ) , 'map' ) ;
523+ if ( relation . fk_name && relation . fk_name !== defaultFkName )
524+ ab . addArg ( ( ab ) => ab . StringLiteral . setValue ( relation . fk_name ) , 'map' ) ;
529525
530526 return ab ;
531527 } ) ;
@@ -537,7 +533,7 @@ export function syncRelation({
537533 options . fieldCasing ,
538534 similarRelations > 0
539535 ? `${ oppositeFieldPrefix } ${ lowerCaseFirst ( sourceModel . name ) } _${ firstColumn } `
540- : `${ lowerCaseFirst ( resolveNameCasing ( options . fieldCasing , sourceModel . name ) . name ) } ${ relation . references . type === 'many' ? 's' : '' } ` ,
536+ : `${ lowerCaseFirst ( resolveNameCasing ( options . fieldCasing , sourceModel . name ) . name ) } ${ relation . references . type === 'many' ? 's' : '' } ` ,
541537 ) ;
542538
543539 if ( targetModel . fields . find ( ( f ) => f . name === oppositeFieldName ) ) {
@@ -573,13 +569,7 @@ export function syncRelation({
573569 * mapping via field references and consolidates the synthetic enums back into
574570 * the original shared enum so the merge phase can match them correctly.
575571 */
576- export function consolidateEnums ( {
577- newModel,
578- oldModel,
579- } : {
580- newModel : Model ;
581- oldModel : Model ;
582- } ) {
572+ export function consolidateEnums ( { newModel, oldModel } : { newModel : Model ; oldModel : Model } ) {
583573 const newEnums = newModel . declarations . filter ( ( d ) => isEnum ( d ) ) as Enum [ ] ;
584574 const newDataModels = newModel . declarations . filter ( ( d ) => d . $type === 'DataModel' ) as DataModel [ ] ;
585575 const oldDataModels = oldModel . declarations . filter ( ( d ) => d . $type === 'DataModel' ) as DataModel [ ] ;
0 commit comments