Skip to content

Commit

Permalink
refactor: separate command query datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
haroldcohen committed Nov 15, 2023
1 parent 9c5ace4 commit b7284cb
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# TYPEORM

TYPEORM_MASTER_DATABASE_HOST="localhost"
TYPEORM_MASTER_DATABASE_PORT=5432
TYPEORM_MASTER_DATABASE_NAME="megahertz"
TYPEORM_MASTER_DATABASE_PASSWORD="password"
TYPEORM_MASTER_DATABASE_USER="user"
TYPEORM_MASTER_DROP_BEFORE_SYNCHRONIZE=true
TYPEORM_MASTER_LOGGING=false

TYPEORM_DATABASE_HOST="localhost"
TYPEORM_DATABASE_PORT=5432
TYPEORM_DATABASE_NAME="megahertz"
Expand Down
22 changes: 18 additions & 4 deletions src/configuration/injection/containers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ import 'dotenv/config';
/**
* Configures tsyringe to instantiate a TypeORM DataSource using an env setting file.*
*/
const useAppDataSource = (): void => {
const useAppDataSources = (): void => {
// Most of the variables are currently not typed or parsed correctly
// See ticket https://github.com/thetribeio/megahertz/issues/15
const appDataSource = new DataSource({
const commandAppDataSource = new DataSource({
type: "postgres",
host: process.env.TYPEORM_MASTER_DATABASE_HOST as string,
port: Number(process.env.TYPEORM_MASTER_DATABASE_PORT),
username: process.env.TYPEORM_MASTER_DATABASE_USER as string,
password: process.env.TYPEORM_MASTER_DATABASE_PASSWORD as string,
database: process.env.TYPEORM_MASTER_DATABASE_NAME as string,
synchronize: true,
logging: false,
entities: ['src/driven/repositories/typeorm/entities/*.{ts,js}'],
subscribers: [],
migrations: ['src/driven/repositories/typeorm/migrations/*.{ts,js}'],
});
const queryAppDataSource = new DataSource({
type: "postgres",
host: process.env.TYPEORM_DATABASE_HOST as string,
port: Number(process.env.TYPEORM_DATABASE_PORT),
Expand All @@ -21,7 +34,8 @@ const useAppDataSource = (): void => {
subscribers: [],
migrations: ['src/driven/repositories/typeorm/migrations/*.{ts,js}'],
});
container.register("DataSource", {useValue: appDataSource});
container.register("QueryDataSource", {useValue: queryAppDataSource});
container.register("CommandDataSource", {useValue: commandAppDataSource});
}

export default useAppDataSource;
export default useAppDataSources;
2 changes: 1 addition & 1 deletion src/driven/repositories/typeorm/carRental/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class TypeORMCarRentalReadRepository implements CarRentalReadRepo

private readonly dataSource: DataSource;

constructor(@inject("DataSource") dataSource: DataSource) {
constructor(@inject("QueryDataSource") dataSource: DataSource) {
this.dataSource = dataSource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import TypeORMCustomerFactory from 'tests/integration/typeorm/seeding/factories/
import TypeORMCarModelFactory from 'tests/integration/typeorm/seeding/factories/carModel';
import TypeORMCarFactory from 'tests/integration/typeorm/seeding/factories/car';
import TypeORMCarRentalFactory from 'tests/integration/typeorm/seeding/factories/carRental';
import useAppDataSource from 'src/configuration/injection/containers/database';
import useAppDataSources from 'src/configuration/injection/containers/database';
import {runDataSourceBeforeEachOps} from 'tests/integration/typeorm/utils/setup';
import {runDataSourceAfterEachOps} from 'tests/integration/typeorm/utils/tearDown';

Expand Down Expand Up @@ -63,7 +63,7 @@ describe.each([
advanceTo(Date.now());
useTestingUtilities();
dateParser = container.resolve("DateParser");
useAppDataSource();
useAppDataSources();
useTypeORMRepositories();
expressApp = createApp();
server = startServer(expressApp);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/typeorm/carRental/query/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {container} from 'tsyringe';
import {v4} from 'uuid';
import {advanceTo} from 'jest-date-mock';
import useTypeORMRepositories from 'src/configuration/injection/containers/repositories/typeorm';
import useAppDataSource from 'src/configuration/injection/containers/database';
import useAppDataSources from 'src/configuration/injection/containers/database';
import DateParser from 'tests/utils/dateParser';
import useTestingUtilities from 'tests/configuration/containers/utils';
import TypeORMCarRentalReadRepository from 'src/driven/repositories/typeorm/carRental/read';
Expand Down Expand Up @@ -57,7 +57,7 @@ describe.each([
advanceTo(Date.now());
useTestingUtilities();
dateParser = container.resolve("DateParser");
useAppDataSource();
useAppDataSources();
useTypeORMRepositories();
repository = container.resolve("CarRentalReadRepositoryInterface");
})
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/typeorm/seeding/factories/car.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {TypeORMCar} from 'src/driven/repositories/typeorm/entities';

export default class TypeORMCarFactory extends Factory<TypeORMCar> {
protected entity = TypeORMCar;
protected dataSource = container.resolve("DataSource") as DataSource;
protected dataSource = container.resolve("CommandDataSource") as DataSource;

protected attrs(): FactorizedAttrs<TypeORMCar> {
return {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/typeorm/seeding/factories/carModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {TypeORMCarModel} from 'src/driven/repositories/typeorm/entities';

export default class TypeORMCarModelFactory extends Factory<TypeORMCarModel> {
protected entity = TypeORMCarModel;
protected dataSource = container.resolve("DataSource") as DataSource;
protected dataSource = container.resolve("CommandDataSource") as DataSource;

protected attrs(): FactorizedAttrs<TypeORMCarModel> {
return {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/typeorm/seeding/factories/carRental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {TypeORMCar, TypeORMCarRental, TypeORMCustomer} from 'src/driven/reposito

export default class TypeORMCarRentalFactory extends Factory<TypeORMCarRental> {
protected entity = TypeORMCarRental;
protected dataSource = container.resolve("DataSource") as DataSource;
protected dataSource = container.resolve("CommandDataSource") as DataSource;

protected attrs(): FactorizedAttrs<TypeORMCarRental> {
return {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/typeorm/seeding/factories/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {TypeORMCustomer} from 'src/driven/repositories/typeorm/entities';

export default class TypeORMCustomerFactory extends Factory<TypeORMCustomer> {
protected entity = TypeORMCustomer;
protected dataSource = container.resolve("DataSource") as DataSource;
protected dataSource = container.resolve("CommandDataSource") as DataSource;

protected attrs(): FactorizedAttrs<TypeORMCustomer> {
return {
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/typeorm/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {container} from 'tsyringe';
import {DataSource} from 'typeorm';

export const runDataSourceBeforeEachOps = async () => {
const dataSource: DataSource = container.resolve("DataSource");
await dataSource.initialize();
await dataSource.synchronize();
const queryDataSource: DataSource = container.resolve("QueryDataSource");
await queryDataSource.initialize();
await queryDataSource.synchronize();
const commandDataSource: DataSource = container.resolve("CommandDataSource");
await commandDataSource.initialize();
await commandDataSource.synchronize();
}
8 changes: 5 additions & 3 deletions tests/integration/typeorm/utils/tearDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {container} from 'tsyringe';
import {DataSource} from 'typeorm';

export const runDataSourceAfterEachOps = async () => {
const dataSource: DataSource = container.resolve("DataSource");
await dataSource.dropDatabase();
await dataSource.destroy();
const queryDataSource: DataSource = container.resolve("QueryDataSource");
const commandDataSource: DataSource = container.resolve("CommandDataSource");
await commandDataSource.dropDatabase();
await queryDataSource.destroy();
await commandDataSource.destroy();
}

0 comments on commit b7284cb

Please sign in to comment.