From d343fa6b6a51777a5b737ad2b4119ef0da89c1cd Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Sun, 27 Aug 2023 15:51:09 +0000 Subject: [PATCH] feat(js): create nestjs-specific files --- js/hooks/post_gen_project.sh | 8 ++ js/{{ cookiecutter.project_name }}/.gitignore | 3 + .../.licenserc.yaml | 7 +- .../.pre-commit-config.yaml | 4 + .../.prettierignore | 4 + .../nest-cli.json | 8 ++ .../nestjs/.eslintrc.js | 25 ++++ .../nestjs/.github/workflows/build.yml | 127 ++++++++++++++++++ .../nestjs/.prettierrc | 5 + .../nestjs/.vscode/settings.json | 17 +++ .../nestjs/Dockerfile | 42 ++++++ .../nestjs/docker-compose.yaml | 32 +++++ .../nestjs/src/app.module.ts | 54 ++++++++ .../nestjs/src/config/db.ts | 42 ++++++ .../nestjs/src/config/index.ts | 5 + .../nestjs/src/config/logger.ts | 26 ++++ .../nestjs/src/config/server.ts | 6 + .../src/health/health.controller.spec.ts | 51 +++++++ .../nestjs/src/health/health.controller.ts | 28 ++++ .../nestjs/src/health/health.module.ts | 9 ++ .../nestjs/src/lib/pinoTypeOrmLogger.ts | 74 ++++++++++ .../nestjs/src/main.ts | 42 ++++++ .../nestjs/src/migrations/.gitkeep | 0 .../nestjs/test/app.e2e-spec.ts | 24 ++++ .../nestjs/test/jest-e2e.json | 9 ++ .../package.json | 75 +++++++++++ .../{ => svelte}/.vscode/settings.json | 4 +- .../tsconfig.build.json | 4 + .../tsconfig.json | 21 +++ 29 files changed, 747 insertions(+), 9 deletions(-) create mode 100644 js/{{ cookiecutter.project_name }}/.prettierignore create mode 100644 js/{{ cookiecutter.project_name }}/nest-cli.json create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/.eslintrc.js create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/.github/workflows/build.yml create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/.prettierrc create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/.vscode/settings.json create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/Dockerfile create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/docker-compose.yaml create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/app.module.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/config/db.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/config/index.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/config/logger.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/config/server.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.spec.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/health/health.module.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/lib/pinoTypeOrmLogger.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/main.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/src/migrations/.gitkeep create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/test/app.e2e-spec.ts create mode 100644 js/{{ cookiecutter.project_name }}/nestjs/test/jest-e2e.json create mode 100644 js/{{ cookiecutter.project_name }}/package.json rename js/{{ cookiecutter.project_name }}/{ => svelte}/.vscode/settings.json (90%) create mode 100644 js/{{ cookiecutter.project_name }}/tsconfig.build.json create mode 100644 js/{{ cookiecutter.project_name }}/tsconfig.json diff --git a/js/hooks/post_gen_project.sh b/js/hooks/post_gen_project.sh index 788c7a6..ebccc27 100644 --- a/js/hooks/post_gen_project.sh +++ b/js/hooks/post_gen_project.sh @@ -2,6 +2,14 @@ set -e +# Include hidden files +# @link https://askubuntu.com/questions/259383/how-can-i-get-mv-or-the-wildcard-to-move-hidden-files +shopt -s dotglob + +cp -rf {{ cookiecutter.type }}/* ./ + +rm -Rf nestjs svelte + git init -b main git add . git commit -m "chore: initial commit" diff --git a/js/{{ cookiecutter.project_name }}/.gitignore b/js/{{ cookiecutter.project_name }}/.gitignore index de1ec19..08e68df 100644 --- a/js/{{ cookiecutter.project_name }}/.gitignore +++ b/js/{{ cookiecutter.project_name }}/.gitignore @@ -9,3 +9,6 @@ Thumbs.db .commit .devbox .envrc + +coverage +.nyc_output diff --git a/js/{{ cookiecutter.project_name }}/.licenserc.yaml b/js/{{ cookiecutter.project_name }}/.licenserc.yaml index 941b9f8..bf78653 100644 --- a/js/{{ cookiecutter.project_name }}/.licenserc.yaml +++ b/js/{{ cookiecutter.project_name }}/.licenserc.yaml @@ -10,12 +10,7 @@ header: - "go.*" - "**/*.{json,md,yml,yaml}" comment: on-failure - language: - Go: - extensions: - - ".go" - comment_style_id: SlashAsterisk dependency: files: - - go.mod # If this is a Go project. + - package.json # If this is a npm project. diff --git a/js/{{ cookiecutter.project_name }}/.pre-commit-config.yaml b/js/{{ cookiecutter.project_name }}/.pre-commit-config.yaml index e96b2a5..073aa00 100644 --- a/js/{{ cookiecutter.project_name }}/.pre-commit-config.yaml +++ b/js/{{ cookiecutter.project_name }}/.pre-commit-config.yaml @@ -32,3 +32,7 @@ repos: rev: v0.9.2 hooks: - id: markdownlint-cli2 + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "v3.0.2" + hooks: + - id: prettier diff --git a/js/{{ cookiecutter.project_name }}/.prettierignore b/js/{{ cookiecutter.project_name }}/.prettierignore new file mode 100644 index 0000000..93e600d --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/.prettierignore @@ -0,0 +1,4 @@ +*.md +*.yml +*.yaml +*.json diff --git a/js/{{ cookiecutter.project_name }}/nest-cli.json b/js/{{ cookiecutter.project_name }}/nest-cli.json new file mode 100644 index 0000000..f9aa683 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nest-cli.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/nest-cli", + "collection": "@nestjs/schematics", + "sourceRoot": "src", + "compilerOptions": { + "deleteOutDir": true + } +} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/.eslintrc.js b/js/{{ cookiecutter.project_name }}/nestjs/.eslintrc.js new file mode 100644 index 0000000..259de13 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/.eslintrc.js @@ -0,0 +1,25 @@ +module.exports = { + parser: '@typescript-eslint/parser', + parserOptions: { + project: 'tsconfig.json', + tsconfigRootDir: __dirname, + 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'], + 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': 'off', + }, +}; diff --git a/js/{{ cookiecutter.project_name }}/nestjs/.github/workflows/build.yml b/js/{{ cookiecutter.project_name }}/nestjs/.github/workflows/build.yml new file mode 100644 index 0000000..f0c8ab2 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/.github/workflows/build.yml @@ -0,0 +1,127 @@ +name: Build +on: + push: + branches: + - main + tags: + - "v*.*.*" + pull_request: + branches: + - main + workflow_dispatch: +permissions: + contents: write + packages: write +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: go.mod + + - uses: actions/setup-python@v4 + with: + python-version: 3.x + + - name: Install dependencies + run: go install ./... + + - name: go-vet + run: go vet -v ./... + + - uses: pre-commit/action@v3.0.0 + + build: + runs-on: ubuntu-latest + needs: + - test + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # Required for goreleaser changelog to work correctly + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: {{ '${{ github.actor }}' }} + password: {{ '${{ secrets.GITHUB_TOKEN }}' }} + + - name: Get branch names + id: branch-name + uses: tj-actions/branch-names@v6 + with: + strip_tag_prefix: v + + - name: Generate Docker tag + id: docker + run: | + if [ "{{ '${{ steps.branch-name.outputs.is_tag }}' }}" = "true" ]; + then + # Latest tag + IMG_NAME="ghcr.io/${GITHUB_REPOSITORY,,}:latest" + # Tag name (usually vX.Y.Z) + IMG_NAME="${IMG_NAME},ghcr.io/${GITHUB_REPOSITORY,,}:{{ '${{ steps.branch-name.outputs.tag }}' }}" + + echo "image_name=${IMG_NAME}" >> "$GITHUB_OUTPUT" + echo "platforms=linux/amd64,linux/arm64,linux/arm/v7" >> "$GITHUB_OUTPUT" + else + # Use branch naming convention + TAG="branch-{{ '${{ steps.branch-name.outputs.current_branch }}' }}" + # Change "/" for "-" + TAG="${TAG//\//-}" + # Set to lowercase + TAG="${TAG,,}" + + echo "image_name=ghcr.io/${GITHUB_REPOSITORY,,}:${TAG}" >> "$GITHUB_OUTPUT" + echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT" + fi + + if [ "{{ '${{ steps.branch-name.outputs.is_tag }}' }}" = "true" ]; + then + echo "version={{ '${{ steps.branch-name.outputs.tag }}' }}" >> "$GITHUB_OUTPUT" + else + echo "version=development" >> "$GITHUB_OUTPUT" + fi + + echo "container_tagged_image=ghcr.io/${GITHUB_REPOSITORY,,}:${GITHUB_SHA}" >> "$GITHUB_OUTPUT" + echo "commit_id=${GITHUB_SHA}" >> "$GITHUB_OUTPUT" + echo "gitRepo=github.com/${GITHUB_REPOSITORY}" >> "$GITHUB_OUTPUT" + + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + build-args: | + GIT_COMMIT={{ '${{ steps.docker.outputs.commit_id }}' }} + GIT_REPO={{ '${{ steps.docker.outputs.gitRepo }}' }} + VERSION={{ '${{ steps.docker.outputs.version }}' }} + platforms: {{ '${{ steps.docker.outputs.platforms }}' }} + push: {{ '${{ github.ref == \'refs/heads/main\' }}' }} + tags: {{ '${{ steps.docker.outputs.image_name }},${{ steps.docker.outputs.container_tagged_image }}' }} + + - name: Set up Go + if: steps.branch-name.outputs.is_tag == 'true' + uses: actions/setup-go@v3 + with: + go-version: '>=1.20.0' + + - name: Run GoReleaser + if: steps.branch-name.outputs.is_tag == 'true' + uses: goreleaser/goreleaser-action@v4 + with: + version: latest + args: release --clean + env: + GIT_REPO: {{ '${{ steps.docker.outputs.gitRepo }}' }} + GITHUB_TOKEN: {{ '${{ secrets.GITHUB_TOKEN }}' }} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/.prettierrc b/js/{{ cookiecutter.project_name }}/nestjs/.prettierrc new file mode 100644 index 0000000..9409967 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/.prettierrc @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "trailingComma": "all", + "printWidth": 80 +} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/.vscode/settings.json b/js/{{ cookiecutter.project_name }}/nestjs/.vscode/settings.json new file mode 100644 index 0000000..c808525 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/.vscode/settings.json @@ -0,0 +1,17 @@ +{ + "editor.formatOnSave": true, + "editor.rulers": [ + 80 + ], + "yaml.schemas": { + "https://json.schemastore.org/github-workflow.json": [ + ".github/workflows/*.{yml,yaml}" + ] + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } +} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/Dockerfile b/js/{{ cookiecutter.project_name }}/nestjs/Dockerfile new file mode 100644 index 0000000..e474e7b --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/Dockerfile @@ -0,0 +1,42 @@ +FROM node:lts-alpine AS dev +ARG GIT_COMMIT +ARG GIT_REPO +ARG VERSION +USER node +WORKDIR /home/node/app +ENV GIT_REPO="${GIT_REPO}" +ENV GIT_COMMIT="${GIT_COMMIT}" +ENV VERSION="${VERSION}" +COPY --chown=node:node . . +ENV PORT=3000 +EXPOSE 3000 +CMD [ "npm", "run", "start:dev" ] + +FROM node:lts-alpine AS builder +USER node +USER node +WORKDIR /home/node/app +COPY --from=dev /home/node/app . +RUN npm ci \ + && npm run build + +FROM node:lts-alpine +ARG GIT_COMMIT +ARG GIT_REPO +ARG VERSION +WORKDIR /opt/app +ENV GIT_REPO="${GIT_REPO}" +ENV GIT_COMMIT="${GIT_COMMIT}" +ENV VERSION="${VERSION}" +ENV SERVER_PORT=3000 +COPY --from=builder /home/node/app/dist dist +COPY --from=builder /home/node/app/node_modules node_modules +COPY --from=builder /home/node/app/package.json package.json +COPY --from=builder /home/node/app/package-lock.json package-lock.json +RUN npm prune --production \ + && npm rebuild \ + && npm dedupe \ + && npm version ${VERSION} --no-git-tag-version --allow-same-version || true +USER node +EXPOSE 3000 +CMD [ "npm", "run", "start:prod" ] diff --git a/js/{{ cookiecutter.project_name }}/nestjs/docker-compose.yaml b/js/{{ cookiecutter.project_name }}/nestjs/docker-compose.yaml new file mode 100644 index 0000000..42cef10 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/docker-compose.yaml @@ -0,0 +1,32 @@ +services: + app: + build: + context: . + target: dev + environment: + DB_TYPE: mysql + DB_HOST: mysql + DB_USERNAME: app + DB_PASSWORD: password + DB_NAME: app + DB_PORT: "3306" + DB_MIGRATIONS_RUN: "true" + DB_SYNC: "false" + volumes: + - .:/home/node/app + ports: + - 9000:3000 + links: + - mysql + restart: on-failure + + # Third-party services + mysql: + image: mysql:8 + environment: + MYSQL_RANDOM_ROOT_PASSWORD: "true" + MYSQL_USER: app + MYSQL_PASSWORD: password + MYSQL_DATABASE: app + ports: + - 4000:3306 diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/app.module.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/app.module.ts new file mode 100644 index 0000000..3e3e1bc --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/app.module.ts @@ -0,0 +1,54 @@ +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { Module } from '@nestjs/common'; +import { LoggerModule, Params, PinoLogger } from 'nestjs-pino'; +import config from './config'; +import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; +import { PinoTypeOrmLogger } from './lib/pinoTypeOrmLogger'; +import { LoggerOptions } from 'typeorm'; +import { HealthModule } from './health/health.module'; + +@Module({ + imports: [ + ConfigModule.forRoot({ + isGlobal: true, + load: config, + }), + LoggerModule.forRootAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService): Promise => + configService.get('logger'), + }), + TypeOrmModule.forRootAsync({ + imports: [ConfigModule, LoggerModule], + inject: [ConfigService, PinoLogger], + useFactory: async ( + configService: ConfigService, + pinoLogger: PinoLogger, + ): Promise => { + const db = configService.get('db'); + + /* Replace the default TypeOrm logger with PinoTypeOrmLogger */ + let logger: 'debug' | PinoTypeOrmLogger = 'debug'; + let logging = false; + + if (configService.get('db.logging', false)) { + logger = new PinoTypeOrmLogger(pinoLogger); + logging = true; + } + + return { + ...db, + logging, + logger, + }; + }, + }), + + // Load the internal modules + HealthModule, + ], + controllers: [], + providers: [], +}) +export class AppModule {} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/config/db.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/config/db.ts new file mode 100644 index 0000000..7d87539 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/config/db.ts @@ -0,0 +1,42 @@ +import { registerAs } from '@nestjs/config'; +import { TypeOrmModuleOptions } from '@nestjs/typeorm'; +import * as path from 'path'; +import { LoggerOptions } from 'typeorm/logger/LoggerOptions'; + +export default registerAs('db', (): TypeOrmModuleOptions => { + /* SSL configuration should default to undefined */ + let ssl: any; + if (process.env.DB_USE_SSL === 'true') { + ssl = { + ca: process.env.DB_SSL_CA, + cert: process.env.DB_SSL_CERT, + key: process.env.DB_SSL_KEY, + }; + } + + let logging: boolean | string | undefined = false; + const loggingVar = process.env.DB_LOGGING; + + if (loggingVar === 'true') { + logging = true; + } else if (loggingVar === 'false') { + logging = false; + } else { + logging = loggingVar; + } + + return { + ssl, + type: (process.env.DB_TYPE as 'mysql' | 'mariadb') ?? 'mysql', + host: process.env.DB_HOST, + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + port: process.env.DB_PORT ? Number(process.env.DB_PORT) : undefined, + migrationsRun: process.env.DB_MIGRATIONS_RUN !== 'false', + synchronize: process.env.DB_SYNC === 'true', + autoLoadEntities: true, + logging: (logging as LoggerOptions) ?? true, + migrations: [path.join(__dirname, '..', 'migrations', '*{.ts,.js}')], + }; +}); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/config/index.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/config/index.ts new file mode 100644 index 0000000..c0667b9 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/config/index.ts @@ -0,0 +1,5 @@ +import db from './db'; +import logger from './logger'; +import server from './server'; + +export default [db, logger, server]; diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/config/logger.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/config/logger.ts new file mode 100644 index 0000000..55b6713 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/config/logger.ts @@ -0,0 +1,26 @@ +import { registerAs } from '@nestjs/config'; +import { Params } from 'nestjs-pino'; +import * as pino from 'pino'; +import * as uuid from 'uuid'; + +export default registerAs( + 'logger', + (): Params => ({ + pinoHttp: { + level: process.env.LOG_LEVEL ?? 'info', + serializers: pino.stdSerializers, + genReqId: () => uuid.v4(), + customLogLevel: (_, res, err) => { + const { statusCode } = res; + if (statusCode >= 400 && statusCode !== 404 && statusCode < 500) { + return 'warn'; + } + if (statusCode >= 500 || err) { + return 'error'; + } + + return 'debug'; + }, + }, + }), +); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/config/server.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/config/server.ts new file mode 100644 index 0000000..1a427f1 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/config/server.ts @@ -0,0 +1,6 @@ +import { registerAs } from '@nestjs/config'; + +export default registerAs('server', () => ({ + enableShutdownHooks: process.env.SERVER_ENABLE_SHUTDOWN_HOOKS === 'true', + port: Number(process.env.SERVER_PORT ?? 3000), +})); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.spec.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.spec.ts new file mode 100644 index 0000000..56292e7 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.spec.ts @@ -0,0 +1,51 @@ +import { HealthController } from './health.controller'; +import { + HealthCheckService, + TerminusModule, + TypeOrmHealthIndicator, +} from '@nestjs/terminus'; +import { Test, TestingModule } from '@nestjs/testing'; + +describe('HealthController', () => { + let controller: HealthController; + let service: any; + let dbCheck: any; + + beforeEach(async () => { + service = { + check: jest.fn(), + }; + + dbCheck = { + pingCheck: jest.fn(), + }; + + const module: TestingModule = await Test.createTestingModule({ + imports: [TerminusModule], + controllers: [HealthController], + providers: [ + { + provide: HealthCheckService, + useValue: service, + }, + { + provide: TypeOrmHealthIndicator, + useValue: dbCheck, + }, + ], + }).compile(); + + controller = module.get(HealthController); + }); + + it('should return a successful result', async () => { + service.check.mockResolvedValue('OK'); + dbCheck.pingCheck.mockResolvedValue('DB OK'); + + await expect(controller.check()).resolves.toBe('OK'); + + expect(service.check).toHaveBeenCalled(); + + await expect(service.check.mock.calls[0][0][0]()).resolves.toBe('DB OK'); + }); +}); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.ts new file mode 100644 index 0000000..1d1bb73 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.controller.ts @@ -0,0 +1,28 @@ +import { Controller, Get, VERSION_NEUTRAL } from '@nestjs/common'; +import { + HealthCheckService, + HealthCheck, + TypeOrmHealthIndicator, +} from '@nestjs/terminus'; + +@Controller({ + path: 'health', + version: VERSION_NEUTRAL, +}) +export class HealthController { + constructor( + private health: HealthCheckService, + private db: TypeOrmHealthIndicator, + ) {} + + @Get() + @HealthCheck() + check() { + return this.health.check([ + () => + this.db.pingCheck('database', { + timeout: 30, + }), + ]); + } +} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.module.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.module.ts new file mode 100644 index 0000000..3ed2a84 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/health/health.module.ts @@ -0,0 +1,9 @@ +import { HealthController } from './health.controller'; +import { Module } from '@nestjs/common'; +import { TerminusModule } from '@nestjs/terminus'; + +@Module({ + imports: [TerminusModule], + controllers: [HealthController], +}) +export class HealthModule {} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/lib/pinoTypeOrmLogger.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/lib/pinoTypeOrmLogger.ts new file mode 100644 index 0000000..349c30a --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/lib/pinoTypeOrmLogger.ts @@ -0,0 +1,74 @@ +import { PinoLogger } from 'nestjs-pino'; +import { Logger as TypeOrmLogger } from 'typeorm'; + +export class PinoTypeOrmLogger implements TypeOrmLogger { + constructor(protected logger: PinoLogger) {} + + logQuery(query: string, parameters?: any[]) { + this.logger.debug( + { + query, + parameters, + }, + 'TypeORM query', + ); + } + + logQueryError(err: string, query: string, parameters?: any[]) { + this.logger.error( + { + err, + query, + parameters, + }, + 'TypeORM error', + ); + } + + logQuerySlow(time: number, query: string, parameters?: any[]) { + this.logger.warn( + { + time, + query, + parameters, + }, + 'TypeORM slow query', + ); + } + + logSchemaBuild(schemaMessage: string) { + this.logger.debug( + { + schemaMessage, + }, + 'TypeORM schema build', + ); + } + + logMigration(migrationMessage: string) { + this.logger.debug( + { + migrationMessage, + }, + 'TypeORM log migration', + ); + } + + log(level: 'log' | 'info' | 'warn', logMessage: any) { + if (level === 'warn') { + this.logger.warn( + { + logMessage, + }, + 'TypeORM log warning', + ); + } else { + this.logger.info( + { + logMessage, + }, + 'TypeORM log message', + ); + } + } +} diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/main.ts b/js/{{ cookiecutter.project_name }}/nestjs/src/main.ts new file mode 100644 index 0000000..e0e1392 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/src/main.ts @@ -0,0 +1,42 @@ +import { AppModule } from './app.module'; +import { VersioningType } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { NestFactory } from '@nestjs/core'; +import { NestExpressApplication } from '@nestjs/platform-express'; +import { Logger, PinoLogger } from 'nestjs-pino'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule, { + bufferLogs: true, + }); + + app.disable('x-powered-by'); + const config = app.get(ConfigService); + app.useLogger(app.get(Logger)); + + app.enableVersioning({ + type: VersioningType.URI, + defaultVersion: '1', + }); + + const logger = await app.resolve(PinoLogger); + + if (config.get('server.enableShutdownHooks')) { + logger.debug('Enabling shutdown hooks'); + app.enableShutdownHooks(); + } else { + logger.debug('Shutdown hooks not enabled'); + } + + const port = config.get('server.port'); + + logger.debug('Starting HTTP listener'); + await app.listen(port); + logger.info({ port }, 'Application running'); +} + +bootstrap().catch((err) => { + /* Unlikely to get to here but a final catchall */ + console.log(err.stack); + process.exit(1); +}); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/src/migrations/.gitkeep b/js/{{ cookiecutter.project_name }}/nestjs/src/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/js/{{ cookiecutter.project_name }}/nestjs/test/app.e2e-spec.ts b/js/{{ cookiecutter.project_name }}/nestjs/test/app.e2e-spec.ts new file mode 100644 index 0000000..f3b8d30 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/test/app.e2e-spec.ts @@ -0,0 +1,24 @@ +import { AppModule } from './../src/app.module'; +import { INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import * as request from 'supertest'; + +describe('AppController (e2e)', () => { + let app: INestApplication; + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + it('/ (GET)', () => { + return request(app.getHttpServer()) + .get('/') + .expect(200) + .expect('Hello World!'); + }); +}); diff --git a/js/{{ cookiecutter.project_name }}/nestjs/test/jest-e2e.json b/js/{{ cookiecutter.project_name }}/nestjs/test/jest-e2e.json new file mode 100644 index 0000000..e9d912f --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/nestjs/test/jest-e2e.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/js/{{ cookiecutter.project_name }}/package.json b/js/{{ cookiecutter.project_name }}/package.json new file mode 100644 index 0000000..5db7599 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/package.json @@ -0,0 +1,75 @@ +{ + "name": "{{ cookiecutter.project_name }}", + "version": "0.0.0", + "description": "{{ cookiecutter.description }}", + "author": "{{ cookiecutter.author }}", + "private": true, + "license": "Apache-2.0", + "scripts": { + "build": "nest build", + "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", + "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "start": "nest start", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/main", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "test:e2e": "jest --config ./test/jest-e2e.json" + }, + "dependencies": { + "@nestjs/common": "^10.0.0", + "@nestjs/config": "^3.0.0", + "@nestjs/core": "^10.0.0", + "@nestjs/platform-express": "^10.0.0", + "@nestjs/terminus": "^10.0.1", + "@nestjs/typeorm": "^10.0.0", + "mysql2": "^3.6.0", + "nestjs-pino": "^3.4.0", + "pino": "^8.15.0", + "reflect-metadata": "^0.1.13", + "rxjs": "^7.8.1" + }, + "devDependencies": { + "@nestjs/cli": "^10.0.0", + "@nestjs/schematics": "^10.0.0", + "@nestjs/testing": "^10.0.0", + "@types/express": "^4.17.17", + "@types/jest": "^29.5.2", + "@types/node": "^20.3.1", + "@types/supertest": "^2.0.12", + "@typescript-eslint/eslint-plugin": "^6.0.0", + "@typescript-eslint/parser": "^6.0.0", + "eslint": "^8.42.0", + "eslint-config-prettier": "^9.0.0", + "eslint-plugin-prettier": "^5.0.0", + "jest": "^29.5.0", + "prettier": "^3.0.0", + "source-map-support": "^0.5.21", + "supertest": "^6.3.3", + "ts-jest": "^29.1.0", + "ts-loader": "^9.4.3", + "ts-node": "^10.9.1", + "tsconfig-paths": "^4.2.0", + "typescript": "^5.1.3" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "../coverage", + "testEnvironment": "node" + } +} diff --git a/js/{{ cookiecutter.project_name }}/.vscode/settings.json b/js/{{ cookiecutter.project_name }}/svelte/.vscode/settings.json similarity index 90% rename from js/{{ cookiecutter.project_name }}/.vscode/settings.json rename to js/{{ cookiecutter.project_name }}/svelte/.vscode/settings.json index e274547..9543518 100644 --- a/js/{{ cookiecutter.project_name }}/.vscode/settings.json +++ b/js/{{ cookiecutter.project_name }}/svelte/.vscode/settings.json @@ -13,8 +13,7 @@ }, "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" - } - {%- if cookiecutter.type == "svelte" %}, + }, "[html]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, @@ -24,5 +23,4 @@ "[svelte]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } - {%- endif %} } diff --git a/js/{{ cookiecutter.project_name }}/tsconfig.build.json b/js/{{ cookiecutter.project_name }}/tsconfig.build.json new file mode 100644 index 0000000..64f86c6 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] +} diff --git a/js/{{ cookiecutter.project_name }}/tsconfig.json b/js/{{ cookiecutter.project_name }}/tsconfig.json new file mode 100644 index 0000000..95f5641 --- /dev/null +++ b/js/{{ cookiecutter.project_name }}/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./", + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": false, + "noImplicitAny": false, + "strictBindCallApply": false, + "forceConsistentCasingInFileNames": false, + "noFallthroughCasesInSwitch": false + } +}