/nestjs-coffee-project

Primary LanguageTypeScriptMIT LicenseMIT

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

Installation

$ pnpm install

Docker

Start containers in detached / background mode

$ docker-compose up -d

Stop containers

$ docker-compose down**

Running the app

# development
$ pnpm run start

# watch mode
$ pnpm run start:dev

# production mode
$ pnpm run start:prod

Test

# unit tests
$ pnpm run test

# e2e tests
$ pnpm run test:e2e

# test coverage
$ pnpm run test:cov

Migration TypeORM example

/* typeorm-cli.config.ts */
export default new DataSource({
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'postgres',
    password: 'pass123',
    database: 'postgres',
    entities: [],
    migrations: [],
});

Creating a TypeOrm Migration

$ npx typeorm migration:create src/migrations/CoffeeRefactor

CoffeeRefactor being the NAME we are giving "this" migration

public async up(queryRunner: QueryRunner): Promise<any> {
  await queryRunner.query(
    `ALTER TABLE "coffee" RENAME COLUMN "name" TO "title"`,
  );
}

public async down(queryRunner: QueryRunner): Promise<any> {
  await queryRunner.query(
    `ALTER TABLE "coffee" RENAME COLUMN "title" TO "name"`,
  );
}

RUNNING MIGRATIONS

💡 IMPORTANT 💡 You must BUILD your Nest project (so that everything is output to the /dist/ folder, before a Migration can run, it needs compiled files.

// Compile project first
$ pnpm run build

// Run migration(s)
$ npx typeorm migration:run -d dist/typeorm-cli.config

// REVERT migration(s)
$ npx typeorm migration:revert -d dist/typeorm-cli.config

// Let TypeOrm generate migrations (for you)
$ npx typeorm migration:generate src/migrations/SchemaSync -d dist/typeorm-cli.config

Troubleshooting

  1. If the error message is "aggregateError":
When running nestjs project I have the issue:
aggregateError: 
    at internalConnectMultiple (node:net:1114:18)
    at afterConnectMultiple (node:net:1667:5)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
Waiting for the debugger to disconnect..

Solution:

The error message, "AggregateError," typically indicates that there was an issue when connecting to a network resource. In a NestJS application, this could relate to issues with connecting to databases, external services, or other network-related operations.

  • Check Database Connection: if NestJS application is connecting to a database, ensure that the database server is up and running, and that the connection details (such as host, port, username, and password) are correctly configured in your application's configuration file, check your .env file.

On this case run Docker compose to start database container:

$ docker-compose up -d

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.