-
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.
Merge pull request #12 from ronati/db-service
feat(arango): create nestjs service
- Loading branch information
Showing
12 changed files
with
8,208 additions
and
3,141 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
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 |
---|---|---|
|
@@ -53,3 +53,5 @@ jobs: | |
run: npm ci | ||
- name: Lint | ||
run: npm run lint | ||
- name: Test | ||
run: npm test |
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,16 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
testEnvironment: "node", | ||
transform: { | ||
"\\.[jt]sx?$": [ | ||
"ts-jest", | ||
{ useESM: true, tsconfig: "./tsconfig.jest.json" }, | ||
], | ||
}, | ||
moduleNameMapper: { | ||
"(.+)\\.js": "$1", | ||
}, | ||
extensionsToTreatAsEsm: [".ts"], | ||
collectCoverage: true, | ||
coverageProvider: "v8", | ||
}; |
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,6 @@ | ||
import type { Config } from "arangojs/connection.js"; | ||
|
||
export const MODULE_OPTIONS = Symbol("NARANGO_CONFIG"); | ||
export interface NarangoModuleOptions { | ||
database: Config; | ||
} |
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,20 @@ | ||
import { Module } from "@nestjs/common"; | ||
import type { DynamicModule } from "@nestjs/common"; | ||
|
||
import { NarangoService } from "./narango.service.js"; | ||
import { MODULE_OPTIONS } from "./narango.config.js"; | ||
import type { NarangoModuleOptions } from "./narango.config.js"; | ||
|
||
@Module({}) | ||
export class NarangoModule { | ||
static register(options: NarangoModuleOptions): DynamicModule { | ||
return { | ||
module: NarangoModule, | ||
providers: [ | ||
{ provide: MODULE_OPTIONS, useValue: options }, | ||
NarangoService, | ||
], | ||
exports: [NarangoService], | ||
}; | ||
} | ||
} |
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,19 @@ | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import type { OnApplicationShutdown } from "@nestjs/common"; | ||
import { Database } from "arangojs"; | ||
|
||
import { MODULE_OPTIONS } from "./narango.config.js"; | ||
import type { NarangoModuleOptions } from "./narango.config.js"; | ||
|
||
@Injectable() | ||
export class NarangoService implements OnApplicationShutdown { | ||
public db: Database; | ||
|
||
constructor(@Inject(MODULE_OPTIONS) private options: NarangoModuleOptions) { | ||
this.db = new Database(options.database); | ||
} | ||
|
||
onApplicationShutdown() { | ||
this.db.close(); | ||
} | ||
} |
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,74 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { describe, expect, it, jest } from "@jest/globals"; | ||
import { Test } from "@nestjs/testing"; | ||
|
||
import { NarangoModule } from "./narango.module.js"; | ||
import { NarangoService } from "./narango.service.js"; | ||
|
||
const closeSpy = jest.fn(); | ||
jest.mock("arangojs", () => ({ | ||
Database: class { | ||
public options: object; | ||
|
||
constructor(opt) { | ||
this.options = opt; | ||
} | ||
|
||
close() { | ||
closeSpy(); | ||
} | ||
}, | ||
})); | ||
|
||
describe("Narango module", () => { | ||
it("can be imported in another module with database options", async () => { | ||
const options = { | ||
url: "http://localhost:8529", | ||
auth: { | ||
username: "root", | ||
password: "password", | ||
}, | ||
databaseName: "dbName", | ||
}; | ||
|
||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
NarangoModule.register({ | ||
database: options, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
const service = module.get<NarangoService>(NarangoService); | ||
expect(service).toBeDefined(); | ||
expect(service.db).toBeDefined(); | ||
expect((service.db as any).options).toEqual( | ||
expect.objectContaining(options), | ||
); | ||
}); | ||
|
||
it("close arango connection when application shutdown", async () => { | ||
const options = { | ||
url: "http://localhost:8529", | ||
auth: { | ||
username: "root", | ||
password: "password", | ||
}, | ||
databaseName: "dbName", | ||
}; | ||
|
||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
NarangoModule.register({ | ||
database: options, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
await module.close(); | ||
|
||
expect(closeSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
/* eslint-enable @typescript-eslint/no-explicit-any */ |
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,3 +1,4 @@ | ||
module.exports = { | ||
"*": "prettier --write --ignore-unknown", | ||
"*.{ts,js}": "eslint --cache", | ||
}; |
Oops, something went wrong.