query argument names #57
Replies: 5 comments 6 replies
-
|
Yeah I agree it could be a nice improvement, I'm adding a couple of considerations:
|
Beta Was this translation helpful? Give feedback.
-
|
What about : -- type AccountRow
-- $1 account_id
-- $2 is_verified
select
account.id,
account.name,
account.avatar
from
account
where
account.id = $1 and account.verified = $2-- type AccountRow
-- $1 avatar_url, nullable
-- $2 account_id
update
account
set
avatar = $1
where
id = $2
returning
id,
name,
avatarLeading to something like : pub type AccountRow {
AccountRow(
id: Int,
name: String,
avatar: Option(String),
)
}
pub fn find_account(db, account_id, is_verified) {
let decoder = {
use id <- decode.field(0, decode.int)
use name <- decode.field(1, decode.string)
use avatar <- decode.field(3, decode.optional(decode.string))
decode.success(AccountRow(id:, name:, avatar:))
}
let query = "..."
pog.query(query)
|> pog.parameter(pog.text(account_id))
|> pog.parameter(pog.bool(is_verified))
|> pog.returning(account_decoder())
|> pog.execute(db)
}
pub fn update_account(db, avatar_url, account_id) {
let decoder = {
use id <- decode.field(0, decode.int)
use name <- decode.field(1, decode.string)
use avatar <- decode.field(3, decode.optional(decode.string))
decode.success(AccountRow(id:, name:, avatar:))
}
let query = "..."
pog.query(query)
|> pog.parameter(pog.nullable(avatar_url))
|> pog.parameter(pog.string(account_id))
|> pog.returning(decoder)
|> pog.execute(db)
}This is just food for thoughts, I'd love to help if you believe a Gleam beginner will not screw your code 😅 |
Beta Was this translation helpful? Give feedback.
-
|
For now, I’d like to propose a simple approach for generating comments from SQL statements. |
Beta Was this translation helpful? Give feedback.
-
|
I think a comment-based convention would be great from a developer experience perspective. But I understand it would be a controversial addition. 🙂 Especially when you have several parameters of the same type and similar meanings, for things like i.e. limits & offsets |
Beta Was this translation helpful? Give feedback.
-
|
Also there is https://github.com/daniellionel01/parrot which is similar to Squirrel but use https://sqlc.dev/. I haven't tried but Claude tells me that this is possible: INSERT INTO contacts (
email,
first_name,
last_name,
phone,
company,
title,
stage,
profile_picture_url,
notes,
created_at,
updated_at
)
VALUES (
@email,
@first_name,
@last_name,
@phone,
@company,
@title,
@stage,
@profile_picture_url,
@notes,
NOW(),
NOW()
)
RETURNING
id,
first_name,
last_name,
email,
phone,
company,
title,
stage,
profile_picture_url,
notes,
created_at::timestamp,
updated_at::timestamp;pub type CreateContactRow {
CreateContactRow(
id: Int,
first_name: String,
last_name: String,
email: String,
phone: Option(String),
company: Option(String),
title: Option(String),
stage: String,
profile_picture_url: Option(String),
notes: Option(String),
created_at: Timestamp,
updated_at: Timestamp,
)
}
pub fn create_contact(
db: pog.Connection,
email email: String,
first_name first_name: String,
last_name last_name: String,
phone phone: Option(String),
company company: Option(String),
title title: Option(String),
stage stage: String,
profile_picture_url profile_picture_url: Option(String),
notes notes: Option(String),
) -> Result(pog.Returned(CreateContactRow), pog.QueryError) {
...
}Maybe Squirrel could take a similar approach. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
At the moment query arguments are just called
arg_1,arg_2etc. It would be nice to have a way of renaming them, to improve usability of the generated gleam code.Perhaps we could provide names in a doc comment in the sql?
Beta Was this translation helpful? Give feedback.
All reactions