DR
Feat: custom select on customTypes
drizzle-team/drizzle-orm#1423

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

Claim

Total prize pool $30
Total paid $0
Status Pending
Submitted November 11, 2023
Last updated November 11, 2023

Contributors

AN

Angelelz

@Angelelz

100%

Sponsors

SE

Seated

@Seated

$30