Skip to content

Commit

Permalink
chore: Use nest/typeorm package
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Mar 2, 2024
1 parent fec17bc commit 8b0f9a7
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 121 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.2.9",
"@nestjs/swagger": "^7.1.16",
"@nestjs/typeorm": "^10.0.2",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.3",
"axios": "^1.6.2",
Expand Down
21 changes: 20 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,27 @@ import { Module } from '@nestjs/common';
import { LanguageModule } from './languages/language.module';
import { SharedModule } from './shared/shared.module';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { entitySchemas } from './shared/_dependencyInjection/entitySchemas';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), SharedModule, LanguageModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.POSTGRESQL_DB_URL,
synchronize: false,
logging: true,
entities: entitySchemas,
subscribers: [],
migrationsRun: true,
autoLoadEntities: false,
logger: 'advanced-console',
migrations: [`${__dirname}../migrations/**/*{.ts,.js}`],
poolSize: 10,
}),
SharedModule,
LanguageModule,
],
})
export class AppModule {}
3 changes: 0 additions & 3 deletions src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { rabbitMqConfig } from './shared/infrastructure/messenger/rabbitMq/config';
import { DataSourceHandler } from './shared/infrastructure/persistence/typeOrm/dataSourceHandler';
import * as dotenv from 'dotenv';

async function bootstrap() {
Expand All @@ -13,8 +12,6 @@ async function bootstrap() {
options: rabbitMqConfig,
});

await DataSourceHandler.getInstance().initialize();

await rabbitMq.listen();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AuthSession from '@src/languages/domain/auth/authSession';
import AuthSessionId from '@src/languages/domain/auth/authSessionId';
import SessionTransformer from '../transformers/sessionTransformer';

