Skip to content

Commit 39078e8

Browse files
vitaly-tflovilmart
authored andcommitted
Improving use of query methods. (#2353)
Improving use of query methods.
1 parent fa736f1 commit 39078e8

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class PostgresStorageAdapter {
9393
}
9494

9595
_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") )')
9797
.catch(error => {
9898
if (error.code === PostgresDuplicateRelationError) {
9999
// Table already exists, must have been created by a different request. Ignore error.
@@ -124,22 +124,22 @@ export class PostgresStorageAdapter {
124124
patternsArray.push(`$${index * 2 + 2}:name $${index * 2 + 3}:raw`);
125125
});
126126
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]))
128128
.catch(error => {
129129
if (error.code === PostgresDuplicateRelationError) {
130130
// Table already exists, must have been created by a different request. Ignore error.
131131
} else {
132132
throw error;
133133
}
134134
})
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 }))
136136
.then(() => schema);
137137
}
138138

139139
addFieldIfNotExists(className, fieldName, type) {
140140
// TODO: Must be revised for invalid logic...
141141
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>', {
143143
className,
144144
fieldName,
145145
postgresType: parseTypeToPostgresType(type)
@@ -154,13 +154,13 @@ export class PostgresStorageAdapter {
154154
throw error;
155155
}
156156
})
157-
.then(() => t.query('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>', {className}))
157+
.then(() => t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>', {className}))
158158
.then(result => {
159159
if (fieldName in result[0].schema) {
160160
throw "Attempted to add a field that already exists";
161161
} else {
162162
result[0].schema.fields[fieldName] = type;
163-
return t.query(
163+
return t.none(
164164
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>',
165165
{schema: result[0].schema, className}
166166
);
@@ -177,7 +177,7 @@ export class PostgresStorageAdapter {
177177

178178
// Delete all data known to this adapter. Used for testing.
179179
deleteAllClasses() {
180-
return this._client.query('SELECT "className" FROM "_SCHEMA"')
180+
return this._client.any('SELECT "className" FROM "_SCHEMA"')
181181
.then(results => {
182182
const classes = ['_SCHEMA', ...results.map(result => result.className)];
183183
return this._client.tx(t=>t.batch(classes.map(className=>t.none('DROP TABLE $<className:name>', { className }))));
@@ -220,7 +220,7 @@ export class PostgresStorageAdapter {
220220
// this adapter doesn't know about the schema, return a promise that rejects with
221221
// undefined as the reason.
222222
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 })
224224
.then(result => {
225225
if (result.length === 1) {
226226
return result[0].schema;
@@ -271,7 +271,7 @@ export class PostgresStorageAdapter {
271271
let valuesPattern = valuesArray.map((val, index) => `$${index + 2 + columnsArray.length}${(['_rperm','_wperm'].includes(columnsArray[index])) ? '::text[]' : ''}`).join(',');
272272
let qs = `INSERT INTO $1:name (${columnsPattern}) VALUES (${valuesPattern})`
273273
let values = [className, ...columnsArray, ...valuesArray]
274-
return this._client.query(qs, values)
274+
return this._client.none(qs, values)
275275
.then(() => ({ ops: [object] }))
276276
.catch(error => {
277277
if (error.code === PostgresUniqueIndexViolationError) {
@@ -286,7 +286,7 @@ export class PostgresStorageAdapter {
286286
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
287287
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
288288
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)
290290
.then(count => {
291291
if (count === 0) {
292292
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
@@ -343,7 +343,7 @@ export class PostgresStorageAdapter {
343343
values.push(...where.values);
344344

345345
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)
347347
.then(val => val[0]); // TODO: This is unsafe, verification is needed, or a different query method;
348348
}
349349

@@ -364,7 +364,7 @@ export class PostgresStorageAdapter {
364364
if (limit !== undefined) {
365365
values.push(limit);
366366
}
367-
return this._client.query(qs, values)
367+
return this._client.any(qs, values)
368368
.then(results => results.map(object => {
369369
Object.keys(schema.fields).filter(field => schema.fields[field].type === 'Pointer').forEach(fieldName => {
370370
object[fieldName] = { objectId: object[fieldName], __type: 'Pointer', className: schema.fields[fieldName].targetClass };
@@ -407,7 +407,7 @@ export class PostgresStorageAdapter {
407407
const constraintName = `unique_${fieldNames.sort().join('_')}`;
408408
const constraintPatterns = fieldNames.map((fieldName, index) => `$${index + 3}:name`);
409409
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])
411411
.catch(error => {
412412
if (error.code === PostgresDuplicateRelationError && error.message.includes(constraintName)) {
413413
// Index already exists. Ignore error.
@@ -424,9 +424,8 @@ export class PostgresStorageAdapter {
424424
values.push(...where.values);
425425

426426
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);
430429
}
431430
}
432431

0 commit comments

Comments
 (0)