Coder Social home page Coder Social logo

goorm's Introduction

GoORM

GoORM is an ORM for Go. It lets you map Go structs to tables in a database. It's intended to be very lightweight, doing very little beyond what you really want. For example, when fetching data, instead of re-inventing a query syntax, we just delegate your query to the underlying database, so you can write the "where" clause of your SQL statements directly. This allows you to have more flexibility while giving you a convenience layer. But GoORM also has some smart defaults, for those times when complex queries aren't necessary.

Installing GoORM

go get github.com/astaxie/goorm

How do we use it?

Open a database

orm := goorm.NewORM("127.0.0.1", "3306", "test", "xiemengjun", "123456", "utf8")

Change Database

orm.SelectDb("test2")  

Model a struct after a table in the db

type Person struct {
	Id int64
	Name string
	Age int64
}

Create an object and save it

var someone Person
someone.Name = "john"
someone.Age = 20

orm.Save(&someone)

Fetch a single object

var person1 Person
orm.Get(&person1, "id = ?", 3)

var person2 Person
orm.Get(&person2, 3) // this is shorthand for the version above

var person3 Person
orm.Get(&person3, "name = ?", "john") // more complex query

var person4 Person
orm.Get(&person4, "name = ? and age < ?", "john", 88) // even more complex

Fetch multiple objects

var bobs []Person
err := orm.GetAll(&bobs, "name = ?", "bob")

var everyone []Person
err := orm.GetAll(&everyone, "") // use empty string to omit "where" clause

Saving new and existing objects

person2.Name = "Jack" // an already-existing person in the database, from the example above
db.Save(&person2)

var newGuy Person
newGuy.Name = "that new guy"
newGuy.Age = 27

db.Save(&newGuy)
// newGuy.Id is suddenly valid, and he's in the database now.

goorm's People

Contributors

astaxie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.