10000 Raw SQL shortcuts similar to get, take, takeOptional · Issue #369 · romeerez/orchid-orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Raw SQL shortcuts similar to get, take, takeOptional #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
IlyaSemenov opened this issue Aug 19, 2024 · 1 comment
Open

Raw SQL shortcuts similar to get, take, takeOptional #369

IlyaSemenov opened this issue Aug 19, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@IlyaSemenov
Copy link
Contributor
IlyaSemenov commented Aug 19, 2024

As a developer, sometimes I need to fetch a single value with raw SQL, such as the next value in sequence. Currently, the proposed way is:

const walletId = (await db.$query<{ wallet_id: number }>`select nextval('wallet_id_seq') as wallet_id`).rows[0].wallet_id

This works, but is somewhat ugly.

Another approach is abusing db.table.get:

const walletId = await db.user.get(sql<number>`nextval('wallet_id_seq')`)

This also works, and much simpler to read, however it's coupled to db.user (or some other table) for no reason at all. (Also, it needs at least one record in the respective table).

I was thinking, what if there was not only db.$query but also db.$get and possibly db.$take / db.$takeOptional? Or perhaps some common raw SQL wrapper with "usual" ORM methods like db.$raw.select/get/take/...(something else?)...?

@romeerez romeerez added the enhancement New feature or request label Sep 29, 2024
@romeerez
Copy link
Owner
romeerez commented Sep 29, 2024

This needs some thinking about what to add, how to add, what to depreciate and remove, I'd like to postpone this for later, since it's already doable just somewhat messy.

Ideally:

  • get for a single value, throws when not found, and I'm still considering renaming it to getOrThrow for explicitness
  • pluck for a flat array of values
  • exec for no result
  • etc.
  • transform if you wish to transform the value in a callback. No problem to map the result, but the chained transform may be a bit more declarative.
  • type to use a column type for both typing and parsing, so decimal is parsed in a way how it's configured (Decimal lib in your case).

This all should be taken into account, ideally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
0