From 08ba738227bc3b429652fd9f718ae0d73e479d34 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 15:58:20 +0000 Subject: [PATCH] refactoring database code (#4448) * refactoring database code Starting to refactor the database code for better use of promises + ES6 generators, to prepare for ES7 await/async. * Update PostgresStorageAdapter.js * Update PostgresStorageAdapter.js naming the transaction. --- .../Postgres/PostgresStorageAdapter.js | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 4f44c1f9..433e63ef 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -836,35 +836,26 @@ export class PostgresStorageAdapter { // Returns a Promise. deleteFields(className, schema, fieldNames) { debug('deleteFields', className, fieldNames); - return Promise.resolve() - .then(() => { - fieldNames = fieldNames.reduce((list, fieldName) => { - const field = schema.fields[fieldName] - if (field.type !== 'Relation') { - list.push(fieldName); - } - delete schema.fields[fieldName]; - return list; - }, []); + fieldNames = fieldNames.reduce((list, fieldName) => { + const field = schema.fields[fieldName] + if (field.type !== 'Relation') { + list.push(fieldName); + } + delete schema.fields[fieldName]; + return list; + }, []); - const values = [className, ...fieldNames]; - const columns = fieldNames.map((name, idx) => { - return `$${idx + 2}:name`; - }).join(', DROP COLUMN'); + const values = [className, ...fieldNames]; + const columns = fieldNames.map((name, idx) => { + return `$${idx + 2}:name`; + }).join(', DROP COLUMN'); - const doBatch = (t) => { - const batch = [ - t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}) - ]; - if (values.length > 1) { - batch.push(t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values)); - } - return batch; - } - return this._client.tx((t) => { - return t.batch(doBatch(t)); - }); - }); + return this._client.tx('delete-fields', function * (t) { + yield t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}); + if (values.length > 1) { + yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values); + } + }); } // Return a promise for all schemas known to this adapter, in Parse format. In case the