@@ -627,13 +627,11 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
627
627
const distance = fieldValue . $maxDistance ;
628
628
const distanceInKM = distance * 6371 * 1000 ;
629
629
patterns . push (
630
- `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${
631
- index + 2
630
+ `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${ index + 2
632
631
} )::geometry) <= $${ index + 3 } `
633
632
) ;
634
633
sorts . push (
635
- `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${
636
- index + 2
634
+ `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${ index + 2
637
635
} )::geometry) ASC`
638
636
) ;
639
637
values . push ( fieldName , point . longitude , point . latitude , distanceInKM ) ;
@@ -681,8 +679,7 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
681
679
}
682
680
const distanceInKM = distance * 6371 * 1000 ;
683
681
patterns . push (
684
- `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${
685
- index + 2
682
+ `ST_DistanceSphere($${ index } :name::geometry, POINT($${ index + 1 } , $${ index + 2
686
683
} )::geometry) <= $${ index + 3 } `
687
684
) ;
688
685
values . push ( fieldName , point . longitude , point . latitude , distanceInKM ) ;
@@ -862,19 +859,22 @@ export class PostgresStorageAdapter implements StorageAdapter {
862
859
_stream : any ;
863
860
_uuid : any ;
864
861
schemaCacheTtl : ?number ;
862
+ disableIndexFieldValidation : boolean ;
865
863
866
864
constructor ( { uri, collectionPrefix = '' , databaseOptions = { } } : any ) {
867
865
const options = { ...databaseOptions } ;
868
866
this . _collectionPrefix = collectionPrefix ;
869
867
this . enableSchemaHooks = ! ! databaseOptions . enableSchemaHooks ;
868
+ this . disableIndexFieldValidation = ! ! databaseOptions . disableIndexFieldValidation ;
869
+
870
870
this . schemaCacheTtl = databaseOptions . schemaCacheTtl ;
871
- for ( const key of [ 'enableSchemaHooks' , 'schemaCacheTtl' ] ) {
871
+ for ( const key of [ 'enableSchemaHooks' , 'schemaCacheTtl' , 'disableIndexFieldValidation' ] ) {
872
872
delete options [ key ] ;
873
873
}
874
874
875
875
const { client, pgp } = createClient ( uri , options ) ;
876
876
this . _client = client ;
877
- this . _onchange = ( ) => { } ;
877
+ this . _onchange = ( ) => { } ;
878
878
this . _pgp = pgp ;
879
879
this . _uuid = uuidv4 ( ) ;
880
880
this . canSortOnJoinTables = false ;
@@ -991,7 +991,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
991
991
delete existingIndexes [ name ] ;
992
992
} else {
993
993
Object . keys ( field ) . forEach ( key => {
994
- if ( ! Object . prototype . hasOwnProperty . call ( fields , key ) ) {
994
+ if (
995
+ ! this . disableIndexFieldValidation &&
996
+ ! Object . prototype . hasOwnProperty . call ( fields , key )
997
+ ) {
995
998
throw new Parse . Error (
996
999
Parse . Error . INVALID_QUERY ,
997
1000
`Field ${ key } does not exist, cannot add index.`
@@ -1006,8 +1009,22 @@ export class PostgresStorageAdapter implements StorageAdapter {
1006
1009
}
1007
1010
} ) ;
1008
1011
await conn . tx ( 'set-indexes-with-schema-format' , async t => {
1009
- if ( insertedIndexes . length > 0 ) {
1010
- await self . createIndexes ( className , insertedIndexes , t ) ;
1012
+ try {
1013
+ if ( insertedIndexes . length > 0 ) {
1014
+ await self . createIndexes ( className , insertedIndexes , t ) ;
1015
+ }
1016
+ } catch ( e ) {
1017
+ // pg-promise use Batch error see https://github.com/vitaly-t/spex/blob/e572030f261be1a8e9341fc6f637e36ad07f5231/src/errors/batch.js#L59
1018
+ const columnDoesNotExistError = e . getErrors && e . getErrors ( ) [ 0 ] && e . getErrors ( ) [ 0 ] . code === '42703' ;
1019
+ // Specific case when the column does not exist
1020
+ if ( columnDoesNotExistError ) {
1021
+ // If the disableIndexFieldValidation is true, we should ignore the error
1022
+ if ( ! this . disableIndexFieldValidation ) {
1023
+ throw e ;
1024
+ }
1025
+ } else {
1026
+ throw e ;
1027
+ }
1011
1028
}
1012
1029
if ( deletedIndexes . length > 0 ) {
1013
1030
await self . dropIndexes ( className , deletedIndexes , t ) ;
@@ -1625,16 +1642,14 @@ export class PostgresStorageAdapter implements StorageAdapter {
1625
1642
index += 2;
1626
1643
} else if (fieldValue.__op === 'Remove') {
1627
1644
updatePatterns.push(
1628
- ` $$ { index } :name = array_remove ( COALESCE ( $$ { index} :name , '[]' ::jsonb ) , $$ {
1629
- index + 1
1645
+ ` $$ { index } :name = array_remove ( COALESCE ( $$ { index} :name , '[]' ::jsonb ) , $$ { index + 1
1630
1646
} ::jsonb ) `
1631
1647
);
1632
1648
values.push(fieldName, JSON.stringify(fieldValue.objects));
1633
1649
index += 2;
1634
1650
} else if (fieldValue.__op === 'AddUnique') {
1635
1651
updatePatterns.push(
1636
- ` $$ { index } :name = array_add_unique ( COALESCE ( $$ { index} :name , '[]' ::jsonb ) , $$ {
1637
- index + 1
1652
+ ` $$ { index } :name = array_add_unique ( COALESCE ( $$ { index} :name , '[]' ::jsonb ) , $$ { index + 1
1638
1653
} ::jsonb ) `
1639
1654
);
1640
1655
values.push(fieldName, JSON.stringify(fieldValue.objects));
@@ -1745,8 +1760,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
1745
1760
updateObject = ` COALESCE ( $$ { index} :name , '{}' ::jsonb ) `;
1746
1761
}
1747
1762
updatePatterns.push(
1748
- ` $$ { index } :name = ( $ { updateObject} $ { deletePatterns } ${incrementPatterns } || $$ {
1749
- index + 1 + keysToDelete . length
1763
+ ` $$ { index } :name = ( $ { updateObject} $ { deletePatterns } ${incrementPatterns } || $$ { index + 1 + keysToDelete . length
1750
1764
} ::jsonb ) `
1751
1765
);
1752
1766
values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue));
@@ -2185,8 +2199,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
2185
2199
groupByFields.push(` "${source}" `);
2186
2200
}
2187
2201
columns.push(
2188
- ` EXTRACT ( $ {
2189
- mongoAggregateToPostgres [ operation ]
2202
+ ` EXTRACT ( $ { mongoAggregateToPostgres [ operation ]
2190
2203
} FROM $$ { index } :name AT TIME ZONE 'UTC' ) ::integer AS $$ { index + 1 } :name `
2191
2204
);
2192
2205
values.push(source, alias);
0 commit comments