pgstore

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func New(ctx context.Context, db *sql.DB) (*sqlstore.Store, error)

New returns a store on an open Postgres database, creating the schema when it is missing.

func Open

func Open(ctx context.Context, dsn string) (*sqlstore.Store, error)

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
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL