Skip to content

What is the best strategy for using node-postgres in multiuser CRUD application #936

@sairum

Description

@sairum

Sorry if this is not the right place to ask this question, but after reading a lot of documentation on using 'pg' module in express (or any other RDBMS, for that matter), I feel that most examples are tailored to a single DB user, even though they appear to be multiuser environments (like many blog or todo applications, where each 'user' actually uses an hidden login/password pair to commit data to the DB). The documentation of node-postgres is also not clear about the issue I report below...

Let me explain. I'm currently refactoring a LAMP (Apache+PHP+Postgres) CRUD application into a Node+Express+node-postgres one. Most of the business logic is made within the Postgres server (user permissions, stored procedures, materialized views, etc). This application is a sort of a RESTful API. By "sort of", I mean an API that is not totally stateless, as it uses sessions to keep users names/passwords in the server during the whole duration of the session, which are then used in each access to the DB (typical PHP pattern). This works well in a multiuser environment, and the postgres server is responsible for maintaining data integrity, even when conflicting commands are issued by different users simultaneously!

My question is: what is the best strategy to implement a true multiuser CRUD application in node+express+node-postgres, using all the facilities offered by the PostgreSQL engine, like user credentials, permissions, etc?

a) use a global pg instance and change the connectionString in pg.connect (with user:password stored in session) for each request (API endpoint)

b) use a new pgClient for each request (with user:password stored in session) and avoid pg connection pooling altogether (eventually using connection pooling in the server, eg. pgbouncer or pgpool)

c) use a local pg instance in a private thread (app clusters) for each authorized user, maintaining pg's connection pooling

I feel that the last option is the least feasible, as each thread (run on a dedicated CPU) should use a single server, limiting the number of users logged to the number of CPUs. But what about the other two? I've seen a few posts demonstrating both patterns, but they are quite old and maybe not implementing the best practices of node and pg.

Thanks in advance

Antonio

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions