feat(db): add memory storage driver

removes the sqlite dependency for tests, also brings back the option for
users to run zero persistence instances of tinyauth.

adds new mapErr fn for sqlc wrapper gen to prevent sql errors from
leaking out of the store implementation.
This commit is contained in:
Scott McKendry
2026-05-04 05:02:27 +12:00
parent 9566105245
commit ef8bbd8c9f
15 changed files with 443 additions and 90 deletions
+21 -3
View File
@@ -449,18 +449,18 @@ func buildBody(m methodInfo) string {
// no repo-typed result → direct return
if len(m.Results) == 0 || m.Results[0].RepoType == "" {
return "\treturn " + call + "\n"
return "\treturn mapErr(" + call + ")\n"
}
r := m.Results[0]
if r.IsSlice {
return fmt.Sprintf(
"\trows, err := %s\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tout := make([]%s, len(rows))\n\tfor i, row := range rows {\n\t\tout[i] = %s(row)\n\t}\n\treturn out, nil\n",
"\trows, err := %s\n\tif err != nil {\n\t\treturn nil, mapErr(err)\n\t}\n\tout := make([]%s, len(rows))\n\tfor i, row := range rows {\n\t\tout[i] = %s(row)\n\t}\n\treturn out, nil\n",
call, r.RepoType, converterFn(r.TypeStr),
)
}
return fmt.Sprintf(
"\tr, err := %s\n\tif err != nil {\n\t\treturn %s{}, err\n\t}\n\treturn %s(r), nil\n",
"\tr, err := %s\n\tif err != nil {\n\t\treturn %s{}, mapErr(err)\n\t}\n\treturn %s(r), nil\n",
call, r.RepoType, converterFn(r.TypeStr),
)
}
@@ -477,6 +477,8 @@ package {{.PkgName}}
import (
"context"
"database/sql"
"errors"
"{{.RepoPkg}}"
)
@@ -491,6 +493,22 @@ func NewStore(q *Queries) repository.Store {
return &Store{q: q}
}
var errMap = []struct {
from error
to error
}{
{sql.ErrNoRows, repository.ErrNotFound},
}
func mapErr(err error) error {
for _, e := range errMap {
if errors.Is(err, e.from) {
return e.to
}
}
return err
}
{{range .ModelTypes -}}
func {{converterFn .}}(v {{.}}) repository.{{.}} {
return repository.{{.}}(v)