gopherdb

package module
v0.0.1-beta.4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 17 Imported by: 0

README

GopherDb

image

GopherDb is a lightweight embedded database engine written in Go, designed to replicate the basic features of MongoDB for local applications. It allows you to store documents in BSON format, supports indexes for efficient searches, and uses a query language similar to MongoDB’s. Additionally, it automatically generates the _id field for each document, ensuring compatibility with MongoDB’s data format.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingFieldForIndex is returned when a field is missing for an index.
	ErrMissingFieldForIndex = errors.New("missing field for index")
	// ErrEmptyIndexFields is returned when an index has no fields.
	ErrEmptyIndexFields = errors.New("empty index fields")
	// ErrDuplicateIndexField is returned when a duplicate index field is found.
	ErrDuplicateIndexField = errors.New("duplicate index field")
	// ErrUniqueIndexViolation is returned when a unique index is violated.
	ErrUniqueIndexViolation = errors.New("unique index violation")
	// ErrIndexAlreadyExists is returned when an index already exists.
	ErrIndexAlreadyExists = errors.New("index already exists")
	// ErrInvalidValueType is returned when an invalid value type is used.
	ErrInvalidValueType = errors.New("invalid value type")
	// ErrMapTypeConversionFailed is returned when a map type conversion fails.
	ErrMapTypeConversionFailed = errors.New("map type conversion failed")
	// ErrUnsupportedTypeForMapConversion is returned when an unsupported type is used for map conversion.
	ErrUnsupportedTypeForMapConversion = errors.New("unsupported type for map conversion")
	// ErrMustBePointer is returned when a value is not a pointer.
	ErrMustBePointer = errors.New("must be a pointer")
	// ErrDocumentNotFound is returned when a document is not found.
	ErrDocumentNotFound = errors.New("document not found")
	// ErrDocumentIsNil is returned when a document is nil.
	ErrDocumentIsNil = errors.New("document is nil")
	// ErrDocumentSliceIsNil is returned when a document slice is nil.
	ErrDocumentSliceIsNil = errors.New("document slice is nil")
	// ErrDocumentSlicePointerIsNil is returned when a document slice pointer is nil.
	ErrDocumentSlicePointerIsNil = errors.New("document slice pointer is nil")
	// ErrDocumentSliceEmpty is returned when a document slice is empty.
	ErrDocumentSliceEmpty = errors.New("document slice is empty")
	// ErrDocumentSliceTypeInvalid is returned when a document slice type is invalid.
	ErrDocumentSliceTypeInvalid = errors.New("document slice type is invalid")
	// ErrDocumentSliceElementTypeInvalid is returned when a document slice element type is invalid.
	ErrDocumentSliceElementTypeInvalid = errors.New("document slice element type is invalid")
	// ErrDocumentPointerIsNil is returned when a document pointer is nil.
	ErrDocumentPointerIsNil = errors.New("document pointer is nil")
	// ErrDocumentTypeInvalid is returned when a document type is invalid.
	ErrDocumentTypeInvalid = errors.New("document type is invalid")
	// ErrDocumentIDNotFound is returned when a document ID is not found.
	ErrDocumentIDNotFound = errors.New("document ID not found")
	// ErrDocumentIDNoEditable is returned when a document ID is not editable.
	ErrDocumentIDNoEditable = errors.New("document ID no editable")
)

Functions

This section is empty.

Types

type Collection

type Collection struct {
	IndexManager *IndexManager
	// contains filtered or unexported fields
}

Collection is a collection of documents.

func (*Collection) CreateIndex

func (c *Collection) CreateIndex(ctx context.Context, index IndexModel) error

CreateIndex crea un nuevo índice en la colección.

func (*Collection) CreateManyIndexes

func (c *Collection) CreateManyIndexes(ctx context.Context, indexes []IndexModel) error

CreateManyIndexes crea múltiples índices en la colección.

func (*Collection) Delete

func (c *Collection) Delete(filter map[string]any) DeleteManyResult

Delete deletes multiple documents by a filter.

func (*Collection) DeleteByID

func (c *Collection) DeleteByID(id any) DeleteOneResult

DeleteByID deletes a single document by its ID.

func (*Collection) DeleteOne

func (c *Collection) DeleteOne(filter map[string]any) DeleteOneResult

DeleteOne deletes a single document by a filter.

func (*Collection) Find

func (c *Collection) Find(
	filter map[string]any,
	opts ...*options.FindOptions,
) FindResult

Find finds documents by a filter.

func (*Collection) FindByID

func (c *Collection) FindByID(id any) FindOneResult

FindByID finds a document by its ID.

func (*Collection) FindOne

func (c *Collection) FindOne(
	filter map[string]any,
) FindOneResult

FindOne finds a single document by a filter.

func (*Collection) Insert

func (c *Collection) Insert(docs any) InsertManyResult

Insert inserts multiple documents into the collection.

func (*Collection) InsertOne

func (c *Collection) InsertOne(doc any) InsertOneResult

InsertOne inserts a single document into the collection.

func (*Collection) Update

func (c *Collection) Update(
	filter map[string]any,
	docs any,
	opts ...*options.UpdateOptions,
) UpdateManyResult

Update updates multiple documents by a filter.

func (*Collection) UpdateOne

