DEV Community

ILshat Khamitov
ILshat Khamitov

Posted on • Updated on

Adding the Postgres database to the project and running migrations via Flyway for the NestJS-mod application

The database will be lifted via Docker Compose.

Migrations are written manually and run through the Flyway migrator.

The application has its own username and password, which is different from the root one.

Configurations for Docker Compose and Flyway are generated by running NestJS-mod in infrastructure mode.

1. Installing a package to generate Docker Compose and Flyway files

Commands

# Install all need dependencies
npm i --save @nestjs-mod/docker-compose @nestjs-mod/flyway

# Install all need dev-dependencies
npm i --save-dev node-flywaydb@3.0.7
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm i --save @nestjs-mod/docker-compose @nestjs-mod/flyway

added 41 packages, and audited 2484 packages in 23s

299 packages are looking for funding
  run `npm fund` for details

14 vulnerabilities (4 moderate, 10 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

$ npm i --save-dev node-flywaydb@3.0.7

added 15 packages, and audited 2499 packages in 6s

300 packages are looking for funding
  run `npm fund` for details

16 vulnerabilities (6 moderate, 10 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.
Enter fullscreen mode Exit fullscreen mode

2. Adding infrastructure modules to the server code

They will be used to generate additional files and scripts.

Updated file apps/server/src/main.ts

import { DefaultNestApplicationInitializer, DefaultNestApplicationListener, InfrastructureMarkdownReportGenerator, PACKAGE_JSON_FILE, ProjectUtils, bootstrapNestApplication, isInfrastructureMode } from '@nestjs-mod/common';
import { DOCKER_COMPOSE_FILE, DockerCompose, DockerComposePostgreSQL } from '@nestjs-mod/docker-compose'; // <--
import { FLYWAY_JS_CONFIG_FILE, Flyway } from '@nestjs-mod/flyway'; // <--
import { NestjsPinoLoggerModule } from '@nestjs-mod/pino';
import { ECOSYSTEM_CONFIG_FILE, Pm2 } from '@nestjs-mod/pm2';
import { TerminusHealthCheckModule } from '@nestjs-mod/terminus';
import { MemoryHealthIndicator } from '@nestjs/terminus';
import { join } from 'path';
import { AppModule } from './app/app.module';

const appFeatureName = 'app'; // <--
const rootFolder = join(__dirname, '..', '..', '..');
const appFolder = join(rootFolder, 'apps', 'server');

bootstrapNestApplication({
  modules: {
    system: [
      ProjectUtils.forRoot({
        staticConfiguration: {
          applicationPackageJsonFile: join(appFolder, PACKAGE_JSON_FILE),
          packageJsonFile: join(rootFolder, PACKAGE_JSON_FILE),
          envFile: join(rootFolder, '.env'),
        },
      }),
      DefaultNestApplicationInitializer.forRoot({
        staticConfiguration: { bufferLogs: true },
      }),
      NestjsPinoLoggerModule.forRoot(),
      TerminusHealthCheckModule.forRootAsync({
        configurationFactory: (memoryHealthIndicator: MemoryHealthIndicator) => ({
          standardHealthIndicators: [
            {
              name: 'memory_heap',
              check: () => memoryHealthIndicator.checkHeap('memory_heap', 150 * 1024 * 1024),
            },
          ],
        }),
        inject: [MemoryHealthIndicator],
      }),
      DefaultNestApplicationListener.forRoot({
        staticConfiguration: {
          // When running in infrastructure mode, the backend server does not start.
          mode: isInfrastructureMode() ? 'silent' : 'listen',
        },
      }),
    ],
    feature: [AppModule.forRoot()],
    infrastructure: [
      InfrastructureMarkdownReportGenerator.forRoot({
        staticConfiguration: {
          markdownFile: join(appFolder, 'INFRASTRUCTURE.MD'),
          skipEmptySettings: true,
        },
      }),
      Pm2.forRoot({
        configuration: {
          ecosystemConfigFile: join(rootFolder, ECOSYSTEM_CONFIG_FILE),
          applicationScriptFile: join('dist/apps/server/main.js'),
        },
      }),
      DockerCompose.forRoot({
        // <--
        configuration: {
          dockerComposeFileVersion: '3',
          dockerComposeFile: join(appFolder, DOCKER_COMPOSE_FILE),
        },
      }),
      DockerComposePostgreSQL.forRoot(), // <--
      DockerComposePostgreSQL.forFeature({
        // <--
        featureModuleName: appFeatureName,
      }),
      Flyway.forRoot({
        // <--
        staticConfiguration: {
          featureName: appFeatureName,
          migrationsFolder: join(appFolder, 'src', 'migrations'),
          configFile: join(rootFolder, FLYWAY_JS_CONFIG_FILE),
        },
      }),
    ],
  },
});
Enter fullscreen mode Exit fullscreen mode

3. We create documentation for the project and simultaneously create additional code and scripts for Docker Compose and Flyway

Commands

# Build all applications and library
npm run build

# Generate markdown report
npm run docs:infrastructure
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run build

> @nestjs-mod-fullstack/source@0.0.0 build
> npm run generate && npm run tsc:lint && ./node_modules/.bin/nx run-many --exclude=@nestjs-mod-fullstack/source --all -t=build --skip-nx-cache=true


> @nestjs-mod-fullstack/source@0.0.0 generate
> ./node_modules/.bin/nx run-many --exclude=@nestjs-mod-fullstack/source --all -t=generate --skip-nx-cache=true && npm run make-ts-list && npm run lint:fix

 NX   Successfully ran target generate for 0 projects (29ms)


> @nestjs-mod-fullstack/source@0.0.0 make-ts-list
> ./node_modules/.bin/rucken make-ts-list


> @nestjs-mod-fullstack/source@0.0.0 lint:fix
> npm run tsc:lint && ./node_modules/.bin/nx run-many --exclude=@nestjs-mod-fullstack/source --all -t=lint --fix


> @nestjs-mod-fullstack/source@0.0.0 tsc:lint
> ./node_modules/.bin/tsc --noEmit -p tsconfig.base.json


   ✔  nx run server-e2e:lint  [existing outputs match the cache, left as is]
   ✔  nx run server:lint (1s)
   ✔  nx run client:lint (1s)

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target lint for 3 projects (1s)

      With additional flags:
        --fix=true

Nx read the output from the cache instead of running the command for 1 out of 3 tasks.


> @nestjs-mod-fullstack/source@0.0.0 tsc:lint
> ./node_modules/.bin/tsc --noEmit -p tsconfig.base.json


   ✔  nx run server:build:production (3s)
   ✔  nx run client:build:production (15s)

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target build for 2 projects (15s)

 $ npm run docs:infrastructure

> @nestjs-mod-fullstack/source@0.0.0 docs:infrastructure
> export NESTJS_MODE=infrastructure && ./node_modules/.bin/nx run-many --exclude=@nestjs-mod-fullstack/source --all -t=start --parallel=1


 NX   Running target start for project server:

- server

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> nx run server:start

> node dist/apps/server/main.js

[22:07:23.987] INFO (491733): Starting Nest application...
    context: "NestFactory"
[22:07:23.987] INFO (491733): DefaultNestApp dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): ProjectUtilsSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): DefaultNestApplicationInitializerSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): DefaultNestApplicationInitializerShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): NestjsPinoLoggerModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): NestjsPinoLoggerModuleShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): TerminusHealthCheckModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): DefaultNestApplicationListenerSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): DefaultNestApplicationListenerShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.987] INFO (491733): AppModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): AppModuleShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGeneratorSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): Pm2Settings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): Pm2Shared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposeSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQLSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQLSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQLShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): FlywaySettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): FlywayShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGeneratorSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportStorage dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportStorageSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): FlywaySettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): FlywayShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DefaultNestApplicationListenerSettings dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DefaultNestApplicationListenerShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposeShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportStorageShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): AppModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DefaultNestApplicationInitializer dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DefaultNestApplicationListener dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGenerator dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): Flyway dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DefaultNestApplicationListener dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): NestjsPinoLoggerModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): TerminusModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): TerminusModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): ProjectUtilsShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGeneratorShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): Pm2 dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGeneratorShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): Flyway dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): InfrastructureMarkdownReportGenerator dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): LoggerModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): DockerComposePostgreSQLShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): TerminusHealthCheckModuleShared dependencies initialized
    context: "InstanceLoader"
