https://github.com/plopezm/genorm
Simple ORM based on code generation
https://github.com/plopezm/genorm
code-generation database godb golang orm
Last synced: 6 months ago
JSON representation
Simple ORM based on code generation
- Host: GitHub
- URL: https://github.com/plopezm/genorm
- Owner: plopezm
- License: mit
- Created: 2019-07-27T21:12:49.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-31T17:11:45.000Z (almost 7 years ago)
- Last Synced: 2024-06-20T12:43:42.049Z (about 2 years ago)
- Topics: code-generation, database, godb, golang, orm
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# genorm
Genorm is a basic orm based on code generator using the query builder [godb](https://github.com/samonzeweb/godb)
### Installing GENORM
To install genorm download it using golang tools:
```
go get github.com/plopezm/genorm
```
### Supported databases
This code generation tool uses godb to perform the queries. This means that every adapter used in godb is compatible with this library.
Available drivers:
- sqlite
- mssql
- mysql
- postgresql
You can check the updated list [here](https://github.com/samonzeweb/godb/tree/master/adapters)
### How to use
Using genorm is easy, just define your struct like golang db standard.
```
type Book struct {
Id int `db:"id,key,auto"`
Title string `db:"title"`
Author string `db:"author"`
Published time.Time `db:"published"`
}
```
Now we need to add on the top of the file the genorm directive
```
//go:generate genorm -type=Book -driver=sqlite -url=local.db -tableName=books
```
The next step is call golang generate command:
```
go generate ./...
```
A new file will appear with the name "{structName}-repository.go". This file is autogenerated, so if you want to add new fearures, the best approach is to create a new file a using the repository struct created add that funcionality there.
Complete example:
```
package models
import "time"
//go:generate genorm -type=Book -driver=sqlite -url=local.db -tableName=books
type Book struct {
Id int `db:"id,key,auto"`
Title string `db:"title"`
Author string `db:"author"`
Published time.Time `db:"published"`
}
```
Generated file: book-repository.go
```
package models
import (
"fmt"
"github.com/samonzeweb/godb"
"github.com/samonzeweb/godb/adapters/sqlite"
)
type BookRepository struct {
db *godb.DB
}
func (entity *Book) TableName() string {
return "books"
}
func NewBookRepository() *BookRepository {
db, err := godb.Open(sqlite.Adapter, "local.db")
if err != nil {
panic(err)
}
return &BookRepository{
db: db,
}
}
func (this *BookRepository) FindAll() (result []Book, err error) {
result = make([]Book, 0, 0)
err = this.db.Select(&result).Do()
return
}
func (this *BookRepository) FindByFields(fields []string, values []interface{}) (result []Book, err error) {
result = make([]Book, 0, 0)
query := this.db.Select(&result)
for i, field := range fields {
query.Where(fmt.Sprintf("%s = ?", field), values[i])
}
err = query.Do()
return
}
func (this *BookRepository) FindAllWithIterator() (result godb.Iterator, err error) {
entity := &Book{}
result ,err = this.db.SelectFrom(entity.TableName()).DoWithIterator()
return
}
func (this *BookRepository) FindOneById(idField string, idValue interface{}) (result *Book, err error) {
result = &Book{}
err = this.db.Select(result).
Where(fmt.Sprintf("%s = ?", idField), idValue).
Do()
return
}
func (this *BookRepository) RawSQL(queryBuffer *godb.SQLBuffer) (result []Book, err error) {
result = make([]Book, 0, 0)
err = this.db.RawSQL(queryBuffer.SQL(), queryBuffer.Arguments()...).Do(&result)
return
}
func (this *BookRepository) BeginTx() (err error) {
err = this.db.Begin()
return
}
func (this *BookRepository) CommitTx() (err error) {
err = this.db.Commit()
return
}
func (this *BookRepository) RollbackTx() (err error) {
err = this.db.Rollback()
return
}
func (this *BookRepository) CreateOne(newBook *Book) (err error) {
err = this.db.Insert(newBook).Do()
return
}
func (this *BookRepository) UpdateOne(newBook *Book) (err error) {
err = this.db.Update(newBook).Do()
return
}
func (this *BookRepository) DeleteOne(newBook *Book) (err error) {
_ ,err = this.db.Delete(newBook).Do()
return
}
```