-
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.
feat(app-user): userManagement service scaffolding (#1)
* feat: create user-management application * feat: create userModule * build: set root docker-compose and mysql container * build: update version nestjs dependency * feat: set database configuration and add basic entities * build: configure migrations * feat: add configModule * refactor: load dbConfig using configModule * feat: add healthCheck endpoint
- Loading branch information
Showing
29 changed files
with
17,232 additions
and
3,414 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ lerna-debug.log* | |
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
.vscode | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
|
14 changes: 14 additions & 0 deletions
14
apps/user-management/__migrations/Migration20211219172816.ts
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,14 @@ | ||
import { Migration } from '@mikro-orm/migrations'; | ||
|
||
export class Migration20211219172816 extends Migration { | ||
|
||
async up(): Promise<void> { | ||
this.addSql('create table `User` (`id` varchar not null, `created_at` datetime not null, `updated_at` datetime not null, `version` int(11) not null default 1, `external_id` varchar(255) not null, `meta` json null) default character set utf8mb4 engine = InnoDB;'); | ||
this.addSql('alter table `User` add primary key `User_pkey`(`id`);'); | ||
this.addSql('alter table `User` add index `User_created_at_index`(`created_at`);'); | ||
this.addSql('alter table `User` add index `User_updated_at_index`(`updated_at`);'); | ||
this.addSql('alter table `User` add index `User_external_id_index`(`external_id`);'); | ||
this.addSql('alter table `User` add unique `User_external_id_unique`(`external_id`);'); | ||
} | ||
|
||
} |
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,44 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
|
||
import { Resource } from '@apps/user-management/user/infra/persistence/common/Resource'; | ||
import { RootEntity } from '@apps/user-management/user/infra/persistence/common/RootEntity'; | ||
import { User } from '@apps/user-management/user/infra/persistence/user/User'; | ||
|
||
type MigrationMode = 'create' | 'up' | 'down' | 'pending'; | ||
|
||
(async () => { | ||
const mode = process.argv[2] as MigrationMode; | ||
|
||
const orm = await MikroORM.init({ | ||
entities: [RootEntity, Resource, User], | ||
dbName: 'UserManagement', | ||
host: '127.0.0.1', | ||
port: 13306, | ||
user: 'root', | ||
password: 'test', | ||
type: 'mysql', | ||
migrations: { | ||
tableName: 'MikroOrmMigrations', | ||
path: './apps/user-management/__migrations', | ||
}, | ||
}); | ||
|
||
const migrator = orm.getMigrator(); | ||
|
||
switch (mode) { | ||
case 'create': | ||
await migrator.createMigration(); | ||
break; | ||
case 'up': | ||
await migrator.up(); | ||
break; | ||
case 'down': | ||
break; | ||
case 'pending': | ||
const pendingMigrations = await migrator.getPendingMigrations(); | ||
console.log(pendingMigrations.map(migrations => migrations.file)); | ||
break; | ||
} | ||
|
||
await orm.close(true); | ||
})(); |
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,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
import { validationSchema } from '@apps/user-management/config/ValidationSchema'; | ||
import DatabaseConfig from '@apps/user-management/config/DatabaseConfig'; | ||
import { CoreModule } from '@apps/user-management/core/CoreModule'; | ||
import { UserModule } from '@apps/user-management/user/UserModule'; | ||
|
||
const configs = [DatabaseConfig]; | ||
|
||
@Module({ | ||
imports: [ | ||
CoreModule, | ||
ConfigModule.forRoot({ | ||
envFilePath: [ | ||
`${__dirname}/config/envs/.${process.env.NODE_ENV ?? | ||
'development'}.env`, | ||
], | ||
load: [...configs], | ||
isGlobal: true, | ||
validationSchema, | ||
}), | ||
UserModule, | ||
], | ||
controllers: [], | ||
providers: [], | ||
}) | ||
export class AppModule {} |
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,9 @@ | ||
import { registerAs } from '@nestjs/config'; | ||
|
||
export default registerAs('database', () => ({ | ||
host: process.env.DB_HOST, | ||
port: parseInt(process.env.DB_PORT, 10) || 3306, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
dbSchema: process.env.DB_SCHEMA, | ||
})); |
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 @@ | ||
import * as Joi from 'joi'; | ||
|
||
export const validationSchema = Joi.object({ | ||
NODE_ENV: Joi.string() | ||
.valid('production', 'stage', 'dev') | ||
.required(), | ||
SERVICE_ID: Joi.string().required(), | ||
DB_HOST: Joi.string().required(), | ||
DB_PORT: Joi.number() | ||
.positive() | ||
.integer() | ||
.required(), | ||
DB_USER: Joi.string().required(), | ||
DB_PASSWORD: Joi.string().required(), | ||
DB_SCHEMA: Joi.string().required(), | ||
}); |
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,8 @@ | ||
NODE_ENV=dev | ||
SERVICE_ID=UserManagement | ||
|
||
DB_HOST=localhost | ||
DB_PORT=13306 | ||
DB_USER=root | ||
DB_PASSWORD=test | ||
DB_SCHEMA=UserManagement |
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,37 @@ | ||
import { MikroOrmModule } from '@mikro-orm/nestjs'; | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService, ConfigType } from '@nestjs/config'; | ||
|
||
import DatabaseConfig from '@apps/user-management/config/DatabaseConfig'; | ||
|
||
import { HealthModule } from '@apps/user-management/core/health/HealthModule'; | ||
|
||
import { Resource } from '@apps/user-management/user/infra/persistence/common/Resource'; | ||
import { RootEntity } from '@apps/user-management/user/infra/persistence/common/RootEntity'; | ||
import { User } from '@apps/user-management/user/infra/persistence/user/User'; | ||
|
||
@Module({ | ||
imports: [ | ||
MikroOrmModule.forRootAsync({ | ||
imports: [ConfigModule], | ||
useFactory: async (configService: ConfigService) => { | ||
const { host, port, user, password, dbSchema } = configService.get( | ||
'database', | ||
) as ConfigType<typeof DatabaseConfig>; | ||
|
||
return { | ||
entities: [RootEntity, Resource, User], | ||
dbName: dbSchema, | ||
host, | ||
port, | ||
user, | ||
password, | ||
type: 'mysql', | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
HealthModule, | ||
], | ||
}) | ||
export class CoreModule {} |
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 { Controller, Get } from '@nestjs/common'; | ||
import { HealthCheckService } from '@nestjs/terminus'; | ||
|
||
import { DatabaseIndicator } from '@apps/user-management/core/health/indicators/DatabaseIndicator'; | ||
|
||
@Controller('health') | ||
export class HealthController { | ||
constructor( | ||
private readonly health: HealthCheckService, | ||
private readonly dbIndicator: DatabaseIndicator, | ||
) {} | ||
|
||
@Get() | ||
async getHealth() { | ||
return this.health.check([ | ||
async () => this.dbIndicator.isHealthy('UserManagement'), | ||
]); | ||
} | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TerminusModule } from '@nestjs/terminus'; | ||
|
||
import { HealthController } from '@apps/user-management/core/health/HealthController'; | ||
import { DatabaseIndicator } from '@apps/user-management/core/health/indicators/DatabaseIndicator'; | ||
|
||
@Module({ | ||
imports: [TerminusModule], | ||
controllers: [HealthController], | ||
providers: [DatabaseIndicator], | ||
}) | ||
export class HealthModule {} |
25 changes: 25 additions & 0 deletions
25
apps/user-management/src/core/health/indicators/DatabaseIndicator.ts
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,25 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
HealthCheckError, | ||
HealthIndicator, | ||
HealthIndicatorResult, | ||
} from '@nestjs/terminus'; | ||
|
||
@Injectable() | ||
export class DatabaseIndicator extends HealthIndicator { | ||
constructor(private readonly orm: MikroORM) { | ||
super(); | ||
} | ||
|
||
async isHealthy(key: string): Promise<HealthIndicatorResult> { | ||
const healthy = await this.orm.isConnected(); | ||
|
||
const result = this.getStatus(key, healthy); | ||
if (healthy) { | ||
return result; | ||
} | ||
|
||
throw new HealthCheckError('Database is unhealthy', result); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { UserController } from '@apps/user-management/user/interface/UserController'; | ||
|
||
@Module({ | ||
controllers: [UserController], | ||
}) | ||
export class UserModule {} |
12 changes: 12 additions & 0 deletions
12
apps/user-management/src/user/infra/persistence/common/Resource.ts
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,12 @@ | ||
import { Entity, JsonType, Property } from '@mikro-orm/core'; | ||
|
||
import { RootEntity } from '@apps/user-management/user/infra/persistence/common/RootEntity'; | ||
|
||
@Entity({ abstract: true }) | ||
export abstract class Resource extends RootEntity { | ||
@Property({ unique: true, index: true }) | ||
externalId: string; | ||
|
||
@Property({ type: JsonType, nullable: true }) | ||
meta: object; | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/user-management/src/user/infra/persistence/common/RootEntity.ts
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 @@ | ||
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'; | ||
|
||
@Entity({ abstract: true }) | ||
export abstract class RootEntity { | ||
@PrimaryKey({ columnType: 'varchar', length: 128 }) | ||
id: string; | ||
|
||
@Property({ onCreate: () => new Date(), index: true }) | ||
createdAt: Date = new Date(); | ||
|
||
@Property({ onUpdate: () => new Date(), index: true }) | ||
updatedAt: Date; | ||
|
||
@Property({ version: true }) | ||
version: number; | ||
} |
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 { Entity } from '@mikro-orm/core'; | ||
|
||
import { Resource } from '@apps/user-management/user/infra/persistence/common/Resource'; | ||
|
||
@Entity({ tableName: 'User' }) | ||
export class User extends Resource {} |
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,17 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
|
||
@Controller('users') | ||
export class UserController { | ||
constructor(private readonly orm: MikroORM) {} | ||
|
||
@Get('/:userId') | ||
async getUser(@Param('userId') userId: string): Promise<void> { | ||
console.log(await this.orm.isConnected()); | ||
|
||
console.log(userId); | ||
|
||
return; | ||
} | ||
s; | ||
} |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": false, | ||
"outDir": "../../dist/apps/user-management" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"] | ||
} |
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,14 @@ | ||
version: '3.8' | ||
|
||
services: | ||
db: | ||
image: mysql:5.7 | ||
restart: always | ||
environment: | ||
- MYSQL_ROOT_PASSWORD=test | ||
ports: | ||
- '13306:3306' | ||
volumes: | ||
- ./.mysql:/docker-entrypoint-initdb.d | ||
command: | ||
[mysqld, --character-set-server=utf8, --collation-server=utf8_general_ci] |
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,4 +1,27 @@ | ||
{ | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src" | ||
"sourceRoot": "apps/msa-iam-sample/src", | ||
"monorepo": true, | ||
"root": "apps/msa-iam-sample", | ||
"compilerOptions": { | ||
"webpack": true, | ||
"tsConfigPath": "apps/msa-iam-sample/tsconfig.app.json" | ||
}, | ||
"projects": { | ||
"user-management": { | ||
"type": "application", | ||
"root": "apps/user-management", | ||
"entryFile": "main", | ||
"sourceRoot": "apps/user-management/src", | ||
"compilerOptions": { | ||
"tsConfigPath": "apps/user-management/tsconfig.app.json", | ||
"assets": [ | ||
{ | ||
"include": "**/*.env", | ||
"watchAssets": true | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.