import { ConnectorType , DataSource , DMMF , EnvValue , GeneratorConfig } from '@prisma/generator-helper/dist' ; import { valueIs } from './utils' ; const renderAttribute = ( field ) => { const { kind , type } = field ; return { default : ( value ) => { if ( value == null || value == undefined ) return '' ; if ( kind === 'scalar' && type !== 'BigInt' && typeof value == 'string' ) value = `"${ value }"` ; if ( valueIs ( value , [ Number , String , Boolean ] ) || kind === 'enum' ) return `@default(${ value })` ; if ( typeof value === 'object' ) return `@default(${ value . name }(${ value . args }))` ; throw new Error ( `Prismix: Unsupported field attribute ${ value }` ) ; } , isId : ( value ) => ( value ? '@id' : '' ) , isUnique : ( value ) => ( value ? '@unique' : '' ) , isUpdatedAt : ( value ) => ( value ? '@updatedAt' : '' ) , columnName : ( value ) => ( value ? `@map("${ value }")` : '' ) } ; } ; function renderAttributes ( field ) { const { relationFromFields , relationToFields , relationName , kind } = field ; if ( kind == 'scalar' || kind == 'enum' ) { return `${ Object . keys ( field ) . map ( ( property ) => renderAttribute ( field ) [ property ] && renderAttribute ( field ) [ property ] ( field [ property ] ) ) . filter ( ( x ) => ! ! x ) . join ( ' ' ) }` ; } if ( relationFromFields && kind === 'object' ) { return relationFromFields . length > 0 ? `@relation(name: "${ relationName }", fields: [${ relationFromFields }], references: [${ relationToFields }])` : `@relation(name: "${ relationName }")` ; } return '' ; } function renderModelFields ( fields ) { return fields . map ( ( field ) => { const { name , kind , type , isRequired , isList } = field ; if ( kind == 'scalar' ) return `${ name } ${ type }${ isRequired ? '' : '?' } ${ renderAttributes ( field ) }` ; if ( kind == 'object' || kind == 'enum' ) return `${ name } ${ type }${ isList ? '[]' : isRequired ? '' : '?' } ${ renderAttributes ( field ) }` ; throw new Error ( `Prismix: Unsupported field kind "${ kind }"` ) ; } ) ; } function renderIdFieldsOrPrimaryKey ( idFields ) { if ( ! idFields ) return '' ; return idFields . length > 0 ? `@@id([${ idFields . join ( ', ' ) }])` : '' ; } function renderUniqueFields ( uniqueFields ) { return uniqueFields . length > 0 ? uniqueFields . map ( ( eachUniqueField ) => `@@unique([${ eachUniqueField . join ( ', ' ) }])` ) : [ ] ; } function renderDbName ( dbName ) { return dbName ? `@@map("${ dbName }")` : '' ; } function renderUrl ( envValue ) { const value = envValue . fromEnvVar ? `env("${ envValue . fromEnvVar }")` : `"${ envValue . value }"` ; return `url = ${ value }` ; } function renderProvider ( provider ) { return `provider = "${ provider }"` ; } function renderOutput ( path ) { return path ? `output = "${ path }"` : '' ; } function renderBinaryTargets ( binaryTargets ? ) { return binaryTargets ?. length ? `binaryTargets = ${ JSON . stringify ( binaryTargets ) }` : '' ; } function renderPreviewFeatures ( previewFeatures ) { return previewFeatures . length ? `previewFeatures = ${ JSON . stringify ( previewFeatures ) }` : '' ; } function renderBlock ( type , name , things ) { return `${ type } ${ name } {\n${ things . filter ( ( thing ) => thing . length > 1 ) . map ( ( thing ) => `\t${ thing }` ) . join ( '\n' ) }\n}` ; } function deserializeModel ( model ) { const { name , fields , uniqueFields , dbName , idFields , primaryKey } = model ; return renderBlock ( 'model' , name , [ ... renderModelFields ( fields ) , ... renderUniqueFields ( uniqueFields ) , renderDbName ( dbName ) , renderIdFieldsOrPrimaryKey ( idFields || primaryKey ?. fields ) ] ) ; } function deserializeDatasource ( datasource ) { const { activeProvider : provider , name , url } = datasource ; return renderBlock ( 'datasource' , name , [ renderProvider ( provider ) , renderUrl ( url ) ] ) ; } function deserializeGenerator ( generator ) { const { binaryTargets , name , output , provider , previewFeatures } = generator ; return renderBlock ( 'generator' , name , [ renderProvider ( provider . value ) , renderOutput ( output ?. value || null ) , renderBinaryTargets ( binaryTargets as unknown as string [ ] ) , renderPreviewFeatures ( previewFeatures ) ] ) ; } function deserializeEnum ( { name , values , dbName } ) { const outputValues = values . map ( ( { name , dbName } ) => { let result = name ; if ( name !== dbName && dbName ) result += `@map("${ dbName }")` ; return result ; } ) ; return renderBlock ( 'enum' , name , [ ... outputValues , renderDbName ( dbName || null ) ] ) ; } export async function deserializeModels ( models ) { return models . map ( ( model ) => deserializeModel ( model ) ) . join ( '\n' ) ; } export async function deserializeDatasources ( datasources ) { return datasources . map ( ( datasource ) => deserializeDatasource ( datasource ) ) . join ( '\n' ) ; } export async function deserializeGenerators ( generators ) { return generators . map ( ( generator ) => deserializeGenerator ( generator ) ) . join ( '\n' ) ; } export async function deserializeEnums ( enums ) { return enums . map ( ( each ) => deserializeEnum ( each ) ) . join ( '\n' ) ; }