[22:07:23.988] INFO (491733): TerminusHealthCheckModule dependencies initialized
    context: "InstanceLoader"
[22:07:23.996] INFO (491733): TerminusHealthCheckController {/health}:
    context: "RoutesResolver"
[22:07:23.997] INFO (491733): Mapped {/health, GET} route
    context: "RouterExplorer"
[22:07:23.997] INFO (491733): AppController {/}:
    context: "RoutesResolver"
[22:07:23.997] INFO (491733): Mapped {/, GET} route
    context: "RouterExplorer"
[22:07:24.010] DEBUG (491733):
    0: "SERVER_ROOT_DATABASE_URL: Description='Connection string for PostgreSQL with root credentials (example: postgres://postgres:postgres_password@localhost:5432/postgres?schema=public, username must be \"postgres\")', Original Name='rootDatabaseUrl'"
    1: "SERVER_PORT: Description='The port on which to run the server.', Default='3000', Original Name='port'"
    2: "SERVER_HOSTNAME: Description='Hostname on which to listen for incoming packets.', Original Name='hostname'"
    3: "SERVER_APP_DATABASE_URL: Description='Connection string for PostgreSQL with module credentials (example: postgres://feat:feat_password@localhost:5432/feat?schema=public)', Original Name='databaseUrl'"
    context: "All application environments"
