Categorygithub.com/mrz1836/go-datastore
modulepackage
0.7.2
Repository: https://github.com/mrz1836/go-datastore.git
Documentation: pkg.go.dev

# README

go-datastore

Data layer using GORM for accessing models via a Database (MySQL, Postgres, SQLite, MongoDB)

Release Build Status Report codecov Mergify Status Go
Gitpod Ready-to-Code standard-readme compliant Makefile Included Sponsor Donate


Table of Contents


Installation

go-datastore requires a supported release of Go.

go get -u github.com/mrz1836/go-datastore

Documentation

View the generated documentation

GoDoc

Database model assumptions:

  • id If a unique ID is needed, the datastore expects id and uses _id internally for Mongo
  • metadata is an optional field for storing key->value JSON data

Custom array and object fields:

  • WithCustomFields(arrayFields, objectFields ) Use this method to add custom array or object fields (IE: metadata)

Custom methods for Mongo:

  • WithCustomMongoConditionProcessor() Use this method to add custom condition processing for custom object fields
  • WithCustomMongoIndexer() Use this method to add custom mongo indices

Repository Features

This repository was created using MrZ's go-template

Built-in Features

Package Dependencies
Library Deployment

Releases are automatically created when you create a new Git tag!

If you want to manually make releases, please install GoReleaser:

goreleaser for easy binary or library deployment to GitHub and can be installed:

  • using make: make install-releaser
  • using brew: brew install goreleaser

The .goreleaser.yml file is used to configure goreleaser.


Automatic Releases on Tag Creation (recommended)

Automatic releases via GitHub Actions from creating a new tag:

make tag version=1.2.3

Manual Releases (optional)

Use make release-snap to create a snapshot version of the release, and finally make release to ship to production (manually).


Makefile Commands

View all makefile commands

make help

List of all current commands:

all                           Runs multiple commands
clean                         Remove previous builds and any cached data
clean-mods                    Remove all the Go mod cache
coverage                      Shows the test coverage
diff                          Show the git diff
generate                      Runs the go generate command in the base of the repo
godocs                        Sync the latest tag with GoDocs
help                          Show this help message
install                       Install the application
install-all-contributors      Installs all contributors locally
install-go                    Install the application (Using Native Go)
install-releaser              Install the GoReleaser application
lint                          Run the golangci-lint application (install if not found)
release                       Full production release (creates release in GitHub)
release                       Runs common.release then runs godocs
release-snap                  Test the full release (build binaries)
release-test                  Full production test release (everything except deploy)
replace-version               Replaces the version in HTML/JS (pre-deploy)
tag                           Generate a new tag and push (tag version=0.0.0)
tag-remove                    Remove a tag if found (tag-remove version=0.0.0)
tag-update                    Update an existing tag to current commit (tag-update version=0.0.0)
test                          Runs lint and ALL tests
test-ci                       Runs all tests via CI (exports coverage)
test-ci-no-race               Runs all tests via CI (no race) (exports coverage)
test-ci-short                 Runs unit tests via CI (exports coverage)
test-no-lint                  Runs just tests
test-short                    Runs vet, lint and tests (excludes integration tests)
test-unit                     Runs tests and outputs coverage
uninstall                     Uninstall the application (and remove files)
update-contributors           Regenerates the contributors html/list
update-linter                 Update the golangci-lint package (macOS only)
vet                           Run the Go vet application

Examples & Tests

All unit tests and examples run via GitHub Actions and uses Go version 1.22.x. View the configuration file.


Run all tests (including integration tests)

make test

Run tests (excluding integration tests)

make test-short

Benchmarks

Run the Go benchmarks:

make bench

Code Standards

Read more about this Go project's code standards.


Usage

Checkout all the examples!


Contributing

View the contributing guidelines and follow the code of conduct.


How can I help?

All kinds of contributions are welcome :raised_hands:! The most basic way to show your support is to star :star2: the project, or to raise issues :speech_balloon:. You can also support this project by becoming a sponsor on GitHub :clap: or by making a bitcoin donation to ensure this journey continues indefinitely! :rocket:

Stars


Contributors ✨

Thank you to these wonderful people (emoji key):


Mr. Z

🚇 💻 🚧 🛡️

Siggi

🚇 💻 🛡️

This project follows the all-contributors specification.


License

License

# Packages

Package customtypes encapsulates our custom database field types.
Package nrgorm integrates New Relic into the GORM database layer.

# Functions

GetModelBoolAttribute the attribute from the model as a bool.
GetModelName get the name of the model via reflection.
GetModelStringAttribute the attribute from the model as a string.
GetModelTableName get the db table name of the model via reflection.
GetModelType get the model type of the model interface via reflection.
GetModelUnset gets any empty values on the model and makes sure the update actually unsets those values in the database, otherwise this never happens, and we cannot unset.
IsModelSlice returns true if the given interface is a slice of models.
IsSQLEngine check whether the string already is in the slice.
MarshalQueryParams will marshal the custom type.
NewClient creates a new client for all Datastore functionality If no options are given, it will use the defaultClientOptions() ctx may contain a NewRelic txn (or one will be created).
StringInSlice check whether the string already is in the slice.
UnmarshalQueryParams will unmarshal the custom type.
WithAutoMigrate will enable auto migrate database mode (given models) Pointers of structs (IE: &models.Xpub{}).
WithCustomFields will add custom fields to the datastore.
WithCustomMongoConditionProcessor will add a custom mongo condition processor function.
WithCustomMongoIndexer will add a custom mongo index function (returns custom mongo indexes).
WithDebugging will enable debugging mode.
WithLogger will set the custom logger interface.
WithMongo will set the datastore to use MongoDB.
WithMongoConnection will set the datastore to use an existing Mongo database connection.
WithNewRelic will enable the NewRelic wrapper.
WithSQL will load a datastore using either an SQL database config or existing connection.
WithSQLConnection will set the datastore to an existing connection for MySQL or PostgreSQL.
WithSQLite will set the datastore to use SQLite.

# Constants

Supported engines (databases).
index creation constants.
index creation constants.
Supported engines (databases).
Supported engines (databases).
index creation constants.
Supported engines (databases).
SortAsc will sort ascending.
SortDesc will sort descending.
Supported engines (databases).

# Variables

DateFields are standard known date fields.
ErrDuplicateKey error when a record is inserted and conflicts with an existing record.
ErrNoResults error when no results are found.
ErrNoSourceFound is when no source database is found in all given configurations.
ErrNotImplemented is an error when a method is not implemented.
ErrUnknownCollection is thrown when the collection can not be found using the model/name.
ErrUnknownSQL is an error when using a SQL engine that is not known for indexes and migrations.
ErrUnsupportedDriver is when the given SQL driver is not determined to be known or supported.
ErrUnsupportedEngine is used when the engine given is not a known datastore engine.
SQLDatabases is the list of supported SQL databases (via GORM).

# Structs

No description provided by the author
CommonConfig is the common configuration fields between engines.
DatabaseLogWrapper is a special wrapper for the GORM logger.
MongoDBConfig is the configuration for each MongoDB connection.
QueryParams object to use when limiting and sorting database query results.
SQLConfig is the configuration for each SQL connection (mysql or postgresql).
SQLiteConfig is the configuration for each SQLite connection.
Transaction is the internal datastore transaction.

# Interfaces

ClientInterface is the Datastore client interface.
CustomWhereInterface is an interface for the CustomWhere clauses.
GetterInterface is the getter methods.
StorageService is the storage related methods.

# Type aliases

ClientOps allow functional options to be supplied that overwrite default client options.
Engine is the different engines that are supported (database).