...

Package orm

import "rockfic.com/orm"
Overview
Index
Subdirectories

Overview ▾

Index ▾

Constants
Variables
func Connect(uri string, dbName string)
func Create(d any) any
func CreateSlice[T any](d T) []*T
type Counter
type Document
    func (d *Document) Append(field string, a ...interface{}) error
    func (d *Document) Delete() error
    func (d *Document) Pull(field string, a ...any) error
    func (d *Document) Remove() error
    func (d *Document) Save() error
    func (d *Document) Swap(field string, i, j int) error
type DocumentSlice
    func (d *DocumentSlice) Delete() error
    func (d *DocumentSlice) Remove() error
    func (d *DocumentSlice) Save() error
type GridFSFile
type HasID
type HasIDSlice
type IDocument
type IDocumentSlice
type IModel
type InternalIndex
type Model
    func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
    func (m *Model) FindByID(id interface{}) (*Query, error)
    func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
    func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error)
    func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
type Query
    func (q *Query) Exec(result interface{})
    func (q *Query) JSON() (string, error)
    func (q *Query) LoadFile(fields ...string) *Query
    func (q *Query) Populate(fields ...string) *Query
type Reference
type TModelRegistry
    func (r TModelRegistry) Get(name string) *Model
    func (r TModelRegistry) Has(i interface{}) (string, *Model, bool)
    func (r TModelRegistry) HasByName(n string) (string, *Model, bool)
    func (r TModelRegistry) Index(n string) int
    func (r TModelRegistry) Model(mdl ...any)

Package files

document.go document_internals.go document_slice.go errors.go gridfs.go idcounter.go indexes.go model.go query.go registry.go testing.go util.go

Constants

const (
    OP_FIND_ONE   = "findOne"
    OP_FIND_PAGED = "findPaged"
    OP_FIND_ALL   = "findAll"
    OP_FIND       = "find"
)
const COUNTER_COL = "@counters"

Variables

var (
    ErrNotASlice               = errors.New("Current object or field is not a slice!")
    ErrNotAStruct              = errors.New("Current object or field is not a struct!")
    ErrOutOfBounds             = errors.New("Index(es) out of bounds!")
    ErrAppendMultipleDocuments = errors.New("Cannot append to multiple documents!")
    ErrNotSliceOrStruct        = errors.New("Current object or field is not a slice nor a struct!")
    ErrUnsupportedID           = errors.New("Unknown or unsupported id type provided")
)

DB - The mongodb database handle

var DB *mongo.Database

DBClient - The mongodb client

var DBClient *mongo.Client

ModelRegistry - the ModelRegistry stores a map containing pointers to Model instances, keyed by an associated model name

var ModelRegistry = make(TModelRegistry)

NextStringID - Override this function with your own string ID generator!

var NextStringID func() string

func Connect

func Connect(uri string, dbName string)

func Create

func Create(d any) any

Create creates a new instance of a given Document type and returns a pointer to it.

func CreateSlice

func CreateSlice[T any](d T) []*T

CreateSlice - convenience method which creates a new slice of type *T (where T is a type which embeds Document) and returns it

type Counter

type Counter struct {
    Current    any    `bson:"current"`
    Collection string `bson:"collection"`
}

type Document

type Document struct {
    // Created time. updated/added automatically.
    Created time.Time `bson:"createdAt" json:"createdAt"`
    // Modified time. updated/added automatically.
    Modified time.Time `bson:"updatedAt" json:"updatedAt"`
    // contains filtered or unexported fields
}

func (*Document) Append

func (d *Document) Append(field string, a ...interface{}) error

Append appends one or more items to `field`. will error if this Model contains a reference to multiple documents, or if `field` is not a slice.

func (*Document) Delete

func (d *Document) Delete() error

Delete - deletes a model instance from the database

func (*Document) Pull

func (d *Document) Pull(field string, a ...any) error

Pull - removes elements from the subdocument slice stored in `field`.

func (*Document) Remove

func (d *Document) Remove() error

Remove - alias for Delete

func (*Document) Save

func (d *Document) Save() error

Save - updates this Model in the database, or inserts it if it doesn't exist

func (*Document) Swap

func (d *Document) Swap(field string, i, j int) error

Swap - swaps the elements at indexes `i` and `j` in the slice stored at `field`

type DocumentSlice

type DocumentSlice []IDocument

func (*DocumentSlice) Delete

func (d *DocumentSlice) Delete() error

