Skip to content

Commit 3485fb5

Browse files
committed
renamed grimoire to rel
1 parent 0d50a5a commit 3485fb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1068
-1120
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
vendor
2-
coverage.out
32
.tool-versions
43
*.db
54
.vscode/
65
debug.test
76
.idea/
7+
*.out
8+
*.test

.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
env:
22
global:
33
- CC_TEST_REPORTER_ID=42ba59b516289c90de75bec1e73dcf75e601d150b271e8762cee9efcb8d70282
4-
- MYSQL_DATABASE=root@(127.0.0.1:3306)/grimoire_test
5-
- POSTGRESQL_DATABASE=postgres://postgres@localhost/grimoire_test?sslmode=disable
4+
- MYSQL_DATABASE=root@(127.0.0.1:3306)/rel_test
5+
- POSTGRESQL_DATABASE=postgres://postgres@localhost/rel_test?sslmode=disable
66

77
language: go
88
go:
@@ -19,8 +19,8 @@ services:
1919
- postgresql
2020

2121
before_install:
22-
- mysql -e 'CREATE DATABASE grimoire_test;'
23-
- psql -c 'create database grimoire_test;' -U postgres
22+
- mysql -e 'CREATE DATABASE rel_test;'
23+
- psql -c 'create database rel_test;' -U postgres
2424

2525
install:
2626
- go get -u github.com/golang/dep/cmd/dep

README.md

