- Notifications
You must be signed in to change notification settings - Fork3
Postgres module for Nest framework (node.js) 😻
License
Tony133/nestjs-postgres
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A progressiveNode.js framework for building efficient and scalable server-side applications.
PostgreSQL module for Nest framework (node.js) 😻
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
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,}),}),],})
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();}}
@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
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
- Author -Tony133
- Framework -https://nestjs.com
About
Postgres module for Nest framework (node.js) 😻
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.