Skip to content

[Startup MVP recipes #1] Nest.js local dev environment setup

Nest.js

https://docs.nestjs.com/first-steps

Install Nest.js CLI

npm i -g @nestjs/cli

Init and run project

nest new sample-project
cd sample-project/
npm run start

Postgres

Install Postgres on Mac (you can search for other ways of installing Postgres on different environments)

# if on mac
brew install postgresql

# after first installation, you may try to start postgres
brew services restart postgresql

(Optional) Download pgAdmin

We can use pgAdmin to connect to local postgres and create new roles or databases locally. We can also use psql cli and raw SQL queries to do the same work.

For example

(Credits to https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e)

psql postgres
CREATE DATABASE yourdbname;
CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass';
GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser;
# \q to quit postgres

Then we can use yourdbname as local db dev environment for our Nest.js project.

TypeORM

npm install --save @nestjs/typeorm typeorm pg

Add following code:

// app.module.ts

// add import
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
	// add TypeOrm module, warning: in production, don't expose password
	// one option is to hide the password in environment variables
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'sample_nestjs_user',
      password: 'sample_nestjs_password',
      database: 'sample_nestjs_db',
      entities: [],
	  // for local only
	  // setting this flat to true in production may lose data
      synchronize: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

To Wrap Up

This can be a really basic first step setup and please test with:

npm run start

1 thought on “[Startup MVP recipes #1] Nest.js local dev environment setup”

  1. Pingback: [Startup MVP recipes #5.1] A simple resource generated by nest-cli then configured (part 1) - James Zhang

Leave a Reply

Your email address will not be published.