A Go library for building mongo queries with factory functions

FET

Build query dynamically
A Go library for building mongo queries with factory functions.

CI



What is FET?

you can build queries with factory functions. It can make your repository functions more generic, and makes checking query variables easier with the built-in checker system.

Installation

go get github.com/ahmetcanozcan/fet

Getting Started

basic usage of the fet is building a query factory functions, and then this query can be used in mongo driver functions.

filter := fet.Build(
  fet.Field("username").Eq("test"),
)

err := collection.FindOne(ctx, filter)

instead of using Field, you can use with functions directly.

filter := fet.Build(
  fet.WithValueEq("userName", "test"),
)

// is equivalent of:
// filter := bson.M{
//   "userName": "test",
// }

err := collection.FindOne(ctx, filter)

Features

Generic Repository Functions

func (repo *repository) GetByFilters(ctx context.Context, filters ...[]fet.Updater) (*User, error) {
  // ...
  filter := fet.Build(filters...)
  err := collection.FindOne(ctx, filter).Decode(&user)
  // ...
  return user, err
}

// This function can be called
user, err := repo.GetByFilters(
  ctx,
  fet.WithValueEq("username", "test"),
  fet.WithValueEq("password", "test"),
)

user, err := repo.GetByFilters(
  ctx,
  fet.Field("username").Eq("test"),
  fet.Field("password").Eq("test"),
  fet.Or(
    fet.Field("pretty").Eq(true),
    fet.Field("admin").Eq(true),
  )
)

Checker

checker is basic function that returns true depends on field name and query variable. FET has some built-in checker functions such as IfNotNil, IfNotEmpty, IfNotZeroTime.

filter := fet.Build(
  fet.Field("username").Eq(username),
  fet.Field("cardId").Eq(cardId, fet.IfNotEmpty),
)

// filter is change depends on cardId

// if cardId is empty, filter is equivalent of:
filter := bson.M{
  "username": username,
}

// if cardId is not empty, filter is equivalent of:
filter := bson.M{
  "username": username,
  "cardId": cardId,
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Owner
Similar Resources

A library to generate go models from given json files

A library to generate go models from given json files

generate A library to generate go models from given json files Requirements Go 1

May 18, 2022

Go Micro: a standard library for distributed systems development

Go Micro Go Micro is a standard library for distributed systems development. Ove

Oct 4, 2022

GoMicroservices - RESTful microservices written in Go standard library

RESTful microservices written in Go standard library. Folder product corresponds

Sep 10, 2022

Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)

Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)

Mongo Go Models Important Note: We changed package name from github.com/Kamva/mgm/v3(uppercase Kamva) to github.com/kamva/mgm/v3(lowercase kamva) in v

Jan 2, 2023

Opentracing factory to setup by multiples vendors

