From 5bd1f513034bfd6feee322ab73dd1bdc4ba35c3a Mon Sep 17 00:00:00 2001 From: Nargonath Date: Thu, 26 Oct 2023 23:23:45 +0400 Subject: [PATCH 1/5] feat(module): add registerAsync function to Narango module --- README.md | 30 +++++++++++++++++++++++++ lib/index.ts | 2 +- lib/narango.config.ts | 7 ------ lib/narango.module-definition.ts | 15 +++++++++++++ lib/narango.module.ts | 23 +++++-------------- lib/narango.service.ts | 10 ++++++--- lib/narango.spec.ts | 38 +++++++++++++++++++++++++++++++- 7 files changed, 96 insertions(+), 29 deletions(-) delete mode 100644 lib/narango.config.ts create mode 100644 lib/narango.module-definition.ts diff --git a/README.md b/README.md index d74ca7a..4d34649 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,36 @@ For sake of simplicity the database credentials are passed directly to the regis ``` +### Async registration + +If you need to use providers to provide the options to Narango module, you can use `NarangoModule.registerAsync`: + +```ts +import { Module } from "@nestjs/common"; +import { NarangoModule } from "@ronatilabs/narango"; +import { ConfigModule, ConfigService } from "@nestjs/config"; + +@Module({ + imports: [ + ConfigModule.forRoot(), + NarangoModule.registerAsync({ + useFactory: (config: ConfigService) => ({ + database: { + url: config.get("url"), + databaseName: config.get("databaseName"), + auth: { + username: config.get("username"), + password: config.get("password"), + }, + }, + }), + inject: [ConfigService], + }), + ], +}) +export class MyAppModule {} +``` + ## Inject NarangoService Now you can access the `NarangoService` everywhere you need it in your application: diff --git a/lib/index.ts b/lib/index.ts index bb2d392..770326b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,3 +1,3 @@ -export * from "./narango.config"; +export { NarangoModuleOptions } from "./narango.module-definition"; export * from "./narango.service"; export * from "./narango.module"; diff --git a/lib/narango.config.ts b/lib/narango.config.ts deleted file mode 100644 index 5e601b2..0000000 --- a/lib/narango.config.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { Config } from "arangojs/connection"; - -export const MODULE_OPTIONS = Symbol("NARANGO_CONFIG"); -export interface NarangoModuleOptions { - global?: boolean; - database: Config; -} diff --git a/lib/narango.module-definition.ts b/lib/narango.module-definition.ts new file mode 100644 index 0000000..f7a64e6 --- /dev/null +++ b/lib/narango.module-definition.ts @@ -0,0 +1,15 @@ +import type { Config as ArangoDBConfig } from "arangojs/connection"; +import { ConfigurableModuleBuilder } from "@nestjs/common"; + +export interface NarangoModuleOptions { + global?: boolean; + database: ArangoDBConfig; +} + +export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = + new ConfigurableModuleBuilder() + .setExtras({ global: false }, (definition, extras) => ({ + ...definition, + global: extras.global, + })) + .build(); diff --git a/lib/narango.module.ts b/lib/narango.module.ts index af51062..a6863f5 100644 --- a/lib/narango.module.ts +++ b/lib/narango.module.ts @@ -1,21 +1,10 @@ import { Module } from "@nestjs/common"; -import type { DynamicModule } from "@nestjs/common"; import { NarangoService } from "./narango.service"; -import { MODULE_OPTIONS } from "./narango.config"; -import type { NarangoModuleOptions } from "./narango.config"; +import { ConfigurableModuleClass } from "./narango.module-definition"; -@Module({}) -export class NarangoModule { - static register(options: NarangoModuleOptions): DynamicModule { - return { - module: NarangoModule, - global: options.global, - providers: [ - { provide: MODULE_OPTIONS, useValue: options }, - NarangoService, - ], - exports: [NarangoService], - }; - } -} +@Module({ + providers: [NarangoService], + exports: [NarangoService], +}) +export class NarangoModule extends ConfigurableModuleClass {} diff --git a/lib/narango.service.ts b/lib/narango.service.ts index 0b8bcab..54e9085 100644 --- a/lib/narango.service.ts +++ b/lib/narango.service.ts @@ -2,14 +2,18 @@ import { Inject, Injectable } from "@nestjs/common"; import type { OnApplicationShutdown } from "@nestjs/common"; import { Database } from "arangojs"; -import { MODULE_OPTIONS } from "./narango.config"; -import type { NarangoModuleOptions } from "./narango.config"; +import { + MODULE_OPTIONS_TOKEN, + NarangoModuleOptions, +} from "./narango.module-definition"; @Injectable() export class NarangoService implements OnApplicationShutdown { public db: Database; - constructor(@Inject(MODULE_OPTIONS) private options: NarangoModuleOptions) { + constructor( + @Inject(MODULE_OPTIONS_TOKEN) private options: NarangoModuleOptions, + ) { this.db = new Database(options.database); } diff --git a/lib/narango.spec.ts b/lib/narango.spec.ts index 97095a6..8ea09d2 100644 --- a/lib/narango.spec.ts +++ b/lib/narango.spec.ts @@ -4,7 +4,7 @@ import { Test } from "@nestjs/testing"; import { NarangoModule } from "./narango.module"; import { NarangoService } from "./narango.service"; -import { Injectable, Module } from "@nestjs/common"; +import { Global, Injectable, Module } from "@nestjs/common"; const closeSpy = jest.fn(); jest.mock("arangojs", () => ({ @@ -86,6 +86,42 @@ describe("Narango module", () => { expect(serviceFromRoot).toBeDefined(); expect((serviceFromRoot.db as any).options).toEqual(options); }); + + it("can be registered async", async () => { + @Injectable() + class TestService { + public options = { url: "http://overridden.url" }; + constructor() {} + } + + @Global() + @Module({ providers: [TestService], exports: [TestService] }) + class ChildModule {} + + const rootModule = await Test.createTestingModule({ + imports: [ + ChildModule, + NarangoModule.registerAsync({ + useFactory: (testService: TestService) => { + return { + database: { + ...options, + url: testService.options.url, + }, + }; + }, + inject: [TestService], + }), + ], + }).compile(); + + const serviceFromRoot = rootModule.get(NarangoService); + expect(serviceFromRoot).toBeDefined(); + expect((serviceFromRoot.db as any).options).toEqual({ + ...options, + url: "http://overridden.url", + }); + }); }); /* eslint-enable @typescript-eslint/no-explicit-any */ From c6803e3748b96476da276c11cb4cd7522c70ae66 Mon Sep 17 00:00:00 2001 From: Nargonath Date: Thu, 26 Oct 2023 23:25:04 +0400 Subject: [PATCH 2/5] ci(semantic-release): make semantic-release dry-run I need to test whether I should merge the tags from main branch to beta or if semantic-release will see that v1.0.0 was deployed in latest channel so next beta should be v1.1.0-beta.1 --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3024bd9..23b0b88 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,4 +41,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - run: npx semantic-release + run: npx semantic-release --dry-run From 4d46a31a32e02e0d265e11e05fbef7a39fed9b6e Mon Sep 17 00:00:00 2001 From: Nargonath Date: Thu, 26 Oct 2023 23:50:10 +0400 Subject: [PATCH 3/5] ci(semantic-release): remove dry-run --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 23b0b88..3024bd9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,4 +41,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - run: npx semantic-release --dry-run + run: npx semantic-release From ab05754e12d530960e96479059972acaa8221a0e Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 26 Oct 2023 19:51:38 +0000 Subject: [PATCH 4/5] chore(release): 1.1.0-beta.1 [skip ci] # [1.1.0-beta.1](https://github.com/ronati/narango/compare/v1.0.0...v1.1.0-beta.1) (2023-10-26) ### Features * **module:** add registerAsync function to Narango module ([5bd1f51](https://github.com/ronati/narango/commit/5bd1f513034bfd6feee322ab73dd1bdc4ba35c3a)) --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 400de4c..3a1f05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.1.0-beta.1](https://github.com/ronati/narango/compare/v1.0.0...v1.1.0-beta.1) (2023-10-26) + + +### Features + +* **module:** add registerAsync function to Narango module ([5bd1f51](https://github.com/ronati/narango/commit/5bd1f513034bfd6feee322ab73dd1bdc4ba35c3a)) + # 1.0.0 (2023-10-26) diff --git a/package-lock.json b/package-lock.json index c0c6f7a..321b254 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ronatilabs/narango", - "version": "1.0.0", + "version": "1.1.0-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ronatilabs/narango", - "version": "1.0.0", + "version": "1.1.0-beta.1", "license": "ISC", "devDependencies": { "@commitlint/cli": "^17.8.0", diff --git a/package.json b/package.json index e574b34..a3ef04a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ronatilabs/narango", - "version": "1.0.0", + "version": "1.1.0-beta.1", "description": "A NestJS wrapper service for ArangoDB NodeJS driver", "exports": "./dist/index.js", "typings": "./dist/index.d.ts", From 5e8a02ca25e3ae1a51b4da5337ef77042b243803 Mon Sep 17 00:00:00 2001 From: Nargonath Date: Fri, 27 Oct 2023 00:16:01 +0400 Subject: [PATCH 5/5] docs(maintainer): add explanation about merge workflow with semantic-release --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 4d34649..bffc959 100644 --- a/README.md +++ b/README.md @@ -138,3 +138,7 @@ This project is setup with automatic semver versioning based on your commit sema 3. Follow along the wizard to create your commit. 4. Push your commit on the branch. 5. Create your PR. + +## Notes for project's maintainers + +When you merge a PR from `beta` into `main` and it successfully published a new version on the `latest` channel, **don't forget to create a PR from `main` to `beta`**. **This is mandatory** for `semantic-release` to take it into account for next `beta` version.