Help us shape the future of Go! Take the 2026 Go Developer Survey

octopus

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2018 License: MIT Imports: 11 Imported by: 0

README

Octopus

Go Report Card Build Status codecov

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.

package models

import (
    "github.com/Kamva/octopus"
    "github.com/Kamva/octopus/base"
)


type User struct {
    octopus.Scheme // This is optional. This only adds GetKeyName method implementation that returns `id` by default
    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()
}

Supported Databases

  • 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Configurator

type Configurator func(*Model)

Configurator is a function for configuring Model attributes. Usually it is used for adding indices or configure table name, or even configuring drivers with custom drivers

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is an object that responsible for interacting

func (*Model) CloseClient

func (m *Model) CloseClient()

CloseClient close and destroy client connection

func (*Model) Create

func (m *Model) Create(data base.Scheme) error

Create inserts the given filled scheme into model table/collection and return inserted record/document or error if there was any fault in data insertion.

func (*Model) Delete

func (m *Model) Delete(data base.Scheme) error

Delete find a record/document that match with data ID and remove it from related table/collection. It will return error if anything went wrong

func (*Model) EnsureIndex

func (m *Model) EnsureIndex(indices ...base.Index)

EnsureIndex checks for table/collection existence in database, if not found tries to create it. Then it ensures that given indices are exists on table/collection.

func (*Model) Find

func (m *Model) Find(id interface{}) (base.Scheme, error)

Find search for a record/document in model table/collection match with given ID

func (*Model) Initiate

func (m *Model) Initiate(scheme base.Scheme, config base.DBConfig, configurators ...Configurator)

Initiate initialize the model and prepare it for interacting with database

func (*Model) PrepareClient

func (m *Model) PrepareClient()

PrepareClient Prepare client for further actions

func (*Model) Update

func (m *Model) Update(data base.Scheme) error

Update find a record/document that match with data ID and updates its field with data values. It'll return error if anything went wrong during update

func (*Model) Where

func (m *Model) Where(query ...base.Condition) base.QueryBuilder

Where returns a Query Builder based on given conditions on model table/collection that you can fetch, update or delete records/document match the query.

type Scheme

type Scheme struct{}

Scheme is the base scheme that other schemes can embed to have default methods

func (Scheme) GetKeyName

func (s Scheme) GetKeyName() string

GetKeyName return the name of primary key field name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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