@@ -93,7 +93,7 @@ export class PostgresStorageAdapter {
93
93
}
94
94
95
95
_ensureSchemaCollectionExists ( ) {
96
- return this . _client . query ( 'CREATE TABLE "_SCHEMA" ( "className" varChar(120), "schema" jsonb, "isParseClass" bool, PRIMARY KEY ("className") )' )
96
+ return this . _client . none ( 'CREATE TABLE "_SCHEMA" ( "className" varChar(120), "schema" jsonb, "isParseClass" bool, PRIMARY KEY ("className") )' )
97
97
. catch ( error => {
98
98
if ( error . code === PostgresDuplicateRelationError ) {
99
99
// Table already exists, must have been created by a different request. Ignore error.
@@ -124,22 +124,22 @@ export class PostgresStorageAdapter {
124
124
patternsArray . push ( `$${ index * 2 + 2 } :name $${ index * 2 + 3 } :raw` ) ;
125
125
} ) ;
126
126
return this . _ensureSchemaCollectionExists ( )
127
- . then ( ( ) => this . _client . query ( `CREATE TABLE $1:name (${ patternsArray . join ( ',' ) } )` , [ className , ...valuesArray ] ) )
127
+ . then ( ( ) => this . _client . none ( `CREATE TABLE $1:name (${ patternsArray . join ( ',' ) } )` , [ className , ...valuesArray ] ) )
128
128
. catch ( error => {
129
129
if ( error . code === PostgresDuplicateRelationError ) {
130
130
// Table already exists, must have been created by a different request. Ignore error.
131
131
} else {
132
132
throw error ;
133
133
}
134
134
} )
135
- . then ( ( ) => this . _client . query ( 'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)' , { className, schema } ) )
135
+ . then ( ( ) => this . _client . none ( 'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)' , { className, schema } ) )
136
136
. then ( ( ) => schema ) ;
137
137
}
138
138
139
139
addFieldIfNotExists ( className , fieldName , type ) {
140
140
// TODO: Must be revised for invalid logic...
141
141
return this . _client . tx ( "addFieldIfNotExists" , t => {
142
- return t . query ( 'ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>' , {
142
+ return t . none ( 'ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>' , {
143
143
className,
144
144
fieldName,
145
145
postgresType : parseTypeToPostgresType ( type )
@@ -154,13 +154,13 @@ export class PostgresStorageAdapter {
154
154
throw error ;
155
155
}
156
156
} )
157
- . then ( ( ) => t . query ( 'SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>' , { className} ) )
157
+ . then ( ( ) => t . any ( 'SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>' , { className} ) )
158
158
. then ( result => {
159
159
if ( fieldName in result [ 0 ] . schema ) {
160
160
throw "Attempted to add a field that already exists" ;
161
161
} else {
162
162
result [ 0 ] . schema . fields [ fieldName ] = type ;
163
- return t . query (
163
+ return t . none (
164
164
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>' ,
165
165
{ schema : result [ 0 ] . schema , className}
166
166
) ;
@@ -177,7 +177,7 @@ export class PostgresStorageAdapter {
177
177
178
178
// Delete all data known to this adapter. Used for testing.
179
179
deleteAllClasses ( ) {
180
- return this . _client . query ( 'SELECT "className" FROM "_SCHEMA"' )
180
+ return this . _client . any ( 'SELECT "className" FROM "_SCHEMA"' )
181
181
. then ( results => {
182
182
const classes = [ '_SCHEMA' , ...results . map ( result => result . className ) ] ;
183
183
return this . _client . tx ( t => t . batch ( classes . map ( className => t . none ( 'DROP TABLE $<className:name>' , { className } ) ) ) ) ;
@@ -220,7 +220,7 @@ export class PostgresStorageAdapter {
220
220
// this adapter doesn't know about the schema, return a promise that rejects with
221
221
// undefined as the reason.
222
222
getClass ( className ) {
223
- return this . _client . query ( 'SELECT * FROM "_SCHEMA" WHERE "className"=$<className>' , { className } )
223
+ return this . _client . any ( 'SELECT * FROM "_SCHEMA" WHERE "className"=$<className>' , { className } )
224
224
. then ( result => {
225
225
if ( result . length === 1 ) {
226
226
return result [ 0 ] . schema ;
@@ -271,7 +271,7 @@ export class PostgresStorageAdapter {
271
271
let valuesPattern = valuesArray . map ( ( val , index ) => `$${ index + 2 + columnsArray . length } ${ ( [ '_rperm' , '_wperm' ] . includes ( columnsArray [ index ] ) ) ? '::text[]' : '' } ` ) . join ( ',' ) ;
272
272
let qs = `INSERT INTO $1:name (${ columnsPattern } ) VALUES (${ valuesPattern } )`
273
273
let values = [ className , ...columnsArray , ...valuesArray ]
274
- return this . _client . query ( qs , values )
274
+ return this . _client . none ( qs , values )
275
275
. then ( ( ) => ( { ops : [ object ] } ) )
276
276
. catch ( error => {
277
277
if ( error . code === PostgresUniqueIndexViolationError ) {
@@ -286,7 +286,7 @@ export class PostgresStorageAdapter {
286
286
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
287
287
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
288
288
deleteObjectsByQuery ( className , schema , query ) {
289
- return this . _client . one ( `WITH deleted AS (DELETE FROM $<className:name> RETURNING *) SELECT count(*) FROM deleted` , { className } , res => parseInt ( res . count ) )
289
+ return this . _client . one ( `WITH deleted AS (DELETE FROM $<className:name> RETURNING *) SELECT count(*) FROM deleted` , { className } , a => + a . count )
290
290
. then ( count => {
291
291
if ( count === 0 ) {
292
292
throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
@@ -343,7 +343,7 @@ export class PostgresStorageAdapter {
343
343
values . push ( ...where . values ) ;
344
344
345
345
let qs = `UPDATE $1:name SET ${ updatePatterns . join ( ',' ) } WHERE ${ where . pattern } RETURNING *` ;
346
- return this . _client . query ( qs , values )
346
+ return this . _client . any ( qs , values )
347
347
. then ( val => val [ 0 ] ) ; // TODO: This is unsafe, verification is needed, or a different query method;
348
348
}
349
349
@@ -364,7 +364,7 @@ export class PostgresStorageAdapter {
364
364
if ( limit !== undefined ) {
365
365
values . push ( limit ) ;
366
366
}
367
- return this . _client . query ( qs , values )
367
+ return this . _client . any ( qs , values )
368
368
. then ( results => results . map ( object => {
369
369
Object . keys ( schema . fields ) . filter ( field => schema . fields [ field ] . type === 'Pointer' ) . forEach ( fieldName => {
370
370
object [ fieldName ] = { objectId : object [ fieldName ] , __type : 'Pointer' , className : schema . fields [ fieldName ] . targetClass } ;
@@ -407,7 +407,7 @@ export class PostgresStorageAdapter {
407
407
const constraintName = `unique_${ fieldNames . sort ( ) . join ( '_' ) } ` ;
408
408
const constraintPatterns = fieldNames . map ( ( fieldName , index ) => `$${ index + 3 } :name` ) ;
409
409
const qs = `ALTER TABLE $1:name ADD CONSTRAINT $2:name UNIQUE (${ constraintPatterns . join ( ',' ) } )` ;
410
- return this . _client . query ( qs , [ className , constraintName , ...fieldNames ] )
410
+ return this . _client . none ( qs , [ className , constraintName , ...fieldNames ] )
411
411
. catch ( error => {
412
412
if ( error . code === PostgresDuplicateRelationError && error . message . includes ( constraintName ) ) {
413
413
// Index already exists. Ignore error.
@@ -424,9 +424,8 @@ export class PostgresStorageAdapter {
424
424
values . push ( ...where . values ) ;
425
425
426
426
const wherePattern = where . pattern . length > 0 ? `WHERE ${ where . pattern } ` : '' ;
427
- const qs = `SELECT COUNT(*) FROM $1:name ${ wherePattern } ` ;
428
- return this . _client . query ( qs , values )
429
- . then ( result => parseInt ( result [ 0 ] . count ) )
427
+ const qs = `SELECT count(*) FROM $1:name ${ wherePattern } ` ;
428
+ return this . _client . one ( qs , values , a => + a . count ) ;
430
429
}
431
430
}
432
431
0 commit comments