Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
feat: Alternative reproduction
Browse files Browse the repository at this point in the history
  • Loading branch information
AndKiel committed Jul 20, 2023
1 parent a992270 commit 2d2cddd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.MD
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
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"compile": "tsc",
"build": "ncc build --out dist --source-map ./reproduction.ts",
"run:ts-node": "yarn ts-node ./reproduction.ts",
"run:ts-node:alt": "yarn ts-node ./reproduction-alt.ts",
"run:built": "node ./dist/index.js"
},
"resolutions": {
Expand Down
63 changes: 63 additions & 0 deletions reproduction-alt.ts
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;
});
}

0 comments on commit 2d2cddd

Please sign in to comment.