Interesting Ideas
basicdays / co-pg Goto Github PK
View Code? Open in Web Editor NEWPromise wrapper for node-postgres. Works with co-routines and async/await!
License: MIT License
Promise wrapper for node-postgres. Works with co-routines and async/await!
License: MIT License
Was wondering if this Co-Wrapper is still being maintained?
Seems promises won out as the chosen mechanism to handle co-routines over thunks. Will add promise returning functions in addition to the thunk returning methods. Should both work with co.
The examples, especially when visible on the README, should at the very least be robust enough for people to copy and paste into their codebase. For example, both examples so far leak a connection upon error.
Furthermore, the examples might as well show how you would actually use the library in your projects. You would generally implement two abstractions in your db.js
file: a query
function that just runs one query, and a withTransaction
function that wraps queries in a BEGIN/COMMIT/ROLLBACK.
Update: Here are new versions of the wrapper code I snipped out below: https://github.com/danneu/koa-skeleton/blob/4d654c8963848d171e9b2ae3e0ef91721058d1e1/src/db/util.js
<snipped out old code>
These would be much more helpful to people. What do you think?
The idea is that people should be able to copy query
and withTransaction
into their db.js
and immediately get to work.
I am wondering if when running pg-co with node express server, do you connect to the database on every client connection? or keep a global connection?
In other words, here is my node module:
module.exports.set = function(app, worker_id) {
var ms = require("mslibmod"),
co = require('co'),
async = require('async'),
pgconn = require('pgconn').create(),
pg = require('co-pg')(require('pg')),
exec = require('co-exec'),
thunkify = require('thunkify');
app.get('/getLines', function(req, res, next) {
var id = req.param('id');
co(function*() {
try {
var pgHandle = yield pg.connect_(pgconn.dbserver('galaxy'));
var statement = 'select business_name FROM business_info WHERE business_id = ' + '1016';
var result = yield pgHandle[0].query_(statement);
pgHandle[1](); // pg queue done
if (result.rows[0] == undefined){}
ms.log(result.rows[0].business_name);
} catch (e) {
console.error('Panic: ' + e.toString());
}
})();
res.send({});
});
}
notice how I run the command:
var pgHandle = yield pg.connect_(pgconn.dbserver('galaxy'));
everytime a new express client is connected, is this the correct approach?
wouldn't lead to memory leaks?
regards,
Sean.
Getting an error on install with yarn
Hi, I got problem when I connect my local postgreSQL on windows 7.
Here's the code(actually your demo)
var co = require('co');
var pg = require('co-pg')(require('pg'));
var connectionString = 'postgres://postgres:5432@localhost/mmocad_result'; // custom config
co(function *() {
try {
var client = new pg.Client(connectionString);
yield client.connectPromise();
var result = yield client.queryPromise('select * from public.usertable');
console.log(result.rows[0].theTime);
client.end();
} catch (ex) {
console.error(ex);
}
});
catch error: password authentication failed for user "postgres"
. I think is something wrong with my connectionString
, but I hava no idea how to config my user or password.
postgresSQL version 9.6
user postgres
password root
Thanks! =]
Virtualbox is getting annoying to deal with DKMS and Ubuntu secure boot. Docker should hopefully provide a cleaner path going forward.
I have the following line. It fires off and I never hear anything back. It never calls back and nothing is caught in the try-catch so how else can I tell what happened if it never fails and never calls back?
var connectionResults = yield pg.connectPromise(config.connection); // this never calls back after trying to connect
Using require('pg').native
when passed through co-pg
, is not 100% functional yet. Pooled connections are working, single connection directly with the Client
type isn't working yet. Fix should be out soon.
Hi Paul,
I'm still new to generators and 'co' so I can't quite work this out. Is there a way to get at the original 'pg.defaults' so I can then set the poolSize?
My use-case is to allow options to be passed into the koa-pg middleware so that the user can set this to what they require. :)
Many thanks,
Andy
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.