@@ -1261,79 +1261,83 @@ export class PostgresStorageAdapter {
12611261 }
12621262 return Promise . reject ( err ) ;
12631263 } )
1264- . then ( results => results . map ( object => {
1265- Object . keys ( schema . fields ) . forEach ( fieldName => {
1266- if ( schema . fields [ fieldName ] . type === 'Pointer' && object [ fieldName ] ) {
1267- object [ fieldName ] = { objectId : object [ fieldName ] , __type : 'Pointer' , className : schema . fields [ fieldName ] . targetClass } ;
1268- }
1269- if ( schema . fields [ fieldName ] . type === 'Relation' ) {
1270- object [ fieldName ] = {
1271- __type : "Relation" ,
1272- className : schema . fields [ fieldName ] . targetClass
1273- }
1274- }
1275- if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'GeoPoint' ) {
1276- object [ fieldName ] = {
1277- __type : "GeoPoint" ,
1278- latitude : object [ fieldName ] . y ,
1279- longitude : object [ fieldName ] . x
1280- }
1281- }
1282- if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'Polygon' ) {
1283- let coords = object [ fieldName ] ;
1284- coords = coords . substr ( 2 , coords . length - 4 ) . split ( '),(' ) ;
1285- coords = coords . map ( ( point ) => {
1286- return [
1287- parseFloat ( point . split ( ',' ) [ 1 ] ) ,
1288- parseFloat ( point . split ( ',' ) [ 0 ] )
1289- ] ;
1290- } ) ;
1291- object [ fieldName ] = {
1292- __type : "Polygon" ,
1293- coordinates : coords
1294- }
1295- }
1296- if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'File' ) {
1297- object [ fieldName ] = {
1298- __type : 'File' ,
1299- name : object [ fieldName ]
1300- }
1301- }
1302- } ) ;
1303- //TODO: remove this reliance on the mongo format. DB adapter shouldn't know there is a difference between created at and any other date field.
1304- if ( object . createdAt ) {
1305- object . createdAt = object . createdAt . toISOString ( ) ;
1306- }
1307- if ( object . updatedAt ) {
1308- object . updatedAt = object . updatedAt . toISOString ( ) ;
1309- }
1310- if ( object . expiresAt ) {
1311- object . expiresAt = { __type : 'Date' , iso : object . expiresAt . toISOString ( ) } ;
1312- }
1313- if ( object . _email_verify_token_expires_at ) {
1314- object . _email_verify_token_expires_at = { __type : 'Date' , iso : object . _email_verify_token_expires_at . toISOString ( ) } ;
1264+ . then ( results => results . map ( object => this . postgresObjectToParseObject ( className , object , schema ) ) ) ;
1265+ }
1266+
1267+ // Converts from a postgres-format object to a REST-format object.
1268+ // Does not strip out anything based on a lack of authentication.
1269+ postgresObjectToParseObject ( className , object , schema ) {
1270+ Object . keys ( schema . fields ) . forEach ( fieldName => {
1271+ if ( schema . fields [ fieldName ] . type === 'Pointer' && object [ fieldName ] ) {
1272+ object [ fieldName ] = { objectId : object [ fieldName ] , __type : 'Pointer' , className : schema . fields [ fieldName ] . targetClass } ;
1273+ }
1274+ if ( schema . fields [ fieldName ] . type === 'Relation' ) {
1275+ object [ fieldName ] = {
1276+ __type : "Relation" ,
1277+ className : schema . fields [ fieldName ] . targetClass
13151278 }
1316- if ( object . _account_lockout_expires_at ) {
1317- object . _account_lockout_expires_at = { __type : 'Date' , iso : object . _account_lockout_expires_at . toISOString ( ) } ;
1279+ }
1280+ if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'GeoPoint' ) {
1281+ object [ fieldName ] = {
1282+ __type : "GeoPoint" ,
1283+ latitude : object [ fieldName ] . y ,
1284+ longitude : object [ fieldName ] . x
13181285 }
1319- if ( object . _perishable_token_expires_at ) {
1320- object . _perishable_token_expires_at = { __type : 'Date' , iso : object . _perishable_token_expires_at . toISOString ( ) } ;
1286+ }
1287+ if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'Polygon' ) {
1288+ let coords = object [ fieldName ] ;
1289+ coords = coords . substr ( 2 , coords . length - 4 ) . split ( '),(' ) ;
1290+ coords = coords . map ( ( point ) => {
1291+ return [
1292+ parseFloat ( point . split ( ',' ) [ 1 ] ) ,
1293+ parseFloat ( point . split ( ',' ) [ 0 ] )
1294+ ] ;
1295+ } ) ;
1296+ object [ fieldName ] = {
1297+ __type : "Polygon" ,
1298+ coordinates : coords
13211299 }
1322- if ( object . _password_changed_at ) {
1323- object . _password_changed_at = { __type : 'Date' , iso : object . _password_changed_at . toISOString ( ) } ;
1300+ }
1301+ if ( object [ fieldName ] && schema . fields [ fieldName ] . type === 'File' ) {
1302+ object [ fieldName ] = {
1303+ __type : 'File' ,
1304+ name : object [ fieldName ]
13241305 }
1306+ }
1307+ } ) ;
1308+ //TODO: remove this reliance on the mongo format. DB adapter shouldn't know there is a difference between created at and any other date field.
1309+ if ( object . createdAt ) {
1310+ object . createdAt = object . createdAt . toISOString ( ) ;
1311+ }
1312+ if ( object . updatedAt ) {
1313+ object . updatedAt = object . updatedAt . toISOString ( ) ;
1314+ }
1315+ if ( object . expiresAt ) {
1316+ object . expiresAt = { __type : 'Date' , iso : object . expiresAt . toISOString ( ) } ;
1317+ }
1318+ if ( object . _email_verify_token_expires_at ) {
1319+ object . _email_verify_token_expires_at = { __type : 'Date' , iso : object . _email_verify_token_expires_at . toISOString ( ) } ;
1320+ }
1321+ if ( object . _account_lockout_expires_at ) {
1322+ object . _account_lockout_expires_at = { __type : 'Date' , iso : object . _account_lockout_expires_at . toISOString ( ) } ;
1323+ }
1324+ if ( object . _perishable_token_expires_at ) {
1325+ object . _perishable_token_expires_at = { __type : 'Date' , iso : object . _perishable_token_expires_at . toISOString ( ) } ;
1326+ }
1327+ if ( object . _password_changed_at ) {
1328+ object . _password_changed_at = { __type : 'Date' , iso : object . _password_changed_at . toISOString ( ) } ;
1329+ }
13251330
1326- for ( const fieldName in object ) {
1327- if ( object [ fieldName ] === null ) {
1328- delete object [ fieldName ] ;
1329- }
1330- if ( object [ fieldName ] instanceof Date ) {
1331- object [ fieldName ] = { __type : 'Date' , iso : object [ fieldName ] . toISOString ( ) } ;
1332- }
1333- }
1331+ for ( const fieldName in object ) {
1332+ if ( object [ fieldName ] === null ) {
1333+ delete object [ fieldName ] ;
1334+ }
1335+ if ( object [ fieldName ] instanceof Date ) {
1336+ object [ fieldName ] = { __type : 'Date' , iso : object [ fieldName ] . toISOString ( ) } ;
1337+ }
1338+ }
13341339
1335- return object ;
1336- } ) ) ;
1340+ return object ;
13371341 }
13381342
13391343 // Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't
@@ -1406,10 +1410,10 @@ export class PostgresStorageAdapter {
14061410 }
14071411 const child = fieldName . split ( '.' ) [ 1 ] ;
14081412 return results . map ( object => object [ column ] [ child ] ) ;
1409- } ) ;
1413+ } ) . then ( results => results . map ( object => this . postgresObjectToParseObject ( className , object , schema ) ) ) ;
14101414 }
14111415
1412- aggregate ( className , pipeline ) {
1416+ aggregate ( className , schema , pipeline ) {
14131417 debug ( 'aggregate' , className , pipeline ) ;
14141418 const values = [ className ] ;
14151419 let columns = [ ] ;
@@ -1498,17 +1502,19 @@ export class PostgresStorageAdapter {
14981502
14991503 const qs = `SELECT ${ columns } FROM $1:name ${ wherePattern } ${ sortPattern } ${ limitPattern } ${ skipPattern } ${ groupPattern } ` ;
15001504 debug ( qs , values ) ;
1501- return this . _client . any ( qs , values ) . then ( results => {
1502- if ( countField ) {
1503- results [ 0 ] [ countField ] = parseInt ( results [ 0 ] [ countField ] , 10 ) ;
1504- }
1505- results . forEach ( result => {
1506- if ( ! result . hasOwnProperty ( 'objectId' ) ) {
1507- result . objectId = null ;
1505+ return this . _client . any ( qs , values )
1506+ . then ( results => results . map ( object => this . postgresObjectToParseObject ( className , object , schema ) ) )
1507+ . then ( results => {
1508+ if ( countField ) {
1509+ results [ 0 ] [ countField ] = parseInt ( results [ 0 ] [ countField ] , 10 ) ;
15081510 }
1511+ results . forEach ( result => {
1512+ if ( ! result . hasOwnProperty ( 'objectId' ) ) {
1513+ result . objectId = null ;
1514+ }
1515+ } ) ;
1516+ return results ;
15091517 } ) ;
1510- return results ;
1511- } ) ;
15121518 }
15131519
15141520 performInitialization ( { VolatileClassesSchemas } ) {
0 commit comments