@@ -7,6 +7,9 @@ import _ from 'lodash';
77// @flow -disable-next
88import { v4 as uuidv4 } from 'uuid' ;
99import sql from './sql' ;
10+ import { StorageAdapter } from '../StorageAdapter' ;
11+ import type { SchemaType , QueryType , QueryOptions } from '../StorageAdapter' ;
12+ import { relativeTimeToDate } from '../Mongo/MongoTransform' ;
1013
1114const PostgresRelationDoesNotExistError = '42P01' ;
1215const PostgresDuplicateRelationError = '42P07' ;
@@ -22,9 +25,6 @@ const debug = function (...args: any) {
2225 log . debug . apply ( log , args ) ;
2326} ;
2427
25- import { StorageAdapter } from '../StorageAdapter' ;
26- import type { SchemaType , QueryType , QueryOptions } from '../StorageAdapter' ;
27-
2828const parseTypeToPostgresType = type => {
2929 switch ( type . type ) {
3030 case 'String' :
@@ -374,6 +374,11 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
374374 patterns . push (
375375 `(${ constraintFieldName } <> $${ index } OR ${ constraintFieldName } IS NULL)`
376376 ) ;
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+ ) ;
377382 } else {
378383 patterns . push ( `($${ index } :name <> $${ index + 1 } OR $${ index } :name IS NULL)` ) ;
379384 }
@@ -399,6 +404,11 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
399404 if ( fieldName . indexOf ( '.' ) >= 0 ) {
400405 values . push ( fieldValue . $eq ) ;
401406 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+ ) ;
402412 } else {
403413 values . push ( fieldName , fieldValue . $eq ) ;
404414 patterns . push ( `$${ index } :name = $${ index + 1 } ` ) ;
@@ -513,7 +523,12 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
513523 }
514524
515525 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 ) {
517532 patterns . push ( `$${ index } :name IS NOT NULL` ) ;
518533 } else {
519534 patterns . push ( `$${ index } :name IS NULL` ) ;
@@ -757,7 +772,7 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
757772 Object . keys ( ParseToPosgresComparator ) . forEach ( cmp => {
758773 if ( fieldValue [ cmp ] || fieldValue [ cmp ] === 0 ) {
759774 const pgComparator = ParseToPosgresComparator [ cmp ] ;
760- const postgresValue = toPostgresValue ( fieldValue [ cmp ] ) ;
775+ let postgresValue = toPostgresValue ( fieldValue [ cmp ] ) ;
761776 let constraintFieldName ;
762777 if ( fieldName . indexOf ( '.' ) >= 0 ) {
763778 let castType ;
@@ -775,6 +790,24 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
775790 ? `CAST ((${ transformDotField ( fieldName ) } ) AS ${ castType } )`
776791 : transformDotField ( fieldName ) ;
777792 } 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+ }
778811 constraintFieldName = `$${ index ++ } :name` ;
779812 values . push ( fieldName ) ;
780813 }
0 commit comments