Last active
August 29, 2015 13:57
-
-
Save rolaveric/9463958 to your computer and use it in GitHub Desktop.
Example of a Javascript model object converted to Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package user | |
// Interface for a database result row | |
type DBRow interface { | |
GetInt(colnum int) int | |
GetString(colnum int) string | |
} | |
// Interface for a database result | |
type DBResult interface { | |
NextRow() DBRow | |
RowCount() int | |
} | |
// Interface for an object which can be used to make database queries | |
type DB interface { | |
Query(query string, params ...interface{}) DBResult | |
} | |
// Private package variable for the registered DB interface | |
var db DB | |
// Method for registering a DB interface | |
func RegisterDB(newDb DB) { | |
db = newDb | |
} | |
// User type | |
type User struct { | |
Name string | |
ID int | |
} | |
// Save method for the User type | |
func Save(u *User) { | |
db.Query("UPDATE User SET name = ? WHERE id = ?", u.Name, u.ID) | |
} | |
// Function for creating a new User | |
func New(name string) *User { | |
db.Query("INSERT INTO User (name) VALUES (?)", name) | |
id := db.Query("SELECT @@IDENTITY").NextRow().GetInt(0) | |
return &User{name, id} | |
} | |
// Function for getting a single User | |
func Get(id int) *User { | |
result := db.Query("SELECT name FROM User WHERE id = ?", id) | |
if result.RowCount() == 0 { | |
return nil | |
} | |
name := result.NextRow().GetString(0) | |
return &User{name, id} | |
} | |
// Function for getting all users | |
func All() []*User { | |
result := db.Query("SELECT name, id FROM User") | |
users := make([]*User, result.RowCount()) | |
for x, c := 0, result.RowCount(); x < c; x++ { | |
row := result.NextRow() | |
users[x] = &User{row.GetString(0), row.GetInt(1)} | |
} | |
return users | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/gopherjs/gopherjs/js" | |
"github.com/rolaveric/gopherjs-demo/user" | |
"github.com/rolaveric/gopherjs-demo/user/js/db" | |
) | |
// Starting point for compiling JS code | |
func main() { | |
js.Global.Set("user", map[string]interface{}{ | |
"registerDB": RegisterDBJS, | |
"new": user.New, | |
"get": user.Get, | |
"all": user.All, | |
"save": SaveJS, | |
}) | |
} | |
// Takes a DB adapter written in Javascript and wraps it as a DB interface | |
func RegisterDBJS(o *js.Object) { | |
user.RegisterDB(db.JSDB{o}) | |
} | |
// Takes a JS object and wraps it as a User struct | |
func SaveJS(o *js.Object) { | |
user.Save(&user.User{o.Get("Name").String(), o.Get("ID").Int()}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment