Skip to content

Commit

Permalink
fix(core): use write connection for fetching changes after upsert/ups…
Browse files Browse the repository at this point in the history
…ertMany (#4872)

Closes #4868
  • Loading branch information
Arcus16 committed Oct 23, 2023
1 parent 1015f9e commit 6b444ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
fields: returning as EntityField<Entity>[],
ctx: em.transactionContext,
convertCustomTypes: true,
connectionType: 'write',
});
em.getHydrator().hydrate(entity, meta, data2!, em.entityFactory, 'full');
}
Expand Down Expand Up @@ -916,6 +917,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
fields: returning.concat(...add).concat(...uniqueFields as string[]),
ctx: em.transactionContext,
convertCustomTypes: true,
connectionType: 'write',
});

for (const [entity, cond] of loadPK.entries()) {
Expand Down
28 changes: 28 additions & 0 deletions tests/features/read-replicas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ describe('read-replicas', () => {
expect(mock.mock.calls[7][0]).toMatch(/begin.*via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[8][0]).toMatch(/select.*via write connection '127\.0\.0\.1'/);
});

test('use write connection for fetching data after upsert/upsertMany', async () => {
const mock = mockLogger(orm, ['query']);
await orm.em.upsert(Author2, { name: 'Jon Snow', email: 'snow@wall.st', born: new Date('1990-03-23') });
expect(mock.mock.calls[0][0]).toMatch(/via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[1][0]).toMatch(/via write connection '127\.0\.0\.1'/);

await orm.em.upsertMany(Author2, [
{ name: 'Daenerys Stormborn', email: 'dany@dragonstone.ts', born: new Date('1990-03-23') },
{ name: 'Jaime Lannister', email: 'jaime@casterlyrock.ts', born: new Date('1980-03-23') },
]);
expect(mock.mock.calls[2][0]).toMatch(/via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[3][0]).toMatch(/via write connection '127\.0\.0\.1'/);
});
});

describe('when preferReadReplicas is false', () => {
Expand Down Expand Up @@ -229,6 +243,20 @@ describe('read-replicas', () => {
expect(mock.mock.calls[7][0]).toMatch(/begin.*via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[8][0]).toMatch(/select.*via write connection '127\.0\.0\.1'/);
});

test('use write connection for fetching data after upsert/upsertMany', async () => {
const mock = mockLogger(orm, ['query']);
await orm.em.upsert(Author2, { name: 'Jon Snow', email: 'snow@wall.st', born: new Date('1990-03-23') });
expect(mock.mock.calls[0][0]).toMatch(/via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[1][0]).toMatch(/via write connection '127\.0\.0\.1'/);

await orm.em.upsertMany(Author2, [
{ name: 'Daenerys Stormborn', email: 'dany@dragonstone.ts', born: new Date('1990-03-23') },
{ name: 'Jaime Lannister', email: 'jaime@casterlyrock.ts', born: new Date('1980-03-23') },
]);
expect(mock.mock.calls[2][0]).toMatch(/via write connection '127\.0\.0\.1'/);
expect(mock.mock.calls[3][0]).toMatch(/via write connection '127\.0\.0\.1'/);
});
});

});

0 comments on commit 6b444ed

Please sign in to comment.