@@ -3092,6 +3092,8 @@ describe('REST server tests', () => {
30923092 posts Post[]
30933093
30943094 @@unique([name, source])
3095+ @@unique([email_with_underscore])
3096+ email_with_underscore String @unique
30953097 }
30963098
30973099 model Post {
@@ -3101,9 +3103,8 @@ describe('REST server tests', () => {
31013103 authorId Int?
31023104 }
31033105 ` ;
3104- beforeEach ( async ( ) => {
3106+ it ( 'works with id mapping (unique key name as string)' , async ( ) => {
31053107 client = await createTestClient ( schema ) ;
3106-
31073108 const _handler = new RestApiHandler ( {
31083109 schema : client . $schema ,
31093110 endpoint : 'http://localhost/api' ,
@@ -3112,11 +3113,9 @@ describe('REST server tests', () => {
31123113 } ,
31133114 } ) ;
31143115 handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3115- } ) ;
31163116
3117- it ( 'works with id mapping' , async ( ) => {
31183117 await client . user . create ( {
3119- data : { id : 1 , name : 'User1' , source : 'a' } ,
3118+ data : { id : 1 , name : 'User1' , source : 'a' , email_with_underscore : 'e1' } ,
31203119 } ) ;
31213120
31223121 // user is no longer exposed using the `id` field
@@ -3126,7 +3125,6 @@ describe('REST server tests', () => {
31263125 query : { } ,
31273126 client,
31283127 } ) ;
3129-
31303128 expect ( r . status ) . toBe ( 422 ) ;
31313129 expect ( r . body . errors [ 0 ] . code ) . toBe ( 'validation-error' ) ;
31323130
@@ -3137,7 +3135,6 @@ describe('REST server tests', () => {
31373135 query : { } ,
31383136 client,
31393137 } ) ;
3140-
31413138 expect ( r . status ) . toBe ( 200 ) ;
31423139 expect ( r . body . data . attributes . source ) . toBe ( 'a' ) ;
31433140 expect ( r . body . data . attributes . name ) . toBe ( 'User1' ) ;
@@ -3153,7 +3150,6 @@ describe('REST server tests', () => {
31533150 query : { include : 'author' } ,
31543151 client,
31553152 } ) ;
3156-
31573153 expect ( r . status ) . toBe ( 200 ) ;
31583154 expect ( r . body . data . attributes . title ) . toBe ( 'Title1' ) ;
31593155 // Verify author relationship contains the external ID
@@ -3162,6 +3158,57 @@ describe('REST server tests', () => {
31623158 id : 'User1_a' ,
31633159 } ) ;
31643160 } ) ;
3161+
3162+ it ( 'works with id mapping (array of columns)' , async ( ) => {
3163+ client = await createTestClient ( schema ) ;
3164+ const _handler = new RestApiHandler ( {
3165+ schema : client . $schema ,
3166+ endpoint : 'http://localhost/api' ,
3167+ externalIdMapping : {
3168+ User : [ 'name' , 'source' ] ,
3169+ } ,
3170+ } ) ;
3171+ handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3172+
3173+ await client . user . create ( {
3174+ data : { id : 2 , name : 'User2' , source : 'b' , email_with_underscore : 'e2' } ,
3175+ } ) ;
3176+
3177+ let r = await handler ( {
3178+ method : 'get' ,
3179+ path : '/user/User2_b' ,
3180+ query : { } ,
3181+ client,
3182+ } ) ;
3183+ expect ( r . status ) . toBe ( 200 ) ;
3184+ expect ( r . body . data . attributes . source ) . toBe ( 'b' ) ;
3185+ expect ( r . body . data . attributes . name ) . toBe ( 'User2' ) ;
3186+ } ) ;
3187+
3188+ it ( 'works with id mapping (single column with underscore)' , async ( ) => {
3189+ client = await createTestClient ( schema ) ;
3190+ const _handler = new RestApiHandler ( {
3191+ schema : client . $schema ,
3192+ endpoint : 'http://localhost/api' ,
3193+ externalIdMapping : {
3194+ User : [ 'email_with_underscore' ] ,
3195+ } ,
3196+ } ) ;
3197+ handler = ( args ) => _handler . handleRequest ( { ...args , url : new URL ( `http://localhost/${ args . path } ` ) } ) ;
3198+
3199+ await client . user . create ( {
3200+ data : { id : 3 , name : 'User3' , source : 'c' , email_with_underscore : 'e3' } ,
3201+ } ) ;
3202+
3203+ let r = await handler ( {
3204+ method : 'get' ,
3205+ path : '/user/e3' ,
3206+ query : { } ,
3207+ client,
3208+ } ) ;
3209+ expect ( r . status ) . toBe ( 200 ) ;
3210+ expect ( r . body . data . attributes . email_with_underscore ) . toBe ( 'e3' ) ;
3211+ } ) ;
31653212 } ) ;
31663213
31673214 describe ( 'REST server tests - procedures' , ( ) => {
@@ -3266,7 +3313,8 @@ mutation procedure sum(a: Int, b: Int): Int
32663313 const b = args ?. b as number | undefined ;
32673314 return ( a ?? 0 ) + ( b ?? 0 ) ;
32683315 } ,
3269- sumIds : async ( { args } : ProcCtx < SumIdsArgs > ) => ( args . ids as number [ ] ) . reduce ( ( acc , x ) => acc + x , 0 ) ,
3316+ sumIds : async ( { args } : ProcCtx < SumIdsArgs > ) =>
3317+ ( args . ids as number [ ] ) . reduce ( ( acc , x ) => acc + x , 0 ) ,
32703318 echoRole : async ( { args } : ProcCtx < EchoRoleArgs > ) => args . r ,
32713319 echoOverview : async ( { args } : ProcCtx < EchoOverviewArgs > ) => args . o ,
32723320 sum : async ( { args } : ProcCtx < SumArgs > ) => args . a + args . b ,
@@ -3287,7 +3335,7 @@ mutation procedure sum(a: Int, b: Int): Int
32873335 const r = await handler ( {
32883336 method : 'get' ,
32893337 path : '/$procs/echoDecimal' ,
3290- query : { ...json as object , meta : { serialization : meta } } as any ,
3338+ query : { ...( json as object ) , meta : { serialization : meta } } as any ,
32913339 client,
32923340 } ) ;
32933341
@@ -3400,7 +3448,7 @@ mutation procedure sum(a: Int, b: Int): Int
34003448 const r = await handler ( {
34013449 method : 'post' ,
34023450 path : '/$procs/sum' ,
3403- requestBody : { ...json as object , meta : { serialization : meta } } as any ,
3451+ requestBody : { ...( json as object ) , meta : { serialization : meta } } as any ,
34043452 client,
34053453 } ) ;
34063454
0 commit comments