2.3.3 • Published 17 days ago

nestjs-pgpromise v2.3.3

Weekly downloads
222
License
MIT
Repository
-
Last release
17 days ago

Description

This's a nest-pgpromise module for 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 setup in place.

Download from NPM

npm install --save nestjs-pgpromise

Initialize

You need five items in order to connect to the PostgreSQL server.

ParamsDescription
hostHost IP address or URL.
portTCP/IP port number to access the database.
databaseThe name of the database to connect to.
userThe username to access the database.
passwordThe 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.

Provide the credentials for pg-promise module by importing it as :

As Connection object

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

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:

constructor(@Inject(NEST_PGPROMISE_CONNECTION) private readonly pg: IDatabase<any>) {}

Quick Start Example

This example program connects to postgres on localhost and executes a simple select query from table tasks.

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<any>) {}

  @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.

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<any>) {}

  @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.

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:

  new this.pg.$config.pgp.helpers.ColumnSet(['col1', 'col2']);

You can find the details about them in the pg-promise documentation

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

2.3.3

17 days ago

2.3.2

7 months ago

2.3.1

8 months ago

2.3.0

11 months ago

2.2.3

1 year ago

2.2.1

1 year ago

2.2.2

1 year ago

2.0.3

1 year ago

2.2.0

1 year ago

2.0.2

2 years ago

2.1.0

1 year ago

2.0.1

2 years ago

1.5.1

2 years ago

2.0.0

2 years ago

1.5.0

2 years ago

1.5.0-0

2 years ago

1.4.12

2 years ago

1.4.11

2 years ago

1.4.6

2 years ago

1.4.9

2 years ago

1.4.10

2 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.10

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.3-0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago