@@ -3185,6 +3185,28 @@ describe('REST server tests', () => {
31853185 title String
31863186 author User? @relation(fields: [authorId], references: [id])
31873187 authorId Int?
3188+ comments Comment[]
3189+ images Image[]
3190+ }
3191+
3192+ model Comment {
3193+
3194+ id Int @id @default(autoincrement())
3195+ content String
3196+ short_title String
3197+ post_id Int
3198+ post Post @relation(fields: [post_id], references: [id])
3199+
3200+ @@unique([short_title, post_id], map: "short_title_post")
3201+ }
3202+
3203+ model Image {
3204+
3205+ id Int @id @default(autoincrement())
3206+ content String
3207+ short_title String @unique(map: "image_short_title")
3208+ post_id Int
3209+ post Post @relation(fields: [post_id], references: [id])
31883210 }
31893211 ` ;
31903212 beforeEach ( async ( ) => {
@@ -3195,16 +3217,27 @@ describe('REST server tests', () => {
31953217 endpoint : 'http://localhost/api' ,
31963218 externalIdMapping : {
31973219 User : 'name_source' ,
3220+ Image : 'image_short_title' ,
3221+ Comment : 'short_title_post' ,
31983222 } ,
31993223 } ) ;
32003224 handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3201- } ) ;
32023225
3203- it ( 'works with id mapping' , async ( ) => {
32043226 await client . user . create ( {
32053227 data : { id : 1 , name : 'User1' , source : 'a' } ,
32063228 } ) ;
3229+ await client . post . create ( {
3230+ data : { id : 2 , title : 'Some title' , authorId : 1 } ,
3231+ } ) ;
3232+ await client . comment . create ( {
3233+ data : { id : 3 , short_title : 'comment-title' , post_id : 2 , content : 'Comment1' } ,
3234+ } ) ;
3235+ await client . image . create ( {
3236+ data : { id : 1 , short_title : 'image-title' , post_id : 2 , content : 'Image1' } ,
3237+ } ) ;
3238+ } ) ;
32073239
3240+ it ( 'works with id mapping' , async ( ) => {
32083241 // user is no longer exposed using the `id` field
32093242 let r = await handler ( {
32103243 method : 'get' ,
@@ -3248,6 +3281,29 @@ describe('REST server tests', () => {
32483281 id : 'User1_a' ,
32493282 } ) ;
32503283 } ) ;
3284+
3285+ it ( 'works with externalIdMapping as single column with underscore (single unique index)' , async ( ) => {
3286+ const r = await handler ( {
3287+ method : 'get' ,
3288+ path : '/image/image-title' ,
3289+ query : { } ,
3290+ client,
3291+ } ) ;
3292+ expect ( r . status ) . toBe ( 200 ) ;
3293+ expect ( r . body . data . attributes . content ) . toBe ( 'Image1' ) ;
3294+ } ) ;
3295+
3296+ it ( 'works with externalIdMapping using mapped compound unique name' , async ( ) => {
3297+ const r = await handler ( {
3298+ method : 'get' ,
3299+ path : '/comment/comment-title_2' ,
3300+ query : { } ,
3301+ client,
3302+ } ) ;
3303+ expect ( r . status ) . toBe ( 200 ) ;
3304+ expect ( r . body . data . attributes . short_title ) . toBe ( 'comment-title' ) ;
3305+ expect ( r . body . data . attributes . post_id ) . toBe ( 2 ) ;
3306+ } ) ;
32513307 } ) ;
32523308
32533309 describe ( 'REST server tests - procedures' , ( ) => {
0 commit comments