Created
May 14, 2022 06:42
-
-
Save sunnyy02/6ec7fb55f660c1a0b03acd64b88f6588 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Firstly, import MikroOrmModule in App.module | |
@Module({ | |
imports: [MikroOrmModule.forRoot(), CatsModule], | |
}) | |
// The Database configuration is store in mikro-orm.config.ts | |
const config: Options = { | |
entities: [Cat], | |
dbName: 'postgres', | |
type: 'postgresql', | |
port: 5432, | |
host: 'localhost', | |
debug: true, | |
user: 'postgres', | |
password: 'postgres', | |
} as Options; | |
// The config paths are defined in package.json | |
"mikro-orm": { | |
"useTsNode": true, | |
"configPaths": [ | |
"./src/mikro-orm.config.ts", | |
"./dist/mikro-orm.config.js" | |
] | |
} | |
// To use repository pattern, we register entities via forFeature() in feature module | |
@Module({ | |
imports: [MikroOrmModule.forFeature({ entities: [Cat] })], | |
}) | |
export class CatsModule {} | |
// We inject the repository into service | |
constructor( | |
@InjectRepository(Cat) | |
private readonly catRepository: EntityRepository<Cat>, | |
) {} | |
// Then, we can perform database operation | |
this.catRepository.findOne(findOneOptions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment