From 72de2e2b3d2849083883d743488f312e2ce52c51 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 31 Dec 2017 18:45:33 +0000 Subject: [PATCH 1/5] cosmetics * making nicer promise pattern + if->else blocks * removing return if unwanted data from an `UPDATE` --- .../Postgres/PostgresStorageAdapter.js | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 2e06307d34..8d2d6c1f6d 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -589,7 +589,7 @@ export class PostgresStorageAdapter implements StorageAdapter { handleShutdown() { if (!this._client) { - return + return; } this._client.$pool.end(); } @@ -878,11 +878,10 @@ export class PostgresStorageAdapter implements StorageAdapter { debug('getClass', className); return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$', { className }) .then(result => { - if (result.length === 1) { - return result[0].schema; - } else { - throw undefined; + if (result.length !== 1) { + throw undefined; } + return result[0].schema; }).then(toParseSchema); } @@ -1018,11 +1017,10 @@ export class PostgresStorageAdapter implements StorageAdapter { err.userInfo = { duplicated_field: matches[1] }; } } - throw err; - } else { - throw error; + error = err; } - }) + throw error; + }); } // Remove all objects that match the given Parse Query. @@ -1046,12 +1044,12 @@ export class PostgresStorageAdapter implements StorageAdapter { } else { return count; } - }).catch((error) => { - if (error.code === PostgresRelationDoesNotExistError) { - // Don't delete anything if doesn't exist - } else { + }) + .catch(error => { + if (error.code !== PostgresRelationDoesNotExistError) { throw error; } + // ELSE: Don't delete anything if doesn't exist }); } // Return value not currently well specified. @@ -1239,21 +1237,22 @@ export class PostgresStorageAdapter implements StorageAdapter { values.push(...where.values); const whereClause = where.pattern.length > 0 ? `WHERE ${where.pattern}` : ''; - const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause} RETURNING *`; + const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause}`; debug('update: ', qs, values); - return this._client.any(qs, values); + return this._client.none(qs, values); } // Hopefully, we can get rid of this. It's only used for config and hooks. upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any) { debug('upsertOneObject', {className, query, update}); const createValue = Object.assign({}, query, update); - return this.createObject(className, schema, createValue).catch((err) => { + return this.createObject(className, schema, createValue) + .catch(error => { // ignore duplicate value errors as it's upsert - if (err.code === Parse.Error.DUPLICATE_VALUE) { - return this.findOneAndUpdate(className, schema, query, update); + if (error.code !== Parse.Error.DUPLICATE_VALUE) { + throw error; } - throw err; + return this.findOneAndUpdate(className, schema, query, update); }); } @@ -1309,13 +1308,13 @@ export class PostgresStorageAdapter implements StorageAdapter { const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern}`; debug(qs, values); return this._client.any(qs, values) - .catch((err) => { - // Query on non existing table, don't crash - if (err.code === PostgresRelationDoesNotExistError) { - return []; + .catch(error => { + // Query on non existing table, don't crash + if (error.code !== PostgresRelationDoesNotExistError) { + throw error; } - throw err; - }) + return []; + }); .then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); } @@ -1428,11 +1427,12 @@ export class PostgresStorageAdapter implements StorageAdapter { const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : ''; const qs = `SELECT count(*) FROM $1:name ${wherePattern}`; - return this._client.one(qs, values, a => +a.count).catch((err) => { - if (err.code === PostgresRelationDoesNotExistError) { + return this._client.one(qs, values, a => +a.count) + .catch(error => { + if (err.code !== PostgresRelationDoesNotExistError) { + throw err; + } return 0; - } - throw err; }); } @@ -1481,7 +1481,8 @@ export class PostgresStorageAdapter implements StorageAdapter { } const child = fieldName.split('.')[1]; return results.map(object => object[column][child]); - }).then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); + }) + .then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); } aggregate(className: string, schema: any, pipeline: any) { From 1f130156b1fb4a77ef4f74dce7c02dd9cc3497fb Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 31 Dec 2017 18:47:55 +0000 Subject: [PATCH 2/5] Update PostgresStorageAdapter.js --- .../Storage/Postgres/PostgresStorageAdapter.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 8d2d6c1f6d..b3398cf3c1 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -818,9 +818,10 @@ export class PostgresStorageAdapter implements StorageAdapter { } // No _SCHEMA collection. Don't delete anything. } - }).then(() => { - debug(`deleteAllClasses done in ${new Date().getTime() - now}`); - }); + }) + .then(() => { + debug(`deleteAllClasses done in ${new Date().getTime() - now}`); + }); } // Remove the column and all the data. For Relations, the _Join collection is handled @@ -882,7 +883,8 @@ export class PostgresStorageAdapter implements StorageAdapter { throw undefined; } return result[0].schema; - }).then(toParseSchema); + }) + .then(toParseSchema); } // TODO: remove the mongo format dependency in the return value @@ -1055,7 +1057,8 @@ export class PostgresStorageAdapter implements StorageAdapter { // Return value not currently well specified. findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise { debug('findOneAndUpdate', className, query, update); - return this.updateObjectsByQuery(className, schema, query, update).then((val) => val[0]); + return this.updateObjectsByQuery(className, schema, query, update) + .then((val) => val[0]); } // Apply the update to all objects that match the given Parse Query. From 4f5a1aaf31a263d478fe292cadb959a95c096c99 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 31 Dec 2017 19:14:08 +0000 Subject: [PATCH 3/5] Update PostgresStorageAdapter.js --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index b3398cf3c1..cecd82620a 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -1317,7 +1317,7 @@ export class PostgresStorageAdapter implements StorageAdapter { throw error; } return []; - }); + }) .then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); } From c20e8e91758345030573b15fe9443f95d0e6cd8c Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Mon, 1 Jan 2018 03:51:43 +0000 Subject: [PATCH 4/5] Update PostgresStorageAdapter.js Restoring the `UPDATE` result, as apparently it is in fact used. Ouch! :smile: --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index cecd82620a..63f1ba7c56 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -1240,9 +1240,9 @@ export class PostgresStorageAdapter implements StorageAdapter { values.push(...where.values); const whereClause = where.pattern.length > 0 ? `WHERE ${where.pattern}` : ''; - const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause}`; + const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause} RETURNING *`; debug('update: ', qs, values); - return this._client.none(qs, values); + return this._client.any(qs, values); } // Hopefully, we can get rid of this. It's only used for config and hooks. From 5628d349437004b922578807796c0bb09f126b25 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Mon, 1 Jan 2018 15:37:02 +0000 Subject: [PATCH 5/5] Update PostgresStorageAdapter.js --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 63f1ba7c56..168e452c2c 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -1432,8 +1432,8 @@ export class PostgresStorageAdapter implements StorageAdapter { const qs = `SELECT count(*) FROM $1:name ${wherePattern}`; return this._client.one(qs, values, a => +a.count) .catch(error => { - if (err.code !== PostgresRelationDoesNotExistError) { - throw err; + if (error.code !== PostgresRelationDoesNotExistError) { + throw error; } return 0; });