[22:07:24.027] INFO (491733): Nest application successfully started
    context: "NestApplication"

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target start for project server
Enter fullscreen mode Exit fullscreen mode

4. Filling in new environment variables

After launching the server application in infrastructure mode, additional variables will appear in the environment variables file, examples of values can be found in the infrastructure report apps/server/INFRASTRUCTURE.MD.

Updated .env file

SERVER_PORT=3000
SERVER_ROOT_DATABASE_URL=postgres://postgres:postgres_password@localhost:5432/postgres?schema=public
SERVER_APP_DATABASE_URL=postgres://app:app_password@localhost:5432/app?schema=public
# server-postgre-sql (generated)
SERVER_POSTGRE_SQL_POSTGRESQL_USERNAME=postgres
SERVER_POSTGRE_SQL_POSTGRESQL_PASSWORD=postgres_password
SERVER_POSTGRE_SQL_POSTGRESQL_DATABASE=postgres
Enter fullscreen mode Exit fullscreen mode

5. Re-create the project documentation and simultaneously re-create additional code and scripts for Docker Compose and Flyway

Commands

# Generate markdown report
npm run docs:infrastructure
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run docs:infrastructure

> @nestjs-mod-fullstack/source@0.0.0 docs:infrastructure
> export NESTJS_MODE=infrastructure && ./node_modules/.bin/nx run-many --exclude=@nestjs-mod-fullstack/source --all -t=start --parallel=1


 NX   Running target start for project server:

- server

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> nx run server:start

> node dist/apps/server/main.js

[22:22:53.712] INFO (493825): Starting Nest application...
    context: "NestFactory"
