Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: make only a single SELECT to get inserted default and generated …
…values of multiple entities (#6669)

* fix: re-select inserted default and generated values with a single SELECT

closes #6266

* test: add test case for fix of #6266

* fix lint error
  • Loading branch information
dolsup committed Sep 4, 2020
1 parent ef2011d commit 4fc4a1b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/query-builder/ReturningResultsEntityUpdator.ts
Expand Up @@ -117,26 +117,29 @@ export class ReturningResultsEntityUpdator {
// for postgres and mssql we use returning/output statement to get values of inserted default and generated values
// for other drivers we have to re-select this data from the database
if (this.queryRunner.connection.driver.isReturningSqlSupported() === false && insertionColumns.length > 0) {
await Promise.all(entities.map(async (entity, entityIndex) => {
const entityIds = entities.map((entity) => {
const entityId = metadata.getEntityIdMap(entity)!;

// to select just inserted entity we need a criteria to select by.
// for newly inserted entities in drivers which do not support returning statement
// row identifier can only be an increment column
// (since its the only thing that can be generated by those databases)
// or (and) other primary key which is defined by a user and inserted value has it

const returningResult: any = await this.queryRunner.manager
.createQueryBuilder()
.select(metadata.primaryColumns.map(column => metadata.targetName + "." + column.propertyPath))
.addSelect(insertionColumns.map(column => metadata.targetName + "." + column.propertyPath))
.from(metadata.target, metadata.targetName)
.where(entityId)
.setOption("create-pojo") // use POJO because created object can contain default values, e.g. property = null and those properties maight be overridden by merge process
.getOne();

this.queryRunner.manager.merge(metadata.target as any, generatedMaps[entityIndex], returningResult);
}));
return entityId;
});

// to select just inserted entities we need a criteria to select by.
// for newly inserted entities in drivers which do not support returning statement
// row identifier can only be an increment column
// (since its the only thing that can be generated by those databases)
// or (and) other primary key which is defined by a user and inserted value has it

const returningResult: any = await this.queryRunner.manager
.createQueryBuilder()
.select(metadata.primaryColumns.map(column => metadata.targetName + "." + column.propertyPath))
.addSelect(insertionColumns.map(column => metadata.targetName + "." + column.propertyPath))
.from(metadata.target, metadata.targetName)
.where(entityIds)
.setOption("create-pojo") // use POJO because created object can contain default values, e.g. property = null and those properties maight be overridden by merge process
.getMany();

entities.forEach((entity, entityIndex) => {
this.queryRunner.manager.merge(metadata.target as any, generatedMaps[entityIndex], returningResult[entityIndex]);
});
}

entities.forEach((entity, entityIndex) => {
Expand Down
14 changes: 14 additions & 0 deletions test/github-issues/6266/entity/Post.ts
@@ -0,0 +1,14 @@
import { PrimaryGeneratedColumn, Entity, Column, CreateDateColumn } from "../../../../src";

@Entity()
export class Post {

@PrimaryGeneratedColumn()
id?: number;

@Column()
title: string;

@CreateDateColumn()
readonly createdAt?: Date;
}
60 changes: 60 additions & 0 deletions test/github-issues/6266/issue-6266.ts
@@ -0,0 +1,60 @@
import "reflect-metadata";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { Post } from "./entity/Post";
import sinon from "sinon";
import { SelectQueryBuilder } from "../../../src";
import { assert } from "chai";

describe("github issues > #6266 Many identical selects after insert bunch of items", () => {
let connections: Connection[];
const posts: Post[] = [
{
title: "Post 1",
},
{
title: "Post 2",
},
{
title: "Post 3",
},
{
title: "Post 4",
},
];

before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mysql"],
}))
);
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should execute a single SELECT to get inserted default and generated values of multiple entities", () =>
Promise.all(
connections.map(async (connection) => {
const selectSpy = sinon.spy(
SelectQueryBuilder.prototype,
"select"
);

await connection
.createQueryBuilder()
.insert()
.into(Post)
.values(posts)
.execute();

assert.strictEqual(selectSpy.calledOnce, true);

selectSpy.restore();
})
));
});

0 comments on commit 4fc4a1b

Please sign in to comment.