@@ -810,6 +810,7 @@ export class PostgresStorageAdapter {
810
810
let index = 2 ;
811
811
schema = toPostgresSchema ( schema ) ;
812
812
813
+ const originalUpdate = { ...update } ;
813
814
update = handleDotFields ( update ) ;
814
815
// Resolve authData first,
815
816
// So we don't end up with multiple key updates
@@ -914,9 +915,19 @@ export class PostgresStorageAdapter {
914
915
} else if ( typeof fieldValue === 'object'
915
916
&& schema . fields [ fieldName ]
916
917
&& schema . fields [ fieldName ] . type === 'Object' ) {
917
- updatePatterns . push ( `$${ index } :name = $${ index + 1 } ` ) ;
918
- values . push ( fieldName , fieldValue ) ;
919
- index += 2 ;
918
+ const keysToDelete = Object . keys ( originalUpdate ) . filter ( k => {
919
+ // choose top level fields that have a delete operation set
920
+ return originalUpdate [ k ] . __op === 'Delete' && k . split ( '.' ) . length === 2
921
+ } ) . map ( k => k . split ( '.' ) [ 1 ] ) ;
922
+
923
+ const deletePatterns = keysToDelete . reduce ( ( p , c , i ) => {
924
+ return p + ` - '$${ index + 1 + i } :value'` ;
925
+ } , '' ) ;
926
+
927
+ updatePatterns . push ( `$${ index } :name = ( COALESCE($${ index } :name, '{}'::jsonb) ${ deletePatterns } || $${ index + 1 + keysToDelete . length } ::jsonb )` ) ;
928
+
929
+ values . push ( fieldName , ...keysToDelete , JSON . stringify ( fieldValue ) ) ;
930
+ index += 2 + keysToDelete . length ;
920
931
} else if ( Array . isArray ( fieldValue )
921
932
&& schema . fields [ fieldName ]
922
933
&& schema . fields [ fieldName ] . type === 'Array' ) {
0 commit comments