Skip to content

Commit

Permalink
Merge pull request #12 from ronati/db-service
Browse files Browse the repository at this point in the history
feat(arango): create nestjs service
  • Loading branch information
Nargonath authored Oct 24, 2023
2 parents a9fafd8 + aa31864 commit 0a28e34
Show file tree
Hide file tree
Showing 12 changed files with 8,208 additions and 3,141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
run: npx semantic-release --dry-run
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ jobs:
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm test
16 changes: 16 additions & 0 deletions jest.config.cjs
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",
};
6 changes: 6 additions & 0 deletions lib/narango.config.ts
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;
}
20 changes: 20 additions & 0 deletions lib/narango.module.ts
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],
};
}
}
19 changes: 19 additions & 0 deletions lib/narango.service.ts
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();
}
}
74 changes: 74 additions & 0 deletions lib/narango.spec.ts
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 */
1 change: 1 addition & 0 deletions lint-staged.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
"*": "prettier --write --ignore-unknown",
"*.{ts,js}": "eslint --cache",
};
Loading

0 comments on commit 0a28e34

Please sign in to comment.