+15-77
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
# grimoire
2-
[![GoDoc](https://godoc.org/github.com/Fs02/grimoire?status.svg)](https://godoc.org/github.com/Fs02/grimoire) [![Build Status](https://travis-ci.org/Fs02/grimoire.svg?branch=master)](https://travis-ci.org/Fs02/grimoire) [![Go Report Card](https://goreportcard.com/badge/github.com/Fs02/grimoire)](https://goreportcard.com/report/github.com/Fs02/grimoire) [![Maintainability](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/maintainability)](https://codeclimate.com/github/Fs02/grimoire/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/test_coverage)](https://codeclimate.com/github/Fs02/grimoire/test_coverage)
1+
# rel
2+
[![GoDoc](https://godoc.org/github.com/Fs02/rel?status.svg)](https://godoc.org/github.com/Fs02/rel) [![Build Status](https://travis-ci.org/Fs02/rel.svg?branch=master)](https://travis-ci.org/Fs02/rel) [![Go Report Card](https://goreportcard.com/badge/github.com/Fs02/rel)](https://goreportcard.com/report/github.com/Fs02/rel) [![Maintainability](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/maintainability)](https://codeclimate.com/github/Fs02/rel/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/test_coverage)](https://codeclimate.com/github/Fs02/rel/test_coverage)
33

4-
Grimoire is a database access layer inspired by Ecto. It features a flexible query API and built-in validation. It currently supports MySQL, PostgreSQL, and SQLite3 but a custom adapter can be implemented easily using the Adapter interface.
5-
6-
Features:
7-
8-
- Query Builder
9-
- Association Preloading
10-
- Struct style create and update
11-
- Changeset Style create and update
12-
- Builtin validation using changeset
13-
- Multi adapter support
14-
- Logger
15-
16-
## Motivation
17-
18-
Common go ORM accepts struct as a value for modifying records which has a problem of unable to differentiate between an empty, nil, or undefined value. It's a tricky problem especially when you want to have an endpoint that supports partial updates. Grimoire attempts to solve that problem by integrating Changeset system inspired from Elixir's Ecto. Changeset is a form like entity which allows us to not only solve that problem but also help us with casting, validations, and constraints check.
4+
rel is a repository layer for sql database.
195

206
## Install
217

228
```bash
23-
go get github.com/Fs02/grimoire
9+
go get github.com/Fs02/rel
2410
```
2511

2612
## Quick Start
@@ -31,10 +17,9 @@ package main
3117
import (
3218
"time"
3319

34-
"github.com/Fs02/grimoire"
35-
"github.com/Fs02/grimoire/adapter/mysql"
36-
"github.com/Fs02/grimoire/changeset"
37-
"github.com/Fs02/grimoire/params"
20+
"github.com/Fs02/rel"
21+
"github.com/Fs02/rel/adapter/mysql"
22+
"github.com/Fs02/rel/where"
3823
)
3924

4025
type Product struct {
@@ -45,15 +30,6 @@ type Product struct {
4530
UpdatedAt time.Time
4631
}
4732

48-
// ChangeProduct prepares data before database operation.
49-
// Such as casting value to appropriate types and perform validations.
50-
func ChangeProduct(product interface{}, params params.Params) *changeset.Changeset {
51-
ch := changeset.Cast(product, params, []string{"name", "price"})
52-
changeset.ValidateRequired(ch, []string{"name", "price"})
53-
changeset.ValidateMin(ch, "price", 100)
54-
return ch
55-
}
56-
5733
func main() {
5834
// initialize mysql adapter.
5935
adapter, err := mysql.Open("root@(127.0.0.1:3306)/db?charset=utf8&parseTime=True&loc=Local")
@@ -62,60 +38,22 @@ func main() {
6238
}
6339
defer adapter.Close()
6440

65-
// initialize grimoire's repo.
66-
repo := grimoire.New(adapter)
67-
68-
var product Product
41+
// initialize rel's repo.
42+
repo := rel.New(adapter)
6943

7044
// Inserting Products.
71-
// Changeset is used when creating or updating your data.
72-
ch := ChangeProduct(product, params.Map{
73-
"name": "shampoo",
74-
"price": 1000,
75-
})
76-
77-
if ch.Error() != nil {
78-
// handle error
45+
product := Product{
46+
Name: "shampoo",
47+
Price: 1000,
7948
}
80-
81-
// Changeset can also be created directly from json string.
82-
jsonch := ChangeProduct(product, params.ParseJSON(`{
83-
"name": "soap",
84-
"price": 2000,
85-
}`))
86-
87-
// Create products with changeset and return the result to &product,
88-
if err = repo.From("products").Insert(&product, ch); err != nil {
89-
// handle error
90-
}
91-
92-
// or panic when insertion pailed
93-
repo.From("products").MustInsert(&product, jsonch)
49+
repo.Insert(&product)
9450

9551
// Querying Products.
9652
// Find a product with id 1.
97-
repo.From("products").Find(1).MustOne(&product)
98-
99-
// Updating Products.
100-
// Update products with id=1.
101-
repo.From("products").Find(1).MustUpdate(&product, ch)
102-
103-
// Deleting Products.
104-
// Delete Product with id=1.
105-
repo.From("products").Find(1).MustDelete()
53+
repo.One(&product, where.Eq("id", 1))
10654
}
10755
```
10856

109-
## Examples
110-
111-
- [Todo API](https://github.com/Fs02/grimoire-todo-example)
112-
113-
## Documentation
114-
115-
Guides: [https://fs02.github.io/grimoire](https://fs02.github.io/grimoire)
116-
117-
API Documentation: [https://godoc.org/github.com/Fs02/grimoire](https://godoc.org/github.com/Fs02/grimoire)
118-
11957
## License
12058

121-
Released under the [MIT License](https://github.com/Fs02/grimoire/blob/master/LICENSE)
59+
Released under the [MIT License](https://github.com/Fs02/rel/blob/master/LICENSE)

adapter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grimoire
1+
package rel
22

33
// Adapter interface
44
type Adapter interface {

adapter/mysql/mysql.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
// Package mysql wraps mysql driver as an adapter for grimoire.
1+
// Package mysql wraps mysql driver as an adapter for rel.
22
//
33
// Usage:
44
// // open mysql connection.
5-
// adapter, err := mysql.Open("root@(127.0.0.1:3306)/grimoire_test?charset=utf8&parseTime=True&loc=Local")
5+
// adapter, err := mysql.Open("root@(127.0.0.1:3306)/rel_test?charset=utf8&parseTime=True&loc=Local")
66
// if err != nil {
77
// panic(err)
88
// }
99
// defer adapter.Close()
1010
//
11-
// // initialize grimoire's repo.
12-
// repo := grimoire.New(adapter)
11+
// // initialize rel's repo.
12+
// repo := rel.New(adapter)
1313
package mysql
1414

1515
import (
1616
db "database/sql"
1717
"strings"
1818

1919
"github.com/Fs02/go-paranoid"
20-
"github.com/Fs02/grimoire"
21-
"github.com/Fs02/grimoire/adapter/sql"
20+
"github.com/Fs02/rel"
21+
"github.com/Fs02/rel/adapter/sql"
2222
)
2323

2424
// Adapter definition for mysql database.
2525
type Adapter struct {
2626
*sql.Adapter
2727
}
2828

29-
var _ grimoire.Adapter = (*Adapter)(nil)
29+
var _ rel.Adapter = (*Adapter)(nil)
3030

3131
// Open mysql connection using dsn.
3232
func Open(dsn string) (*Adapter, error) {
@@ -56,7 +56,7 @@ func incrementFunc(adapter sql.Adapter) int {
5656
} else {
5757
err = adapter.DB.QueryRow("SHOW VARIABLES LIKE 'auto_increment_increment';").Scan(&variable, &increment)
5858
}
59-
paranoid.Panic(err, "grimoire: MySQL failed to get auto_increment_increment variable")
59+
paranoid.Panic(err, "rel: MySQL failed to get auto_increment_increment variable")
6060

6161
return increment
6262
}
@@ -78,15 +78,15 @@ func errorFunc(err error) error {
7878

7979
switch msg[:errCodeIndex] {
8080
case "Error 1062":
81-
return grimoire.ConstraintError{
81+
return rel.ConstraintError{
8282
Key: sql.ExtractString(msg, "key '", "'"),
83-
Type: grimoire.UniqueConstraint,
83+
Type: rel.UniqueConstraint,
8484
Err: err,
8585
}
8686
case "Error 1452":
87-
return grimoire.ConstraintError{
87+
return rel.ConstraintError{
8888
Key: sql.ExtractString(msg, "CONSTRAINT `", "`"),
89-
Type: grimoire.ForeignKeyConstraint,
89+
Type: rel.ForeignKeyConstraint,
9090
Err: err,
9191
}
9292
default:

adapter/mysql/mysql_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"testing"
66

77
paranoid "github.com/Fs02/go-paranoid"
8-
"github.com/Fs02/grimoire"
9-
"github.com/Fs02/grimoire/adapter/specs"
8+
"github.com/Fs02/rel"
9+
"github.com/Fs02/rel/adapter/specs"
1010
_ "github.com/go-sql-driver/mysql"
1111
"github.com/stretchr/testify/assert"
1212
)
@@ -59,15 +59,15 @@ func dsn() string {
5959
return os.Getenv("MYSQL_DATABASE") + "?charset=utf8&parseTime=True&loc=Local"
6060
}
6161

62-
return "root@tcp(localhost:3306)/grimoire_test?charset=utf8&parseTime=True&loc=Local"
62+
return "root@tcp(localhost:3306)/rel_test?charset=utf8&parseTime=True&loc=Local"
6363
}
6464

6565
func TestAdapter_specs(t *testing.T) {
6666
adapter, err := Open(dsn())
6767
paranoid.Panic(err, "failed to open database connection")
6868
defer adapter.Close()
6969

70-
repo := grimoire.New(adapter)
70+
repo := rel.New(adapter)
7171

7272
// Query Specs
7373
specs.Query(t, repo)
@@ -129,7 +129,7 @@ func TestAdapter_specs(t *testing.T) {
129129
// {"notexist": "13"},
130130
// }
131131

132-
// _, err = adapter.InsertAll(grimoire.Repo{}.From("users"), fields, allchanges)
132+
// _, err = adapter.InsertAll(rel.Repo{}.From("users"), fields, allchanges)
133133

134134
// assert.NotNil(t, err)
135135
// }

adapter/postgres/postgres.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
// Package postgres wraps postgres (pq) driver as an adapter for grimoire.
1+
// Package postgres wraps postgres (pq) driver as an adapter for rel.
22
//
33
// Usage:
44
// // open postgres connection.
5-
// adapter, err := postgres.Open("postgres://postgres@localhost/grimoire_test?sslmode=disable")
5+
// adapter, err := postgres.Open("postgres://postgres@localhost/rel_test?sslmode=disable")
66
// if err != nil {
77
// panic(err)
88
// }
99
// defer adapter.Close()
1010
//
11-
// // initialize grimoire's repo.
12-
// repo := grimoire.New(adapter)
11+
// // initialize rel's repo.
12+
// repo := rel.New(adapter)
1313
package postgres
1414

1515
import (
1616
db "database/sql"
1717
"time"
1818

19-
"github.com/Fs02/grimoire"
20-
"github.com/Fs02/grimoire/adapter/sql"
19+
"github.com/Fs02/rel"
20+
"github.com/Fs02/rel/adapter/sql"
2121
)
2222

2323
// Adapter definition for postgrees database.
2424
type Adapter struct {
2525
*sql.Adapter
2626
}
2727

28-
var _ grimoire.Adapter = (*Adapter)(nil)
28+
var _ rel.Adapter = (*Adapter)(nil)
2929

3030
// Open postgrees connection using dsn.
3131
func Open(dsn string) (*Adapter, error) {
@@ -48,7 +48,7 @@ func Open(dsn string) (*Adapter, error) {
4848
}
4949

5050
// Insert inserts a record to database and returns its id.
51-
func (adapter *Adapter) Insert(query grimoire.Query, changes grimoire.Changes, loggers ...grimoire.Logger) (interface{}, error) {
51+
func (adapter *Adapter) Insert(query rel.Query, changes rel.Changes, loggers ...rel.Logger) (interface{}, error) {
5252
var (
5353
id int64
5454
statement, args = sql.NewBuilder(adapter.Config).Returning("id").Insert(query.Collection, changes)
@@ -64,7 +64,7 @@ func (adapter *Adapter) Insert(query grimoire.Query, changes grimoire.Changes, l
6464
}
6565

6666
// InsertAll inserts multiple records to database and returns its ids.
67-
func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchanges []grimoire.Changes, loggers ...grimoire.Logger) ([]interface{}, error) {
67+
func (adapter *Adapter) InsertAll(query rel.Query, fields []string, allchanges []rel.Changes, loggers ...rel.Logger) ([]interface{}, error) {
6868
var (
6969
ids []interface{}
7070
statement, args = sql.NewBuilder(adapter.Config).Returning("id").InsertAll(query.Collection, fields, allchanges)
@@ -83,7 +83,7 @@ func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchan
8383
return ids, err
8484
}
8585

86-
func (adapter *Adapter) query(statement string, args []interface{}, loggers []grimoire.Logger) (*db.Rows, error) {
86+
func (adapter *Adapter) query(statement string, args []interface{}, loggers []rel.Logger) (*db.Rows, error) {
8787
var (
8888
err error
8989
rows *db.Rows
@@ -96,13 +96,13 @@ func (adapter *Adapter) query(statement string, args []interface{}, loggers []gr
9696
rows, err = adapter.DB.Query(statement, args...)
9797
}
9898

99-
go grimoire.Log(loggers, statement, time.Since(start), err)
99+
go rel.Log(loggers, statement, time.Since(start), err)
100100

101101
return rows, errorFunc(err)
102102
}
103103

104104
// Begin begins a new transaction.
105-
func (adapter *Adapter) Begin() (grimoire.Adapter, error) {
105+
func (adapter *Adapter) Begin() (rel.Adapter, error) {
106106
newAdapter, err := adapter.Adapter.Begin()
107107

108108
return &Adapter{
@@ -122,21 +122,21 @@ func errorFunc(err error) error {
122122

123123
switch constraintType {
124124
case "unique":
125-
return grimoire.ConstraintError{
125+
return rel.ConstraintError{
126126
Key: sql.ExtractString(err.Error(), "constraint \"", "\""),
127-
Type: grimoire.UniqueConstraint,
127+
Type: rel.UniqueConstraint,
128128
Err: err,
129129
}
130130
case "foreign key":
131-
return grimoire.ConstraintError{
131+
return rel.ConstraintError{
132132
Key: sql.ExtractString(err.Error(), "constraint \"", "\""),
133-
Type: grimoire.ForeignKeyConstraint,
133+
Type: rel.ForeignKeyConstraint,
134134
Err: err,
135135
}
136136
case "check":
137-
return grimoire.ConstraintError{
137+
return rel.ConstraintError{
138138
Key: sql.ExtractString(err.Error(), "constraint \"", "\""),
139-
Type: grimoire.CheckConstraint,
139+
Type: rel.CheckConstraint,
140140
Err: err,
141141
}
142142
default:

0 commit comments

Comments
 (0)