odm

package
v1.0.37 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: Apache-2.0 Imports: 15 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureIndexes added in v1.0.17

func EnsureIndexes[T DbModel](
	ctx context.Context,
	client MongoClient,
	tenant string,
) error

EnsureIndexes creates every index the model advertises.

func HashedKey added in v1.0.17

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

func NewModelFrom added in v1.0.4

func NewModelFrom[T any](proto interface{}) *T

Types

type CollectionInterface added in v0.1.41

type CollectionInterface interface {
	UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
	FindOne(ctx context.Context, filter interface{}, opts ...options.Lister[options.FindOneOptions]) *mongo.SingleResult
	Find(ctx context.Context, filter interface{}, opts ...options.Lister[options.FindOptions]) (cur *mongo.Cursor, err error)
	DeleteOne(ctx context.Context, filter interface{}, opts ...options.Lister[options.DeleteOneOptions]) (*mongo.DeleteResult, error)
	Aggregate(ctx context.Context, pipeline interface{}, opts ...options.Lister[options.AggregateOptions]) (*mongo.Cursor, error)
	CountDocuments(ctx context.Context, filter interface{}, opts ...options.Lister[options.CountOptions]) (int64, error)
	Distinct(ctx context.Context, field string, filter any, opts ...options.Lister[options.DistinctOptions]) *mongo.DistinctResult
}

type DbModel

type DbModel interface {
	// odm enforces string ID instead of ObjectID.
	// Principally, odm encourages to use data properties to find unique ID for a document.
	// If multiple fields together can uniquely identify a document, then hash those fields into a single string.
	Id() string
	CollectionName() string
}

type DefaultTimer added in v0.1.41

type DefaultTimer struct{}

func (DefaultTimer) Now added in v0.1.41

func (d DefaultTimer) Now() int64

type Indexed added in v1.0.17

type Indexed interface {
	IndexModels() []mongo.IndexModel
}

Classic B-tree / hashed / compound indexes.

type MongoClient added in v0.1.47

type MongoClient interface {
	Ping(context.Context, *readpref.ReadPref) error
	Database(name string, opts ...options.Lister[options.DatabaseOptions]) *mongo.Database
	Disconnect(ctx context.Context) error
}

func ProvideMongoClient added in v1.0.27

func ProvideMongoClient() MongoClient

type OdmCollectionInterface added in v1.0.0

type OdmCollectionInterface[T DbModel] interface {
	Save(ctx context.Context, model T) <-chan async.Result[struct{}]
	FindOneByID(ctx context.Context, id string) <-chan async.Result[*T]
	FindOne(ctx context.Context, filters bson.M) <-chan async.Result[*T]
	Find(ctx context.Context, filters bson.M, sort bson.D, limit, skip int64) <-chan async.Result[[]T]
	DeleteByID(ctx context.Context, id string) <-chan async.Result[struct{}]
	DeleteOne(ctx context.Context, filters bson.M) <-chan async.Result[struct{}]
	Count(ctx context.Context, filters bson.M) <-chan async.Result[int64]
	DistinctInto(ctx context.Context, field string, filters bson.D, out any) error
	Aggregate(ctx context.Context, pipeline mongo.Pipeline) <-chan async.Result[[]T]
	Exists(ctx context.Context, id string) <-chan async.Result[bool]
	VectorSearch(ctx context.Context, embedding []float32, opts VectorSearchParams) <-chan async.Result[[]SearchHit[T]]
	TermSearch(ctx context.Context, query string, params TermSearchParams) <-chan async.Result[[]SearchHit[T]]
}

func CollectionOf added in v1.0.0

func CollectionOf[T DbModel](client MongoClient, tenant string) OdmCollectionInterface[T]

type SearchHit added in v1.0.17

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

type SearchIndexed added in v1.0.17

type SearchIndexed interface {
	TermSearchIndexSpecs() []TermSearchIndexSpec
}

type TermSearchIndexSpec added in v1.0.17

type TermSearchIndexSpec struct {
	Name  string   // index name
	Paths []string // e.g. fields in the struct that holds the text to be indexed.
}

func (TermSearchIndexSpec) Model added in v1.0.17

type TermSearchParams added in v1.0.17

type TermSearchParams struct {
	IndexName string   // required
	Path      []string // field to search
	Filter    bson.M   // optional filter
	Limit     int      // number of results to return
}

type Timer added in v0.1.41

type Timer interface {
	Now() int64
}

type VectorIndexSpec added in v1.0.17

type VectorIndexSpec struct {
	Name          string `bson:"-"`    //index name
	Type          string `bson:"type"` // field type, e.g. "vector"
	Path          string `bson:"path"` // e.g. field name in the struct/json that holds the embedding.
	NumDimensions int    `bson:"numDimensions"`
	Similarity    string `bson:"similarity,omitempty"` // e.g. "cosine", "dotProduct", "euclidean"
	Quantization  string `bson:"quantization,omitempty"`
}

func (VectorIndexSpec) Model added in v1.0.17

type VectorIndexed added in v1.0.17

type VectorIndexed interface {
	VectorIndexSpecs() []VectorIndexSpec
}

type VectorSearchParams added in v1.0.17

type VectorSearchParams struct {
	IndexName     string // Atlas Vector Search index name
	Path          string // field in the collection that holds the embedding (e.g. "embedding")
	K             int    // number of nearest neighbours
	NumCandidates int    // count of initial candidates to be considered for nearest search. These are approximate neighbours..
	Filter        bson.M // optional pre-filter; nil for none
}

Jump to

Keyboard shortcuts

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