How to use timestamp#2514
-
OverviewHello i'm new to this lib and looking for a way to use the sql
Current stateHere's my config file: version: "2"sql: - engine: "postgresql"queries: "store/sql/queries.sql"schema: "store/sql/schemas.sql"gen: go: package: "store"out: "store"sql_package: "pgx/v5"emit_json_tags: truejson_tags_case_style: "camel"emit_interface: trueemit_pointers_for_null_types: trueMy Schema CREATETABLEIF NOT EXISTS users ( id SERIALPRIMARY KEY, first_name TEXTNOT NULL, last_name TEXTNOT NULL, user_name TEXTNOT NULL, encrypted_password TEXTNOT NULL, created_at TIMESTAMPNOT NULL DEFAULT NOW() );The output from the generate cmd typeUserstruct{IDint32`json:"id"`FirstNamestring`json:"firstName"`LastNamestring`json:"lastName"`UserNamestring`json:"userName"`EncryptedPasswordstring`json:"encryptedPassword"`CreatedAt pgtype.Timestamp`json:"createdAt"` }What I'm trying to doIn my -- name: AddUser :oneINSERT INTO users (first_name, last_name, user_name, encrypted_password, created_at) VALUES ($1, $2, $3, $4, $5) RETURNING *;Generated code constaddUser=`-- name: AddUser :oneINSERT INTO users(first_name, last_name, user_name, encrypted_password, created_at)VALUES ($1, $2, $3, $4, $5)RETURNING id, first_name, last_name, user_name, encrypted_password, created_at`typeAddUserParamsstruct{FirstNamestring`json:"firstName"`LastNamestring`json:"lastName"`UserNamestring`json:"userName"`EncryptedPasswordstring`json:"encryptedPassword"`CreatedAt pgtype.Timestamp`json:"createdAt"` } func (q*Queries) AddUser(ctx context.Context, argAddUserParams) (User, error){row:=q.db.QueryRow(ctx, addUser, arg.FirstName, arg.LastName, arg.UserName, arg.EncryptedPassword, arg.CreatedAt, ) variUsererr:=row.Scan( &i.ID, &i.FirstName, &i.LastName, &i.UserName, &i.EncryptedPassword, &i.CreatedAt, ) returni, err }How would I call this method from my route handler and pass in the func (c*UserController) Add(w http.ResponseWriter, r*http.Request) error{utils.LogRequest(r) addUserReq:=&models.AddUserRequest{} iferr:=utils.DecodeAndWrite(r, addUserReq); err!=nil{returnfmt.Errorf("%w", err) } newUser, _:=models.NewUser( addUserReq.FirstName, addUserReq.LastName, addUserReq.UserName, addUserReq.Password, ) addedUser, err:=c.store.AddUser(r.Context(), *newUser) fmt.Printf("%+v", newUser) iferr!=nil{utils.WriteJSON(w, http.StatusInternalServerError, err) /* go always errors here because of course, `created_at` is invalid */ } returnutils.WriteJSON(w, http.StatusOK, addedUser) } |
BetaWas this translation helpful?Give feedback.
Replies: 1 comment
-
I'll write up a longer answer tomorrow, but here's playground link showing how to use overrides to get what you're looking for. https://play.sqlc.dev/p/a6dd00c89792892ec6f41d0235a3321075aaa2f10337e38efca4babd713e3c9e |
BetaWas this translation helpful?Give feedback.
I'll write up a longer answer tomorrow, but here's playground link showing how to use overrides to get what you're looking for.
https://play.sqlc.dev/p/a6dd00c89792892ec6f41d0235a3321075aaa2f10337e38efca4babd713e3c9e