23
loading...
This website collects cookies to deliver better user experience
npm i -g @nestjs/cli
nest new project-name
? Which package manager would you ❤️ to use?
npm
yarn
Controllers are responsible for handling incoming requests and returning responses to the client.
The main idea of a provider is that it can inject dependencies; this means objects can create various relationships with each other, and the function of “wiring up” instances of objects can largely be delegated to the Nest runtime system.
A module is a class annotated with a @Module() decorator. The @Module() decorator provides metadata that Nest makes use of to organize the application structure.
npm i --save @nestjs/typeorm typeorm
Nest uses TypeORM because it’s the most mature Object Relational Mapper (ORM) available for TypeScript. Since it’s written in TypeScript, it integrates well with the Nest framework.
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
require('dotenv').config();
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mongodb',
url:
process.env.MONGOURL,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
ssl: true,
synchronize: true,
logging: true,
useUnifiedTopology: true,
useNewUrlParser: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
Unique,
} from 'typeorm';
@Entity('users')
@Unique(['email'])
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 128 })
name: string;
@Column({ length: 128 })
email: string;
@Column({ length: 128 })
password: string;
@Column({ length:128 })
type: string;
@Column({ length: 128 })
status: string;
@Column({ nullable: true })
referal: string;
@Column({nullable: true})
resetToken: string;
@Column({ nullable: true })
lastLogin: Date;
@CreateDateColumn()
createdAt: Date;
@CreateDateColumn()
updatedAt: Date;
}
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import * as config from 'config';
const dbConfig = config.get('db');
export const DbConfig: TypeOrmModuleOptions = {
type: 'postgres',
url: process.env.DATABASE_URL,//provide the database url
host: dbConfig.host, //provide the host
port: dbConfig.port , //provide the port number
username: dbConfig.username, //provide the dbusername
password: dbConfig.password , //provide the dbpassword
database: dbConfig.database , //provide the databasename
entities: [__dirname + '/../**/*.entity.{js,ts}'],
migrations: [__dirname + '/../**/migrations/*{.ts,.js}'],
synchronize: false,
migrationsRun: true,
cli: {
migrationsDir: 'migrations',
},
};
import { Module } from '@nestjs/common';
import {DbConfig} from './config/db.config'
import {TypeOrmModule} from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
TypeOrmModule.forRoot(DbConfig),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
npm i -g typeorm
typeorm migration:create -n filename
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
export class CreateUser1234567891012 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
return await queryRunner.createTable(new Table({
name: 'users',
columns: [
{
name: 'id',
type: 'bigint',
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment',
},
{
name: 'name',
type: 'varchar',
},
{
name: 'email',
isUnique: true,
type: 'varchar',
},
{
name: 'referal',
type:'varchar',
isUnique: true,
isNullable: true,
},
{
name: 'resetToken',
type:'varchar',
isUnique: true,
isNullable: true,
},
{
name: 'type',
type:'varchar'
},
{
name: 'password',
type: 'varchar',
isNullable: false,
},
{
name: 'status',
type: 'varchar',
default: '\'ACTIVE\'',
},
{
name: 'lastLogin',
type: 'timestamp',
isNullable: true,
default: 'CURRENT_TIMESTAMP',
},
{
name: 'createdAt',
type: 'timestamp',
default: 'CURRENT_TIMESTAMP',
isNullable: false,
},
{
name: 'updatedAt',
type: 'timestamp',
default: 'CURRENT_TIMESTAMP',
isNullable: false,
},
],
}), true);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(`DROP TABLE "users"`);
}
}
npm run