設定
MBC CQRS サーバーレスフレームワークでは、特定 の要件に合わせてプロジェクトをカスタマイズできます。このガイドでは、フレームワークで利用可能なすべての設定オプションについて説明します。
プロジェクト設定
serverless.yml
サーバーレスアプリケーションのメイン設定ファイルです。
テーブルプレフィックス
フレームワークは NODE_ENV と APP_NAME 環境変数を使用してDynamoDBテーブルプレフィックスを自動生成します。形式は {NODE_ENV}-{APP_NAME}-{tableName} です。例えば、NODE_ENV=dev と APP_NAME=my-app の場合、コマンドテーブルは dev-my-app-command という名前になります。
service: my-app
frameworkVersion: '3'
plugins:
- serverless-offline
- serverless-plugin-typescript
- serverless-dynamodb-local
provider:
name: aws
runtime: nodejs18.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'ap-northeast-1'}
memorySize: 512
timeout: 30
environment:
NODE_ENV: ${self:provider.stage}
APP_NAME: ${self:service}
COGNITO_USER_POOL_ID: ${env:COGNITO_USER_POOL_ID}
COGNITO_CLIENT_ID: ${env:COGNITO_CLIENT_ID}
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- !GetAtt CommandTable.Arn
- !GetAtt DataTable.Arn
custom:
serverless-offline:
httpPort: 3000
lambdaPort: 3002
dynamodb:
stages:
- dev
start:
port: 8000
inMemory: true
migrate: true
NestJS設定
アプリケーションでNestJSモジュールを設定します。
// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CommandModule } from '@mbc-cqrs-serverless/core';
@Module({
imports: [
// Load environment variables
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env.local', '.env'],
}),
// Configure Command Module
CommandModule.register({
tableName: process.env.DYNAMODB_TABLE_NAME,
dataSyncHandlers: [],
}),
],
})
export class AppModule {}
TypeScript設定
tsconfig.json
MBC CQRS Serverlessプロジェクト向けの推奨TypeScript設定。
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2021",
"lib": ["ES2021"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@shared/*": ["src/shared/*"]
},
"outDir": "./dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
ESLint設定
.eslintrc.js
推奨ESLint設定。
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js', 'dist', 'node_modules'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
},
};
モジュール設定
CommandModuleオプション
コアCommandModuleを設定します。
import { Module } from '@nestjs/common';
import { CommandModule } from '@mbc-cqrs-serverless/core';
@Module({
imports: [
CommandModule.register({
// Required: DynamoDB table name
tableName: 'order',
// Optional: Data sync handlers for RDS synchronization
dataSyncHandlers: [OrderRdsSyncHandler],
// Optional: Reserved for future use (not yet implemented)
skipError: false,
// Optional: Disable default DynamoDB data sync handler
disableDefaultHandler: false,
}),
],
})
export class OrderModule {}