https://github.com/kamva/octopus
Octopus is an ORM/ODM written in Go
https://github.com/kamva/octopus
golang mongodb mssql octopus orm postgresql sqlserver
Last synced: about 1 year ago
JSON representation
Octopus is an ORM/ODM written in Go
- Host: GitHub
- URL: https://github.com/kamva/octopus
- Owner: Kamva
- License: mit
- Created: 2018-11-12T07:15:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-07T14:25:22.000Z (almost 7 years ago)
- Last Synced: 2025-04-02T17:51:21.276Z (about 1 year ago)
- Topics: golang, mongodb, mssql, octopus, orm, postgresql, sqlserver
- Language: Go
- Homepage: http://kamva.ir
- Size: 112 KB
- Stars: 9
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Octopus
[](https://goreportcard.com/report/github.com/Kamva/octopus)
[](https://travis-ci.org/Kamva/octopus)
[](https://codecov.io/gh/Kamva/octopus)
Octopus is an ORM/ODM written in Golang. It supports SQL and NoSQL databases and is easy to use.
## Get Started
Run the following command to get octopus package
```
go get -u -t github.com/Kamva/octopus
```
## Usage
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.
```go
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:
```go
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()
}
```
## Supported Databases
- [x] MongoDB
- [x] Data Modelling
- [ ] Raw Query
- [ ] Aggregations
- [ ] Relation Support [via lookup aggregation]
- [x] PostgreSQL
- [x] Data Modelling
- [x] Arrays and Json type support
- [ ] Grouping
- [ ] Raw Query
- [ ] Relation Support
- [x] MSSQL
- [x] Data Modelling
- [ ] Grouping
- [ ] Raw Query
- [ ] Relation Support
- [ ] Stored Procedures
- [ ] MySQL
- [ ] SQLite3