https://github.com/nestcrafts/nestjs-pgpromise
A Module for Utilizing Pg-promise with NestJS
https://github.com/nestcrafts/nestjs-pgpromise
hacktoberfest nest pg-promise
Last synced: 4 months ago
JSON representation
A Module for Utilizing Pg-promise with NestJS
- Host: GitHub
- URL: https://github.com/nestcrafts/nestjs-pgpromise
- Owner: NestCrafts
- Created: 2019-09-24T10:10:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-19T11:16:56.000Z (over 1 year ago)
- Last Synced: 2024-05-19T12:27:59.347Z (over 1 year ago)
- Topics: hacktoberfest, nest, pg-promise
- Language: TypeScript
- Homepage:
- Size: 1.91 MB
- Stars: 29
- Watchers: 3
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
pg-promise Module for Nest framework
## Description
This's a [nest-pgpromise](https://github.com/rubiin/nest-pgpromise) module for [Nest](https://github.com/nestjs/nest).
This quickstart guide will show you how to install and execute an example nestjs program..This document assumes that you have a working [nodejs](http://nodejs.org/) setup in place.
## Download from NPM
```sh
npm install --save nestjs-pgpromise
```## Initialize
You need five items in order to connect to the PostgreSQL server.
| Params | Description |
| :------- | :------------ |
| host | Host IP address or URL. |
| port | TCP/IP port number to access the database. |
| database | The name of the database to connect to. |
| user | The username to access the database. |
| password | The username's password to access the database. |And you can use as well all the other parameters allowed by `pg-promise` package. See the [documentation](https://vitaly-t.github.io/pg-promise/index.html).
Provide the credentials for pg-promise module by importing it as :
## As Connection object
```javascript
import { Module } from '@nestjs/common';
import { NestPgpromiseClientController } from './nest-pgpromise-client.controller';
import { NestPgpromiseModule } from 'nestjs-pgpromise';@Module({
controllers: [NestPgpromiseClientController],
imports: [
NestPgpromiseModule.register({
isGlobal: true,
connection: {
host: 'localhost',
port: 5432,
database: 'cmdbbtbi',
user: 'cmadbbtbi',
password: 'cghQZynG0whwtGki-ci2bpxV5Jw_5k6z',
},
}),
],
})
export class AppModule {}
```## As Connection string
```javascript
import { Module } from '@nestjs/common';
import { NestPgpromiseClientController } from './nest-pgpromise-client.controller';
import { NestPgpromiseModule } from 'nestjs-pgpromise';@Module({
controllers: [NestPgpromiseClientController],
imports: [
NestPgpromiseModule.register(
isGlobal: true,
{
connection: "postgres://YourUserName:YourPassword@YourHost:5432/YourDatabase"
}),
],
})
export class AppModule {}
```Then you can use it in the controller or service by injecting it in the controller as:
```javascript
constructor(@Inject(NEST_PGPROMISE_CONNECTION) private readonly pg: IDatabase) {}
```## Quick Start Example
This example program connects to postgres on localhost and executes a simple `select` query from table `tasks`.
```javascript
import { Controller, Get, Inject, Logger } from '@nestjs/common';
import { NEST_PGPROMISE_CONNECTION } from 'nestjs-pgpromise';
import { IDatabase } from 'pg-promise';@Controller()
export class NestPgpromiseClientController {
private logger = new Logger('controller');
constructor(@Inject(NEST_PGPROMISE_CONNECTION) private readonly pg: IDatabase) {}@Get()
async index() {
this.pg
.any('SELECT * FROM task')
.then(data => {
// success;
this.logger.log(data);
})
.catch(error => {
// error;
this.logger.log(error);
});
}
}
```As `pg-promise` methods return promises, the new `async/await` syntaxis can be used.
```javascript
import { Controller, Get, Inject, Logger } from '@nestjs/common';
import { NEST_PGPROMISE_CONNECTION } from 'nestjs-pgpromise';
import { IDatabase } from 'pg-promise';@Controller()
export class NestPgpromiseClientController {
private logger = new Logger('controller');
constructor(@Inject(NEST_PGPROMISE_CONNECTION) private readonly pg: IDatabase) {}@Get()
async index() {
try {
const data = await this.pg.any('SELECT * FROM task');
// success;
this.logger.log(data);
} catch(e) {
// error;
this.logger.log(error);
}
}
}
```You can also pass in `initoptions` as supported by pg-promise.
```javascript
import { Module } from '@nestjs/common';
import { NestPgpromiseClientController } from './nest-pgpromise-client.controller';
import { NestPgpromiseModule } from 'nestjs-pgpromise';@Module({
controllers: [NestPgpromiseClientController],
imports: [
NestPgpromiseModule.register(
isGlobal: true,
{
connection: {
host: 'localhost',
port: 5432,
database: 'cmdbbtbi',
user: 'cmadbbtbi',
password: 'cghQZynG0whwtGki-ci2bpxV5Jw_5k6z',
},
initOptions:{/* initialization options */};
}),
],
})
export class AppModule {}
```Note: You can then access the underlying PGP object through the $config property, for example:
```javascript
new this.pg.$config.pgp.helpers.ColumnSet(['col1', 'col2']);
```You can find the details about them in the [pg-promise](https://vitaly-t.github.io/pg-promise/index.html) documentation
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Vitaly Tomilov
🚇 ⚠️ 💻
Matthew J. Clemente
🚇 ⚠️ 💻
Jason Santiago
📖
Hector
📖
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!