Comments (3)
I too have read and re-read too and I think I understand where @latobibor is coming from.
The note about being primarily a front-end dev helped me understand the perspective (or at least I think so, please let me know!).
@latobibor my friend I think you are thinking of back-end vs. front-end in the classic web development HR resume keyword sense i.e. backend is like Express app and client aka front-end is like React.
From that angle, yeah, wtf!.
To adjust your angle, forget that web browsers even exist and put yourself in a database library's shoes. Also I think it is helpful to consider why the variable and type names are "pool", "DatabasePool", etc.
A client connection is between a database client and either the database / database cluster or the database cluster’s connection pool (such as pgbouncer). Whenever anything connects to postgres the server will fork a new process (aka a backend to postgres DBA's) to handle it.
Its common to use postgres with connection pool managers like pgbouncer or pgpool-II. You can read a bit about those here (Neon is a postgres provider and at this link they explain how they use pgbouncer): https://neon.tech/docs/connect/connection-pooling
Now imagine you are the mighty @gajus who created slonik for serious CTO level IT problems at scale and you think in terms of database clusters, connection pools, multiple backend database servers (master / slave etc.), replication, etc., etc. The big guns!
In this light it is a very good warning in the docs that you might require a persistent connection to the same backend depending on what setup you have.
I think realistically you will always want to use this syntax so the connection is automatically released:
pool.connect(async (connection) => {
await connection.query(sql.typeAlias('id')`SELECT 1 AS id`);
});
However... now imagine your Node/Express backend (i.e. backend in the context I think you originally had in mind) and imagine you have split up your code to have different repositories, services, etc.
Now imagine the lifecycle of servicing a single request where many of those different repositories / services are tapped to service the request (e.g. maybe the request has something to do with auth, users, etc).
If each of those repositories, services, etc in play independently had their own pool.connect(...)
then each might end up using a different backend (in the context of postgres) and that could be a problem for you. There could be implications with transactions and there could also be extra overhead to grabbing and releasing connections many times.
This is really what the docs are warning you about here.
Some of the setups I've seen for integrating with Fastify, Express, etc, effectively do a pool.connect()
at the start of the request and then make the connection
available throughout the rest of the lifecycle of the request (so then any repositories/services/etc. that follow would use that same connection to avoid potential problems).
I hope I nailed where your point of confusion was and that this answers your question.
All said, I think your question does raise an important point about the clarity in the docs.
After writing this all I feel I can see how both you and gajus are totally right in your respective understanding of "backend" :)
from slonik.
I read and re-read your comment and I still cannot wrap my head around what you are asking of which part is not clear.
How to create a connection?
await pool.connect(async (connection) => {
// This is your connection object:
// connection;
});
from slonik.
Hope this helps @latobibor https://dev.to/gajus/integrating-slonik-with-expressjs-33kn
from slonik.
Related Issues (20)
- Type parser not applied in jsonb_agg fields HOT 1
- Include index in error when trying to bind undefined value HOT 1
- How to use `WHERE id IN` / `WHERE id = ANY` with v34+? HOT 3
- "Argument is not assignable" error in the Result parser interceptor example HOT 6
- Deno support?
- Type aliases not validating with Zod? HOT 4
- Any way I can do WHERE LIKE query? HOT 1
- Produce the original stack trace
- Potential memory leak when using with the Jest's `--detectLeaks` flag HOT 3
- Support pgvector HOT 2
- BigInt break Jest (`JSON.stringify()`) HOT 4
- Make Slonik runtime validation non-Zod specific HOT 6
- What is the expected behavior of serializing BigInt to JSON? HOT 6
- Document `originalError` rename to `cause` HOT 2
- Evaluate adding a way to re-use value bindings HOT 2
- Bug Report: sql.jsonb over-escapes quotation marks HOT 1
- Transform example is not working HOT 1
- How to insert embedding/vector using Slonik? HOT 1
- Backwards compatibility with old sql tag HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slonik.