clerk

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 1 Imported by: 0

README

clerk

📒 A minimalistic library for abstracting database operations

Installation

Adding clerk to your Go module is as easy as calling this command in your project

go get github.com/Becklyn/clerk

Supported databases

clerk has builtin support for the following database/search engines:

  • MongoDB - MongoDB is a document-oriented database
  • Meilisearch - Meilisearch is a powerful and fast search engine

Usage

Being a minimalistic library, clerk only provides the basics. The rest is up to your specific need.

Creating a connection
connection, err := mongodb.NewMongoConnection("mongodb://root:root@localhost:27017")
if err != nil {
	panic(err)
}

defer connection.Close(func(err error) {
	if err != nil {
		panic(err)
	}
})
Using a mongodb transaction
connection, err := mongodb.NewMongoConnection("mongodb://root:root@localhost:27017")
if err != nil {
	panic(err)
}

defer connection.Close(func(err error) {
	if err != nil {
		panic(err)
	}
})

err = connection.WithTransaction(func (ctx context.Context) error {
	// use the ctx when doing operations on the database and it will be part of the transaction automatically

	return nil
})
if err != nil {
	panic(err)
}
Defining a database operator instance
operator := mongodb.NewMongodbOperator[T](connection)

The generic parameter T defines the data type which the operator can interact with. An operator has to be defined for each data type in use with clerk.

Defining a database & collection
collection := clerk.NewDatabase("foo").Collection("bar")

Certain operators only work with collections and don't need a database:

collection := clerk.NewCollection("foo")
Persisting a data in a collection
type Message struct {
    Id   string `bson:"_id"`
    Body string
}

createCtx, createCancel := context.WithTimeout(
    context.Background(),
    time.Second * 5,
)
defer createCancel()

create := clerk.NewCreate(collection, Message{
    Id:   "0",
    Body: "Hello World",
})
if err := create.Execute(createCtx, operator); err != nil {
    panic(err)
}
Querying the collection
type Message struct {
    Id   string `bson:"_id"`
    Body string
}

results := []Message{}

queryCtx, queryCancel := context.WithTimeout(
    context.Background(),
    time.Second * 5,
)
defer queryCancel()

query := clerk.NewQuery[Message](collection).Where("_id", "0")
queryChan, err := query.Execute(queryCtx, operator)
if err != nil {
    panic(err)
}

for result := range queryChan {
    results := append(results, result)
}

fmt.Println(results...)
Sorting the collection
type Message struct {
    Id   string `bson:"_id"`
    Body string
}

results := []Message{}

queryCtx, queryCancel := context.WithTimeout(
    context.Background(),
    time.Second * 5,
)
defer queryCancel()

query := clerk.NewQuery[Message](collection).SortBy("_id", true)
queryChan, err := query.Execute(queryCtx, operator)
if err != nil {
    panic(err)
}

for result := range queryChan {
    results := append(results, result)
}

fmt.Println(results...)

Copyright © 2022 - The cozy team & contributors

Documentation

Index

Constants

This section is empty.

Variables

Functions

func NewCreate

func NewCreate[T any](collection *Collection, data T) *create[T]

func NewDelete

func NewDelete[T any](collection *Collection) *delete[T]

func NewQuery

func NewQuery[T any](collection *Collection) *query[T]

func NewSearch

func NewSearch[T any](collection *Collection, query string) *search[T]

func NewUpdate

func NewUpdate[T any](collection *Collection, data T) *update[T]

func NewWatch

func NewWatch[T any](collection *Collection, operations ...Operation) *watch[T]

Types

type Collection

type Collection struct {
	Database string
	Name     string
}

func NewCollection

func NewCollection(name string) *Collection

func NewCollectionWithDatabase

func NewCollectionWithDatabase(
	database string,
	name string,
) *Collection

type Creator

type Creator[T any] interface {
	Create(
		ctx context.Context,
		collection *Collection,
		data T,
	) error
}

type Database

type Database struct {
	Name string
}

func NewDatabase

func NewDatabase(name string) *Database

func (*Database) Collection

func (d *Database) Collection(name string) *Collection

type Deleter

type Deleter[T any] interface {
	Delete(
		ctx context.Context,
		collection *Collection,
		filter map[string]any,
	) error
}

type Operation

type Operation int
const (
	Create Operation = iota
	Delete
	Update
)

func (Operation) String

func (o Operation) String() string

type Querier

type Querier[T any] interface {
	Query(
		ctx context.Context,
		collection *Collection,
		filter map[string]any,
		sorting map[string]bool,
		skip int,
		take int,
	) (<-chan T, error)
}

type Searcher

type Searcher[T any] interface {
	Search(
		ctx context.Context,
		collection *Collection,
		query string,
		highlight []string,
		filterable []string,
		filterQuery string,
		skip int,
		take int,
	) (<-chan T, error)
}

type Updater

type Updater[T any] interface {
	Update(
		ctx context.Context,
		collection *Collection,
		filter map[string]any,
		data T,
		upsert bool,
	) error
}

type Watcher

type Watcher[T any] interface {
	Watch(
		ctx context.Context,
		collection *Collection,
		operation Operation,
	) (<-chan T, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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