Skip to content

Commit b44385f

Browse files
committed
Updating README.md
1 parent bc71fef commit b44385f

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

README.md

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# dbtpl
22

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.
510

611
<p align="center">
712
<a href="#installing" title="Installing">Installing</a> |
@@ -20,27 +25,27 @@ languages code based on a database schema or a custom query.
2025

2126
#### Supported languages
2227

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.
2530

2631
#### How it works
2732

2833
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.
3338

3439
Currently, `dbtpl` can generate types for tables, enums, stored procedures, and
3540
custom SQL queries for PostgreSQL, MySQL, Oracle, Microsoft SQL Server, and
3641
SQLite3 databases.
3742

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.
4146
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.
4449

4550
## Database Feature Support
4651

@@ -77,7 +82,7 @@ Scoop][] or [via Go][]:
7782

7883
### Installing via Homebrew (macOS and Linux)
7984

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`
8186
command][homebrew]:
8287

8388
```sh
@@ -387,26 +392,15 @@ templates can be found in [templates/types.go](templates/types.go)
387392
Each language, has its own set of templates for `$TYPE` and are
388393
available in the [templates/](templates).
389394
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.
410404
411405
## Examples
412406
@@ -458,9 +452,9 @@ application logic but by `dbtpl` by passing the `--exclude` or `-e` flag:
458452
459453
```sh
460454
# 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
462456
# 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
464458
```
465459
466460
### Example: Custom Template -- adding a `GetMostRecent` lookup for all tables (Go)
@@ -480,7 +474,7 @@ First, we dump the base `dbtpl` Go template:
480474
```sh
481475
$ mkdir -p my-tpl
482476
483-
$ xo dump my-tpl
477+
$ dbtpl dump my-tpl
484478
```
485479
486480
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
532526
code:
533527
534528
```sh
535-
$ xo schema postgres://user:pass@localhost/dbname --src templates/
529+
$ dbtpl schema postgres://user:pass@localhost/dbname --src templates/
536530
```
537531
538532
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`
575569
option to the MySQL driver:
576570
577571
```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
579573
```
580574
581575
And when opening a database connection:
@@ -588,7 +582,7 @@ Additionally, when working with date/time column types in MySQL, one should
588582
pass the `parseTime=true` option to the MySQL driver:
589583
590584
```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
592586
```
593587
594588
And when opening a database connection:
@@ -603,7 +597,7 @@ While not required, one should specify the `loc=auto` option when using `dbtpl`
603597
with a SQLite3 database:
604598
605599
```sh
606-
$ xo schema 'file:mydatabase.sqlite3?loc=auto' -o models
600+
$ dbtpl schema 'file:mydatabase.sqlite3?loc=auto' -o models
607601
```
608602
609603
And when opening a database connection:
@@ -667,13 +661,13 @@ table:
667661
668662
`ALWAYS GENERATED` types will be parsed as Auto PK types for Oracle.
669663
670-
## About xo: Design, Origin, Philosophy, and History
664+
## About dbtpl: Design, Origin, Philosophy, and History
671665
672666
`dbtpl` can likely get you 99% "of the way there" on medium or large database
673667
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`.
677671
678672
### Design
679673
@@ -714,19 +708,24 @@ generate Go code. Additionally, at this time, but tangential to the story, the
714708
API definitions were ported from JSON to Protobuf to make use of its code
715709
generation abilities as well.
716710
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.
721718
722719
### Goals
723720
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.
730729
731730
## Related Projects
732731
@@ -737,7 +736,7 @@ ie, a "self-documenting" schema. `dbtpl` is an end to that pursuit.
737736
738737
### Other Projects
739738
740-
The following projects work with similar concepts as xo:
739+
The following projects work with similar concepts as `dbtpl`:
741740
742741
#### Go Generators
743742

0 commit comments

Comments
 (0)