[22:22:53.712] INFO (493825): DefaultNestApp dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): ProjectUtilsSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DefaultNestApplicationInitializerSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DefaultNestApplicationInitializerShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): NestjsPinoLoggerModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): NestjsPinoLoggerModuleShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): TerminusHealthCheckModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DefaultNestApplicationListenerSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DefaultNestApplicationListenerShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): AppModuleSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): AppModuleShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): InfrastructureMarkdownReportGeneratorSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): Pm2Settings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): Pm2Shared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerComposeSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerComposePostgreSQLSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerComposePostgreSQLSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerComposePostgreSQLShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): FlywaySettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): FlywayShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): InfrastructureMarkdownReportGeneratorSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): InfrastructureMarkdownReportStorage dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): InfrastructureMarkdownReportStorageSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): FlywaySettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.712] INFO (493825): FlywayShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DefaultNestApplicationListenerSettings dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DefaultNestApplicationListenerShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DockerComposeShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): InfrastructureMarkdownReportStorageShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): AppModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): ProjectUtils dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DefaultNestApplicationInitializer dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DefaultNestApplicationListener dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): InfrastructureMarkdownReportGenerator dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): Flyway dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DefaultNestApplicationListener dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): NestjsPinoLoggerModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): TerminusModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): TerminusModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): ProjectUtilsShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): InfrastructureMarkdownReportGeneratorShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): Pm2 dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DockerCompose dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DockerComposePostgreSQL dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): InfrastructureMarkdownReportGeneratorShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): Flyway dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): InfrastructureMarkdownReportGenerator dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): LoggerModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): DockerComposePostgreSQLShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): TerminusHealthCheckModuleShared dependencies initialized
    context: "InstanceLoader"
[22:22:53.713] INFO (493825): TerminusHealthCheckModule dependencies initialized
    context: "InstanceLoader"
[22:22:53.720] INFO (493825): TerminusHealthCheckController {/health}:
    context: "RoutesResolver"
[22:22:53.721] INFO (493825): Mapped {/health, GET} route
    context: "RouterExplorer"
[22:22:53.721] INFO (493825): AppController {/}:
    context: "RoutesResolver"
[22:22:53.722] INFO (493825): Mapped {/, GET} route
    context: "RouterExplorer"
[22:22:53.735] DEBUG (493825):
    0: "SERVER_ROOT_DATABASE_URL: Description='Connection string for PostgreSQL with root credentials (example: postgres://postgres:postgres_password@localhost:5432/postgres?schema=public, username must be \"postgres\")', Original Name='rootDatabaseUrl'"
    1: "SERVER_PORT: Description='The port on which to run the server.', Default='3000', Original Name='port'"
    2: "SERVER_HOSTNAME: Description='Hostname on which to listen for incoming packets.', Original Name='hostname'"
    3: "SERVER_APP_DATABASE_URL: Description='Connection string for PostgreSQL with module credentials (example: postgres://feat:feat_password@localhost:5432/feat?schema=public)', Original Name='databaseUrl'"
    context: "All application environments"
[22:22:53.759] INFO (493825): Nest application successfully started
    context: "NestApplication"

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target start for project server
Enter fullscreen mode Exit fullscreen mode

6. Copy the updated file with the environment variables as an example

Since the file with environment variables is located under gitignore, when cloning an empty project, you need to re-enter all the values for environment variables, to quickly launch the project, you need to have an example of a filled file with environment variables.

Commands

cp -r ./.env ./example.env
Enter fullscreen mode Exit fullscreen mode

7. Creating a migration with an example of creating a table

Commands

# Create migrations folder
mkdir -p ./apps/server/src/migrations

# Create empty migration
npm run flyway:create:server
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run flyway:create:server

> @nestjs-mod-fullstack/source@0.0.0 flyway:create:server
> ./node_modules/.bin/nx run server:flyway-create-migration


> nx run server:flyway-create-migration

> echo 'select 1;' > ./apps/server/src/migrations/V`date +%Y%m%d%H%M`__NewMigration.sql


——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target flyway-create-migration for project server (42ms)


 NX   Nx detected a flaky task

  server:flyway-create-migration

Flaky tasks can disrupt your CI pipeline. Automatically retry them with Nx Cloud. Learn more at https://nx.dev/ci/features/flaky-tasks
Enter fullscreen mode Exit fullscreen mode

8. We describe the sql script for creating a demo table

Migration file apps/server/src/migrations/V202408112241__New Migration.sql

