Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Year cosmetics :) #4475

Merged
merged 5 commits into from
Jan 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export class PostgresStorageAdapter implements StorageAdapter {

handleShutdown() {
if (!this._client) {
return
return;
}
this._client.$pool.end();
}
Expand Down Expand Up @@ -818,9 +818,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
}
// 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
Expand Down Expand Up @@ -878,12 +879,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
debug('getClass', className);
return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
.then(result => {
if (result.length === 1) {
return result[0].schema;
} else {
throw undefined;
if (result.length !== 1) {
throw undefined;
}
}).then(toParseSchema);
return result[0].schema;
})
.then(toParseSchema);
}

// TODO: remove the mongo format dependency in the return value
Expand Down Expand Up @@ -1018,11 +1019,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
err.userInfo = { duplicated_field: matches[1] };
}
}
throw err;
} else {
throw error;
error = err;
}
})
throw error;
});
}

// Remove all objects that match the given Parse Query.
Expand All @@ -1046,18 +1046,19 @@ export class PostgresStorageAdapter implements StorageAdapter {
} else {
return count;
}
}).catch((error) => {
if (error.code === PostgresRelationDoesNotExistError) {
// Don't delete anything if doesn't exist
} else {
})
.catch(error => {
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
}
// ELSE: Don't delete anything if doesn't exist
});
}
// Return value not currently well specified.
findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any> {
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.
Expand Down Expand Up @@ -1248,12 +1249,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any) {
debug('upsertOneObject', {className, 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
if (err.code === Parse.Error.DUPLICATE_VALUE) {
return this.findOneAndUpdate(className, schema, query, update);
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
throw error;
}
throw err;
return this.findOneAndUpdate(className, schema, query, update);
});
}

Expand Down Expand Up @@ -1309,12 +1311,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern}`;
debug(qs, values);
return this._client.any(qs, values)
.catch((err) => {
// Query on non existing table, don't crash
if (err.code === PostgresRelationDoesNotExistError) {
return [];
.catch(error => {
// Query on non existing table, don't crash
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
}
throw err;
return [];
})
.then(results => results.map(object => this.postgresObjectToParseObject(className, object, schema)));
}
Expand Down Expand Up @@ -1428,11 +1430,12 @@ export class PostgresStorageAdapter implements StorageAdapter {

const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
const qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
return this._client.one(qs, values, a => +a.count).catch((err) => {
if (err.code === PostgresRelationDoesNotExistError) {
return this._client.one(qs, values, a => +a.count)
.catch(error => {
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
}
return 0;
}
throw err;
});
}

Expand Down Expand Up @@ -1481,7 +1484,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
}
const child = fieldName.split('.')[1];
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) {
Expand Down