func (*DocumentSlice) Remove

func (d *DocumentSlice) Remove() error

func (*DocumentSlice) Save

func (d *DocumentSlice) Save() error

type GridFSFile

type GridFSFile struct {
    ID     primitive.ObjectID `bson:"_id"`
    Name   string             `bson:"filename"`
    Length int                `bson:"length"`
}

type HasID

HasID is a simple interface that you must implement in your models, using a pointer receiver. This allows for more flexibility in cases where your ID isn't an ObjectID (e.g., int, uint, string...).

and yes, those darn ugly ObjectIDs are supported :)

type HasID interface {
    Id() any
    SetId(id any)
}

type HasIDSlice

type HasIDSlice []HasID

type IDocument

type IDocument interface {
    Append(field string, a ...interface{}) error
    Pull(field string, a ...any) error
    Swap(field string, i, j int) error
    Delete() error
    Remove() error
    Save() error
    // contains filtered or unexported methods
}

type IDocumentSlice

type IDocumentSlice interface {
    Delete() error
    Remove() error
    Save() error
    // contains filtered or unexported methods
}

type IModel

type IModel interface {
    FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
    Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
    FindByID(id interface{}) (*Query, error)
    FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
    FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error)
    // contains filtered or unexported methods
}

type InternalIndex

prolly won't need to use indexes, but just in case...

type InternalIndex struct {
    Fields  []string
    Options []string
    // contains filtered or unexported fields
}

type Model

Model - type which contains "static" methods like Find, FindOne, etc.

type Model struct {
    Indexes map[string][]InternalIndex
    Type    reflect.Type
    // contains filtered or unexported fields
}

func (*Model) Find

func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error)

Find - find all documents satisfying `query`. returns a pointer to a Query for further chaining.

func (*Model) FindByID

func (m *Model) FindByID(id interface{}) (*Query, error)

FindByID - find a single document by its _id field. Wrapper around FindOne with an ID query as its first argument

func (*Model) FindOne

func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)

FindOne - find a single document satisfying `query`. returns a pointer to a Query for further chaining.

func (*Model) FindPaged

func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error)

FindPaged - Wrapper around FindAll with the Skip and Limit options populated. returns a pointer to a Query for further chaining.

func (*Model) FindRaw

func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

FindRaw - find documents satisfying `query` and return a plain mongo cursor.

type Query

type Query struct {
    // contains filtered or unexported fields
}

func (*Query) Exec

func (q *Query) Exec(result interface{})

Exec - executes the query and puts its results into the provided argument.

Will panic if called more than once on the same Query instance.

func (*Query) JSON

func (q *Query) JSON() (string, error)

JSON - marshals this Query's results into json format

func (*Query) LoadFile

func (q *Query) LoadFile(fields ...string) *Query

LoadFile - loads the contents of one or more files stored in gridFS into the fields named by `fields`.

gridFS fields can be either a `string` or `[]byte`, and are tagged with `gridfs:"BUCKET,FILE_FORMAT` where:

func (*Query) Populate

func (q *Query) Populate(fields ...string) *Query

Populate populates document references via reflection

type Reference

Reference stores a typed document reference

type Reference struct {
    // owning model name
    Model string
    // the name of the struct field
    FieldName string
    // index of field in owning struct
    Idx int
    // the type of the referenced object
    HydratedType reflect.Type

    // field kind (struct, slice, ...)
    Kind reflect.Kind
    // contains filtered or unexported fields
}

type TModelRegistry

type TModelRegistry map[string]*Model

func (TModelRegistry) Get

func (r TModelRegistry) Get(name string) *Model

func (TModelRegistry) Has

func (r TModelRegistry) Has(i interface{}) (string, *Model, bool)

Has returns the model typename and Model instance corresponding to the argument passed, as well as a boolean indicating whether it was found. otherwise returns `"", nil, false`

func (TModelRegistry) HasByName

func (r TModelRegistry) HasByName(n string) (string, *Model, bool)

HasByName functions almost identically to Has, except that it takes a string as its argument.

func (TModelRegistry) Index

func (r TModelRegistry) Index(n string) int

Index returns the index at which the Document struct is embedded

func (TModelRegistry) Model

func (r TModelRegistry) Model(mdl ...any)

Model registers models in the ModelRegistry, where they can be accessed via a model's struct name

Subdirectories

Name Synopsis
..
muck You can edit this code! Click here and start typing.