Documentation
¶
Overview ¶
Package db @notice Opens the Postgres pool luima's resolvers query through.
@dev One function, because connecting is the only part of the data layer that is the same in every application. Everything else is your models and the crud package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect @notice Opens the pool the resolvers query through and proves it works.
@dev Two things about pg.ParseURL that the connection string has to respect:
- It rejects any query parameter other than sslmode, application_name and connect_timeout, with "pg: options other than ... are not supported". pgx-style tuning such as ?default_query_exec_mode=simple_protocol is therefore a startup failure here, not a no-op.
- With sslmode absent it returns &tls.Config{InsecureSkipVerify: true}, so a managed Postgres connects over TLS with nothing to configure — and nothing verified. ?sslmode=verify-full is what verifies it, and Connect fills in the ServerName that pg.ParseURL leaves empty so that mode can actually complete a handshake.
Both postgres:// and postgresql:// schemes parse. ?connect_timeout=N bounds the whole boot round trip here, not just the dial.
Connect takes a URL and nothing else, deliberately. It is not the only way in: everything it does is three exported calls, so tuning that pg.Options exposes and a DSN cannot express — ReadTimeout, PoolSize, an OnConnect that sets statement_timeout — is a matter of calling pg.ParseURL, editing the *pg.Options and calling pg.Connect yourself. Query timeouts do not need that: server.Config.RequestTimeout puts a deadline on the resolver's context, and go-pg turns that into both the socket deadline and a Postgres CancelRequest.
Unlike the server this was lifted from, Connect returns its error rather than calling log.Fatal, and takes the URL rather than reading os.Getenv: a library must not kill the caller's process, choose their logging, or read configuration behind their back. Write log.Fatal(err) at the call site if that is what you want. Connect's errors never contain the DSN, so logging one does not leak the password.
@param url a postgres:// or postgresql:// connection string @return *pg.DB a live pool, already proven with a round trip; nil on error @return error a parse failure, or the ping failure with the pool already closed
Types ¶
This section is empty.