mongo

package
v0.6.1 Latest Latest
Warning

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

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

README

godal/mongo

PkgGoDev codecov

Generic MongoDB DAO implementation.

Guideline

General

  • DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.

Use GenericDaoMongo (and godal.IGenericBo) directly

  • Define a DAO struct that implements IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.

Implement custom MongoDB business DAOs and BOs

  • Define and implement the business DAO (Note: DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt).
  • Define functions to transform godal.IGenericBo to business BO and vice versa.

Optionally, create a helper function to create DAO instances.

Examples: see examples and examples_sta.

This package uses go.mongodb.org/mongo-driver/mongo as MongoDB driver.

Documentation

Overview

Package mongo provides a generic MongoDB implementation of godal.IGenericDao.

General guideline:

  • DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.

Guideline: Use GenericDaoMongo (and godal.IGenericBo) directly

  • Define a DAO struct that implements IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.

  • Optionally, create a helper function to create DAO instances.

    import ( "github.com/btnguyen2k/consu/reddo" "github.com/btnguyen2k/godal" godalmongo "github.com/btnguyen2k/godal/mongo" prommongo "github.com/btnguyen2k/prom/mongo" )

    type myGenericDaoMongo struct { *godalmongo.GenericDaoMongo }

    // GdaoCreateFilter implements godal.IGenericDao.GdaoCreateFilter. func (dao *myGenericDaoMongo) GdaoCreateFilter(collectionName string, bo godal.IGenericBo) godal.FilterOpt { id := bo.GboGetAttrUnsafe(fieldId, reddo.TypeString) return &godal.FilterOptFieldOpValue{FieldName: fieldId, Operator: godal.FilterOpEqual, Value: id} }

    // newGenericDaoMongo is convenient method to create myGenericDaoMongo instances. func newGenericDaoMongo(mc *prommongo.MongoConnect, txModeOnWrite bool) godal.IGenericDao { dao := &myGenericDaoMongo{} dao.GenericDaoMongo = godalmongo.NewGenericDaoMongo(mc, godal.NewAbstractGenericDao(dao)) dao.SetTxModeOnWrite(txModeOnWrite) return dao }

    Since MongoDB is schema-less, GenericRowMapperMongo should be sufficient. NewGenericDaoMongo(...) creates a *GenericDaoMongo that uses GenericRowMapperMongo under-the-hood.

Guideline: Implement custom MongoDB business DAOs and BOs

  • Define and implement the business DAO (Note: DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt).

  • Optionally, create a helper function to create DAO instances.

  • Define functions to transform godal.IGenericBo to business BO and vice versa.

    import ( "github.com/btnguyen2k/consu/reddo" "github.com/btnguyen2k/godal" godalmongo "github.com/btnguyen2k/godal/mongo" prommongo "github.com/btnguyen2k/prom/mongo" )

    // BoApp defines business object app type BoApp struct { Id string `json:"id"` Description string `json:"desc"` Value int `json:"val"` }

    func (app *BoApp) ToGbo() godal.IGenericBo { gbo := godal.NewGenericBo()

    // method 1: populate attributes one by one gbo.GboSetAttr("id" , app.Id) gbo.GboSetAttr("desc", app.Description) gbo.GboSetAttr("val" , app.Value)

    // method 2: transfer all attributes at once if err := gbo.GboImportViaJson(app); err!=nil { panic(err) }

    return gbo }

    func NewBoAppFromGbo(gbo godal.IGenericBo) *BoApp { app := BoApp{}

    // method 1: populate attributes one by one app.Id = gbo.GboGetAttrUnsafe("id", reddo.TypeString).(string) app.Description = gbo.GboGetAttrUnsafe("desc", reddo.TypeString).(string) app.Value = int(gbo.GboGetAttrUnsafe("val", reddo.TypeInt).(int64))

    // method 2: transfer all attributes at once if err := gbo.GboTransferViaJson(&app); err!=nil { panic(err) }

    return &app }

    // DaoAppMongodb is MongoDB-implementation of business dao type DaoAppMongodb struct { *godalmongo.GenericDaoMongo collectionName string }

    // NewDaoAppMongodb is convenient method to create DaoAppMongodb instances. func NewDaoAppMongodb(mc *prommongo.MongoConnect, collectionName string, txModeOnWrite bool) *DaoAppMongodb { dao := &DaoAppMongodb{collectionName: collectionName} dao.GenericDaoMongo = godalmongo.NewGenericDaoMongo(mc, godal.NewAbstractGenericDao(dao)) dao.SetTxModeOnWrite(txModeOnWrite) return dao }

    Since MongoDB is schema-less, GenericRowMapperMongo should be sufficient. NewGenericDaoMongo(...) creates a *GenericDaoMongo that uses GenericRowMapperMongo under-the-hood.

