Nostradamus Prophecy Machine

    @skazka/server-pg

    0.0.12 • Public • Published

    Server PostgreSQL

    Skazka Server PostgreSQL client.

    NPM

    How to install

    npm i @skazka/server @skazka/server-pg pg config
    

    With yarn:

    yarn add @skazka/server @skazka/server-pg pg config
    

    Optionally you can add http server, error handler, logger, router, request and response:

    npm i @skazka/server-http @skazka/server-router @skazka/server-error @skazka/server-logger @skazka/server-request @skazka/server-response
    

    With yarn:

    yarn add @skazka/server-http @skazka/server-router @skazka/server-error @skazka/server-logger @skazka/server-request @skazka/server-response
    

    How to use

    Config

    config/default.json

    {
      "pg": {
        "host": "127.0.0.1",
        "port": 5432,
        "user": "user",
        "password": "password",
        "database": "database"
      }
    }

    Server module

    const App = require('@skazka/server');
    const Router = require('@skazka/server-router');
    
    const pg = require('@skazka/server-pg');
            
    const error = require('@skazka/server-error');
    const logger = require('@skazka/server-logger');
    
    const request = require('@skazka/server-request');
    const response = require('@skazka/server-response');
            
    const server = require('@skazka/server-http');
            
    const app = new App();
    const router = new Router();
            
    app.all([
      error(),
      logger(),
      request(),
      pg(),
      response(),
    ]);
        
    app.then(async (ctx) => {
      // it works for each request
      const result = await ctx.pg.query('SELECT * FROM users;');
    });
        
    router.get('/data').then(async (ctx) => {
      const result = await ctx.pg.query('SELECT * FROM users;');
                
      return ctx.response(result); 
    });
            
    app.then(router.resolve());
            
    server.createHttpServer(app);

    With connection:

    app.then(async (ctx) => {
      const client = await ctx.pg.connect();
      const result = await client.query('SELECT * FROM users;');
      client.release();
      
      return ctx.response(result); 
    });

    Any other module

    const pool = require('@skazka/server-pg/pool');
        
    async () => {
      try {
        const result = await pool.query('SELECT * FROM users;');
      } catch (error) {
        
      }
    };

    With connection:

    const pool = require('@skazka/server-pg/pool');
        
    async () => {
      try {
        const client = await ctx.pg.connect();
        const result = await client.query('SELECT * FROM users;');
      } catch (error) {
    
      } finally {
        client.release();
      }
    };

    Transactions

    const App = require('@skazka/server');
    const app = new App();
    const error = require('@skazka/server-error');
    const logger = require('@skazka/server-logger');
        
    const pg = require('@skazka/server-pg');
        
    app.all([
      error(),
      logger(),
      pg(),
    ]);
        
    app.then(async (ctx) => {
      const client = await ctx.pg.connect();
            
      try {
        await client.query('BEGIN');
        await client.query('DELETE FROM users;');
        await client.query('COMMIT');
      } catch (e) {
        await client.query('ROLLBACK');
      } finally {
        client.release();
      }
    });

    Or in any other module:

    const pool = require('@skazka/server-pg/pool');
        
    async () => {
      const client = await pool.connect();
        
      try {
       await client.query('BEGIN');
       await client.query('DELETE FROM users;');
       await client.query('COMMIT');
      } catch (error) {
        await client.query('ROLLBACK');
      } finally {
        client.release();
      }
    };

    Install

    npm i @skazka/server-pg

    DownloadsWeekly Downloads

    8

    Version

    0.0.12

    License

    MIT

    Unpacked Size

    4.78 kB

    Total Files

    4

    Last publish

    Collaborators

    • evheniy.bystrov
    • skazkajs