Skip to content

Commit fc6a2fd

Browse files
authored
New Year cosmetics :) (#4475)
* cosmetics * making nicer promise pattern + if->else blocks * removing return if unwanted data from an `UPDATE` * Update PostgresStorageAdapter.js * Update PostgresStorageAdapter.js * Update PostgresStorageAdapter.js Restoring the `UPDATE` result, as apparently it is in fact used. Ouch! 😄 * Update PostgresStorageAdapter.js
1 parent 1063137 commit fc6a2fd

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
589589

590590
handleShutdown() {
591591
if (!this._client) {
592-
return
592+
return;
593593
}
594594
this._client.$pool.end();
595595
}
@@ -818,9 +818,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
818818
}
819819
// No _SCHEMA collection. Don't delete anything.
820820
}
821-
}).then(() => {
822-
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
823-
});
821+
})
822+
.then(() => {
823+
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
824+
});
824825
}
825826

826827
// Remove the column and all the data. For Relations, the _Join collection is handled
@@ -878,12 +879,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
878879
debug('getClass', className);
879880
return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
880881
.then(result => {
881-
if (result.length === 1) {
882-
return result[0].schema;
883-
} else {
884-
throw undefined;
882+
if (result.length !== 1) {
883+
throw undefined;
885884
}
886-
}).then(toParseSchema);
885+
return result[0].schema;
886+
})
887+
.then(toParseSchema);
887888
}
888889

889890
// TODO: remove the mongo format dependency in the return value
@@ -1018,11 +1019,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
10181019
err.userInfo = { duplicated_field: matches[1] };
10191020
}
10201021
}
1021-
throw err;
1022-
} else {
1023-
throw error;
1022+
error = err;
10241023
}
1025-
})
1024+
throw error;
1025+
});
10261026
}
10271027

10281028
// Remove all objects that match the given Parse Query.
@@ -1046,18 +1046,19 @@ export class PostgresStorageAdapter implements StorageAdapter {
10461046
} else {
10471047
return count;
10481048
}
1049-
}).catch((error) => {
1050-
if (error.code === PostgresRelationDoesNotExistError) {
1051-
// Don't delete anything if doesn't exist
1052-
} else {
1049+
})
1050+
.catch(error => {
1051+
if (error.code !== PostgresRelationDoesNotExistError) {
10531052
throw error;
10541053
}
1054+
// ELSE: Don't delete anything if doesn't exist
10551055
});
10561056
}
10571057
// Return value not currently well specified.
10581058
findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any> {
10591059
debug('findOneAndUpdate', className, query, update);
1060-
return this.updateObjectsByQuery(className, schema, query, update).then((val) => val[0]);
1060+
return this.updateObjectsByQuery(className, schema, query, update)
1061+
.then((val) => val[0]);
10611062
}
10621063

10631064
// Apply the update to all objects that match the given Parse Query.
@@ -1248,12 +1249,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
12481249
upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any) {
12491250
debug('upsertOneObject', {className, query, update});
12501251
const createValue = Object.assign({}, query, update);
1251-
return this.createObject(className, schema, createValue).catch((err) => {
1252+
return this.createObject(className, schema, createValue)
1253+
.catch(error => {
12521254
// ignore duplicate value errors as it's upsert
1253-
if (err.code === Parse.Error.DUPLICATE_VALUE) {
1254-
return this.findOneAndUpdate(className, schema, query, update);
1255+
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
1256+
throw error;
12551257
}
1256-
throw err;
1258+
return this.findOneAndUpdate(className, schema, query, update);
12571259
});
12581260
}
12591261

@@ -1309,12 +1311,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
13091311
const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern}`;
13101312
debug(qs, values);
13111313
return this._client.any(qs, values)
1312-
.catch((err) => {
1313-
// Query on non existing table, don't crash
1314-
if (err.code === PostgresRelationDoesNotExistError) {
1315-
return [];
1314+
.catch(error => {
1315+
// Query on non existing table, don't crash
1316+
if (error.code !== PostgresRelationDoesNotExistError) {
1317+
throw error;
13161318
}
1317-
throw err;
1319+
return [];
13181320
})
13191321
.then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
13201322
}
@@ -1428,11 +1430,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
14281430

14291431
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
14301432
const qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
1431-
return this._client.one(qs, values, a => +a.count).catch((err) => {
1432-
if (err.code === PostgresRelationDoesNotExistError) {
1433+
return this._client.one(qs, values, a => +a.count)
1434+
.catch(error => {
1435+
if (error.code !== PostgresRelationDoesNotExistError) {
1436+
throw error;
1437+
}
14331438
return 0;
1434-
}
1435-
throw err;
14361439
});
14371440
}
14381441

@@ -1481,7 +1484,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
14811484
}
14821485
const child = fieldName.split('.')[1];
14831486
return results.map(object => object[column][child]);
1484-
}).then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
1487+
})
1488+
.then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
14851489
}
14861490

14871491
aggregate(className: string, schema: any, pipeline: any) {

0 commit comments

Comments
 (0)