@@ -624,8 +624,16 @@ export class PostgresStorageAdapter {
624
624
} ) ;
625
625
626
626
columnsArray = columnsArray . concat ( Object . keys ( geoPoints ) ) ;
627
- let initialValues = valuesArray . map ( ( val , index ) => `$${ index + 2 + columnsArray . length } ${ ( [ '_rperm' , '_wperm' ] . includes ( columnsArray [ index ] ) ) ? '::text[]' : '' } ` ) ;
628
-
627
+ let initialValues = valuesArray . map ( ( val , index ) => {
628
+ let termination = '' ;
629
+ let fieldName = columnsArray [ index ] ;
630
+ if ( [ '_rperm' , '_wperm' ] . includes ( fieldName ) ) {
631
+ termination = '::text[]' ;
632
+ } else if ( schema . fields [ fieldName ] && schema . fields [ fieldName ] . type === 'Array' ) {
633
+ termination = '::jsonb' ;
634
+ }
635
+ return `$${ index + 2 + columnsArray . length } ${ termination } ` ;
636
+ } ) ;
629
637
let geoPointsInjects = Object . keys ( geoPoints ) . map ( ( key , idx ) => {
630
638
let value = geoPoints [ key ] ;
631
639
valuesArray . push ( value . latitude , value . longitude ) ;
@@ -725,17 +733,21 @@ export class PostgresStorageAdapter {
725
733
values . push ( fieldName , fieldValue . amount ) ;
726
734
index += 2 ;
727
735
} else if ( fieldValue . __op === 'Add' ) {
728
- updatePatterns . push ( `$${ index } :name = COALESCE($${ index } :name, '[]'::jsonb) || $${ index + 1 } ` ) ;
736
+ updatePatterns . push ( `$${ index } :name = COALESCE($${ index } :name, '[]'::jsonb) || array_to_json( $${ index + 1 } )::jsonb ` ) ;
729
737
values . push ( fieldName , fieldValue . objects ) ;
730
738
index += 2 ;
731
739
} else if ( fieldValue . __op === 'Delete' ) {
732
740
updatePatterns . push ( `$${ index } :name = $${ index + 1 } ` )
733
741
values . push ( fieldName , null ) ;
734
742
index += 2 ;
735
743
} else if ( fieldValue . __op === 'Remove' ) {
736
- return Promise . reject ( new Parse . Error ( Parse . Error . OPERATION_FORBIDDEN , 'Postgres does not support Remove operator.' ) ) ;
744
+ updatePatterns . push ( `$${ index } :name = array_remove(COALESCE($${ index } :name, '[]'::jsonb), $${ index + 1 } ::jsonb)` )
745
+ values . push ( fieldName , JSON . stringify ( fieldValue . objects ) ) ;
746
+ index += 2 ;
737
747
} else if ( fieldValue . __op === 'AddUnique' ) {
738
- return Promise . reject ( new Parse . Error ( Parse . Error . OPERATION_FORBIDDEN , 'Postgres does not support AddUnique operator.' ) ) ;
748
+ updatePatterns . push ( `$${ index } :name = array_add_unique(COALESCE($${ index } :name, '[]'::jsonb), $${ index + 1 } ::jsonb)` ) ;
749
+ values . push ( fieldName , JSON . stringify ( fieldValue . objects ) ) ;
750
+ index += 2 ;
739
751
} else if ( fieldName === 'updatedAt' ) { //TODO: stop special casing this. It should check for __type === 'Date' and use .iso
740
752
updatePatterns . push ( `$${ index } :name = $${ index + 1 } ` )
741
753
values . push ( fieldName , fieldValue ) ;
@@ -958,11 +970,18 @@ export class PostgresStorageAdapter {
958
970
throw err ;
959
971
} ) ;
960
972
} ) ;
961
-
962
973
return Promise . all ( promises ) . then ( ( ) => {
963
- return this . _client . any ( json_object_set_key ) . catch ( ( err ) => {
964
- console . error ( err ) ;
965
- } )
974
+ return Promise . all ( [
975
+ this . _client . any ( json_object_set_key ) . catch ( ( err ) => {
976
+ console . error ( err ) ;
977
+ } ) ,
978
+ this . _client . any ( array_add_unique ) . catch ( ( err ) => {
979
+ console . error ( err ) ;
980
+ } ) ,
981
+ this . _client . any ( array_remove ) . catch ( ( err ) => {
982
+ console . error ( err ) ;
983
+ } )
984
+ ] ) ;
966
985
} ) . then ( ( ) => {
967
986
debug ( `initialzationDone in ${ new Date ( ) . getTime ( ) - now } ` ) ;
968
987
} )
@@ -992,5 +1011,29 @@ SELECT concat(\'{\', string_agg(to_json("key") || \':\' || "value", \',\'), \'}\
992
1011
SELECT "key_to_set", to_json("value_to_set")::jsonb) AS "fields"\
993
1012
$function$;'
994
1013
1014
+ const array_add_unique = `CREATE OR REPLACE FUNCTION "array_add_unique"(
1015
+ "array" jsonb,
1016
+ "values" jsonb
1017
+ )
1018
+ RETURNS jsonb
1019
+ LANGUAGE sql
1020
+ IMMUTABLE
1021
+ STRICT
1022
+ AS $function$
1023
+ SELECT to_jsonb(ARRAY(SELECT DISTINCT jsonb_array_elements("values" || "array")))
1024
+ $function$;` ;
1025
+
1026
+ const array_remove = `CREATE OR REPLACE FUNCTION "array_remove"(\
1027
+ "array" jsonb,\
1028
+ "values" jsonb\
1029
+ )\
1030
+ RETURNS jsonb \
1031
+ LANGUAGE sql \
1032
+ IMMUTABLE \
1033
+ STRICT \
1034
+ AS $function$ \
1035
+ SELECT to_jsonb(ARRAY(SELECT * FROM jsonb_array_elements("array") as elt WHERE elt NOT IN (SELECT * FROM (SELECT jsonb_array_elements("values")) AS sub))) \
1036
+ $function$;` ;
1037
+
995
1038
export default PostgresStorageAdapter ;
996
1039
module . exports = PostgresStorageAdapter ; // Required for tests
0 commit comments