Coder Social home page Coder Social logo

pg's Introduction

Very simple PostgreSQL async pool api for nim

nimble install pg

Github Actions

API reference

This library has no dependencies other than the Nim standard libarary.

About

To open a pool with 10 connections simply use:

let pg = newAsyncPool("localhost", "user", "password", "dbname", 10)

By using a connection pool, 10 connections are opened and when your application makes a query a free connection is taking from the pool. Otherwise your query will wait for a free connection to become available. This allows your application to run any number of queries at once. By default PostgreSQL will accept 100 connections. PostgreSQL can run multiple queries at once as well but they will start impacting each other especially if they read or write to same tables, or you have run out of CPU cores.

You can open a single connection with db_postgres's api:

let pg = open("localhost", "user", "password", "dbname")

But you have to be careful to not run more then one query at once on the connection.

Get rows

If running in {.async.} function use await:

let rows = await pg.rows(sql"SELECT 1")
for row in rows:
  echo row

Otherwies you can use waitFor:

let rows = waitFor pg.rows(sql"SELECT 1")

You can add multiple paramaters that will be escaped:

let rows = await pg.rows(sql"SELECT ?, pg_sleep(1);", @["foo"])

Run query without results

You can also run query by ignoring results in {.async.} function use await:

await pg.exec(sql"UPDATE TABLE foo SET a=1")

Concurrency:

Run 10 queries at once:

let pool = newAsyncPool("localhost", "", "", "test", 2)
var futures = newSeq[Future[seq[Row]]]()
for i in 0..<20:
  futures.add pool.rows(sql"SELECT ?, pg_sleep(1);", @[$i])
for f in futures:
  var res = await f
echo res

Run query but don't care about the results or when it will finish:

asyncCheck pg.exec(sql"UPDATE TABLE foo SET a=1")

pg's People

Contributors

treeform avatar yu-vitaqua-fer-chronos avatar david-kunz avatar

Watchers

 avatar

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.