Skip to content

Feat: custom select on customTypes#1423

Open
Angelelz wants to merge 12 commits into
drizzle-team:mainfrom
Angelelz:feat-custom-select
Open

Feat: custom select on customTypes#1423
Angelelz wants to merge 12 commits into
drizzle-team:mainfrom
Angelelz:feat-custom-select

Conversation

@Angelelz

@Angelelz Angelelz commented Oct 26, 2023

Copy link
Copy Markdown
Contributor

This PR will close #554 and will close #1083.

I have added a new API for customTypes that allows wrapping the column name with custom sql. The syntax is the following:

const getAsLower = customType<{
  data: string;
}>({
  dataType: () => "text",
  customSelect: (column) => sql`lower(${column})`
});

You can then use the customType as follows:

export const users = mysqlTable("users", {
  id: int("id").autoincrement().primaryKey(),
  name: text("name").notNull(),
  lowercase: getAsLower("lowercase").default(sql`(\`name\`)`),
  managerId: int("manager_id")
    .references((): AnyMySqlColumn => users.id)
    .$default(() => 1),
});

Then, a select like this:

const q = await db.select().from(users);

Will result in the following query:

select `id`, `name`, lower(`lowercase`) as `lowercase`, `manager_id` from `users`

Notice how as opposed to the suggestion in #1083 it's not necessary to define a mapWith or an alias.
By default, the column will be aliased to the column name, and the custom decoder can be defined the usual way with fromDriver.

/claim #554
/claim #1083

@Angelelz Angelelz marked this pull request as draft October 26, 2023 12:55
@Angelelz Angelelz marked this pull request as ready for review October 27, 2023 00:02

@mauriciabad mauriciabad left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1st of all, thanks for your work on this library!

After that said, check out the 2 comments, they may be worth solving before merge, but if you want you can merge and tackle them in a following PR.

Another suggestion I have is to add the pertinent explanations in the documentation, so this feature is discoverable and developers know it exists and how it works. I assume this will be done in a separate PR, but I mention it anyway.

I'm specially interested on this feature because I'm developing a project that will use geospatial data (POINT and more).
And, I'm more than happy to help if needed, although I'm not familiar with Drizzle's codebase, I may still be useful. 😊

Comment thread drizzle-orm/src/mysql-core/columns/custom.ts Outdated
Comment thread drizzle-orm/src/mysql-core/dialect.ts Outdated
@ronnostic

Copy link
Copy Markdown

LGTM!

@fvaldes33

Copy link
Copy Markdown

Getting this merged would be amazing. Working on custom PostGIS types and not having this is a big blocker.

Thanks for the work on this @Angelelz

@mauriciabad

Copy link
Copy Markdown

Same, here. I also use geospatial data and this is needed.

@hauserkristof

Copy link
Copy Markdown

Please merge this! This would be super useful, especially in spatial applications!

@mauriciabad

Copy link
Copy Markdown

@AlexBlokh @AndriiSherman @dankochetov @Angelelz please take a look

@bernatfortet

Copy link
Copy Markdown

@Angelelz any chance you can marge this?

@Angelelz

Copy link
Copy Markdown
Contributor Author

Only core team member can merge PRs. Just upvote and wait for when they have time to review.

@balloman

balloman commented Apr 27, 2024

Copy link
Copy Markdown

Is there any feedback from the drizzle team on this, at least? It seems there is pretty widespread support for this, and It's pretty disheartening from a library to see an issue you encounter has been pending for 6+ months. Will this get resolved? Are the devs uninterested in supporting this feature? Is there something wrong with the code? At least some background would be useful to understand next steps for builders.

@dankochetov

Copy link
Copy Markdown
Contributor

Unfortunately, we don't have enough capacity to look into this currently, but we agree that this is a useful feature and it will be eventually resolved.

@mwanago

mwanago commented Jan 18, 2025

Copy link
Copy Markdown

This feature would be super useful!

@robinsimonklein

Copy link
Copy Markdown

Any news about this ?

@shahzaib-cmyk

Copy link
Copy Markdown

This would have been very helpfull in my current project. Any idea when progress might be made?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Default select for custom types Allow wrapping the column in custom SQL for custom types