Skip to content

Instantly share code, notes, and snippets.

@sunnyy02
Created May 14, 2022 06:42
Show Gist options
  • Save sunnyy02/6ec7fb55f660c1a0b03acd64b88f6588 to your computer and use it in GitHub Desktop.
Save sunnyy02/6ec7fb55f660c1a0b03acd64b88f6588 to your computer and use it in GitHub Desktop.
// 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