This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
70 additions
and
0 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,8 +1,14 @@ | ||
```shell | ||
docker-compose up -d | ||
yarn install | ||
|
||
# reproduction.ts | ||
yarn build | ||
yarn run:ts-node | ||
yarn run:built | ||
|
||
# reproduction-alt.ts | ||
yarn run:ts-node:alt | ||
|
||
docker-compose stop | ||
``` |
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,63 @@ | ||
import "reflect-metadata"; | ||
|
||
import { BaseEntity, Entity, PrimaryKey, SerializedPrimaryKey } from "@mikro-orm/core"; | ||
import { EntityRepository, ObjectId } from "@mikro-orm/mongodb"; | ||
import { InjectRepository, MikroOrmModule } from "@mikro-orm/nestjs"; | ||
import { INestApplicationContext, Injectable, Module, Scope } from "@nestjs/common"; | ||
import { NestFactory } from "@nestjs/core"; | ||
import { LoggerModule, PinoLogger } from "nestjs-pino"; | ||
|
||
@Entity({ collection: "entities" }) | ||
export class MongoEntity extends BaseEntity<MongoEntity, "id"> { | ||
@PrimaryKey() | ||
public _id!: ObjectId; | ||
|
||
@SerializedPrimaryKey() | ||
public id!: string; | ||
} | ||
|
||
@Injectable() | ||
export class MongoService { | ||
constructor(@InjectRepository(MongoEntity) private readonly repository: EntityRepository<MongoEntity>) {} | ||
} | ||
|
||
@Module({ providers: [MongoService] }) | ||
export class MongoModule {} | ||
|
||
@Module({ | ||
imports: [ | ||
LoggerModule.forRoot(), | ||
MikroOrmModule.forRootAsync({ | ||
inject: [PinoLogger], | ||
useFactory: (logger: PinoLogger) => { | ||
logger.setContext("MikroOrm"); | ||
|
||
return { | ||
type: "mongo", | ||
clientUrl: "mongodb://127.0.0.1:27017/dbName", // run a local dockerized MongoDB v3 instance | ||
dbName: "dbName", | ||
entities: [MongoEntity], | ||
debug: ["query"], // Log all queries | ||
logger: message => logger.info(message), | ||
}; | ||
}, | ||
}), | ||
MongoModule, | ||
], | ||
}) | ||
class AppModule {} | ||
|
||
let applicationContext: INestApplicationContext; | ||
|
||
if (require.main === module) { | ||
(async () => { | ||
applicationContext = await NestFactory.createApplicationContext(AppModule); | ||
applicationContext = await applicationContext.init(); | ||
await applicationContext.resolve(MongoService); | ||
await applicationContext.close(); | ||
process.exit(0) | ||
})().catch(async error => { | ||
await applicationContext?.close(); | ||
throw error; | ||
}); | ||
} |