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.
This commit is contained in:
Vitaly Tomilov
2017-12-24 15:58:20 +00:00
committed by GitHub
parent 33de770709
commit 08ba738227

View File

@@ -836,35 +836,26 @@ export class PostgresStorageAdapter {
// Returns a Promise. // Returns a Promise.
deleteFields(className, schema, fieldNames) { deleteFields(className, schema, fieldNames) {
debug('deleteFields', className, fieldNames); debug('deleteFields', className, fieldNames);
return Promise.resolve() fieldNames = fieldNames.reduce((list, fieldName) => {
.then(() => { const field = schema.fields[fieldName]
fieldNames = fieldNames.reduce((list, fieldName) => { if (field.type !== 'Relation') {
const field = schema.fields[fieldName] list.push(fieldName);
if (field.type !== 'Relation') { }
list.push(fieldName); delete schema.fields[fieldName];
} return list;
delete schema.fields[fieldName]; }, []);
return list;
}, []);
const values = [className, ...fieldNames]; const values = [className, ...fieldNames];
const columns = fieldNames.map((name, idx) => { const columns = fieldNames.map((name, idx) => {
return `$${idx + 2}:name`; return `$${idx + 2}:name`;
}).join(', DROP COLUMN'); }).join(', DROP COLUMN');
const doBatch = (t) => { return this._client.tx('delete-fields', function * (t) {
const batch = [ yield t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className});
t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className}) if (values.length > 1) {
]; yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values);
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 a promise for all schemas known to this adapter, in Parse format. In case the // Return a promise for all schemas known to this adapter, in Parse format. In case the