Codegen
Gofs can generate the database CRUD functions and sql using go templates. This requires gofs annotations on the struct fields and the struct itself.
Annotations
Lets prepare List
for code generation. The annotation above the struct tell gofs to generate the db helper functions and database create schema for List. The field annotations tell gofs that ID is the primary key and Name is searchable. These fields will use the relational model and be created as fields. All fields can be accessed from the json serialized blob.
//go:generate gofs codegen db sql
type List struct {
ID string `json:"id" gofs:"pk"`
Name string `json:"name" gofs:"searchable"`
Items []string `json:"items"`
}
Running codegen will create files with the suffix _generated
, specifically /internal/app/list/list_db_generated.go
with helper functions for the database CRUD operations, and /internal/db/migrations/lists_generated.sql
. In the mytodo example, once these generated files are created, you will need to remove the hand written code added in the persistence section.
mytodo
|--internal
| |--app
| | |--list
| | |--list.go
| | |--list_db_generated.go
| |--db
| |--migrations
| |--lists_generated.sql
Gofs templates
Gofs templates can be found in the .gofs/templates
folder. You can modify these templates to suit your needs. You can also add new templates and use them to generate your app specific code.