db

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileLimit   = errors.New("file limit reached")
	ErrNameTaken   = errors.New("file already exists with that name")
	ErrAPIKeyLimit = errors.New("api key limit reached")
)

Functions

This section is empty.

Types

type APIKeys added in v0.13.0

type APIKeys interface {
	// Create creates a new API key. If a user has maxKeys or more keys, ErrAPIKeyLimit is returned.
	Create(ctx context.Context, key *snips.APIKey, maxKeys uint64) error
	// FindByTokenHash returns an API key by its token hash.
	FindByTokenHash(ctx context.Context, tokenHash string) (*snips.APIKey, error)
	// FindByUser returns all API keys for a user, newest first.
	FindByUser(ctx context.Context, userID string) ([]*snips.APIKey, error)
	// Delete deletes a user's API key by ID, reporting whether a key was deleted.
	Delete(ctx context.Context, id, userID string) (bool, error)
	// Touch updates an API key's last_used_at timestamp.
	Touch(ctx context.Context, id string) error
}

type Cursor added in v0.13.0

type Cursor struct {
	Offset uint64
	ID     string
}

Cursor contains backend-neutral pagination state. SQLite uses Offset while PostgreSQL uses the seek fields appropriate to each listing query.

type DB

type DB struct {
	Migrator
	io.Closer
	Files      Files
	PublicKeys PublicKeys
	Users      Users
	Revisions  Revisions
	APIKeys    APIKeys
}

DB groups database operations by their owning table.

type Files added in v0.13.0

type Files interface {
	// Find returns a file by its ID. It does not include file content.
	Find(ctx context.Context, id string) (*snips.File, error)
	// FindWithContent returns a file and its decompressed content by ID in a single query.
	FindWithContent(ctx context.Context, id string) (*snips.File, []byte, error)
	// Create creates a new file, setting file.Size from content. If a user has more than maxFiles, an error is returned.
	Create(ctx context.Context, file *snips.File, content []byte, maxFiles uint64) error
	// FindContent returns a file's decompressed content by ID.
	FindContent(ctx context.Context, id string) ([]byte, error)
	// Update updates a file's metadata, never its content.
	Update(ctx context.Context, file *snips.File) error
	// UpdateContent updates a file and replaces its content, setting file.Size.
	UpdateContent(ctx context.Context, file *snips.File, content []byte) error
	// Delete deletes a file by its ID.
	Delete(ctx context.Context, id string) error
	// DeleteByUser deletes all of a user's files and their revisions, returning the number of files deleted.
	DeleteByUser(ctx context.Context, userID string) (int64, error)
	// FindByUser returns a user's files, newest first. It does not include file content.
	FindByUser(ctx context.Context, userID string, opts ...PageOption) ([]*snips.File, error)
	// FindByName returns a user's file with the given name (case-insensitive). It does not include file content.
	FindByName(ctx context.Context, userID, name string) (*snips.File, error)
	// CountByUser returns the number of files a user has.
	CountByUser(ctx context.Context, userID string) (int64, error)
}

type Migrator added in v0.13.0

type Migrator interface {
	// Migrate migrates the database.
	Migrate(ctx context.Context) error
}

type Page added in v0.13.0

type Page struct {
	Limit  uint64
	Offset uint64
	Cursor Cursor
}

Page contains resolved pagination settings for a database backend.

func ResolvePage added in v0.13.0

func ResolvePage(opts ...PageOption) Page

ResolvePage applies pagination options for use by a database backend.

type PageOption added in v0.13.0

type PageOption func(*Page)

PageOption tunes pagination on listing queries; omitting options returns all rows.

func WithCursor added in v0.13.0

func WithCursor(cursor Cursor) PageOption

WithCursor resumes a listing from an opaque API cursor.

func WithLimit added in v0.13.0

func WithLimit(limit uint64) PageOption

WithLimit caps the number of rows returned.

type PublicKeys added in v0.13.0

type PublicKeys interface {
	// FindByFingerprint returns a public key by its fingerprint.
	FindByFingerprint(ctx context.Context, fingerprint string) (*snips.PublicKey, error)
}

type Revisions added in v0.13.0

type Revisions interface {
	// Create creates a new file revision. If maxRevisions > 0, prunes oldest revisions exceeding the limit.
	Create(ctx context.Context, revision *snips.Revision, diff []byte, maxRevisions uint64) error
	// FindDiff returns a revision's decompressed diff by ID.
	FindDiff(ctx context.Context, id string) ([]byte, error)
	// FindByFileID returns a file's revisions, newest first. It does not include diff content.
	FindByFileID(ctx context.Context, fileID string, opts ...PageOption) ([]*snips.Revision, error)
	// FindByFileIDAndSequence returns a revision by file ID and sequence number. It does not include diff content.
	FindByFileIDAndSequence(ctx context.Context, fileID string, sequence int64) (*snips.Revision, error)
	// CountByFileID returns the number of revisions for a file.
	CountByFileID(ctx context.Context, fileID string) (int64, error)
}

type Users added in v0.13.0

type Users interface {
	// CreateWithPublicKey creates a new user with a public key.
	CreateWithPublicKey(ctx context.Context, publickey *snips.PublicKey) (*snips.User, error)
	// Find returns a user by its ID.
	Find(ctx context.Context, id string) (*snips.User, error)
	// Update updates a user's mutable fields (currently theme color and updated_at).
	Update(ctx context.Context, user *snips.User) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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