# README
A project of SENROK Open Source
Go ODM
Go-odm, a Golang Object Document Mapping for MongoDB.
Table of contents
Features
- Define your models and perform CRUD operations with hooks before/after each operation.
- Built-in Soft-Delete
- Support the Multi-Database Instance
- Wrap the official Mongo Go Driver.
Installation
go get github.com/senrok/go-odm
Get started
Setup a db config:
opts, err := DefaultOpts(SetDatabase(MONGODB_URL, MONGODB_DB_NAME))
Define Model
type Doc struct {
DefaultModel `bson:",inline"`
Name string `bson:"name"`
Age int `bson:"age"`
}
Insert a new Document
err := opts.Coll(&doc).Create(context.TODO(), &doc)
Update a document
docs:=getYourData()
updatedAt := docs[0].UpdatedAt
docs[0].Name = "weny updated"
err := opts.Coll(&Doc{}).UpdateOne(context.TODO(), docs[0])
Soft-Delete a document
err := opts.Coll(&Doc{}).SoftDeleteOne(context.TODO(), data[0])
Restore a document
err = opts.Coll(&Doc{}).RestoreOne(context.TODO(), data[0])
Delete a document
err := opts.Coll(&Doc{}).DeleteOne(context.TODO(), data[0])
Find a document
err := opts.Coll(&Doc{}).FindOne(context.TODO(), bson.M{"name": "weny"}, &result)
Transactions
err := opts.TransactionWithCtx(context.TODO(), func(session mongo.Session, sc mongo.SessionContext) error {
err := opts.Coll(d).Create(sc, d)
if err != nil {
return err
}
return session.CommitTransaction(sc)
})
Documentation
License
The Project is licensed under the Apache License.
# Functions
CollName returns a model's collection name.
DefaultOpts returns a Options cloned from defaultOptions.
NewOpts returns a new Options.
SetDatabase set up the client connection via a specific connection string and specific database name.
ToSnakeCase returns snake_case of the provided value.
TransactionWithClient creates a transaction with the given client.
# Variables
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
DefaultModel type contains default IDField, Auto-update CreatedAt UpdatedAt TimestampFields, DeletedAtFields which allow user soft-delete data.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
CollectionGetter interface contains a method to return a model's custom collection.
CollectionNameGetter interface contains a method to return the collection name of a model.
CreatedHook is called after a model has been created.
CreatingHook is called before saving a new model to the database.
DeletedHook is called after a model is deleted.
DeletingHook is called before a model is deleted.
IModel interface.
RestoredHook is called after soft restoring a model.
RestoringHook is called before soft restoring a model.
SavedHook is called after a model is saved to the database.
SavingHook is called before a model (new or existing) is saved to the database.
SoftDeletedHook is called after soft deleting a model.
SoftDeletingHook is called before soft deleting a model.
UpdatedHook is called after a model is updated.
UpdatingHook is called before updating a model.
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
TransactionFunc is a handler to manage a transaction.