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
This commit is contained in:
Vitaly Tomilov
2018-01-01 20:33:41 +00:00
committed by GitHub
parent 10631371e6
commit fc6a2fddc4

View File

@@ -589,7 +589,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
handleShutdown() { handleShutdown() {
if (!this._client) { if (!this._client) {
return return;
} }
this._client.$pool.end(); this._client.$pool.end();
} }
@@ -818,9 +818,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
} }
// No _SCHEMA collection. Don't delete anything. // No _SCHEMA collection. Don't delete anything.
} }
}).then(() => { })
debug(`deleteAllClasses done in ${new Date().getTime() - now}`); .then(() => {
}); debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
});
} }
// Remove the column and all the data. For Relations, the _Join collection is handled // Remove the column and all the data. For Relations, the _Join collection is handled
@@ -878,12 +879,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
debug('getClass', className); debug('getClass', className);
return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className }) return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
.then(result => { .then(result => {
if (result.length === 1) { if (result.length !== 1) {
return result[0].schema; throw undefined;
} else {
throw undefined;
} }
}).then(toParseSchema); return result[0].schema;
})
.then(toParseSchema);
} }
// TODO: remove the mongo format dependency in the return value // TODO: remove the mongo format dependency in the return value
@@ -1018,11 +1019,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
err.userInfo = { duplicated_field: matches[1] }; err.userInfo = { duplicated_field: matches[1] };
} }
} }
throw err; error = err;
} else {
throw error;
} }
}) throw error;
});
} }
// Remove all objects that match the given Parse Query. // Remove all objects that match the given Parse Query.
@@ -1046,18 +1046,19 @@ export class PostgresStorageAdapter implements StorageAdapter {
} else { } else {
return count; return count;
} }
}).catch((error) => { })
if (error.code === PostgresRelationDoesNotExistError) { .catch(error => {
// Don't delete anything if doesn't exist if (error.code !== PostgresRelationDoesNotExistError) {
} else {
throw error; throw error;
} }
// ELSE: Don't delete anything if doesn't exist
}); });
} }
// Return value not currently well specified. // Return value not currently well specified.
findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any> { findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any> {
debug('findOneAndUpdate', className, query, update); debug('findOneAndUpdate', className, query, update);
return this.updateObjectsByQuery(className, schema, query, update).then((val) => val[0]); return this.updateObjectsByQuery(className, schema, query, update)
.then((val) => val[0]);
} }
// Apply the update to all objects that match the given Parse Query. // Apply the update to all objects that match the given Parse Query.
@@ -1248,12 +1249,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any) { upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any) {
debug('upsertOneObject', {className, query, update}); debug('upsertOneObject', {className, query, update});
const createValue = Object.assign({}, query, update); const createValue = Object.assign({}, query, update);
return this.createObject(className, schema, createValue).catch((err) => { return this.createObject(className, schema, createValue)
.catch(error => {
// ignore duplicate value errors as it's upsert // ignore duplicate value errors as it's upsert
if (err.code === Parse.Error.DUPLICATE_VALUE) { if (error.code !== Parse.Error.DUPLICATE_VALUE) {
return this.findOneAndUpdate(className, schema, query, update); throw error;
} }
throw err; return this.findOneAndUpdate(className, schema, query, update);
}); });
} }
@@ -1309,12 +1311,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern}`; const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern}`;
debug(qs, values); debug(qs, values);
return this._client.any(qs, values) return this._client.any(qs, values)
.catch((err) => { .catch(error => {
// Query on non existing table, don't crash // Query on non existing table, don't crash
if (err.code === PostgresRelationDoesNotExistError) { if (error.code !== PostgresRelationDoesNotExistError) {
return []; throw error;
} }
throw err; return [];
}) })
.then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); .then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
} }
@@ -1428,11 +1430,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : ''; const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
const qs = `SELECT count(*) FROM $1:name ${wherePattern}`; const qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
return this._client.one(qs, values, a => +a.count).catch((err) => { return this._client.one(qs, values, a => +a.count)
if (err.code === PostgresRelationDoesNotExistError) { .catch(error => {
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
}
return 0; return 0;
}
throw err;
}); });
} }
@@ -1481,7 +1484,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
} }
const child = fieldName.split('.')[1]; const child = fieldName.split('.')[1];
return results.map(object => object[column][child]); return results.map(object => object[column][child]);
}).then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema))); })
.then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
} }
aggregate(className: string, schema: any, pipeline: any) { aggregate(className: string, schema: any, pipeline: any) {