Code Monkey home page Code Monkey logo

octopus's Introduction

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 {
    // 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: "[email protected]", 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

octopus's People

Contributors

behzadsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.