1.1.2 • Published 5 years ago

@tdqs/koa-pg v1.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

@tdqs/koa-pg

The MIT License (MIT)

@tdqs/koa-pg koa middleware based on pg.

Table of Contents

Installation
API documentation
Example

Installation

    $ npm install @tdqs/koa-pg

API documentation

This is a simple code overview of how to use @tdqs/koa-pg.

KoaPg.constructor

Param nameDescriptionMandatory
appInstance object of koa.ApplicationYES
configobject which Implementation PoolConfigYES
urlobject which Implementation IKoaPg for allow or prohibit the use of databasesNO
modeWhite List or Black List Default whiteNO

code simplification

import { KoaPg } from '@tdqs/koa-pg';
import * as Koa from 'koa';

const conf = {
  database: 'database',
  host: 'host',
  password: 'password',
  port: 5432,
  user: 'user',
};

const app = new Koa();
const koaPg = new KoaPg(app, conf, { '/get': '*', '/sleep': ['get'] });

app.use(koaPg.middleware());
app.use(async ctx => {
  if (ctx.path === '/get') {
    ctx.body = JSON.stringify(await ctx.conn.query('SELECT 123'));
  } else if (ctx.path === '/sleep') {
    ctx.body = JSON.stringify(
      await ctx.conn.query(`SELECT pg_sleep(${ctx.query.time})`),
    );
  } else {
    ctx.body = `conn: ${ctx.conn}`; // undefined
  }
});

app.listen(8080);

add typings/index.d.ts for hints

import { IKoaPg } from '@tdqs/koa-pg';
import 'koa';
import { Pool, PoolClient } from 'pg';

declare module 'koa' {
  interface BaseContext {
    koaPg: IKoaPg;
  }

  interface Context {
    conn: PoolClient;
  }
}

Example

Following example could be found inside /example directory.

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago