Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Postgres module for Nest framework (node.js) 😻

License

NotificationsYou must be signed in to change notification settings

Tony133/nestjs-postgres

Repository files navigation

Nest Logo

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

NPM VersionPackage LicenseNPM DownloadsCircleCICoverageDiscordBackers on Open CollectiveSponsors on Open CollectiveSupport us

Description

PostgreSQL module for Nest framework (node.js) 😻

Installation

First install the module viayarn ornpm orpnpm and do not forget to install the driver package as well:

    $ npm i --save nest-postgres pg

or

    $ yarn add nest-postgres pg

or

    $ pnpm add nest-postgres pg

Table of Contents

Usage

PostgresModule

PostgresModule is the primary entry point for this package and can be used synchronously

@Module({imports:[PostgresModule.forRoot({connectionString:'postgresql://[user]:[password]@[host]/[nameDb]',// or// host: 'localhost',// database: [:databaseName],// password: [:passwordDb],// user: [:userDb],// port: 5432,}),],})

or asynchronously

@Module({imports:[PostgresModule.forRootAsync({useFactory:()=>({connectionString:'postgresql://[user]:[password]@[host]/[nameDb]',// or// host: 'localhost',// database: [:databaseName],// password: [:passwordDb],// user: [:userDb],// port: 5432,}),}),],})

Example of use

UsersService:

import{Client}from'pg';import{InjectClient}from'nest-postgres';@Injectable()exportclassUsersService{constructor(@InjectClient()privatereadonlypg:Client){}publicasyncfindAll():Promise<User[]>{constusers=awaitthis.pg.query('SELECT * FROM users');returnusers.rows;}}

UsersController:

import{Controller,Get}from'@nestjs/common';import{UsersService}from'./users.service';@Controller()exportclassUsersController{constructor(privatereadonlyusersService:UsersService){}  @Get()asyncgetAllUsers():Promise<User[]>{returnawaitthis.usersService.findAll();}}

Multi Connections Database

@Module({imports:[PostgresModule.forRoot({connectionString:'postgresql://postgres:pass123@localhost:5432/nest1',// or// host: 'localhost',// database: [:databaseName],// password: [:passwordDb],// user: [:userDb],// port: 5432,},'db1Connection',),PostgresModule.forRoot({connectionString:'postgresql://postgres:pass123@localhost:5434/nest2',},'db2Connection',),],controllers:[],providers:[],})exportclassAppModule{}

Usage example with Multi Connection

PostService:

import{Client}from'pg';import{InjectConnection}from'nest-postgres';import{CreatePostDto}from'./dto/create-post.dto';import{Post}from'./interfaces/post.interface';@Injectable()exportclassPostService{constructor(    @InjectConnection('db2Connection')privatedbConnection:Client,){}publicasyncfindAll():Promise<Post[]>{constusers=awaitthis.dbConnection.query('SELECT * FROM posts');returnusers.rows;}publicasynccreate(createPostDto:CreatePostDto):Promise<Post[]>{try{constuser=awaitthis.dbConnection.query('INSERT INTO posts (title, description)  VALUES ($1, $2) RETURNING *',[createPostDto.title,createPostDto.description],);returnuser.rows;}catch(err){thrownewHttpException(err,HttpStatus.BAD_REQUEST);}}}

UsersService:

import{Client}from'pg';import{InjectConnection}from'nest-postgres';import{CreateUserDto}from'./dto/create-user.dto';import{User}from'./interfaces/user.interface';@Injectable()exportclassUsersService{constructor(    @InjectConnection('db1Connection')privatedbConnection:Client,){}publicasyncfindAll():Promise<User[]>{constusers=awaitthis.dbConnection.query('SELECT * FROM users');returnusers.rows;}publicasynccreate(createUserDto:CreateUserDto):Promise<User[]>{try{constuser=awaitthis.dbConnection.query('INSERT INTO users (firstName, lastName)  VALUES ($1, $2) RETURNING *',[createUserDto.firstName,createUserDto.lastName],);returnuser.rows;}catch(err){thrownewHttpException(err,HttpStatus.BAD_REQUEST);}}}

For more information onnode-postgres for Nodejs seehere

Contribute

Feel free to help this library, I'm quite busy with also another Nestjs packages, but the community will appreciate the effort of improving this library. Make sure you follow the guidelines

Stay in touch

License

MIT licensed

About

Postgres module for Nest framework (node.js) 😻

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp