@@ -7,6 +7,9 @@ import _ from 'lodash';
7
7
// @flow -disable-next
8
8
import { v4 as uuidv4 } from 'uuid' ;
9
9
import sql from './sql' ;
10
+ import { StorageAdapter } from '../StorageAdapter' ;
11
+ import type { SchemaType , QueryType , QueryOptions } from '../StorageAdapter' ;
12
+ import { relativeTimeToDate } from '../Mongo/MongoTransform' ;
10
13
11
14
const PostgresRelationDoesNotExistError = '42P01' ;
12
15
const PostgresDuplicateRelationError = '42P07' ;
@@ -22,9 +25,6 @@ const debug = function (...args: any) {
22
25
log . debug . apply ( log , args ) ;
23
26
} ;
24
27
25
- import { StorageAdapter } from '../StorageAdapter' ;
26
- import type { SchemaType , QueryType , QueryOptions } from '../StorageAdapter' ;
27
-
28
28
const parseTypeToPostgresType = type => {
29
29
switch ( type . type ) {
30
30
case 'String' :
@@ -374,6 +374,11 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
374
374
patterns . push (
375
375
`(${ constraintFieldName } <> $${ index } OR ${ constraintFieldName } IS NULL)`
376
376
) ;
377
+ } else if ( typeof fieldValue . $ne === 'object' && fieldValue . $ne . $relativeTime ) {
378
+ throw new Parse . Error (
379
+ Parse . Error . INVALID_JSON ,
380
+ '$relativeTime can only be used with the $lt, $lte, $gt, and $gte operators'
381
+ ) ;
377
382
} else {
378
383
patterns . push ( `($${ index } :name <> $${ index + 1 } OR $${ index } :name IS NULL)` ) ;
379
384
}
@@ -399,6 +404,11 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
399
404
if ( fieldName . indexOf ( '.' ) >= 0 ) {
400
405
values . push ( fieldValue . $eq ) ;
401
406
patterns . push ( `${ transformDotField ( fieldName ) } = $${ index ++ } ` ) ;
407
+ } if ( typeof fieldValue . $eq === 'object' && fieldValue . $eq . $relativeTime ) {
408
+ throw new Parse . Error (
409
+ Parse . Error . INVALID_JSON ,
410
+ '$relativeTime can only be used with the $lt, $lte, $gt, and $gte operators'
411
+ ) ;
402
412
} else {
403
413
values . push ( fieldName , fieldValue . $eq ) ;
404
414
patterns . push ( `$${ index } :name = $${ index + 1 } ` ) ;
@@ -513,7 +523,12 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
513
523
}
514
524
515
525
if ( typeof fieldValue . $exists !== 'undefined' ) {
516
- if ( fieldValue . $exists ) {
526
+ if ( typeof fieldValue . $exists === 'object' && fieldValue . $exists . $relativeTime ) {
527
+ throw new Parse . Error (
528
+ Parse . Error . INVALID_JSON ,
529
+ '$relativeTime can only be used with the $lt, $lte, $gt, and $gte operators'
530
+ ) ;
531
+ } else if ( fieldValue . $exists ) {
517
532
patterns . push ( `$${ index } :name IS NOT NULL` ) ;
518
533
} else {
519
534
patterns . push ( `$${ index } :name IS NULL` ) ;
@@ -757,7 +772,7 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
757
772
Object . keys ( ParseToPosgresComparator ) . forEach ( cmp => {
758
773
if ( fieldValue [ cmp ] || fieldValue [ cmp ] === 0 ) {
759
774
const pgComparator = ParseToPosgresComparator [ cmp ] ;
760
- const postgresValue = toPostgresValue ( fieldValue [ cmp ] ) ;
775
+ let postgresValue = toPostgresValue ( fieldValue [ cmp ] ) ;
761
776
let constraintFieldName ;
762
777
if ( fieldName . indexOf ( '.' ) >= 0 ) {
763
778
let castType ;
@@ -775,6 +790,24 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
775
790
? `CAST ((${ transformDotField ( fieldName ) } ) AS ${ castType } )`
776
791
: transformDotField ( fieldName ) ;
777
792
} else {
793
+ if ( typeof postgresValue === 'object' && postgresValue . $relativeTime ) {
794
+ if ( schema . fields [ fieldName ] . type !== 'Date' ) {
795
+ throw new Parse . Error (
796
+ Parse . Error . INVALID_JSON ,
797
+ '$relativeTime can only be used with Date field'
798
+ ) ;
799
+ }
800
+ const parserResult = relativeTimeToDate ( postgresValue . $relativeTime ) ;
801
+ if ( parserResult . status === 'success' ) {
802
+ postgresValue = toPostgresValue ( parserResult . result ) ;
803
+ } else {
804
+ console . error ( 'Error while parsing relative date' , parserResult ) ;
805
+ throw new Parse . Error (
806
+ Parse . Error . INVALID_JSON ,
807
+ `bad $relativeTime (${ key } ) value. ${ parserResult . info } `
808
+ ) ;
809
+ }
810
+ }
778
811
constraintFieldName = `$${ index ++ } :name` ;
779
812
values . push ( fieldName ) ;
780
813
}
0 commit comments