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

Commit

Permalink
feat: Scope demo
Browse files Browse the repository at this point in the history
  • Loading branch information
AndKiel committed Jul 21, 2023
1 parent 2d2cddd commit 5a4a934
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"compile": "tsc",
"build": "ncc build --out dist --source-map ./reproduction.ts",
"build:alt": "ncc build --out dist --source-map ./reproduction-alt.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"
Expand All @@ -27,9 +28,9 @@
"tslib": "2.6.0"
},
"devDependencies": {
"@nestjs/cli": "10.1.4",
"@nestjs/cli": "10.1.10",
"@nestjs/schematics": "10.0.1",
"@nestjs/testing": "10.0.5",
"@nestjs/testing": "10.1.0",
"@types/node": "18.16.19",
"@vercel/ncc": "0.36.1",
"ts-node": "10.9.1",
Expand Down
18 changes: 14 additions & 4 deletions reproduction-alt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export class MongoEntity extends BaseEntity<MongoEntity, "id"> {

@Injectable()
export class MongoService {
constructor(@InjectRepository(MongoEntity) private readonly repository: EntityRepository<MongoEntity>) {}
constructor(@InjectRepository(MongoEntity) public readonly repository: EntityRepository<MongoEntity>) {}
}

@Module({ providers: [MongoService] })
@Module({ imports: [MikroOrmModule.forFeature([MongoEntity])], providers: [MongoService] })
export class MongoModule {}

@Module({
Expand All @@ -34,13 +34,15 @@ export class MongoModule {}

return {
type: "mongo",
clientUrl: "mongodb://127.0.0.1:27017/dbName", // run a local dockerized MongoDB v3 instance
clientUrl: "mongodb://127.0.0.1:27017/dbName", // local dockerized MongoDB v3 instance
dbName: "dbName",
entities: [MongoEntity],
debug: ["query"], // Log all queries
logger: message => logger.info(message),
};
},
// If scope is not passed, using repository throws ValidationError
scope: Scope.REQUEST,
}),
MongoModule,
],
Expand All @@ -53,7 +55,15 @@ if (require.main === module) {
(async () => {
applicationContext = await NestFactory.createApplicationContext(AppModule);
applicationContext = await applicationContext.init();
await applicationContext.resolve(MongoService);
// If scope is passed to MikroOrmModule.forRootAsync, applicationContext.get works but repository is undefined
// .resolve must be used instead
const mongoService = await applicationContext.resolve(MongoService);
await mongoService.repository.findAll(); // With scope it works

// With scope each resolved new MongoService instance gets EntityManager with _id incremented by 1
console.info("EntityManager _id is %d", (await applicationContext.resolve(MongoService)).repository.getEntityManager()._id)
console.info("EntityManager _id is %d", (await applicationContext.resolve(MongoService)).repository.getEntityManager()._id)

await applicationContext.close();
process.exit(0)
})().catch(async error => {
Expand Down
62 changes: 31 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
rxjs "7.8.1"
source-map "0.7.4"

"@angular-devkit/[email protected].3":
version "16.1.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-16.1.3.tgz#51bceb2d3472b0458ab197ddfae3c2138120b86f"
integrity sha512-cFhNdJHumNMZGD3NYxOtNuMGRQXeDnKbwvK+IJmKAttXt8na6EvURR/ZxZOI7rl/YRVX+vcNSdtXz3hE6g+Isw==
"@angular-devkit/[email protected].4":
version "16.1.4"
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-16.1.4.tgz#033ff3ab4d024ee3d8f4c6f809eed10d716ad0ab"
integrity sha512-WCAzNi9LxpFIi2WVPaJQd2kHPqCnCexWzUZN05ltJuBGCQL1O+LgRHGwnQ4WZoqmrF5tcWt2a3GFtJ3DgMc1hw==
dependencies:
ajv "8.12.0"
ajv-formats "2.1.1"
jsonc-parser "3.2.0"
rxjs "7.8.1"
source-map "0.7.4"

"@angular-devkit/[email protected].3":
version "16.1.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics-cli/-/schematics-cli-16.1.3.tgz#ef6d84d280cd1759795d3ee544728b48a3377282"
integrity sha512-uZAi5xrvZLgQkqVsR0wxbUZgiMHVNstOnNm446Bs69iadbLymvWvdg95hYPL3IWAlYrNiwwGNSjyp6JS0Qn06Q==
"@angular-devkit/[email protected].4":
version "16.1.4"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics-cli/-/schematics-cli-16.1.4.tgz#5f626e9c50aa6d567a99569dbb74bd4f3a95f9c6"
integrity sha512-/m05+9jCV6jYcQZhDoQXo8neusE1HGU5oM+Jw2xtl3oube8vzbymhwq1SoDeMlnhMnhnxg4rMsghEgRROAq4bA==
dependencies:
"@angular-devkit/core" "16.1.3"
"@angular-devkit/schematics" "16.1.3"
"@angular-devkit/core" "16.1.4"
"@angular-devkit/schematics" "16.1.4"
ansi-colors "4.1.3"
inquirer "8.2.4"
symbol-observable "4.0.0"
Expand All @@ -47,12 +47,12 @@
ora "5.4.1"
rxjs "7.8.1"

"@angular-devkit/[email protected].3":
version "16.1.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-16.1.3.tgz#150f1531d3b7932698c0f1bd0cba59dbc9b54c82"
integrity sha512-hWEuQnfQOgcSs4YX6iF4QR/34ROeSPaMi7lQOYg33hStg+pnk/JDdIU0f2nrIIz3t0jqAj+5VXVLBJvOCd84vg==
"@angular-devkit/[email protected].4":
version "16.1.4"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-16.1.4.tgz#91d4d82ee273dd7f24c17b9c7fbbfff11d7cab63"
integrity sha512-yjRgwHAfFaeuimgbQtjwSUyXzEHpMSdTRb2zg+TOp6skoGvHOG8xXFJ7DjBkSMeAQdFF0fkxhPS9YmlxqNc+7A==
dependencies:
"@angular-devkit/core" "16.1.3"
"@angular-devkit/core" "16.1.4"
jsonc-parser "3.2.0"
magic-string "0.30.0"
ora "5.4.1"
Expand Down Expand Up @@ -180,14 +180,14 @@
resolved "https://registry.yarnpkg.com/@mikro-orm/nestjs/-/nestjs-5.2.0.tgz#259f94b63a3b1e0582fbcbfe333b82189f0b2f2a"
integrity sha512-DZNqTQCoycU4R6Wc3mMOH9Tb3sXGnpFlKil0/gdiezP2oS0B4UPBN3GKysA+SZJ9nmbI7p1OVxymlzj8CJbZbA==

"@nestjs/[email protected].4":
version "10.1.4"
resolved "https://registry.yarnpkg.com/@nestjs/cli/-/cli-10.1.4.tgz#448b04da315a7a310417fbd8c64eb2f91d4e65ba"
integrity sha512-ojFeKG/O7LW2G0k6z44On0YTFf2B9N59unzcsdLUPzZyZJXzrEKM3L3jSx4b5AKzfc5RoiBbJdbFZGNRmvInHQ==
"@nestjs/[email protected].10":
version "10.1.10"
resolved "https://registry.yarnpkg.com/@nestjs/cli/-/cli-10.1.10.tgz#ae4cdc26e9e02602f653237a4e68d94f764c0722"
integrity sha512-5adPl6m2X0uOrK4IPUbzBJUqZ//UKsKi9c7yAR3uCu+moSDMieG/YeKt+zGLnF2gaCdHdgeWWoxI0nfMVE5uCA==
dependencies:
"@angular-devkit/core" "16.1.3"
"@angular-devkit/schematics" "16.1.3"
"@angular-devkit/schematics-cli" "16.1.3"
"@angular-devkit/core" "16.1.4"
"@angular-devkit/schematics" "16.1.4"
"@angular-devkit/schematics-cli" "16.1.4"
"@nestjs/schematics" "^10.0.1"
chalk "4.1.2"
chokidar "3.5.3"
Expand All @@ -203,7 +203,7 @@
source-map-support "0.5.21"
tree-kill "1.2.2"
tsconfig-paths "4.2.0"
tsconfig-paths-webpack-plugin "4.0.1"
tsconfig-paths-webpack-plugin "4.1.0"
typescript "5.1.6"
webpack "5.88.1"
webpack-node-externals "3.0.0"
Expand Down Expand Up @@ -250,10 +250,10 @@
jsonc-parser "3.2.0"
pluralize "8.0.0"

"@nestjs/testing@10.0.5":
version "10.0.5"
resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-10.0.5.tgz#31cac7b9816351dff7706b3ff9af0387cf608f4b"
integrity sha512-TQcFOxR+kIibMbYg71yajic3289/Iw7B5LliYiZ4Pz36BZvU0TRMYqpxaGAlX/Srk0BCpP99ZHoofm8dqZKmxw==
"@nestjs/testing@10.1.0":
version "10.1.0"
resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-10.1.0.tgz#f40a79df1a2bd01c7363c1d1d212a22481eb5c17"
integrity sha512-TqV/21PuU5GJ543oqLTrmQhWUiWwB7DDRcj5cknUdaOst+Kkwp0Sad3/5svcWgOB+QfFbwYlvIDeCkKJshZzPg==
dependencies:
tslib "2.6.0"

Expand Down Expand Up @@ -2193,10 +2193,10 @@ [email protected]:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tsconfig-paths-webpack-plugin@4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-4.0.1.tgz#a24651d0f69668a1abad38d3c2489855c257460d"
integrity sha512-m5//KzLoKmqu2MVix+dgLKq70MnFi8YL8sdzQZ6DblmCdfuq/y3OqvJd5vMndg2KEVCOeNz8Es4WVZhYInteLw==
tsconfig-paths-webpack-plugin@4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-4.1.0.tgz#3c6892c5e7319c146eee1e7302ed9e6f2be4f763"
integrity sha512-xWFISjviPydmtmgeUAuXp4N1fky+VCtfhOkDUFIv5ea7p4wuTomI4QTrXvFBX2S4jZsmyTSrStQl+E+4w+RzxA==
dependencies:
chalk "^4.1.0"
enhanced-resolve "^5.7.0"
Expand Down

0 comments on commit 5a4a934

Please sign in to comment.