Skip to content

Commit 89945ce

Browse files
vitaly-tflovilmart
authored andcommitted
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.
1 parent 9c4c6b4 commit 89945ce

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -843,35 +843,26 @@ export class PostgresStorageAdapter implements StorageAdapter {
843843
// Returns a Promise.
844844
deleteFields(className: string, schema: SchemaType, fieldNames: string[]): Promise<void> {
845845
debug('deleteFields', className, fieldNames);
846-
return Promise.resolve()
847-
.then(() => {
848-
fieldNames = fieldNames.reduce((list: string[], fieldName: string) => {
849-
const field = schema.fields[fieldName]
850-
if (field.type !== 'Relation') {
851-
list.push(fieldName);
852-
}
853-
delete schema.fields[fieldName];
854-
return list;
855-
}, []);
856-
857-
const values = [className, ...fieldNames];
858-
const columns = fieldNames.map((name, idx) => {
859-
return `$${idx + 2}:name`;
860-
}).join(', DROP COLUMN');
861-
862-
const doBatch = (t) => {
863-
const batch = [
864-
t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className})
865-
];
866-
if (values.length > 1) {
867-
batch.push(t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values));
868-
}
869-
return batch;
870-
}
871-
return this._client.tx((t) => {
872-
return t.batch(doBatch(t));
873-
});
874-
});
846+
fieldNames = fieldNames.reduce((list, fieldName) => {
847+
const field = schema.fields[fieldName]
848+
if (field.type !== 'Relation') {
849+
list.push(fieldName);
850+
}
851+
delete schema.fields[fieldName];
852+
return list;
853+
}, []);
854+
855+
const values = [className, ...fieldNames];
856+
const columns = fieldNames.map((name, idx) => {
857+
return `$${idx + 2}:name`;
858+
}).join(', DROP COLUMN');
859+
860+
return this._client.tx('delete-fields', function * (t) {
861+
yield t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className});
862+
if (values.length > 1) {
863+
yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values);
864+
}
865+
});
875866
}
876867

877868
// Return a promise for all schemas known to this adapter, in Parse format. In case the

0 commit comments

Comments
 (0)