GithubHelp home page GithubHelp logo

taller-postgres's Introduction

taller-postgres

Sample with Webpack.

Environment with:

Make tasks of environment

  • Build docker environment - $ make build
  • UP and RUN docker environment - $ make run
  • Stop docker environment - $ make stop
  • Destroy docker environment - $ make down

First steps after environment builded

taller:~/app(master)$ npm install
taller:~/app(master)$ npm test
taller:~/app(master)$ npm run build

##How to usage?

import tallerPostgres from 'taller-postgres'

// create a config to configure both pooling behavior
// and client options
// note: all config is optional and the environment variables
// will be read if the config is not present
const config = {
  user: 'foo', //env var: PGUSER
  database: 'my_db', //env var: PGDATABASE
  password: 'secret', //env var: PGPASSWORD
  port: 5432, //env var: PGPORT
  max: 10, // max number of clients in the pool
  idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
}

//this initializes a connection pool
//it will keep idle connections open for a 30 seconds
//and set a limit of maximum 10 idle clients
const pool = tallerPostgres(config)

try {
  // to run a query we can acquire a client from the pool,
  // run a query on the client, and then return the client to the pool
  const client = await pool.connect()

  // Start transaction
  await client.startTransaction()

  // Execute SQL of searches
  const {rows} = await client.execute('SELECT * FROM customers')

  // Execute SQL of inserts
  const {rows} = await client.execute('INSERT INTO customers (name, birthday) VALUES ("Test", "1988-06-10")')

  // Execute SQL of updates
  const {rows} = await client.execute('UPDATE customers SET name = "Test", birthday = "1988-06-10" WHERE (id = 1)')

  // Execute SQL of deletes
  const {rows} = await client.execute('DELETE FROM customers WHERE (id = 1)')

  // Commit transaction
  await client.commit()

  // Or rollback transaction
  await client.rollback()

  // release the client back to the pool
  await client.release()
} catch (error) {
  console.log(error)
}

taller-postgres's People

Contributors

luiz-simples avatar

Stargazers

Pedro Monad avatar

Watchers

Sebastian avatar Handrus Stephan Nogueira avatar Henrique Recidive avatar Reva Gomes avatar Helal Ferrari Cabral avatar James Cloos avatar  avatar Lucas Constantino Silva avatar Rafael Caceres avatar Edmar Gomes avatar  avatar

Forkers

pedromonad

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.