CREATE TABLE IF NOT EXISTS "AppDemo"(
    id uuid DEFAULT uuid_generate_v4() NOT NULL,
    "name" varchar(128) NOT NULL,
    "createdAt" timestamp DEFAULT "now"() NOT NULL,
    "updatedAt" timestamp DEFAULT "now"() NOT NULL,
    CONSTRAINT "PK_APP_DEMO" PRIMARY KEY (id)
);

CREATE UNIQUE INDEX IF NOT EXISTS "UQ_APP_DEMO" ON "AppDemo"("name");
Enter fullscreen mode Exit fullscreen mode

9. Launching the database

Commands

npm run docker-compose:start-prod:server
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run docker-compose:start-prod:server

> @nestjs-mod-fullstack/source@0.0.0 docker-compose:start-prod:server
> export COMPOSE_INTERACTIVE_NO_CLI=1 && docker-compose -f ./apps/server/docker-compose-prod.yml --env-file ./apps/server/docker-compose-prod.env --compatibility up -d

Creating network "server_server-network" with driver "bridge"
Pulling server-postgre-sql (bitnami/postgresql:15.5.0)...
15.5.0: Pulling from bitnami/postgresql
c9b6a72ca844: Pull complete
Digest: sha256:6887635cc793826e1e177d0cee151a878baa6747e37389252e89b034a30f04bc
Status: Downloaded newer image for bitnami/postgresql:15.5.0
Creating server-postgre-sql ... done
Enter fullscreen mode Exit fullscreen mode

10. Launching the creation of application databases

Commands

npm run db:create
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run db:create

> @nestjs-mod-fullstack/source@0.0.0 db:create
> ./node_modules/.bin/nx run-many -t=db-create


   ✔  nx run server:db-create (787ms)

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target db-create for project server (819ms)


 NX   Nx detected a flaky task

  server:db-create

Flaky tasks can disrupt your CI pipeline. Automatically retry them with Nx Cloud. Learn more at https://nx.dev/ci/features/flaky-tasks
Enter fullscreen mode Exit fullscreen mode

11. We apply migrations

Commands

npm run flyway:migrate
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run flyway:migrate

> @nestjs-mod-fullstack/source@0.0.0 flyway:migrate
> ./node_modules/.bin/nx run-many -t=flyway-migrate


   ✔  nx run server:flyway-migrate (1s)

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target flyway-migrate for project server (1s)


 NX   Nx detected a flaky task

  server:flyway-migrate

Flaky tasks can disrupt your CI pipeline. Automatically retry them with Nx Cloud. Learn more at https://nx.dev/ci/features/flaky-tasks
Enter fullscreen mode Exit fullscreen mode

12. We connect to the database and get a list of tables

Commands

docker exec -it server-postgre-sql psql postgres://app:app_password@localhost:5432/app
\d
Enter fullscreen mode Exit fullscreen mode

Console output
docker exec -it server-postgre-sql psql postgres://app:app_password@localhost:5432/app
psql (15.5)
Type "help" for help.

app-> \d
 public | AppDemo      | table | app
 public | __migrations | table | app

app->
Enter fullscreen mode Exit fullscreen mode

13. Stopping the database

Commands

npm run docker-compose:stop-prod:server
Enter fullscreen mode Exit fullscreen mode

Console output
$ npm run docker-compose:stop-prod:server

> @nestjs-mod-fullstack/source@0.0.0 docker-compose:stop-prod:server
> export COMPOSE_INTERACTIVE_NO_CLI=1 && docker-compose -f ./apps/server/docker-compose-prod.yml --env-file ./apps/server/docker-compose-prod.env down

Stopping server-postgre-sql ... done
Removing server-postgre-sql ... done
Enter fullscreen mode Exit fullscreen mode

In the next post, I will add working with the database via PrismaORM to the server...

Links

https://nestjs.com - the official website of the framework
https://nestjs-mod.com - the official website of additional utilities
https://github.com/nestjs-mod/nestjs-mod-fullstack - the project from the post
https://github.com/nestjs-mod/nestjs-mod-fullstack/commit/4661c2f29170568fdf62d3ba0dd4bf20a79a1afa - commit to current changes

Top comments (0)