-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06f1e5d
commit 633f97b
Showing
17 changed files
with
129 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
{ | ||
"name": "awesome-nestjs-boilerplate", | ||
"version": "10.0.0", | ||
"description": "Awesome NestJS Boilerplate, Typescript, Postgres, TypeORM", | ||
"description": "Awesome NestJS Boilerplate, Typescript, Postgres, MikroOrm", | ||
"author": "Narek Hakobyan <[email protected]>", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"build:prod": "nest build", | ||
"start:dev": "ts-node src/main.ts", | ||
"start:prod": "node dist/main.js", | ||
"typeorm": "typeorm-ts-node-esm", | ||
"migration:generate": "yarn run typeorm migration:generate -d ormconfig", | ||
"migration:create": "yarn run typeorm migration:create -d ormconfig", | ||
"new": "hygen new", | ||
"migration:revert": "yarn run typeorm migration:revert", | ||
"schema:drop": "yarn run typeorm schema:drop", | ||
"migration:generate": "mikro-orm migration:creater", | ||
"migration:create": "mikro-orm migration:creater --blank", | ||
"migration:down": "mikro-orm migration:down", | ||
"migration:fresh": "mikro-orm migration:fresh", | ||
"schema:fresh": "mikro-orm schema:fresh", | ||
"watch:dev": "ts-node-dev src/main.ts", | ||
"debug:dev": "cross-env TS_NODE_CACHE=false ts-node-dev --inspect --ignore '/^src/.*\\.spec\\.ts$/' src/main.ts", | ||
"lint": "eslint . --ext .ts", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { AnyEntity, EntityManager } from '@mikro-orm/postgresql'; | ||
import { EntityRepository } from '@mikro-orm/postgresql'; | ||
|
||
export class ExtendedEntityRepository< | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
T extends object, | ||
> extends EntityRepository<T> { | ||
persist(entity: AnyEntity | AnyEntity[]): EntityManager { | ||
return this.em.persist(entity); | ||
} | ||
|
||
async persistAndFlush(entity: AnyEntity | AnyEntity[]): Promise<void> { | ||
await this.em.persistAndFlush(entity); | ||
} | ||
|
||
remove(entity: AnyEntity): EntityManager { | ||
return this.em.remove(entity); | ||
} | ||
|
||
async removeAndFlush(entity: AnyEntity): Promise<void> { | ||
await this.em.removeAndFlush(entity); | ||
} | ||
|
||
async flush(): Promise<void> { | ||
return this.em.flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
// import { | ||
// type EntitySubscriberInterface, | ||
// EventSubscriber, | ||
// type InsertEvent, | ||
// type UpdateEvent, | ||
// } from 'typeorm'; | ||
// | ||
// import { generateHash } from '../common/utils'; | ||
// import { UserEntity } from '../modules/user/user.entity'; | ||
// | ||
// @EventSubscriber() | ||
// export class UserSubscriber implements EntitySubscriberInterface<UserEntity> { | ||
// listenTo(): typeof UserEntity { | ||
// return UserEntity; | ||
// } | ||
// | ||
// beforeInsert(event: InsertEvent<UserEntity>): void { | ||
// if (event.entity.password) { | ||
// event.entity.password = generateHash(event.entity.password); | ||
// } | ||
// } | ||
// | ||
// beforeUpdate(event: UpdateEvent<UserEntity>): void { | ||
// // FIXME check event.databaseEntity.password | ||
// const entity = event.entity as UserEntity; | ||
// | ||
// if (entity.password !== event.databaseEntity.password) { | ||
// entity.password = generateHash(entity.password!); | ||
// } | ||
// } | ||
// } | ||
import type { | ||
EventArgs, | ||
EventSubscriber, | ||
FlushEventArgs, | ||
} from '@mikro-orm/core'; | ||
import { EntityManager } from '@mikro-orm/postgresql'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { generateHash } from '../common/utils'; | ||
import { UserEntity } from '../modules/user/user.entity'; | ||
|
||
@Injectable() | ||
export class UserSubscriber implements EventSubscriber<UserEntity> { | ||
constructor(em: EntityManager) { | ||
em.getEventManager().registerSubscriber(this); | ||
} | ||
|
||
getSubscribedEntities() { | ||
return [UserEntity]; | ||
} | ||
|
||
onFlush(args: FlushEventArgs): void { | ||
for (const changeSet of args.uow.getChangeSets()) { | ||
const changedPassword = changeSet.payload.password; | ||
|
||
if (changedPassword) { | ||
changeSet.entity.password = generateHash(changedPassword); | ||
args.uow.recomputeSingleChangeSet(changeSet.entity); | ||
} | ||
} | ||
} | ||
|
||
beforeUpdate(event: EventArgs<UserEntity>): void { | ||
const entity = event.entity; | ||
|
||
if (entity.password !== event.changeSet?.entity.password) { | ||
entity.password = generateHash(entity.password!); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import { MikroOrmModule } from '@mikro-orm/nestjs'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { UserSubscriber } from '../../entity-subscribers/user-subscriber.ts'; | ||
import { CreateSettingsHandler } from './commands/create-settings.command'; | ||
import { UserController } from './user.controller'; | ||
import { UserEntity } from './user.entity'; | ||
import { UserService } from './user.service'; | ||
import { UserSettingsEntity } from './user-settings.entity'; | ||
import { MikroOrmModule } from '@mikro-orm/nestjs'; | ||
|
||
const handlers = [CreateSettingsHandler]; | ||
|
||
@Module({ | ||
imports: [MikroOrmModule.forFeature([UserEntity, UserSettingsEntity])], | ||
controllers: [UserController], | ||
exports: [UserService], | ||
providers: [UserService, ...handlers], | ||
providers: [UserService, UserSubscriber, ...handlers], | ||
}) | ||
export class UserModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.