See more examples in 'examples' directory on project's GitHub: https://github.com/btnguyen2k/godal/tree/master/examples

To create MongoConnect instances, see package github.com/btnguyen2k/prom/mongo

Index

Constants

This section is empty.

Variables

View Source
var (
	// GenericRowMapperMongoInstance is a pre-created instance of GenericRowMapperMongo that is ready to use.
	GenericRowMapperMongoInstance godal.IRowMapper = &GenericRowMapperMongo{}
)

Functions

This section is empty.

Types

type GenericDaoMongo

type GenericDaoMongo struct {
	*godal.AbstractGenericDao
	// contains filtered or unexported fields
}

GenericDaoMongo is MongoDB implementation of godal.IGenericDao.

Function implementations (n = No, y = Yes, i = inherited):

  • (n) GdaoCreateFilter(collectionName string, bo godal.IGenericBo) godal.FilterOpt
  • (y) GdaoDelete(collectionName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoDeleteMany(collectionName string, filter godal.FilterOpt) (int, error)
  • (y) GdaoFetchOne(collectionName string, filter godal.FilterOpt) (godal.IGenericBo, error)
  • (y) GdaoFetchMany(collectionName string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)
  • (y) GdaoCreate(collectionName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoUpdate(collectionName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoSave(collectionName string, bo godal.IGenericBo) (int, error)

func NewGenericDaoMongo

func NewGenericDaoMongo(mongoConnect *mongo.MongoConnect, agdao *godal.AbstractGenericDao) *GenericDaoMongo

NewGenericDaoMongo constructs a new MongoDB implementation of godal.IGenericDao with 'txModeOnWrite=false'.

func (*GenericDaoMongo) BuildFilter added in v0.5.0

func (dao *GenericDaoMongo) BuildFilter(collectionName string, filter godal.FilterOpt) (bson.M, error)

BuildFilter transforms a godal.FilterOpt to MongoDB-compatible filter map.

See MongoDB query selector (https://docs.mongodb.com/manual/reference/operator/query/#query-selectors).

Available since v0.5.0

func (*GenericDaoMongo) GdaoCreate

func (dao *GenericDaoMongo) GdaoCreate(collectionName string, bo godal.IGenericBo) (int, error)

GdaoCreate implements godal.IGenericDao.GdaoCreate.

func (*GenericDaoMongo) GdaoCreateWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoCreateWithContext(ctx context.Context, collectionName string, bo godal.IGenericBo) (int, error)

GdaoCreateWithContext is is MongoDB variant of GdaoCreate.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoDelete added in v0.1.0

func (dao *GenericDaoMongo) GdaoDelete(collectionName string, bo godal.IGenericBo) (int, error)

GdaoDelete implements godal.IGenericDao.GdaoDelete.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoDeleteMany

func (dao *GenericDaoMongo) GdaoDeleteMany(collectionName string, filter godal.FilterOpt) (int, error)

GdaoDeleteMany implements godal.IGenericDao.GdaoDeleteMany.

func (*GenericDaoMongo) GdaoDeleteManyWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoDeleteManyWithContext(ctx context.Context, collectionName string, filter godal.FilterOpt) (int, error)

GdaoDeleteManyWithContext is is MongoDB variant of GdaoDeleteMany.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoDeleteWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoDeleteWithContext(ctx context.Context, collectionName string, bo godal.IGenericBo) (int, error)

GdaoDeleteWithContext is is MongoDB variant of GdaoDelete.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoFetchMany

func (dao *GenericDaoMongo) GdaoFetchMany(collectionName string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)

GdaoFetchMany implements godal.IGenericDao.GdaoFetchMany.

  • nil filter means "match all".

func (*GenericDaoMongo) GdaoFetchManyWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoFetchManyWithContext(ctx context.Context, collectionName string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)

GdaoFetchManyWithContext is is MongoDB variant of GdaoFetchMany.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoFetchOne

func (dao *GenericDaoMongo) GdaoFetchOne(collectionName string, filter godal.FilterOpt) (godal.IGenericBo, error)

GdaoFetchOne implements godal.IGenericDao.GdaoFetchOne.

func (*GenericDaoMongo) GdaoFetchOneWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoFetchOneWithContext(ctx context.Context, collectionName string, filter godal.FilterOpt) (godal.IGenericBo, error)

GdaoFetchOneWithContext is is MongoDB variant of GdaoFetchOne.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoSave

func (dao *GenericDaoMongo) GdaoSave(collectionName string, bo godal.IGenericBo) (int, error)

GdaoSave implements godal.IGenericDao.GdaoSave.

func (*GenericDaoMongo) GdaoSaveWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoSaveWithContext(ctx context.Context, collectionName string, bo godal.IGenericBo) (int, error)

GdaoSaveWithContext is is MongoDB variant of GdaoSave.

Available: since v0.1.0

func (*GenericDaoMongo) GdaoUpdate

func (dao *GenericDaoMongo) GdaoUpdate(collectionName string, bo godal.IGenericBo) (int, error)

GdaoUpdate implements godal.IGenericDao.GdaoUpdate.

func (*GenericDaoMongo) GdaoUpdateWithContext added in v0.1.0

func (dao *GenericDaoMongo) GdaoUpdateWithContext(ctx context.Context, collectionName string, bo godal.IGenericBo) (int, error)

GdaoUpdateWithContext is is MongoDB variant of GdaoUpdate.

Available: since v0.1.0

func (*GenericDaoMongo) GetMongoCollection

func (dao *GenericDaoMongo) GetMongoCollection(collectionName string, opts ...*options.CollectionOptions) *mongodrv.Collection

GetMongoCollection returns the MongoDB collection object specified by 'collectionName'.

func (*GenericDaoMongo) GetMongoConnect

func (dao *GenericDaoMongo) GetMongoConnect() *mongo.MongoConnect

GetMongoConnect returns the '*mongo.MongoConnect' instance attached to this DAO.

func (*GenericDaoMongo) GetTxModeOnWrite added in v0.1.0

func (dao *GenericDaoMongo) GetTxModeOnWrite() bool

GetTxModeOnWrite returns 'true' if transaction mode is enabled on write operations, 'false' otherwise.

MongoDB's implementation of GdaoCreate is "get/check and write". It can be done either in transaction (txModeOnWrite=true) or non-transaction (txModeOnWrite=false) mode.

Available: since v0.1.0

func (*GenericDaoMongo) MongoDeleteMany

func (dao *GenericDaoMongo) MongoDeleteMany(ctx context.Context, collectionName string, filter godal.FilterOpt) (*mongodrv.DeleteResult, error)

MongoDeleteMany performs a MongoDB's delete-many command on the specified collection.

func (*GenericDaoMongo) MongoFetchMany

func (dao *GenericDaoMongo) MongoFetchMany(ctx context.Context, collectionName string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) (*mongodrv.Cursor, error)

MongoFetchMany performs a MongoDB's find command on the specified collection.

func (*GenericDaoMongo) MongoFetchOne

func (dao *GenericDaoMongo) MongoFetchOne(ctx context.Context, collectionName string, filter godal.FilterOpt) *mongodrv.SingleResult

MongoFetchOne performs a MongoDB's find-one command on the specified collection.

func (*GenericDaoMongo) MongoInsertOne

func (dao *GenericDaoMongo) MongoInsertOne(ctx context.Context, collectionName string, doc interface{}) (*mongodrv.InsertOneResult, error)

MongoInsertOne performs a MongoDB's insert-one command on the specified collection.

  • ctx: can be used to pass a transaction down to the operation.

func (*GenericDaoMongo) MongoSaveOne

func (dao *GenericDaoMongo) MongoSaveOne(ctx context.Context, collectionName string, filter godal.FilterOpt, doc interface{}) *mongodrv.SingleResult

MongoSaveOne performs a MongoDB's find-one-and-replace command with 'upsert=true' on the specified collection.

func (*GenericDaoMongo) MongoUpdateOne

func (dao *GenericDaoMongo) MongoUpdateOne(ctx context.Context, collectionName string, filter godal.FilterOpt, doc interface{}) *mongodrv.SingleResult

MongoUpdateOne performs a MongoDB's find-one-and-replace command with 'upsert=false' on the specified collection.

func (*GenericDaoMongo) SetMongoConnect added in v0.0.2

func (dao *GenericDaoMongo) SetMongoConnect(mc *mongo.MongoConnect) *GenericDaoMongo

SetMongoConnect attaches a '*mongo.MongoConnect' instance to this DAO.

Available since v0.0.2

func (*GenericDaoMongo) SetTxModeOnWrite added in v0.1.0

func (dao *GenericDaoMongo) SetTxModeOnWrite(enabled bool) *GenericDaoMongo

SetTxModeOnWrite enables/disables transaction mode on write operations.

MongoDB's implementation of GdaoCreate is "get/check and write". It can be done either in transaction (txModeOnWrite=true) or non-transaction (txModeOnWrite=false) mode. As of MongoDB 4.0, transactions are available for replica set deployments only. Since MongoDB 4.2, transactions are also available for sharded cluster. By default, GenericDaoMongo is created with 'txModeOnWrite=false'. However, it is recommended to set 'txModeOnWrite=true' whenever possible.

Available: since v0.1.0

func (*GenericDaoMongo) WrapTransaction added in v0.0.4

func (dao *GenericDaoMongo) WrapTransaction(ctx context.Context, txFunc func(sctx mongodrv.SessionContext) error) error

WrapTransaction wraps a function inside a transaction.

  • txFunc: the function to wrap. If the function returns error, the transaction will be aborted, otherwise transaction is committed.

Available: since v0.0.4

type GenericRowMapperMongo added in v0.0.2

type GenericRowMapperMongo struct {
}

GenericRowMapperMongo is a generic implementation of godal.IRowMapper for MongoDB.

Implementation rules:

  • ToRow : transform godal.IGenericBo "as-is" to map[string]interface{}.
  • ToBo : expect input is a map[string]interface{}, or JSON data (string or array/slice of bytes), transforms input to godal.IGenericBo via JSON unmarshalling.
  • ColumnsList : return []string{"*"} (MongoDB is schema-free, hence column-list is not used).
  • ToDbColName : return the input field name "as-is".
  • ToBoFieldName: return the input column name "as-is".

Available: since v0.0.2.

func (*GenericRowMapperMongo) ColumnsList added in v0.0.2

func (mapper *GenericRowMapperMongo) ColumnsList(_ string) []string

ColumnsList implements godal.IRowMapper.ColumnsList.

This function returns []string{"*"} since MongoDB is schema-free (hence column-list is not used).

func (*GenericRowMapperMongo) ToBo added in v0.0.2

func (mapper *GenericRowMapperMongo) ToBo(collectionName string, row interface{}) (godal.IGenericBo, error)

ToBo implements godal.IRowMapper.ToBo.

This function expects input to be a map[string]interface{}, or JSON data (string or array/slice of bytes), transforms it to godal.IGenericBo via JSON unmarshalling. Field names are kept intact.

func (*GenericRowMapperMongo) ToBoFieldName added in v0.5.0

func (mapper *GenericRowMapperMongo) ToBoFieldName(_, colName string) string

ToBoFieldName implements godal.IRowMapper.ToBoFieldName.

This function returns the input column name "as-is".

func (*GenericRowMapperMongo) ToDbColName added in v0.5.0

func (mapper *GenericRowMapperMongo) ToDbColName(_, fieldName string) string

ToDbColName implements godal.IRowMapper.ToDbColName.

This function returns the input field name "as-is".

func (*GenericRowMapperMongo) ToRow added in v0.0.2

func (mapper *GenericRowMapperMongo) ToRow(collectionName string, bo godal.IGenericBo) (interface{}, error)

ToRow implements godal.IRowMapper.ToRow.

This function transforms godal.IGenericBo to map[string]interface{}. Field names are kept intact.

Jump to

Keyboard shortcuts

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