@@ -3178,6 +3178,8 @@ describe('REST server tests', () => {
31783178 posts Post[]
31793179
31803180 @@unique([name, source])
3181+ @@unique([email_with_underscore])
3182+ email_with_underscore String @unique
31813183 }
31823184
31833185 model Post {
@@ -3189,7 +3191,6 @@ describe('REST server tests', () => {
31893191 ` ;
31903192 beforeEach ( async ( ) => {
31913193 client = await createTestClient ( schema ) ;
3192-
31933194 const _handler = new RestApiHandler ( {
31943195 schema : client . $schema ,
31953196 endpoint : 'http://localhost/api' ,
@@ -3198,11 +3199,9 @@ describe('REST server tests', () => {
31983199 } ,
31993200 } ) ;
32003201 handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3201- } ) ;
32023202
3203- it ( 'works with id mapping' , async ( ) => {
32043203 await client . user . create ( {
3205- data : { id : 1 , name : 'User1' , source : 'a' } ,
3204+ data : { id : 1 , name : 'User1' , source : 'a' , email_with_underscore : 'e1' } ,
32063205 } ) ;
32073206
32083207 // user is no longer exposed using the `id` field
@@ -3212,7 +3211,6 @@ describe('REST server tests', () => {
32123211 query : { } ,
32133212 client,
32143213 } ) ;
3215-
32163214 expect ( r . status ) . toBe ( 422 ) ;
32173215 expect ( r . body . errors [ 0 ] . code ) . toBe ( 'validation-error' ) ;
32183216
@@ -3223,7 +3221,6 @@ describe('REST server tests', () => {
32233221 query : { } ,
32243222 client,
32253223 } ) ;
3226-
32273224 expect ( r . status ) . toBe ( 200 ) ;
32283225 expect ( r . body . data . attributes . source ) . toBe ( 'a' ) ;
32293226 expect ( r . body . data . attributes . name ) . toBe ( 'User1' ) ;
@@ -3239,7 +3236,6 @@ describe('REST server tests', () => {
32393236 query : { include : 'author' } ,
32403237 client,
32413238 } ) ;
3242-
32433239 expect ( r . status ) . toBe ( 200 ) ;
32443240 expect ( r . body . data . attributes . title ) . toBe ( 'Title1' ) ;
32453241 // Verify author relationship contains the external ID
@@ -3248,6 +3244,57 @@ describe('REST server tests', () => {
32483244 id : 'User1_a' ,
32493245 } ) ;
32503246 } ) ;
3247+
3248+ it ( 'works with id mapping (array of columns)' , async ( ) => {
3249+ client = await createTestClient ( schema ) ;
3250+ const _handler = new RestApiHandler ( {
3251+ schema : client . $schema ,
3252+ endpoint : 'http://localhost/api' ,
3253+ externalIdMapping : {
3254+ User : [ 'name' , 'source' ] ,
3255+ } ,
3256+ } ) ;
3257+ handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3258+
3259+ await client . user . create ( {
3260+ data : { id : 2 , name : 'User2' , source : 'b' , email_with_underscore : 'e2' } ,
3261+ } ) ;
3262+
3263+ let r = await handler ( {
3264+ method : 'get' ,
3265+ path : '/user/User2_b' ,
3266+ query : { } ,
3267+ client,
3268+ } ) ;
3269+ expect ( r . status ) . toBe ( 200 ) ;
3270+ expect ( r . body . data . attributes . source ) . toBe ( 'b' ) ;
3271+ expect ( r . body . data . attributes . name ) . toBe ( 'User2' ) ;
3272+ } ) ;
3273+
3274+ it ( 'works with id mapping (single column with underscore)' , async ( ) => {
3275+ client = await createTestClient ( schema ) ;
3276+ const _handler = new RestApiHandler ( {
3277+ schema : client . $schema ,
3278+ endpoint : 'http://localhost/api' ,
3279+ externalIdMapping : {
3280+ User : 'email_with_underscore' ,
3281+ } ,
3282+ } ) ;
3283+ handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3284+
3285+ await client . user . create ( {
3286+ data : { id : 3 , name : 'User3' , source : 'c' , email_with_underscore : 'e3' } ,
3287+ } ) ;
3288+
3289+ let r = await handler ( {
3290+ method : 'get' ,
3291+ path : '/user/e3' ,
3292+ query : { } ,
3293+ client,
3294+ } ) ;
3295+ expect ( r . status ) . toBe ( 200 ) ;
3296+ expect ( r . body . data . attributes . email_with_underscore ) . toBe ( 'e3' ) ;
3297+ } ) ;
32513298 } ) ;
32523299
32533300 describe ( 'REST server tests - procedures' , ( ) => {
@@ -3352,7 +3399,8 @@ mutation procedure sum(a: Int, b: Int): Int
33523399 const b = args ?. b as number | undefined ;
33533400 return ( a ?? 0 ) + ( b ?? 0 ) ;
33543401 } ,
3355- sumIds : async ( { args } : ProcCtx < SumIdsArgs > ) => ( args . ids as number [ ] ) . reduce ( ( acc , x ) => acc + x , 0 ) ,
3402+ sumIds : async ( { args } : ProcCtx < SumIdsArgs > ) =>
3403+ ( args . ids as number [ ] ) . reduce ( ( acc , x ) => acc + x , 0 ) ,
33563404 echoRole : async ( { args } : ProcCtx < EchoRoleArgs > ) => args . r ,
33573405 echoOverview : async ( { args } : ProcCtx < EchoOverviewArgs > ) => args . o ,
33583406 sum : async ( { args } : ProcCtx < SumArgs > ) => args . a + args . b ,
@@ -3373,7 +3421,7 @@ mutation procedure sum(a: Int, b: Int): Int
33733421 const r = await handler ( {
33743422 method : 'get' ,
33753423 path : '/$procs/echoDecimal' ,
3376- query : { ...json as object , meta : { serialization : meta } } as any ,
3424+ query : { ...( json as object ) , meta : { serialization : meta } } as any ,
33773425 client,
33783426 } ) ;
33793427
@@ -3486,7 +3534,7 @@ mutation procedure sum(a: Int, b: Int): Int
34863534 const r = await handler ( {
34873535 method : 'post' ,
34883536 path : '/$procs/sum' ,
3489- requestBody : { ...json as object , meta : { serialization : meta } } as any ,
3537+ requestBody : { ...( json as object ) , meta : { serialization : meta } } as any ,
34903538 client,
34913539 } ) ;
34923540
0 commit comments