@@ -1261,79 +1261,83 @@ export class PostgresStorageAdapter {
1261
1261
}
1262
1262
return Promise . reject ( err ) ;
1263
1263
} )
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
1315
1278
}
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
1318
1285
}
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
1321
1299
}
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 ]
1324
1305
}
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
+ }
1325
1330
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
+ }
1334
1339
1335
- return object ;
1336
- } ) ) ;
1340
+ return object ;
1337
1341
}
1338
1342
1339
1343
// Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't
@@ -1406,10 +1410,10 @@ export class PostgresStorageAdapter {
1406
1410
}
1407
1411
const child = fieldName . split ( '.' ) [ 1 ] ;
1408
1412
return results . map ( object => object [ column ] [ child ] ) ;
1409
- } ) ;
1413
+ } ) . then ( results => results . map ( object => this . postgresObjectToParseObject ( className , object , schema ) ) ) ;
1410
1414
}
1411
1415
1412
- aggregate ( className , pipeline ) {
1416
+ aggregate ( className , schema , pipeline ) {
1413
1417
debug ( 'aggregate' , className , pipeline ) ;
1414
1418
const values = [ className ] ;
1415
1419
let columns = [ ] ;
@@ -1498,17 +1502,19 @@ export class PostgresStorageAdapter {
1498
1502
1499
1503
const qs = `SELECT ${ columns } FROM $1:name ${ wherePattern } ${ sortPattern } ${ limitPattern } ${ skipPattern } ${ groupPattern } ` ;
1500
1504
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 ) ;
1508
1510
}
1511
+ results . forEach ( result => {
1512
+ if ( ! result . hasOwnProperty ( 'objectId' ) ) {
1513
+ result . objectId = null ;
1514
+ }
1515
+ } ) ;
1516
+ return results ;
1509
1517
} ) ;
1510
- return results ;
1511
- } ) ;
1512
1518
}
1513
1519
1514
1520
performInitialization ( { VolatileClassesSchemas } ) {
0 commit comments