a work in progress to make go webscale
This is a (sort of) reimplementation of Mongoid for Go, using mongo-go-driver as the connection interface. The primary focus is on ease of use, convenience.
Mongoid is an ODM (Object-Document Mapper) framework for MongoDB in
RubyGo
Many things (most things?) don't directly translate from Ruby to Go, but major themes of the Document-related interfaces from the Mongoid API are replicated as closely as possible, with adjustments as needed to facilitate language differences.
Also, I don't represent or work for MongoDB, Inc.
- Uses Go structs as the primary document interface - ie, build your own custom document definitions using native syntax
- Supports all builtin Go data-types as document field-types
- Supports custom structs as document field-types (embedded documents)
- Supports maps and slices/arrays as dynamic/flexible field-types
- Supports custom field data-types (custom structs with their own bson marshaling methods)
- Default values for new document objects
- Change tracking - identify which fields have been altered since new object creation or since loading from the database, as well as the previous values
- Atomic updates - only changed fields are written to the datastore during save operations, same as Ruby Mongoid
- Query builder interface - concatenating method calls to build complex queries
-
Save and recall query Scopes (as well as default scopes per ModelType)
-
Model relationships: one-to-one, one-to-many, many-to-many (and the inverses)
- Lazy loading for cross-document associations by default
- Easy basis to spawn new custom Query builders
-
Custom Callbacks based on document lifecycle events (onCreate, onUpdate, onDelete)
-
Custom Validations for document lifecycle events (onCreate, onUpdate, onDelete)
-
Plugin architecture allows for adhoc add-on functionality (think Mongoid::Paranoia, Mongoid::Versioning, etc)
-
MongoDB connection configuration via JSON, YAML, or ENV vars
Add the library to your project
cd ~/yourGoProjectDir
go get -u github.com/eshork/go-mongoid
Configure a MongoDB server
import mongoid "https://github.com/eshork/go-mongoid"
gMongoidConfig := mongoid.Config{
Clients: []mongoid.Client{
{
Name: "default",
Hosts: []string{"localhost:27017"},
Database: "yourDbNameHere",
},
},
}
mongoid.Configure(&gMongoidConfig)
Define a document model and register it
type MyDocument struct {
mongoid.Base
MyValue string
}
var MyDocuments = mongoid.Register(&MyDocument{})
Make a new item and save it
newDoc := MyDocuments.New().(*MyDocument)
newDoc.MyValue = "something noteworthy"
newDoc.Save()
var mongoid.ObjectID recordId = newDoc.ID
Retrieve stored records by ID
foundDoc := MyDocuments.Find(recordId).One().(*MyDocument)
Check the wiki for additional setup information and examples.
Refer to the examples/ directory for some use case examples to get you started.
Run grift docs
to start a local godoc server to view the embedded source documentation.