Skip to content

Commit df39b74

Browse files
author
Jeff Wendling
committed
more readme updates
1 parent 16983c1 commit df39b74

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

README.md

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# DBX
22

3-
DBX is a tool to generate database schemas and code to operate with it. It currently generates Go bindings to Postgres and/or SQLite, but it should be fairly straightforward to add other database *and* language targets.
3+
DBX is a tool to generate database schemas and code to operate with it. It
4+
currently generates Go bindings to Postgres and/or SQLite, but it should be
5+
fairly straightforward to add other database *and* language targets.
46

57
## How it works
68

@@ -42,34 +44,24 @@ source with the command
4244
$ dbx.v1 golang example.dbx .
4345
```
4446

45-
This will create an `example.go` file in the current directory.
47+
This will create an `example.go` file in the current directory. Check the
48+
output of `dbx.v1 golang` for more options like controling the package name or
49+
other features of the generated code.
4650

47-
Generating a schema is also pretty straightforward:
51+
Generating a schema is also straightforward:
4852

4953
```
50-
$ dbx.v1 schema examples.dbx
54+
$ dbx.v1 schema examples.dbx .
5155
```
5256

53-
By default DBX will generate code for all of the models and fields and use the
54-
postgres SQL dialect. You can support multiple SQL dialects:
57+
This creates an `example.dbx.postgres.sql` file in the current directory with
58+
sql statements to create the tables for the models.
5559

56-
```
57-
$ dbx.v1 golang -d postgres -d sqlite3 example.dbx .
58-
$ dbx.v1 schema -d postgres -d sqlite3 example.dbx .
59-
```
60-
61-
Note that these commands are intended to normally be used with `//go:generate`
62-
directives, such as:
60+
By default DBX will generate code for all of the models and fields and use the
61+
postgres SQL dialect. See the dialects section below for more discussion on
62+
other supported dialects and how to generate them.
6363

64-
```
65-
//go:generate dbx.v1 golang -d postgres -d sqlite3 example.dbx .
66-
//go:generate dbx.v1 schema -d postgres -d sqlite3 example.dbx .
67-
```
68-
69-
Check the output of `dbx.v1 golang` for more options including how to pass
70-
other SQL dialects.
71-
72-
This example package doesn't do very much because we didn't ask for very much,
64+
This example package doesn't do very much because we didn't ask for very much,
7365
but it does include a struct definition like
7466

7567
```
@@ -86,16 +78,25 @@ as well as concrete types `DB` and `Tx`, and interfaces that they implement
8678
that look like
8779

8880
```
89-
type TXMethods interface {
90-
DeleteAll(ctx context.Context) (count int64, err error)
81+
type Methods interface {
82+
}
83+
84+
type TxMethods interface {
85+
Methods
86+
87+
Commit() error
88+
Rollback() error
9189
}
9290
9391
type DBMethods interface {
9492
Schema() string
95-
TXMethods
93+
Methods
9694
}
9795
```
9896

97+
The `Methods` interface is shared between the `Tx` and `DB` interfaces and will
98+
contain methods to interact with the database when they are generated.
99+
99100
The package comes with some customizable hooks.
100101

101102
```
@@ -144,13 +145,24 @@ read one (
144145
Regenerating the Go code will expand our database interface:
145146

146147
```
147-
type TXMethods interface {
148-
DeleteAll(ctx context.Context) (count int64, err error)
148+
type Methods interface {
149+
Create_User(ctx context.Context,
150+
user_id User_Id_Field,
151+
user_name User_Name_Field) (
152+
user *User, err error)
153+
154+
Delete_User_By_Pk(ctx context.Context,
155+
user_pk User_Pk_Field) (
156+
deleted bool, err error)
149157
150-
Create_User(ctx context.Context, user_id User_Id_Field, user_name User_Name_Field) (user *User, err error)
151-
Delete_User_By_Pk(ctx context.Context, user_pk User_Pk_Field) (deleted bool, err error)
152-
Get_User_By_Pk(ctx context.Context, user_pk User_Pk_Field) (user *User, err error)
153-
Update_User_By_Pk(ctx context.Context, user_pk User_Pk_Field, update UpdateUser) (user *User, err error)
158+
Get_User_By_Pk(ctx context.Context,
159+
user_pk User_Pk_Field) (
160+
user *User, err error)
161+
162+
Update_User_By_Pk(ctx context.Context,
163+
user_pk User_Pk_Field,
164+
update User_Update_Fields) (
165+
user *User, err error)
154166
}
155167
```
156168

@@ -182,12 +194,14 @@ func createUser(ctx context.Context, db *DB) (user *User, err error) {
182194
if err == nil {
183195
err = tx.Commit()
184196
} else {
185-
tx.Rollback() // log this perhaps?
197+
// tx.Rollback() returns an error, perhaps we should log it, or
198+
// do something else? the choice is yours.
199+
tx.Rollback()
186200
}
187201
}()
188202
189203
return tx.Create_User(ctx,
190-
User_Id("some unique id i just generated"),
204+
User_Id("some unique id i just generated"),
191205
User_Name("Donny B. Xavier"))
192206
}
193207
```
@@ -232,13 +246,35 @@ func createUser(ctx context.Context, db *DB) (user *User, err error) {
232246
DBX does not generate this helper for you so that you can have full control
233247
over how you want to handle the error in the Rollback case.
234248

235-
If you added that file with the `WithTx` helper, it's also a great place to add
236-
a `go:generate` directive:
249+
### Dialects
250+
251+
DBX doesn't work with just Postgres, and is designed to be agnostic to many
252+
different database engines. Currently, it supports Postgres and SQLite3. Any
253+
of the above commands can be passed the `--dialect` (or, shorthand `-d`) flag
254+
to specify additional dialects. For example, running
255+
256+
```
257+
dbx.v1 schema -d postgres -d sqlite3 example.dbx .
258+
dbx.v1 golang -d postgres -d sqlite3 example.dbx .
259+
```
260+
261+
will create both `example.dbx.postgres.sql` and `example.dbx.sqlite3.sql` with
262+
the statements required to create the tables, and generate the Go code to
263+
operate with both sqlite3 and postgres.
264+
265+
### Generate
266+
267+
All of these commands are intended to normally be used with `//go:generate`
268+
directives, such as:
237269

238270
```
239-
//go:generate dbx.v1 golang example.dbx .
271+
//go:generate dbx.v1 golang -d postgres -d sqlite3 example.dbx .
272+
//go:generate dbx.v1 schema -d postgres -d sqlite3 example.dbx .
240273
```
241274

275+
A great spot to put them would be in the file that modifies the hooks and adds
276+
other customizations.
277+
242278
## Details
243279

244280
Detailed documentation below. If you notice any difference between the

0 commit comments

Comments
 (0)