telemetry Opentracing factory to setup by multiples vendors. Usage telemetry.InitTracer( telemetry.WithJaeger(os.Getenv("APP_NAME")), telemetr

Jul 23, 2021

go-github client factory.

go-github client factory.

go-github-client go-github client factory.

Nov 25, 2022

Type-safe, DRY, flexible test fixture factory based on Go 1.18+ Generics

Fixtory is a test fixture factory which initializes type-safe, DRY, flexible fixtures with the power of Generics. By using Fixtory... No more redundan

Jan 4, 2023

Qmgo - The Go driver for MongoDB. It‘s based on official mongo-go-driver but easier to use like Mgo.

Qmgo English | 简体中文 Qmgo is a Go driver for MongoDB . It is based on MongoDB official driver, but easier to use like mgo (such as the chain call). Qmg

Dec 28, 2022

Instagram Backend HTTP REST API using GO Lang and Mongo DB

Instagram Backend HTTP REST API using GO Lang and Mongo DB Project for Appointy Summer Internship . Project built within 25 hrs, with no prior knowled

Oct 10, 2021

REST API of Instagram's functionalities; developed using GO (and Mongo).

REST API of Instagram's functionalities; developed using GO (and Mongo).

Instagram-backend-api REST API of Instagram's functionalities; developed using GO (and Mongo). Constraints: Complete API has been developed using Go M

Oct 9, 2021

A rudimentary go program that allows you to mount a mongo database as a FUSE file system

This is a rudimentary go program that allows you to mount a mongo database as a

Dec 29, 2021

Mongo-backed Static Site Generator for fun

MDBSSG MongoBacked SSG next: set up casbin middleware after: factor out common or large patterns in existing handlers after: refactor UserModel.CheckS

Feb 27, 2022

Rest API Template [GO + Fiber + Mongo]

GoFiber MongoDB Rest API Template Feature: Full CRUD REST API MongoDB native driver; no use of orm/odm How to structure a production ready API (Model/

Jan 6, 2022

Go-gin-mongo-api - A backend RESTful API built using golang, gin and mongoDB

go-gin-mongo-API This is a RESTful backend API which is developed using the gola

Jul 19, 2022

Echo-mongo-api - Simple Golang REST application with Echo Framework & MongoDB

Echo-mongo-api - Simple Golang REST application with Echo Framework & MongoDB

Feb 14, 2022

Project Flogo is an open source ecosystem of opinionated event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.

Project Flogo is an open source ecosystem of opinionated  event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.

Project Flogo is an Open Source ecosystem for event-driven apps Ecosystem | Core | Flows | Streams | Flogo Rules | Go Developers | When to use Flogo |

Dec 31, 2022

PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Dec 30, 2022

This application demonstrates how to launch high-performance "serverless" functions from the YoMo framework to process streaming data. The functions are embedded in a WebAssembly VM, WasmEdge, for safety, security, portability, and manageability.

This application demonstrates how to launch high-performance

Streaming Image Recognition by WebAssembly This project demonstrates how to process a video stream in real-time using WebAssembly and apply a pre-trai

Nov 9, 2022

Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

iter Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transformi

Dec 16, 2022
Comments
  • IsNotNil and other checks not working

    IsNotNil and other checks not working

    Hi

    If I have something like this var mySliceIntf interface{} var myInt int mySliceIntf = myInt

    fet.Field("col").In(myInt, fet.IfNotNil) does not eliminate "col" because IfNotNil takes in value as interface{} type and hence the comparison value != nil does not work. Please let me know.

    Thanks

Related tags
Go-relax - Framework for building RESTful API's in Go

Go-Relax Build fast and complete RESTful APIs in Go Go-Relax aims to provide the tools to help developers build RESTful web services, and information

Sep 28, 2022
mongodb helper functions, document and pipeline builder

MongoUtils Mongodb helper functions, document and pipeline builder. Helpers ParseObjectID Parse object id from string. ParseObjectID(id string) *primi

Dec 12, 2021
GCP Cloud Functions ready to Go starter with hot reload 🔥

GCP Cloud Functions - Go Starter Features: funcFramework already set up - ready for local development and testing. Hot Reload ready-to-go thanks to th

Dec 16, 2021
A standard library for microservices.

Go kit Go kit is a programming toolkit for building microservices (or elegant monoliths) in Go. We solve common problems in distributed systems and ap

Jan 1, 2023
Netflix's Hystrix latency and fault tolerance library, for Go

hystrix-go Hystrix is a great project from Netflix. Hystrix is a latency and fault tolerance library designed to isolate points of access to remote sy

Dec 28, 2022
A library to help you create pipelines in Golang

pipeline Pipeline is a go library that helps you build pipelines without worrying about channel management and concurrency. It contains common fan-in

Dec 13, 2022
Canonicity testing library

What are canonical tests? That's when instead of comparing the expected and actual values in code: assert.Equal(t, expected, actual) You instead asser

Dec 29, 2021
String Service. Microservice example using gokit library

Example of Microservices using go-kit Go kit is a collection of Go (golang) packages (libraries) that help you build robust, reliable, maintainable mi

Dec 6, 2021
Make Endpoint API using Golang, Mux Library, MySQL, and Redis

EndPoint API (Create and Read) I'm Using Golang, Mux Library, MySQL, and Redis Golang Documentation : https://go.dev/doc/ Golang Instalation : https:/

Dec 12, 2021
Go-fastapi: a library to quickly build APIs. It is inspired by Python's popular FastAPI
Go-fastapi: a library to quickly build APIs. It is inspired by Python's popular FastAPI

go-fastapi go-fastapi is a library to quickly build APIs. It is inspired by Pyth

Jan 1, 2023