export default new EntitySchema<AuthSession>({
export const AuthSessionSchema = new EntitySchema<AuthSession>({
name: AuthSession.name,
tableName: 'auth_sessions',
target: AuthSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValueObjectTransformer } from '@src/shared/infrastructure/persistence/t
import LanguageCollectionTransformer from '../transformers/languageCollectionTransformer';
import LanguageCollection from '@src/languages/domain/country/languageCollection';

export default new EntitySchema<Country>({
export const CountrySchema = new EntitySchema<Country>({
name: Country.name,
tableName: 'countries',
target: Country,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { EntitySchema } from 'typeorm';
import Expression from '@src/languages/domain/term/expression/expression';
import ExpressionTermCollectionTransformer from '../transformers/expressionTermCollectionTransformer';
import ExpressionTermCollection from '@src/languages/domain/term/expression/expressionTermCollection';
import { termSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/term';
import { TermSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/term';

export default new EntitySchema<Expression>({
export const ExpressionSchema = new EntitySchema<Expression>({
name: Expression.name,
tableName: 'expressions',
target: Expression,
type: 'entity-child',
columns: {
...termSchema.options.columns,
...TermSchema.options.columns,
terms: {
type: 'json',
transformer: new ExpressionTermCollectionTransformer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EntitySchema } from 'typeorm';
import Term from '@src/languages/domain/term/term';
import TermId from '@src/languages/domain/term/termId';

export const termSchema = new EntitySchema<Term>({
export const TermSchema = new EntitySchema<Term>({
name: Term.name,
target: Term,
columns: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import UserId from '@src/languages/domain/user/userId';
import Email from '@src/shared/domain/valueObjects/email';
import { ValueObjectTransformer } from '@src/shared/infrastructure/persistence/typeOrm/transformers/valueObjectTransformer';

export default new EntitySchema<User>({
export const UserSchema = new EntitySchema<User>({
name: User.name,
tableName: 'users',
target: User,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { EntitySchema } from 'typeorm';
import Word from '@src/languages/domain/term/word/word';
import WordTermCollectionTransformer from '../transformers/wordTermCollectionTransformer';
import WordTermCollection from '@src/languages/domain/term/word/wordTermCollection';
import { termSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/term';
import { TermSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/term';

export default new EntitySchema<Word>({
export const WordSchema = new EntitySchema<Word>({
name: Word.name,
tableName: 'words',
target: Word,
type: 'entity-child',
columns: {
...termSchema.options.columns,
...TermSchema.options.columns,
terms: {
type: 'json',
transformer: new WordTermCollectionTransformer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import TypeOrmRepository from '@src/shared/infrastructure/persistence/typeOrm/ty

@Injectable()
export default class TypeOrmAuthSessionRepository extends TypeOrmRepository implements AuthSessionRepository {
constructor() {
super();
}

async save(authSession: AuthSession): Promise<any> {
return await this.em.save(authSession);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import TypeOrmRepository from '@src/shared/infrastructure/persistence/typeOrm/ty

@Injectable()
export default class TypeOrmCountryRepository extends TypeOrmRepository implements CountryRepository {
constructor() {
super();
}

async findAll(): Promise<Country[]> {
return await this.em.find(Country);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import Term from '@src/languages/domain/term/term';

@Injectable()
export default class TypeOrmTermRepository extends TypeOrmRepository implements TermRepository {
constructor() {
super();
}

async findById(id: TermId): Promise<Term | null> {
return await this.em.findOne(Term, { where: { id: id } } as any);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export default class TypeOrmUserRepository extends TypeOrmRepository implements UserRepository {
constructor() {
super();
}

async findById(id: UserId): Promise<User | null> {
return await this.em.findOne(User, { where: { id: id } } as any);
}
Expand Down
5 changes: 4 additions & 1 deletion src/languages/language.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import { projections } from '@src/languages/_dependencyInjection/projectionHandl
import { readLayers } from '@src/languages/_dependencyInjection/readLayers';
import { services as LanguageServices } from '@src/languages/_dependencyInjection/services';
import NestProjectionBus from '@src/shared/infrastructure/bus/nestProjectionBus';
import { TypeOrmModule } from '@nestjs/typeorm';
import { entitySchemas } from '@src/shared/_dependencyInjection/entitySchemas';

@Module({
imports: [],
imports: [TypeOrmModule.forFeature(entitySchemas)],
exports: [TypeOrmModule],
controllers: [...controllers],
providers: [
...services,
Expand Down
5 changes: 1 addition & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import Logger, { LOGGER } from './shared/domain/logger';
import { DataSourceHandler } from './shared/infrastructure/persistence/typeOrm/dataSourceHandler';
import { ValidationPipe } from '@nestjs/common';
import helmet from 'helmet';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
Expand Down Expand Up @@ -38,13 +37,11 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await DataSourceHandler.getInstance().initialize();

const logger: Logger = app.get(LOGGER);

await app.init();

app.listen(port, () => {
await app.listen(port, () => {
logger.log(`App is running at http://localhost:${port} in ${process.env.NODE_ENV} mode`);
console.log('Press CTRL-C to stop\n');
});
Expand Down
8 changes: 8 additions & 0 deletions src/shared/_dependencyInjection/entitySchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AuthSessionSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/authSession';
import { CountrySchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/country';
import { ExpressionSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/expression';
import { TermSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/term';
import { WordSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/word';
import { UserSchema } from '@src/languages/infrastructure/persistence/typeOrm/entities/user';

export const entitySchemas = [AuthSessionSchema, CountrySchema, TermSchema, ExpressionSchema, WordSchema, UserSchema];
4 changes: 4 additions & 0 deletions src/shared/_dependencyInjection/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import { EVENT_STORE_REPOSITORY } from '@src/shared/domain/eventStore/eventStore
import MongoEventStoreRepository from '@src/shared/infrastructure/persistence/mongo/repositories/mongoEventStoreRepository';
import { PersistDomainEventsSubscriber } from '@src/shared/infrastructure/subscribers/persistDomainEventsSubscriber';
import MongoConnection from '@src/shared/infrastructure/persistence/mongo/mongoConnection';
import { TypeOrmTransactionalEntityManager } from '@src/shared/infrastructure/persistence/typeOrm/typeOrmTransactionalEntityManager';
import TypeOrmTransactionalDecorator from '@src/shared/infrastructure/persistence/typeOrm/typeOrmTransactionalDecorator';

export const services = [
NestJwtAuthGuard,
JwtStrategy,
NestProjectionBus,
TypeOrmTransactionalEntityManager,
TypeOrmTransactionalDecorator,
{
provide: 'MONGO_CLIENT',
useFactory: async () => {
Expand Down
8 changes: 6 additions & 2 deletions src/shared/infrastructure/bus/nestCommandBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import TypeOrmTransactionalDecorator from '@src/shared/infrastructure/persistenc

@Injectable()
export default class NestCommandBus implements ICommandBus {
constructor(private commandBus: CommandBus) {}
constructor(private commandBus: CommandBus, private readonly transactionalDecorator: TypeOrmTransactionalDecorator) {}

async dispatch(command: Command): Promise<void> {
await new TypeOrmTransactionalDecorator(this.commandBus).execute(command);
await this.transactionalDecorator.execute(this.commandToExecute.bind(this, command));
}

private async commandToExecute(command: Command): Promise<void> {
await this.commandBus.execute(command);
}
}
12 changes: 0 additions & 12 deletions src/shared/infrastructure/persistence/typeOrm/dataSource.ts

This file was deleted.

61 changes: 0 additions & 61 deletions src/shared/infrastructure/persistence/typeOrm/dataSourceHandler.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { EntityManager } from 'typeorm';
import { DataSourceHandler } from './dataSourceHandler';
import { TypeOrmTransactionalEntityManager } from '@src/shared/infrastructure/persistence/typeOrm/typeOrmTransactionalEntityManager';
import { Injectable } from '@nestjs/common';

@Injectable()
export default abstract class TypeOrmRepository {
constructor(private readonly transactionalEntityManager: TypeOrmTransactionalEntityManager) {}

get em(): EntityManager {
return DataSourceHandler.getInstance().entityManager;
return this.transactionalEntityManager.em();
}
}
Loading

0 comments on commit 8b0f9a7

Please sign in to comment.