GithubHelp home page GithubHelp logo

y's Introduction

y

Build Status

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

Install

go get github.com/lann/squirrel
go get github.com/Repo2/y

Actions

Fetch

Fetch executes SELECT statement and returns a collection of objects.

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

Find

Find modifies a query for custom selection.

type Order struct {
  ID    int64 `db:"id,pk"`
  Price int   `db:"price"`
}
c, err := y.New(Order{}).
  Find(
  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  `db:"id,pk"`
  Title string `db:"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  `db:"id,pk,autoincr"`
  Name string `db:"name"`
}
user := User{Name: "Harry"}
err := y.New(&user).Put(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", user)

Update

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

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

Join

Join builds relations by foreign keys

type Device struct {
  ID     int64 `db:",pk"`
  UserID int64 `db:",fk"`
  Name   string
}
type User struct {
  ID          int64     `db:"id,pk"`
  DeviceArray []*Device `db:"-"`
}
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!
  • Add Delete action

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.