@@ -3013,4 +3013,65 @@ describe('REST server tests', () => {
30133013 expect ( r . body . data . attributes . enabled ) . toBe ( false ) ;
30143014 } ) ;
30153015 } ) ;
3016+
3017+ describe ( 'REST server tests - model name mapping' , ( ) => {
3018+ const schema = `
3019+ model User {
3020+ id String @id @default(cuid())
3021+ name String
3022+ posts Post[]
3023+ }
3024+
3025+ model Post {
3026+ id String @id @default(cuid())
3027+ title String
3028+ author User? @relation(fields: [authorId], references: [id])
3029+ authorId String?
3030+ }
3031+ ` ;
3032+ beforeAll ( async ( ) => {
3033+ const params = await loadSchema ( schema ) ;
3034+ prisma = params . prisma ;
3035+ zodSchemas = params . zodSchemas ;
3036+ modelMeta = params . modelMeta ;
3037+
3038+ const _handler = makeHandler ( {
3039+ endpoint : 'http://localhost/api' ,
3040+ modelNameMapping : {
3041+ myUser : 'user' ,
3042+ myPost : 'post' ,
3043+ } ,
3044+ } ) ;
3045+ handler = ( args ) =>
3046+ _handler ( { ...args , zodSchemas, modelMeta, url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3047+ } ) ;
3048+
3049+ it ( 'works with name mapping' , async ( ) => {
3050+ // using original model name
3051+ await expect (
3052+ handler ( {
3053+ method : 'post' ,
3054+ path : '/user' ,
3055+ query : { } ,
3056+ requestBody : { data : { type : 'user' , attributes : { name : 'User1' } } } ,
3057+ prisma,
3058+ } )
3059+ ) . resolves . toMatchObject ( {
3060+ status : 400 ,
3061+ } ) ;
3062+
3063+ // using mapped model name
3064+ await expect (
3065+ handler ( {
3066+ method : 'post' ,
3067+ path : '/myUser' ,
3068+ query : { } ,
3069+ requestBody : { data : { type : 'user' , attributes : { name : 'User1' } } } ,
3070+ prisma,
3071+ } )
3072+ ) . resolves . toMatchObject ( {
3073+ status : 201 ,
3074+ } ) ;
3075+ } ) ;
3076+ } ) ;
30163077} ) ;
0 commit comments