Code Monkey home page Code Monkey logo

y's Introduction

y

Build Status

Be faster with Y. The simplest ORM-like framework for Golang.

Install

go get gopkg.in/Masterminds/squirrel.v1
go get github.com/Repo2/y

Actions

Fetch

Fetch executes SELECT statement and returns a collection of objects.

type Account struct {
  ID   int64  `y:"id,pk"`
  Name string `y:"name"`
}
c, err := y.New(Account{}).Fetch(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", c.List())

FindBy

FindBy modifies a query for custom selection.

type Order struct {
  ID    int64 `y:"id,pk"`
  Price int   `y:"price"`
}
c, err := y.New(Order{}).
  FindBy(
  func(b squirrel.SelectBuilder) squirrel.SelectBuilder {
    return b.Where("price > ?", 10)
  }).
  Fetch(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", c.List())

Load

Load executes SELECT statement for one row and loads the object in self.

type Todo struct {
  ID    int64  `y:"id,pk"`
  Title string `y:"title"`
}
todo := Todo{ID: 1}
err := y.New(&todo).Load(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", todo)

Put

Put executes INSERT statement and assigns auto-increment value.

type User struct {
  ID   int64  `y:"id,pk,autoincr"`
  Name string `y:"name"`
}
user := User{Name: "Harry"}
_, err := y.New(&user).Put(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", user)

You can use Put for batch statement also.

type Log struct {
  Msg string
}
logs := []Log{
  {"It"}, {"Works"},
}
affected, err := y.New(logs).Put(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", affected)

Update

Update executes UPDATE statement. The action compares origin and modified objects by their version in the database.

type Car struct {
  ID    int64 `y:"id,pk"`
  Power int   `y:"power"`
  y.Versionable
}
var err error
car := Car{ID: 1}
err = y.New(&car).MustLoad(db).Update(db, y.Values{"power": 50})
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", car)

Delete

Delete executes DELETE statement. The action removes an object by primary keys.

type Account struct {
	ID    int64 `y:",pk"`
	Email string
}
acc := Account{ID: 1}
affected, err := y.New(acc).Delete(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("Affected rows: %d\n", affected)

Join

Join builds relations by foreign keys

type Device struct {
  ID     int64 `y:",pk"`
  UserID int64 `y:",fk"`
  Name   string
}
type User struct {
  ID          int64     `y:"id,pk"`
  DeviceArray []*Device `y:"-"`
}
users, err := y.New(User{}).Fetch(db)
if err != nil {
  log.Panicln(err)
}
if !users.Empty() {
  devices, _ := y.New(Device{}).Join(db, users)
  log.Printf("All users with their devices: %#v\n", users)
  log.Printf("All devices: %#v\n", devices)
}

TODO

  • More tests!

y's People

Contributors

regeda avatar

Watchers

 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.