{"id":16465786,"url":"https://github.com/tony133/nestjs-postgres","last_synced_at":"2025-03-16T18:31:27.277Z","repository":{"id":39591032,"uuid":"506775672","full_name":"Tony133/nestjs-postgres","owner":"Tony133","description":"Postgres module for Nest framework (node.js) 😻","archived":false,"fork":false,"pushed_at":"2025-03-11T22:07:23.000Z","size":2860,"stargazers_count":18,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-11T23:19:57.703Z","etag":null,"topics":["javascript","nest","nestjs","nestjs-postgres","nestjs-postgresql","postgres","postgresql","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tony133.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-23T20:03:04.000Z","updated_at":"2025-03-11T22:07:26.000Z","dependencies_parsed_at":"2024-01-28T03:18:58.481Z","dependency_job_id":"af02b832-14ec-4ae2-badb-604a84d9edbd","html_url":"https://github.com/Tony133/nestjs-postgres","commit_stats":{"total_commits":841,"total_committers":5,"mean_commits":168.2,"dds":0.0642092746730083,"last_synced_commit":"9a5b86b92b6bb6719eb02bbafdd4e826538189c9"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tony133","download_url":"https://codeload.github.com/Tony133/nestjs-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","nest","nestjs","nestjs-postgres","nestjs-postgresql","postgres","postgresql","typescript"],"created_at":"2024-10-11T11:35:13.627Z","updated_at":"2025-03-16T18:31:26.769Z","avatar_url":"https://github.com/Tony133.png","language":"TypeScript","funding_links":["https://opencollective.com/nest","https://paypal.me/kamilmysliwiec"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"200\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456\n[circleci-url]: https://circleci.com/gh/nestjs/nest\n\n  \u003cp align=\"center\"\u003eA progressive \u003ca href=\"http://nodejs.org\" target=\"_blank\"\u003eNode.js\u003c/a\u003e framework for building efficient and scalable server-side applications.\u003c/p\u003e\n    \u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@nestjs/core.svg\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@nestjs/core.svg\" alt=\"Package License\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@nestjs/common.svg\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://circleci.com/gh/nestjs/nest\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/circleci/build/github/nestjs/nest/master\" alt=\"CircleCI\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://coveralls.io/github/nestjs/nest?branch=master\" target=\"_blank\"\u003e\u003cimg src=\"https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9\" alt=\"Coverage\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://discord.gg/G7Qnnhy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/discord-online-brightgreen.svg\" alt=\"Discord\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#backer\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/nest/backers/badge.svg\" alt=\"Backers on Open Collective\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#sponsor\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/nest/sponsors/badge.svg\" alt=\"Sponsors on Open Collective\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://paypal.me/kamilmysliwiec\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Donate-PayPal-ff3f59.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://opencollective.com/nest#sponsor\"  target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg\" alt=\"Support us\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://twitter.com/nestframework\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/twitter/follow/nestframework.svg?style=social\u0026label=Follow\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n  \u003c!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)\n  [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)--\u003e\n\n## Description\n\nPostgreSQL module for Nest framework (node.js) 😻\n\n## Installation\n\nFirst install the module via `yarn` or `npm` or `pnpm` and do not forget to install the driver package as well:\n\n\n```bash\n    $ npm i --save nest-postgres pg\n```\nor\n\n```bash\n    $ yarn add nest-postgres pg\n```\n\nor\n\n```bash\n    $ pnpm add nest-postgres pg\n```\n\n## Table of Contents\n\n- [Description](#description)\n- [Table of Contents](#table-of-contents)\n- [Usage](#usage)\n  - [PostgresModule](#postgresmodule)\n  - [MultiConnectionsDatabase](#multi-connections-database)\n  - [ExampleOfUse](#example-of-use)\n\n## Usage\n\n### PostgresModule\n\nPostgresModule is the primary entry point for this package and can be used synchronously\n\n```typescript\n@Module({\n  imports: [\n    PostgresModule.forRoot({\n        connectionString: 'postgresql://[user]:[password]@[host]/[nameDb]',\n        // or \n        // host: 'localhost',\n        // database: [:databaseName],\n        // password: [:passwordDb],\n        // user: [:userDb],\n        // port: 5432,\n    }),\n  ],\n})\n```\n\nor asynchronously\n\n```typescript\n@Module({\n  imports: [\n    PostgresModule.forRootAsync({\n      useFactory: () =\u003e ({\n        connectionString: 'postgresql://[user]:[password]@[host]/[nameDb]',\n        // or \n        // host: 'localhost',\n        // database: [:databaseName],\n        // password: [:passwordDb],\n        // user: [:userDb],\n        // port: 5432,\n      }),\n    }),\n  ],\n})\n```\n\n## Example of use\n\nUsersService:\n\n```typescript\nimport { Client } from 'pg';\nimport { InjectClient } from 'nest-postgres';\n\n@Injectable()\nexport class UsersService {\n  constructor(@InjectClient() private readonly pg: Client) {}\n\n  public async findAll(): Promise\u003cUser[]\u003e {\n    const users = await this.pg.query('SELECT * FROM users');\n    return users.rows;\n  }\n}\n```\n\nUsersController:\n\n```typescript\nimport { Controller, Get } from '@nestjs/common';\nimport { UsersService } from './users.service';\n\n@Controller()\nexport class UsersController {\n  constructor(private readonly usersService: UsersService) {}\n\n  @Get()\n  async getAllUsers(): Promise\u003cUser[]\u003e {\n    return await this.usersService.findAll();\n  }\n}\n```\n\n## Multi Connections Database\n\n```typescript\n@Module({\n  imports: [\n    PostgresModule.forRoot(\n      {\n        connectionString: 'postgresql://postgres:pass123@localhost:5432/nest1',\n        // or \n        // host: 'localhost',\n        // database: [:databaseName],\n        // password: [:passwordDb],\n        // user: [:userDb],\n        // port: 5432,\n      },\n      'db1Connection',\n    ),\n    PostgresModule.forRoot(\n      {\n        connectionString: 'postgresql://postgres:pass123@localhost:5434/nest2',\n      },\n      'db2Connection',\n    ),\n  ],\n  controllers: [],\n  providers: [],\n})\nexport class AppModule {}\n```\n\nUsage example with Multi Connection\n\nPostService:\n\n```typescript\nimport { Client } from 'pg';\nimport { InjectConnection } from 'nest-postgres';\nimport { CreatePostDto } from './dto/create-post.dto';\nimport { Post } from './interfaces/post.interface';\n\n@Injectable()\nexport class PostService {\n  constructor(\n    @InjectConnection('db2Connection')\n    private dbConnection: Client,\n  ) {}\n\n  public async findAll(): Promise\u003cPost[]\u003e {\n    const users = await this.dbConnection.query('SELECT * FROM posts');\n    return users.rows;\n  }\n\n  public async create(createPostDto: CreatePostDto): Promise\u003cPost[]\u003e {\n    try {\n      const user = await this.dbConnection.query(\n        'INSERT INTO posts (title, description)  VALUES ($1, $2) RETURNING *',\n        [createPostDto.title, createPostDto.description],\n      );\n      return user.rows;\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n}\n```\n\nUsersService:\n\n```typescript\nimport { Client } from 'pg';\nimport { InjectConnection } from 'nest-postgres';\nimport { CreateUserDto } from './dto/create-user.dto';\nimport { User } from './interfaces/user.interface';\n\n@Injectable()\nexport class UsersService {\n  constructor(\n    @InjectConnection('db1Connection')\n    private dbConnection: Client,\n  ) {}\n\n  public async findAll(): Promise\u003cUser[]\u003e {\n    const users = await this.dbConnection.query('SELECT * FROM users');\n    return users.rows;\n  }\n\n  public async create(createUserDto: CreateUserDto): Promise\u003cUser[]\u003e {\n    try {\n      const user = await this.dbConnection.query(\n        'INSERT INTO users (firstName, lastName)  VALUES ($1, $2) RETURNING *',\n        [createUserDto.firstName, createUserDto.lastName],\n      );\n      return user.rows;\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n}\n```\n\nFor more information on `node-postgres` for Nodejs see [here](https://node-postgres.com/)\n\n## Contribute\nFeel 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\n\n## Stay in touch\n\n- Author - [Tony133](https://github.com/Tony133)\n- Framework - [https://nestjs.com](https://nestjs.com/)\n\n## License\n\n [MIT licensed](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftony133%2Fnestjs-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftony133%2Fnestjs-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftony133%2Fnestjs-postgres/lists"}