1
1
# dbtpl
2
2
3
- ` dbtpl ` is a command-line tool to generate _ idiomatic_ code for different
4
- languages code based on a database schema or a custom query.
3
+ ` dbtpl ` is a command-line tool to inspect and generate templated code based on
4
+ a database schema or a custom database query.
5
+
6
+ In addition to being able to generate standardized "model" code for a database,
7
+ ` dbtpl ` is also capable of creating schema creation scripts for a database,
8
+ generating JSON/YAML definitions, and [ Graphviz] ( https://graphviz.org ) diagrams
9
+ for schemas.
5
10
6
11
<p align =" center " >
7
12
<a href =" #installing " title =" Installing " >Installing</a > |
@@ -20,27 +25,27 @@ languages code based on a database schema or a custom query.
20
25
21
26
#### Supported languages
22
27
23
- At the moment, ` dbtpl ` only supports [ Go] ( https://golang.org ) . Support for other
24
- languages will come soon .
28
+ At the moment, ` dbtpl ` only supports [ Go] ( https://golang.org ) . Support for
29
+ other languages is possible, but not currently planned .
25
30
26
31
#### How it works
27
32
28
33
In schema mode, ` dbtpl ` connects to your database and generates code using Go
29
- templates. ` dbtpl ` works by using database metadata and SQL introspection queries to
30
- discover the types and relationships contained within a schema, and applying a
31
- standard set of base (or customized) Go [ templates] ( templates ) against the
32
- discovered relationships.
34
+ templates. ` dbtpl ` works by using database metadata and SQL introspection
35
+ queries to discover the types and relationships contained within a schema, and
36
+ applying a standard set of base (or customized) Go [ templates] ( templates )
37
+ against the discovered relationships.
33
38
34
39
Currently, ` dbtpl ` can generate types for tables, enums, stored procedures, and
35
40
custom SQL queries for PostgreSQL, MySQL, Oracle, Microsoft SQL Server, and
36
41
SQLite3 databases.
37
42
38
- > ** Note:** While the code generated by dbtpl is production quality, it is not the
39
- > goal, nor the intention for dbtpl to be a "silver bullet," nor to completely
40
- > eliminate the manual authoring of SQL / Go code.
43
+ > ** Note:** While the code generated by dbtpl is production quality, it is not
44
+ > the goal, nor the intention for dbtpl to be a "silver bullet," nor to
45
+ > completely eliminate the manual authoring of SQL / Go code.
41
46
42
- In query mode, ` dbtpl ` parses your query to generate code from Go templates.
43
- It finds related tables in your database to ensure type safety.
47
+ In query mode, ` dbtpl ` parses your query to generate code from Go templates. It
48
+ finds related tables in your database to ensure type safety.
44
49
45
50
## Database Feature Support
46
51
@@ -77,7 +82,7 @@ Scoop][] or [via Go][]:
77
82
78
83
### Installing via Homebrew (macOS and Linux)
79
84
80
- Install ` dbtpl ` from the [ ` xo/dbtpl ` tap] [ xo-tap ] in the usual way with the [ ` brew `
85
+ Install ` dbtpl ` from the [ ` xo/xo ` tap] [ xo-tap ] in the usual way with the [ ` brew `
81
86
command] [ homebrew ] :
82
87
83
88
``` sh
@@ -387,26 +392,15 @@ templates can be found in [templates/types.go](templates/types.go)
387
392
Each language, has its own set of templates for ` $TYPE ` and are
388
393
available in the [templates/](templates).
389
394
390
- | Template File | [Type](templates/types.go) | Description |
391
- | :----------------------------: | :------------------------: | -------------------------------------------------------------------------------------- |
392
- | hdr.dbtpl.\* .tpl | | Base template. Executed with content for a template. |
393
- | db.dbtpl.\* .tpl | | Package level template with base types and interface data. Generated once per package. |
394
- | schema/enum.dbtpl.\* .tpl | Enum | Template for schema enum type definitions. Generates types and related methods. |
395
- | schema/foreignkey.dbtpl.\* .tpl | ForeignKey | Template for foreign key relationships. Generates related method. |
396
- | schema/index.dbtpl.\* .tpl | Index | Template for schema indexes. Generates related method. |
397
- | schema/proc.dbtpl.\* .tpl | Proc | Template to generate functions to call defined stored procedures in the db. |
398
- | schema/typedef.dbtpl.\* .tpl | Type | Template for schema table/views. |
399
- | query/custom.dbtpl.\* .tpl | Query | Template for custom query execution. |
400
- | query/typedef.dbtpl.\* .tpl | Type | Template for custom query' s generated type. |
401
-
402
- For example, Go has [`templates/gotpl/schema/foreignkey.dbtpl.go.tpl`](templates/gotpl/schema/foreignkey.dbtpl.go.tpl)
403
- which defines the template used by `dbtpl` for generating a function to get the
404
- foreign key type in Go. The templates are designed to be Database agnostic, so
405
- they are used for both PostgreSQL and Microsoft SQL the same, and all other
406
- supported database types. The template is passed a different instance of
407
- `templates.ForeignKey` instance (for each foreign key in a table). To get the
408
- `Name` field in from `ForeignKey`, the template can use ` {{ .Data.Name }}`, or
409
- any other field similarly.
395
+ | Template File | Description |
396
+ | -------------------- | -------------------------------------------------------------------------------------- |
397
+ | ` * .go` | Template logic |
398
+ | ` hdr.dbtpl.* .tpl` | File header template. Executed with content for each generated file. |
399
+ | ` db.dbtpl.* .tpl` | Package level template with base types and interface data. Generated once per package. |
400
+ | ` query.dbtpl.* .tpl` | Template for custom query execution. |
401
+ | ` schema.dbtpl.* .tpl` | Template for custom query' s generated type. |
402
+
403
+ _\*_ - is the template type, for example `go`, `json`, `yaml`, etc.
410
404
411
405
## Examples
412
406
@@ -458,9 +452,9 @@ application logic but by `dbtpl` by passing the `--exclude` or `-e` flag:
458
452
459
453
```sh
460
454
# Ignore special fields
461
- $ xo schema postgres://user:pass@host/db -e users.created_at -e users.modified_at
455
+ $ dbtpl schema postgres://user:pass@host/db -e users.created_at -e users.modified_at
462
456
# or, To ignore these fields in all tables
463
- $ xo schema postgres://user:pass@host/db -e *.created_at -e *.modified_at
457
+ $ dbtpl schema postgres://user:pass@host/db -e *.created_at -e *.modified_at
464
458
```
465
459
466
460
### Example: Custom Template -- adding a `GetMostRecent` lookup for all tables (Go)
@@ -480,7 +474,7 @@ First, we dump the base `dbtpl` Go template:
480
474
```sh
481
475
$ mkdir -p my-tpl
482
476
483
- $ xo dump my-tpl
477
+ $ dbtpl dump my-tpl
484
478
```
485
479
486
480
We can now modify the templates to suit our specific schema, adding lookups,
@@ -532,7 +526,7 @@ We can then use the templates in conjunction with `dbtpl` to generate our "model
532
526
code:
533
527
534
528
```sh
535
- $ xo schema postgres://user:pass@localhost/dbname --src templates/
529
+ $ dbtpl schema postgres://user:pass@localhost/dbname --src templates/
536
530
```
537
531
538
532
There will now be a `GetMostRecentUsers` func defined in `models/user.dbtpl.go`,
@@ -575,7 +569,7 @@ escaped using any of the `--escape-*` options, you must pass the `sql_mode=ansi`
575
569
option to the MySQL driver:
576
570
577
571
` ` ` sh
578
- $ xo --escape-all ' mysql://user:pass@host/?parseTime=true&sql_mode=ansi' -o models
572
+ $ dbtpl --escape-all ' mysql://user:pass@host/?parseTime=true&sql_mode=ansi' -o models
579
573
` ` `
580
574
581
575
And when opening a database connection:
@@ -588,7 +582,7 @@ Additionally, when working with date/time column types in MySQL, one should
588
582
pass the ` parseTime=true` option to the MySQL driver:
589
583
590
584
` ` ` sh
591
- $ xo schema ' mysql://user:pass@host/dbname?parseTime=true' -o models
585
+ $ dbtpl schema ' mysql://user:pass@host/dbname?parseTime=true' -o models
592
586
` ` `
593
587
594
588
And when opening a database connection:
@@ -603,7 +597,7 @@ While not required, one should specify the `loc=auto` option when using `dbtpl`
603
597
with a SQLite3 database:
604
598
605
599
` ` ` sh
606
- $ xo schema ' file:mydatabase.sqlite3?loc=auto' -o models
600
+ $ dbtpl schema ' file:mydatabase.sqlite3?loc=auto' -o models
607
601
` ` `
608
602
609
603
And when opening a database connection:
@@ -667,13 +661,13 @@ table:
667
661
668
662
`ALWAYS GENERATED` types will be parsed as Auto PK types for Oracle.
669
663
670
- ## About xo : Design, Origin, Philosophy, and History
664
+ ## About dbtpl : Design, Origin, Philosophy, and History
671
665
672
666
`dbtpl` can likely get you 99% "of the way there" on medium or large database
673
667
schemas and 100% of the way there for small or trivial database schemas. In
674
- short, xo is a great launching point for developing standardized packages for
675
- standard database abstractions/relationships, and `dbtpl`' s most common use-case is
676
- indeed in a code generation pipeline, ala ` stringer` .
668
+ short, `dbtpl` is a great launching point for developing standardized packages
669
+ for standard database abstractions/relationships, and `dbtpl`' s most common
670
+ use-case is indeed in a code generation pipeline, ala ` stringer` .
677
671
678
672
### Design
679
673
@@ -714,19 +708,24 @@ generate Go code. Additionally, at this time, but tangential to the story, the
714
708
API definitions were ported from JSON to Protobuf to make use of its code
715
709
generation abilities as well.
716
710
717
- ` dbtpl` is the open source version of that code generation tool, and is the fruits
718
- of those development efforts. It is hoped that others will be able to use and
719
- expand ` dbtpl` to support other databases -- SQL or otherwise -- and that ` dbtpl` can
720
- become a common tool in any Go developer' s toolbox.
711
+ ` dbtpl` is the open source version of that code generation tool, and is the
712
+ fruits of those development efforts. It is hoped that others will be able to
713
+ use and expand ` dbtpl` to support other databases -- SQL or otherwise -- and
714
+ that ` dbtpl` can become a common tool in any Go developer' s toolbox.
715
+
716
+ In May of 2025, the project was renamed from `xo` to `dbtpl` to more readily
717
+ convey the tool' s purpose.
721
718
722
719
# ## Goals
723
720
724
- Part of `dbtpl`' s goals is to avoid writing an ORM, or an ORM-like in Go, and to
725
- instead generate static, type-safe, fast, and idiomatic Go code across
726
- languages and databases. Additionally, the ` dbtpl` developers are of the opinion
727
- that relational databases should have proper, well-designed relationships and
728
- all the related definitions should reside within the database schema itself:
729
- ie, a " self-documenting" schema. ` dbtpl` is an end to that pursuit.
721
+ Part of ` dbtpl` ' s goals is to avoid writing an ORM, or an ORM-like in Go, and
722
+ to instead generate static, type-safe, fast, and idiomatic Go code across
723
+ languages and databases.
724
+
725
+ Additionally, the `dbtpl` developers are of the opinion that relational
726
+ databases should have proper, well-designed relationships and all the related
727
+ definitions should reside within the database schema itself: ie, a
728
+ "self-documenting" schema. `dbtpl` is an end to that pursuit.
730
729
731
730
## Related Projects
732
731
@@ -737,7 +736,7 @@ ie, a "self-documenting" schema. `dbtpl` is an end to that pursuit.
737
736
738
737
### Other Projects
739
738
740
- The following projects work with similar concepts as xo :
739
+ The following projects work with similar concepts as `dbtpl` :
741
740
742
741
#### Go Generators
743
742
0 commit comments