File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -805,7 +805,9 @@ class RequestHandler extends APIHandlerBase {
805805 } , { } ) ;
806806
807807 upsertPayload . create = { ...attributes } ;
808- upsertPayload . update = { ...attributes } ;
808+ upsertPayload . update = {
809+ ...Object . fromEntries ( Object . entries ( attributes ) . filter ( ( e ) => ! matchFields . includes ( e [ 0 ] ) ) ) ,
810+ } ;
809811
810812 if ( relationships ) {
811813 for ( const [ key , data ] of Object . entries < any > ( relationships ) ) {
Original file line number Diff line number Diff line change @@ -1839,6 +1839,39 @@ describe('REST server tests', () => {
18391839 } ,
18401840 } ) ;
18411841 } ) ;
1842+
1843+ it ( 'upsert an existing entity' , async ( ) => {
1844+ await prisma . user . create ( {
1845+ data : { myId : 'user1' , email : 'user1@abc.com' } ,
1846+ } ) ;
1847+
1848+ const r = await handler ( {
1849+ method : 'post' ,
1850+ path : '/user' ,
1851+ query : { } ,
1852+ requestBody : {
1853+ data : {
1854+ type : 'user' ,
1855+ attributes : { myId : 'user1' , email : 'user2@abc.com' } ,
1856+ } ,
1857+ meta : {
1858+ operation : 'upsert' ,
1859+ matchFields : [ 'myId' ] ,
1860+ } ,
1861+ } ,
1862+ prisma,
1863+ } ) ;
1864+
1865+ expect ( r . status ) . toBe ( 201 ) ;
1866+ expect ( r . body ) . toMatchObject ( {
1867+ jsonapi : { version : '1.1' } ,
1868+ data : {
1869+ type : 'user' ,
1870+ id : 'user1' ,
1871+ attributes : { email : 'user2@abc.com' } ,
1872+ } ,
1873+ } ) ;
1874+ } ) ;
18421875 } ) ;
18431876
18441877 describe ( 'PUT' , ( ) => {
You can’t perform that action at this time.
0 commit comments