Octopus is an ORM/ODM written in Golang. It supports SQL and NoSQL databases and is easy to use.
Run the following command to get octopus package
go get -u -t github.com/Kamva/octopus
For using octopus you need a scheme and a model. Scheme represent the field in your desired table (or collection), and Model is the struct that interact with the database.
Note that the scheme must implement octopus/base.Scheme
interface. The model struct must embed the octopus.Model
and run Initiate
method on its constructor.
package models
import (
"github.com/Kamva/octopus"
"github.com/Kamva/octopus/base"
)
type User struct {
// This is optional. This only adds `GetKeyName` method implementation that
// returns `id` by default. for MongoDB you should use `octopus.MongoScheme`
// or implemet `GetKeyName` method yourself, as default primary key in Mongo
// is `_id`.
octopus.Scheme
ID int `sql:"pk"`
Name string `sql:"column:full_name"`
Email string `sql:"unique"`
Password string
RawData map[string]string `sql:"ignore"` // Add ignore tag if the field does not exists on table
}
func (u User) GetID() interface{} {
return u.ID
}
type UserModel struct {
octopus.Model
}
func NewUserModel() *UserModel {
model := &UserModel{}
config := base.DBConfig{Driver:base.PG, Host:"localhost", Port: "5432", Database: "MyDatabase"}
model.Initiate(&User{}, config)
return model
}
Then you can use model like this:
package main
import (
"github.com/Kamva/octopus/term"
"models"
)
func main() {
model := models.NewUserModel()
// Find a user by ID
user, err := model.Find(1)
if err != nil {
panic(err)
}
// Create a new record
newUser := User{Name: "John Doe", Email: "john.doe@email.com", Password: "HashedPassword"}
model.Create(&newUser)
// Update a record
user.Name = "New Name"
model.Update(user)
// Delete a record
model.Delete(user)
// Query the table
model.Where(term.Equal{Field: "name", Value: "John Doe"}).First()
}
- MongoDB
- Data Modelling
- Raw Query
- Aggregations
- Relation Support [via lookup aggregation]
- PostgreSQL
- Data Modelling
- Arrays and Json type support
- Grouping
- Raw Query
- Relation Support
- MSSQL
- Data Modelling
- Grouping
- Raw Query
- Relation Support
- Stored Procedures
- MySQL
- SQLite3