Skip to content

feat(repository): upgrade hydrateMany so that it batches queries per … #44

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name: Tests

# Controls when the action will run.
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
Expand All @@ -26,22 +26,22 @@ jobs:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: env sync
run: cp .env.dist .env && export $(cat ./.env | xargs)
run: cp .env.dist .env && export $(cat ./.env | xargs)

# Runs a single command using the runners shell
- name: build docker db
run: docker-compose up -d
run: docker compose up -d

- name: install
run: yarn install --ignore-scripts

- name: build
run: yarn build

- name: check docker
run: docker-compose up -d
run: docker compose up -d

# Runs a set of commands using the runners shell
- name: tests
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: "3"

services:
db:
image: mysql:5.7
env_file: .env
ports:
ports:
- 3306:3306
volumes:
- db_data:/var/lib/mysql
Expand Down
122 changes: 121 additions & 1 deletion src/__tests__/polymorphic.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSource, Repository } from 'typeorm';
import { DataSource, DeepPartial } from 'typeorm';
import { AdvertEntity } from './entities/advert.entity';
import { UserEntity } from './entities/user.entity';
import { config } from 'dotenv';
Expand Down Expand Up @@ -324,4 +324,124 @@ describe('AbstractPolymorphicRepository', () => {
});
});
});

describe('Batch', () => {
describe('hydrate', () => {
it('Can hydrate entities with polymorphic relationships properly', async () => {
const advertRepository = AbstractPolymorphicRepository.createRepository(
connection,
AdvertRepository,
);
const userRepository = AbstractPolymorphicRepository.createRepository(
connection,
UserRepository,
);
const merchantRepository = connection.getRepository(MerchantEntity);

const user = await userRepository.save(new UserEntity());
const user2 = await userRepository.save(new UserEntity());
const user3 = await userRepository.save(new UserEntity());
const merchant = await merchantRepository.save(new MerchantEntity());
const merchant2 = await merchantRepository.save(new MerchantEntity());

type ManifestItem = {
config: DeepPartial<AdvertEntity>;
advert: AdvertEntity | null;
user: UserEntity | null;
};
const manifest: Array<ManifestItem> = [
{ config: { owner: user }, advert: null, user: user },
{ config: { owner: user2 }, advert: null, user: user2 },
{ config: { owner: user3 }, advert: null, user: user3 },
{ config: { owner: merchant }, advert: null, user: null },
{ config: { owner: merchant2 }, advert: null, user: null },
{ config: { creator: merchant2 }, advert: null, user: null },
{
config: { owner: user, creator: merchant2 },
advert: null,
user: user,
},
{
config: { owner: user2, creator: merchant2 },
advert: null,
user: user2,
},
];

// save all the items first the maximize the chance of
// hydration errors
for (const item of manifest) {
const entity = await advertRepository.save(
advertRepository.create(item.config),
);
item.advert = entity;
}

/********************************
* test advert hydration (parent)
********************************/
const adverts = await advertRepository.find();
const advertManifestMap = manifest.reduce((acc, item) => {
if (item.advert) {
acc[item.advert.id] = item;
}
return acc;
}, {});

for (const advert of adverts) {
const manifestItem = advertManifestMap[advert.id];
if (!manifestItem) {
throw new Error('this should not happen.');
}

const {
config: { owner, creator },
} = manifestItem;
if (owner) {
expect(advert.owner).toBeInstanceOf(owner.constructor);
expect(advert.owner.id).toBe(owner.id);
}

if (creator) {
expect(advert.creator).toBeInstanceOf(creator.constructor);
expect(advert.creator.id).toBe(creator.id);
}
}

/********************************
* test user hydration (child)
********************************/
const users = await userRepository.find();
const usersAdvertMap = manifest.reduce<Record<number, AdvertEntity[]>>(
(acc, item) => {
if (item.user && item.advert) {
acc[item.user.id] = acc[item.user.id] || [];
acc[item.user.id].push(item.advert);
}
return acc;
},
{},
);

for (const user of users) {
const adverts = usersAdvertMap[user.id];
if (!adverts || !adverts.length) {
throw new Error('this should not happen.');
}

const actualIds = user.adverts
.map((advert) => {
return advert.id;
})
.sort();
const expectedIds = adverts
.map((advert) => {
return advert.id;
})
.sort();
expect(actualIds).toEqual(expectedIds);
}
});
});
});
});
Loading