mongodb

package
v0.0.0-...-2545618 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// SettingsDB database with settings
	SettingsDB = "datel"

	// SettingsUsersCol collection with user settings
	SettingsUsersCol = "users"
)
View Source
const (
	// LabelTypeName Name of the key for the "label", i.e. gets priority for display
	LabelTypeName = "_label"
)

Variables

View Source
var (
	//ErrCollectionNotFound collection not found error
	ErrCollectionNotFound = errors.New("collection not found")

	//ErrFieldNotFound field not found error
	ErrFieldNotFound = errors.New("field not found")
)
View Source
var (

	// CtxTimeout default timeout for database connection
	CtxTimeout = 10 * time.Second
)
View Source
var (
	// ErrItemNotFound item not found error
	ErrItemNotFound = errors.New("item not found")
)
View Source
var (
	// ErrUserNotFound user not found error
	ErrUserNotFound = errors.New("user not found")
)

Functions

func CurrentURI

func CurrentURI() string

CurrentURI get URI of currently used Instance

func IDFilter

func IDFilter(id string) bson.M

IDFilter creates a bson filter for filtering based on _id

Types

type Collection

type Collection struct {
	Name     string  `json:"name" bson:"name"`
	Label    string  `json:"label" bson:"label"`
	Database string  `json:"database" bson:"database"`
	Fields   []Field `json:"fields" bson:"fields"`
}

Collection collection in database and its fields

func (*Collection) FieldIndex

func (c *Collection) FieldIndex(key string) int

FieldIndex find field index by key

type Field

type Field struct {
	Key   string    `json:"key" bson:"key"`
	Label string    `json:"label" bson:"label"`
	Type  FieldType `json:"type" bson:"type"`
}

Field fields of collection

type FieldType

type FieldType struct {
	Name       string                 `json:"name" bson:"name"`
	Properties map[string]interface{} `json:"properties,omitempty" bson:"properties,omitempty"`
}

FieldType type of field

type Instance

type Instance struct {
	Cli *mongo.Client
	URI string
}

Instance database connection instance

func GetInstance

func GetInstance(URI string) (*Instance, error)

GetInstance gets the current instance so that it doesn't have to reconnect.

func (*Instance) AddItem

func (m *Instance) AddItem(item Item) error

AddItem adds an entry to the database, if ID is "", database will created it

func (*Instance) DeleteItem

func (m *Instance) DeleteItem(item Item) error

DeleteItem deletes item which gets filtered by the item.ID

func (*Instance) DeleteItemByID

func (m *Instance) DeleteItemByID(col Collection, id string) error

DeleteItemByID deletes item by ID

func (*Instance) DeleteOne

func (m *Instance) DeleteOne(database, collection string, filter interface{}) (*mongo.DeleteResult, error)

DeleteOne deletes one document

func (*Instance) DropCollection

func (m *Instance) DropCollection(database, collection string) error

DropCollection drops / deletes specified collection

func (*Instance) GetItems

func (m *Instance) GetItems(col Collection, filter interface{}) ([]Item, error)

GetItems gets items from the database

func (*Instance) GetUser

func (m *Instance) GetUser(name string) (User, error)

GetUser gets user settings from the database

func (*Instance) HasCollection

func (m *Instance) HasCollection(database, collection string) (bool, error)

HasCollection finds out if the database contains the given collection

func (*Instance) InsertOne

func (m *Instance) InsertOne(database, collection string, v interface{}) error

InsertOne inserts one document to the database

func (*Instance) ReplaceItem

func (m *Instance) ReplaceItem(item Item) error

ReplaceItem replaces the item which is filtered by ID

func (*Instance) ReplaceOne

func (m *Instance) ReplaceOne(database, collection string, filter, v interface{}) (*mongo.UpdateResult, error)

ReplaceOne replaces one document

func (*Instance) UnmarshallCollection

func (m *Instance) UnmarshallCollection(database, collection string, filter interface{}, v interface{}) error

UnmarshallCollection retrieves data from the collection using a filter. Similar to JSON Unmarshall

func (*Instance) UpdateItem

func (m *Instance) UpdateItem(item Item) error

UpdateItem updates item in the collection. Existing fields are kept.

func (*Instance) UpdateOne

func (m *Instance) UpdateOne(database, collection string, filter, update interface{}) (*mongo.UpdateResult, error)

UpdateOne updates one document

func (*Instance) UserAddCollection

func (m *Instance) UserAddCollection(user *User, col Collection) error

UserAddCollection adds collection to the user

func (*Instance) UserDeleteCollection

func (m *Instance) UserDeleteCollection(user *User, col Collection) error

UserDeleteCollection deletes collection from user settings

func (*Instance) UserUpdate

func (m *Instance) UserUpdate(userID string, update interface{}) (*mongo.UpdateResult, error)

UserUpdate update user settings

func (*Instance) UserUpdateCollection

func (m *Instance) UserUpdateCollection(user *User, col Collection) error

UserUpdateCollection updates collections of the user

type Item

type Item struct {
	ID         string      `json:"_id" bson:"_id"`
	Collection *Collection `json:"-" bson:"-"`
	Label      string      `json:"-" bson:"-"`
	Data       []ItemData  `json:"data" bson:"data"`
}

Item item (mongo document) in collection

func EmptyItem

func EmptyItem(col Collection) Item

EmptyItem creates an empty item based on the collection fields

func ItemFromMultipart

func ItemFromMultipart(col Collection, multipart *multipart.Form) Item

ItemFromMultipart converts a multipart request to an item

type ItemData

type ItemData struct {
	Field Field       `json:"field" bson:"field"`
	Value interface{} `json:"value" bson:"value"`
}

ItemData items of the document

type User

type User struct {
	ID              string       `json:"_id" bson:"_id"`
	Username        string       `json:"username" bson:"username"`
	DefaultDatabase string       `json:"default_database" bson:"default_database"`
	Collections     []Collection `json:"collections" bson:"collections"`
}

User user settings

func (*User) CollectionByName

func (u *User) CollectionByName(name string) (Collection, error)

CollectionByName find collection by name

func (*User) CollectionIndex

func (u *User) CollectionIndex(name string) int

CollectionIndex find collection index by name

Jump to

Keyboard shortcuts

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