Skip to content

Commit 16a9aa7

Browse files
authored
refactoring database code (parse-community#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 84a91fd commit 16a9aa7

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
@@ -836,35 +836,26 @@ export class PostgresStorageAdapter {
836836
// Returns a Promise.
837837
deleteFields(className, schema, fieldNames) {
838838
debug('deleteFields', className, fieldNames);
839-
return Promise.resolve()
840-
.then(() => {
841-
fieldNames = fieldNames.reduce((list, fieldName) => {
842-
const field = schema.fields[fieldName]
843-
if (field.type !== 'Relation') {
844-
list.push(fieldName);
845-
}
846-
delete schema.fields[fieldName];
847-
return list;
848-
}, []);
849-
850-
const values = [className, ...fieldNames];
851-
const columns = fieldNames.map((name, idx) => {
852-
return `$${idx + 2}:name`;
853-
}).join(', DROP COLUMN');
854-
855-
const doBatch = (t) => {
856-
const batch = [
857-
t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className})
858-
];
859-
if (values.length > 1) {
860-
batch.push(t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values));
861-
}
862-
return batch;
863-
}
864-
return this._client.tx((t) => {
865-
return t.batch(doBatch(t));
866-
});
867-
});
839+
fieldNames = fieldNames.reduce((list, fieldName) => {
840+
const field = schema.fields[fieldName]
841+
if (field.type !== 'Relation') {
842+
list.push(fieldName);
843+
}
844+
delete schema.fields[fieldName];
845+
return list;
846+
}, []);
847+
848+
const values = [className, ...fieldNames];
849+
const columns = fieldNames.map((name, idx) => {
850+
return `$${idx + 2}:name`;
851+
}).join(', DROP COLUMN');
852+
853+
return this._client.tx('delete-fields', function * (t) {
854+
yield t.none('UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>', {schema, className});
855+
if (values.length > 1) {
856+
yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values);
857+
}
858+
});
868859
}
869860

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

0 commit comments

Comments
 (0)