-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbartlett.go
More file actions
39 lines (33 loc) · 864 Bytes
/
Copy pathbartlett.go
File metadata and controls
39 lines (33 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package bartlett automatically generates an API from your database schema.
package bartlett
import (
"database/sql"
"net/http"
)
// A UserIDProvider is a function that is able to use an incoming request to produce a user ID.
type UserIDProvider func(r *http.Request) (interface{}, error)
// Bartlett holds all of the configuration necessary to generate an API from the database.
type Bartlett struct {
DB *sql.DB
Driver Driver
Tables []Table
Users UserIDProvider
}
func (b *Bartlett) ProbeTables(writable bool) *Bartlett {
tables := b.Driver.ProbeTables(b.DB)
for _, tbl := range tables {
if !b.hasTable(tbl.Name) {
tbl.Writable = writable
b.Tables = append(b.Tables, tbl)
}
}
return b
}
func (b *Bartlett) hasTable(name string) bool {
for _, tbl := range b.Tables {
if tbl.Name == name {
return true
}
}
return false
}