db

package
v1.0.31 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	// RegisterEntries tells the database to register the given entries
	// In the case of the mongodb database this means we'll create a collection for each entry
	RegisterEntries(entries ...Entry)

	// FindOne finds one entry inside the database
	// Returns err == mongo.ErrNoDocuments if no documents where found
	FindOne(result Entry, filters bson.M, opts ...FindOptions) error

	// Find finds multiple entries in the database
	// The entry argument is to determain on which collection we execute the query
	Find(entry Entry, results any, filters bson.M, opts ...FindOptions) error

	// Insert inserts an entry into the database
	Insert(data ...Entry) error

	// UpdateID updates an entry in the database
	UpdateByID(data Entry) error

	// DeleteByID deletes an entry from the database
	DeleteByID(entry Entry, ids ...primitive.ObjectID) error

	// Count counts the number of documents in the database for the specific filter
	// If filter is nil the number of all the documents is returned
	Count(entry Entry, filter bson.M) (uint64, error)
}

Connection is a abstract interface for a database connection There are 2 main implementations of this: - MongoConnection (For the MongoDB driver) - TestConnection (For a fake temp database)

type Entry

type Entry interface {
	// Get the _id field of the entry
	GetID() primitive.ObjectID

	// Set the _id field of the entry
	SetID(primitive.ObjectID)

	// CollectionName should yield the collection name for the entry
	CollectionName() string

	// DefaultFindFilters can return a default filter used in find queries
	// If nil is returned this is not used
	DefaultFindFilters() bson.M

	// Indexes returns the indexes for the entry
	// If nil is returned no more indexes will be set
	// Note that by default the there is always an index of the _id field
	Indexes() []mongo.IndexModel
}

Entry are the functions required to put/get things in/from the database To implement use:

type User struct {
    // Adds the _id field and implements the remaining functions from Entry
    M `bson:",inline"`

    Username string
}

func (*User) CollectionName() {
    return "users"
}

type FindOptions

type FindOptions struct {
	// NoDefaultFilters does not include the default filters for the entry provided
	NoDefaultFilters bool
}

FindOptions are options for the find operator in Connection

type M

type M struct {
	ID primitive.ObjectID `` /* 171-byte string literal not displayed */
}

M is a struct that adds an _id field and implements from dbInterfaces.Entry: - GetID - SetID - DefaultFindFilters

func NewM

func NewM() M

NewM returns a new instance of M

func (*M) DefaultFindFilters

func (*M) DefaultFindFilters() bson.M

DefaultFindFilters implements Entry

func (*M) GetID

func (m *M) GetID() primitive.ObjectID

GetID implements Entry

func (*M) Indexes

func (*M) Indexes() []mongo.IndexModel

Indexes implements Entry

func (*M) SetID

func (m *M) SetID(id primitive.ObjectID)

SetID implements Entry

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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