func (c *Collection) UpdateOne(
	filter map[string]any,
	doc any,
	opts ...*options.UpdateOptions,
) UpdateOneResult

UpdateOne updates a single document by a filter.

type CollectionMetadata

type CollectionMetadata struct {
	Name          string       `json:"name"`
	Indexes       []IndexModel `json:"indexes"`
	DocumentCount int64        `json:"document_count"`
}

type Database

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

Database es una base de datos.

func NewDatabase

func NewDatabase(name, path string) (*Database, error)

NewDatabase crea una nueva instancia de Database.

func (*Database) Close

func (db *Database) Close() error

Close cierra la base de datos

func (*Database) Collection

func (db *Database) Collection(name string) (*Collection, error)

Collection devuelve una instancia de Collection para la base de datos

type DatabaseMetadata

type DatabaseMetadata struct {
	Name string `json:"name"`
}

type DeleteManyResult

type DeleteManyResult struct {
	DeletedIDs []any
	Err        error
}

DeleteManyResult es el resultado de una eliminación de múltiples documentos.

type DeleteOneResult

type DeleteOneResult struct {
	DeletedID any
	Err       error
}

DeleteOneResult es el resultado de una eliminación de un documento.

type FindOneResult

type FindOneResult struct {
	IndexUsed *IndexModel
	Err       error
	// contains filtered or unexported fields
}

FindOneResult es el resultado de una consulta de un documento.

func (*FindOneResult) Document

func (r *FindOneResult) Document() map[string]any

Document returns the document of the find one result.

func (*FindOneResult) Unmarshal

func (r *FindOneResult) Unmarshal(result any) error

Unmarshal unmarshals the result into a struct.

type FindResult

type FindResult struct {
	TotalCount int64
	IndexUsed  *IndexModel
	Err        error
	// contains filtered or unexported fields
}

FindResult es el resultado de una consulta.

func (*FindResult) Unmarshal

func (r *FindResult) Unmarshal(results any) error

Unmarshal unmarshals the results into a slice of the given type.

type IndexField

type IndexField struct {
	Name  string `json:"name"`
	Order int    `json:"order"`
}

IndexField represents a field in an index.

type IndexManager

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

IndexManager is a manager for indexes.

func (*IndexManager) CreateMany

func (m *IndexManager) CreateMany(ctx context.Context, indexes []IndexModel) error

CreateMany creates many indexes.

func (*IndexManager) List

func (m *IndexManager) List() []IndexModel

List returns all the indexes for the collection.

type IndexModel

type IndexModel struct {
	Fields  []IndexField `json:"fields"`
	Options IndexOptions `json:"options"`
}

IndexModel represents a model for an index.

func NewIndexModel

func NewIndexModel() *IndexModel

NewIndexModel creates a new index model.

func (*IndexModel) AddField

func (index *IndexModel) AddField(name string, order int) *IndexModel

AddField adds a field to the index model.

func (*IndexModel) SetName

func (index *IndexModel) SetName(name string) *IndexModel

SetOptions sets the options for the index model.

func (*IndexModel) SetUnique

func (index *IndexModel) SetUnique(unique bool) *IndexModel

SetUnique sets the unique flag of the index model.

func (*IndexModel) Value

func (index *IndexModel) Value() IndexModel

Value returns the index model.

type IndexOptions

type IndexOptions struct {
	Name          string `json:"name"`
	Unique        bool   `json:"unique"`
	Autogenerated bool   `json:"autogenerated"`
}

IndexOptions represents the options for an index.

func NewIndexOptions

func NewIndexOptions() *IndexOptions

NewIndexOptions creates a new index options.

func (*IndexOptions) SetName

func (o *IndexOptions) SetName(name string) *IndexOptions

SetName sets the name of the index options.

func (*IndexOptions) SetUnique

func (o *IndexOptions) SetUnique(unique bool) *IndexOptions

SetUnique sets the unique flag of the index options.

func (*IndexOptions) Value

func (o *IndexOptions) Value() IndexOptions

Value returns the index options.

type InsertManyResult

type InsertManyResult struct {
	InsertedIDs []any
	Err         error
}

InsertManyResult es el resultado de una inserción de múltiples documentos.

type InsertOneResult

type InsertOneResult struct {
	InsertedID any
	Err        error
}

InsertOneResult es el resultado de una inserción.

type QueryPlan

type QueryPlan struct {
	IndexUsed   *IndexModel
	IndexFilter map[string]any
	UsedForSort bool
	IsExact     bool
}

QueryPlan is the plan for a query.

type QueryPlanner

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

QueryPlanner is the planner for a query.

func NewQueryPlanner

func NewQueryPlanner(indexes []IndexModel) *QueryPlanner

NewQueryPlanner creates a new QueryPlanner.

func (*QueryPlanner) Plan

func (qp *QueryPlanner) Plan(
	filter map[string]any,
	sort []options.SortField,
) *QueryPlan

Plan plans a query.

type UpdateManyResult

type UpdateManyResult struct {
	UpsertedIDs []any
	Err         error
}

UpdateManyResult es el resultado de una actualización de múltiples documentos.

type UpdateOneResult

type UpdateOneResult struct {
	UpsertedID any
	Err        error
}

UpdateOneResult es el resultado de una actualización de un documento.

Directories

Path Synopsis
cmd
cli command
test command
internal

Jump to

Keyboard shortcuts

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