drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

Feat: custom select on customTypes

Open Angelelz opened this issue 2 years ago • 9 comments

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 avatar Oct 26 '23 05:10 Angelelz

LGTM!

sangrepura avatar Jan 15 '24 00:01 sangrepura

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

fvaldes33 avatar Jan 17 '24 22:01 fvaldes33

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

mauriciabad avatar Jan 18 '24 15:01 mauriciabad

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

hauserkristof avatar Jan 22 '24 22:01 hauserkristof

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

mauriciabad avatar Jan 24 '24 16:01 mauriciabad

@Angelelz any chance you can marge this?

bernatfortet avatar Mar 12 '24 01:03 bernatfortet

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

Angelelz avatar Mar 12 '24 02:03 Angelelz

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.

balloman avatar Apr 27 '24 09:04 balloman

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.

dankochetov avatar May 09 '24 12:05 dankochetov