odm

package
v1.0.45 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 7 modules, 20 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 odm holds what go-api-boot's two data backends share: the Model contract, timestamps, index declarations, search hits and errors.

Each backend has its own API, native to its database:

  • odm/mongodb: collections queried with MongoDB filter documents (bson.M), with Atlas Search and Vector Search;
  • odm/postgres: typed tables queried with SQL conditions, with pgvector and ParadeDB search.

Every IO method in both returns a channel carrying an async.Result, so independent queries run concurrently:

movie, err := async.Await(movies.FindByID(ctx, id))

Search indexes are declared as spec values (see VectorIndexSpec) that the search methods take, so an index is referred to by a Go identifier, not a name string.

Errors from both backends read "odm/<backend>: <tenant>.<name>: <cause>" and wrap their cause, so errors.Is matches ErrNotFound, ErrDuplicateKey and the driver's own errors.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned (wrapped) by the backends' single-document
	// reads, such as FindByID, when nothing matches.
	ErrNotFound = errors.New("document not found")

	// ErrDuplicateKey is returned (wrapped) by Save when a unique index rejects
	// the document.
	ErrDuplicateKey = errors.New("duplicate key")
)

Functions

func HashedKey added in v1.0.17

func HashedKey(fields ...string) (string, error)

HashedKey derives a short, stable id from fields. Order matters.

func NewModelFrom added in v1.0.4

func NewModelFrom[T any](src any) *T

NewModelFrom copies matching fields from src (typically a protobuf message) into a new T.

Types

type IndexKey added in v1.0.45

type IndexKey struct {
	Field string
	Desc  bool
}

IndexKey is one field of an index.

func DescKey added in v1.0.45

func DescKey(field string) IndexKey

DescKey is a descending IndexKey.

func Key added in v1.0.45

func Key(field string) IndexKey

Key is an ascending IndexKey.

type IndexSpec added in v1.0.45

type IndexSpec struct {
	Name   string     // optional; derived from the keys when empty
	Keys   []IndexKey // at least one
	Unique bool
	TTL    time.Duration // MongoDB only: expire documents this long after the (date) key
}

IndexSpec is a regular index over one or more fields (MongoDB) or columns (Postgres).

type Indexed added in v1.0.17

type Indexed interface {
	Indexes() []IndexSpec
}

Indexed declares regular (b-tree) indexes. A model declares its indexes by implementing any of Indexed, TermSearchIndexed and VectorIndexed; each backend's ensure function (mongodb.EnsureCollection, postgres.EnsureTable) creates them. That is a deployment step, not something done at runtime.

Index features a backend doesn't support make its ensure function fail with an error wrapping errors.ErrUnsupported.

type Model added in v1.0.45

type Model interface {
	// Id returns the document's unique id. odm uses string ids derived from the
	// document's data; when several fields identify a document, combine them
	// with HashedKey.
	Id() string
	// CollectionName names the MongoDB collection or Postgres table.
	CollectionName() string
}

Model is a document type stored in a collection (MongoDB) or table (Postgres).

Implement both methods on the value receiver: collections are created from the zero value of T.

type SearchHit added in v1.0.17

type SearchHit[T Model] struct {
	Score float64 `bson:"score"`
	Doc   T       `bson:"doc"`
}

SearchHit is a search result with its relevance score (higher is better).

type Similarity added in v1.0.45

type Similarity string

Similarity is a vector similarity function.

const (
	Cosine     Similarity = "cosine"
	DotProduct Similarity = "dotProduct"
	Euclidean  Similarity = "euclidean"
)

type SystemTimer added in v1.0.45

type SystemTimer struct{}

SystemTimer is the wall-clock Timer.

func (SystemTimer) Now added in v1.0.45

func (SystemTimer) Now() int64

type TermSearchIndexSpec added in v1.0.17

type TermSearchIndexSpec struct {
	Name  string
	Paths []string
}

TermSearchIndexSpec is a full-text index over string fields (MongoDB) or text columns (Postgres).

type TermSearchIndexed added in v1.0.45

type TermSearchIndexed interface {
	TermSearchIndexes() []TermSearchIndexSpec
}

TermSearchIndexed declares full-text search indexes.

type Timer added in v0.1.41

type Timer interface {
	Now() int64
}

Timer supplies Timestamps, in Unix seconds.

type Timestamps added in v1.0.45

type Timestamps struct {
	CreatedOn int64 `bson:"createdOn" db:"created_on" json:"createdOn"`
	UpdatedOn int64 `bson:"updatedOn" db:"updated_on" json:"updatedOn"`
}

Timestamps are the creation and last-save times, in Unix seconds, that Save writes on both backends. Save always writes them, whether or not the model embeds Timestamps, and ignores the values the model carries: CreatedOn is set on the first save and kept, UpdatedOn is set on every save.

Embed Timestamps to read them back. MongoDB needs the inline tag:

type Profile struct {
    odm.Timestamps `bson:",inline"`
    ID   string    `bson:"_id" db:"id"`
    Name string    `bson:"name"`
}

type VectorIndexSpec added in v1.0.17

type VectorIndexSpec struct {
	Name         string
	Path         string // the field (MongoDB) or column (Postgres)
	Dimensions   int
	Similarity   Similarity // Cosine when empty
	Quantization string     // "", "scalar" or "binary" (MongoDB); "scalar" stores half precision on Postgres
	FilterPaths  []string   // MongoDB: fields VectorSearch may pre-filter on
}

VectorIndexSpec is a vector index over an embedding: a bson.Vector or []float32 field on MongoDB, a []float32 field (a vector column) on Postgres.

type VectorIndexed added in v1.0.17

type VectorIndexed interface {
	VectorIndexes() []VectorIndexSpec
}

VectorIndexed declares vector search indexes.

Directories

Path Synopsis
internal
core
Package core is what the odm backends share but don't export: reading a model's declarations and validating them and the queries made with them.
Package core is what the odm backends share but don't export: reading a model's declarations and validating them and the queries made with them.
Package mongodb is go-api-boot's MongoDB ODM: typed collections queried with MongoDB filter documents, plus Atlas Search and Vector Search.
Package mongodb is go-api-boot's MongoDB ODM: typed collections queried with MongoDB filter documents, plus Atlas Search and Vector Search.
Package postgres is go-api-boot's Postgres ORM: typed tables with SQL queries, plus vector search (pgvector) and BM25 term search (ParadeDB).
Package postgres is go-api-boot's Postgres ORM: typed tables with SQL queries, plus vector search (pgvector) and BM25 term search (ParadeDB).

Jump to

Keyboard shortcuts

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