Skip to content

Commit

Permalink
Merge pull request #42 from zhumeisongsong/feature/shared-infrastructure
Browse files Browse the repository at this point in the history
feat: ✨ add @shared/infrastructure-mongoose
  • Loading branch information
zhumeisongsong authored Nov 18, 2024
2 parents 1f18384 + cdecca1 commit 7c500d9
Show file tree
Hide file tree
Showing 31 changed files with 342 additions and 27 deletions.
5 changes: 3 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ USER_PORT=15001
TASK_HOST=localhost
TASK_PORT=15002

DATABASE_HOST=127.0.0.1
DATABASE_PORT=3306
DATABASE_HOST=localhost
DATABASE_PORT=27017
DATABASE_NAME=product_a_main
2 changes: 1 addition & 1 deletion apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IntrospectAndCompose } from '@apollo/gateway';
import { gatewayConfig, userAppConfig } from '@libs/config';
import { gatewayConfig, userAppConfig } from '@shared/config';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
Expand Down
4 changes: 2 additions & 2 deletions apps/users/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { userAppConfig } from '@libs/config';
import { databaseConfig, userAppConfig } from '@shared/config';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

Expand All @@ -10,7 +10,7 @@ import { UsersModule } from '../users/users.module';
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [userAppConfig],
load: [userAppConfig, databaseConfig],
}),
UsersModule,
],
Expand Down
2 changes: 2 additions & 0 deletions apps/users/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GraphQLModule } from '@nestjs/graphql';
import { UsersResolver } from '@users/presentation-resolver';
import { UsersService } from '@users/application';
import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace';
import { DatabaseModule } from '@shared/infrastructure-mongoose';

@Module({
providers: [UsersResolver, UsersService],
Expand All @@ -25,6 +26,7 @@ import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace
sortSchema: true,
plugins: [ApolloServerPluginInlineTrace()],
}),
DatabaseModule,
],
})
export class UsersModule {}
3 changes: 0 additions & 3 deletions libs/config/eslint.config.js

This file was deleted.

9 changes: 0 additions & 9 deletions libs/config/project.json

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions libs/shared/config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../eslint.config.js');

module.exports = [...baseConfig];
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
displayName: 'config',
preset: '../../jest.preset.js',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/config',
coverageDirectory: '../../../coverage/libs/shared/config',
};
9 changes: 9 additions & 0 deletions libs/shared/config/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "shared-config",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/shared/config/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project config --web",
"targets": {}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ describe('databaseConfig', () => {
it('should return the correct database host and port from environment variables', () => {
process.env['DATABASE_HOST'] = 'localhost';
process.env['DATABASE_PORT'] = '3306';
process.env['DATABASE_NAME'] = 'test-db';

const config = databaseConfig();
expect(config.host).toBe('localhost');
expect(config.port).toBe(3306);
expect(config.name).toBe('test-db');
});

it('should return the default port if DATABASE_PORT is not set', () => {
Expand All @@ -27,6 +29,20 @@ describe('databaseConfig', () => {

const config = databaseConfig();
expect(config.host).toBe('localhost');
expect(config.port).toBe(5432);
expect(config.port).toBe(27017);
});
});

it('should return the correct database name from environment variables', () => {
process.env['DATABASE_NAME'] = 'test-db';

const config = databaseConfig();
expect(config.name).toBe('test-db');
});

it('should return the default database name if DATABASE_NAME is not set', () => {
delete process.env['DATABASE_NAME'];

const config = databaseConfig();
expect(config.name).toBe('main');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { z } from 'zod';
const databaseSchema = z.object({
host: z.string().min(1),
port: z.coerce.number().int().min(1).max(65535),
name: z.string().min(1),
});

export type DatabaseConfig = z.infer<typeof databaseSchema>;

export const databaseConfig = registerAs('database', () => {
const config = {
host: process.env['DATABASE_HOST'],
port: process.env['DATABASE_PORT'] || 5432,
host: process.env['DATABASE_HOST'] || 'localhost',
port: process.env['DATABASE_PORT'] || 27017,
name: process.env['DATABASE_NAME'] || 'main',
};

return databaseSchema.parse(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
Expand Down
7 changes: 7 additions & 0 deletions libs/shared/infrastructure/mongoose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# shared-infrastructure-mongoose

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test shared-infrastructure-mongoose` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/shared/infrastructure/mongoose/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../../eslint.config.js');

module.exports = [...baseConfig];
10 changes: 10 additions & 0 deletions libs/shared/infrastructure/mongoose/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'shared-infrastructure-mongoose',
preset: '../../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../../coverage/libs/shared/infrastructure/mongoose',
};
9 changes: 9 additions & 0 deletions libs/shared/infrastructure/mongoose/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "shared-infrastructure-mongoose",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/shared/infrastructure/mongoose/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project shared-infrastructure-mongoose --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/shared/infrastructure/mongoose/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/database.module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Test } from '@nestjs/testing';
import { ConfigModule } from '@nestjs/config';
import { databaseConfig } from '@shared/config';
import { DatabaseModule } from './database.module';

describe('DatabaseModule', () => {
it('should be defined', () => {
expect(DatabaseModule).toBeDefined();
});

// TODO: Add tests for the database module
});
26 changes: 26 additions & 0 deletions libs/shared/infrastructure/mongoose/src/lib/database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { databaseConfig } from '@shared/config';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [databaseConfig],
}),
MongooseModule.forRootAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const databaseConfig = configService.get('database');

return {
uri: `mongodb://${databaseConfig.host}:${databaseConfig.port}`,
dbName: databaseConfig.name,
};
},
}),
],
exports: [MongooseModule],
})
export class DatabaseModule {}
22 changes: 22 additions & 0 deletions libs/shared/infrastructure/mongoose/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/shared/infrastructure/mongoose/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/shared/infrastructure/mongoose/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.4.7",
"@nestjs/graphql": "^12.2.1",
"@nestjs/mongoose": "^10.1.0",
"@nestjs/platform-express": "^10.4.7",
"axios": "^1.7.7",
"graphql": "^16.9.0",
"graphql-tools": "^9.0.2",
"inlineTrace": "link:@apollo/server/plugin/inlineTrace",
"mongoose": "^8.8.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"zod": "^3.23.8"
Expand Down
Loading

0 comments on commit 7c500d9

Please sign in to comment.