Skip to content

Commit

Permalink
fix: [aurora-data-api] Return number of affected rows in UpdatedResul…
Browse files Browse the repository at this point in the history
…t and DeleteResult (#7433)

Closes: #7386
  • Loading branch information
coyoteecd committed Mar 29, 2021
1 parent fc8af5f commit 46aba1d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/query-builder/DeleteQueryBuilder.ts
Expand Up @@ -73,10 +73,14 @@ export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements
const result = await queryRunner.query(sql, parameters);

const driver = queryRunner.connection.driver;
if (driver instanceof MysqlDriver || driver instanceof AuroraDataApiDriver) {
if (driver instanceof MysqlDriver) {
deleteResult.raw = result;
deleteResult.affected = result.affectedRows;

} else if (driver instanceof AuroraDataApiDriver) {
deleteResult.raw = result;
deleteResult.affected = result.numberOfRecordsUpdated;

} else if (driver instanceof SqlServerDriver || driver instanceof PostgresDriver || driver instanceof CockroachDriver) {
deleteResult.raw = result[0] ? result[0] : null;
// don't return 0 because it could confuse. null means that we did not receive this value
Expand Down
4 changes: 4 additions & 0 deletions src/query-builder/UpdateQueryBuilder.ts
Expand Up @@ -110,6 +110,10 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
updateResult.raw = result;
updateResult.affected = result.affectedRows;
}
else if (this.connection.driver instanceof AuroraDataApiDriver) {
updateResult.raw = result;
updateResult.affected = result.numberOfRecordsUpdated;
}
else if (this.connection.driver instanceof BetterSqlite3Driver) { // only works for better-sqlite3
updateResult.raw = result;
updateResult.affected = result.changes;
Expand Down
2 changes: 1 addition & 1 deletion test/github-issues/1308/issue-1308.ts
Expand Up @@ -11,7 +11,7 @@ describe("github issues > #1308 Raw Postgresql Update query result is always an
(connections = await createTestingConnections({
entities: [new EntitySchema<Author>(AuthorSchema), new EntitySchema<Post>(PostSchema)],
dropSchema: true,
enabledDrivers: ["postgres", "mysql", "mariadb"],
enabledDrivers: ["postgres", "mysql", "mariadb", "aurora-data-api"],
}))
);
beforeEach(() => reloadTestingDatabases(connections));
Expand Down
12 changes: 5 additions & 7 deletions test/github-issues/2499/issue-2499.ts
Expand Up @@ -11,18 +11,16 @@ describe("github issues > #2499 Postgres DELETE query result is useless", () =>
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
// skip test for sqlite because sqlite doesn't return any data on delete
// sqljs -- the same
// mongodb requires another test and it is also doesn't return correct number
// of removed documents (possibly a bug with mongodb itself)
enabledDrivers: ["mysql", "mariadb", "mssql", "postgres", "aurora-data-api"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should return correct number of affected rows for mysql, mariadb, postgres", () => Promise.all(connections.map(async connection => {
// skip test for sqlite because sqlite doesn't return any data on delete
// sqljs -- the same
// mongodb requires another test and it is also doesn't return correct number
// of removed documents (possibly a bug with mongodb itself)
if (["mysql", "mariadb", "mssql", "postgres"].indexOf(connection.name) === -1) {
return;
}
const repo = connection.getRepository(Foo);

await repo.save({ id: 1, description: "test1" });
Expand Down

0 comments on commit 46aba1d

Please sign in to comment.