Documentation
¶
Overview ¶
Package pgstore opens an accounts.Store in a Postgres database with the github.com/jackc/pgx/v5 driver. Several account service processes may share one database: its constraints keep email addresses and identities unique, and migrations take an advisory lock.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Dialect = sqlstore.Dialect{ Name: "postgres", Numbered: true, IsUnique: func(err error) bool { var e *pgconn.PgError return errors.As(err, &e) && e.Code == uniqueViolation }, MigrateLock: `SELECT pg_advisory_xact_lock(1633903476)`, }
Dialect is Postgres's sqlstore dialect.
Functions ¶
func New ¶
New returns a store on an open Postgres database, creating the schema when it is missing.
func Open ¶
Open connects to the Postgres database dsn names (a URL or a key=value string, as pgx parses it) and returns a store on it with the schema created. Keep the DSN, which may hold a password, out of logs and command lines: read it from the environment or a file. Close the store to close the connections.
Example ¶
Open connects to Postgres and returns an accounts.Store with the schema created, ready to pass to accounts.New. The example needs a running database, so it is compiled but not run; read the DSN from the environment or a file, never from a command line.
package main
import (
"context"
"fmt"
"os"
"github.com/nsgo-dev/game/pkg/accounts"
"github.com/nsgo-dev/game/pkg/accounts/pgstore"
"github.com/nsgo-dev/game/pkg/accounts/token"
)
func main() {
ctx := context.Background()
store, err := pgstore.Open(ctx, os.Getenv("ACCOUNTS_DATABASE_URL"))
if err != nil {
fmt.Println(err)
return
}
defer store.Close()
pem, err := os.ReadFile("signing-key.pem")
if err != nil {
fmt.Println(err)
return
}
key, err := token.ParsePrivateKey(pem)
if err != nil {
fmt.Println(err)
return
}
svc, err := accounts.New(accounts.DefaultConfig("https://accounts.example.com", "my-game"), accounts.Options{Store: store, Key: key})
if err != nil {
fmt.Println(err)
return
}
_ = svc
}
Output:
Types ¶
This section is empty.