Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ go-pg-sqlc-crud generate -schema ./schema.sql -output ./generated/schema.crud.sq
- `-schema`: Path to the PostgreSQL schema dump file (required)
- `-output`: Path where the generated CRUD file should be written (required)

## Example

```bash
# Generate CRUD operations from a schema file
go-pg-sqlc-crud generate -schema ./schema.sql -output ./generated/crud.sql
```
## Try it yourself

1. Clone the repo
2. `cd example`
3. `docker compose up`
4. `pg_dump ...`
5. Run `go-pg-sqlc-crud generate -schema ./schema.sql -output ./generated/crud.sql`
6. Copy the `crud.sql` over to the directory you've bootstrapped `sqlc` into
7. Make sure it's included in `sqlc.yaml`
8. Run `sqlc generate`
9. If this runs and succeeds, then congrats, the tool works
10. Please LMK or create a PR if you find bugs! TY

## How It Works

Expand Down
11 changes: 7 additions & 4 deletions crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"text/template"
)
Expand Down Expand Up @@ -87,6 +88,9 @@ func GenerateCRUD(tree string) (string, error) {
"add": func(a, b int) int {
return a + b
},
"last": func(x int, a interface{}) bool {
return x == reflect.ValueOf(a).Len()-1
},
}
tmpl := template.Must(template.New("crud").Funcs(funcMap).Parse(`
-- {{ .Schema }}.{{ .Name }} CRUD Operations
Expand All @@ -108,7 +112,7 @@ func GenerateCRUD(tree string) (string, error) {
{{- if ne $c "id" }}
{{- if ne $c "created_at" }}
{{- if ne $c "updated_at" }}
{{ $c }},
{{ $c }}{{ if not (last $i $.Columns) }},{{ end }}
{{- end }}
{{- end }}
{{- end }}
Expand All @@ -120,7 +124,7 @@ func GenerateCRUD(tree string) (string, error) {
{{- if ne $c "id" }}
{{- if ne $c "created_at" }}
{{- if ne $c "updated_at" }}
${{ $count }},
${{ $count }}{{ if not (last $i $.Columns) }},{{ end }}
{{- $count = add $count 1 }}
{{- end }}
{{- end }}
Expand All @@ -135,11 +139,10 @@ func GenerateCRUD(tree string) (string, error) {
{{- range $i, $c := .Columns }}
{{- if ne $c "id" }}
{{- if ne $c "created_at" }}
{{ $c }} = ${{ add $i 1 }},
{{ $c }} = ${{ add $i 1 }}{{ if not (last $i $.Columns) }},{{ end }}
{{- end }}
{{- end }}
{{- end }}
updated_at = now()
WHERE id = $1
RETURNING {{ range $i, $c := .Columns }}{{ if $i }}, {{ end }}{{ $c }}{{ end }};

Expand Down
4 changes: 4 additions & 0 deletions example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTGRES_PASSWORD=initexample
POSTGRES_USER=initexample
POSTGRES_HOST=postgres
POSTGRES_DB=initexample
12 changes: 12 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# example

- sets up a sample postgres db in docker with a sample schema and sample data
- just run this with `docker compose up`
- this should simulate an IRL db that you'd want to do this for
- then use the `go-pg-sqlc-crud` tool to generate your CRUD and see how well it did!

## credit

huge thanks to @asuarezaceves: <https://medium.com/@asuarezaceves/initializing-a-postgresql-database-with-a-dataset-using-docker-compose-a-step-by-step-guide-3feebd5b1545>

i'm using the same file structure and schema as described in that article
Loading