Optional column names in drizzle schema #2834
AndriiSherman
started this conversation in
Ideas
Replies: 2 comments 4 replies
-
Is it possible to do the same for table names? const users = pgTable('users', {
id: integer().primaryKey(),
firstName: varchar(),
admin: boolean(),
// you can still have custom names for you columns
lastName: varchar('last_name', { length: 50 }),
});
// INSTEAD no table name here
const users = pgTable({
id: integer().primaryKey(),
firstName: varchar(),
admin: boolean(),
// you can still have custom names for you columns
lastName: varchar('last_name', { length: 50 }),
}); |
Beta Was this translation helpful? Give feedback.
3 replies
-
I think this would be a great addition! As an extension to this, once the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are making some changes to the schema API and making database column names optional, so your schema can look like this
With all these changes, we decided to introduce another feature and make it possible to have camelCase to snake_case conversions for your queries. For example, you have this schema in your database:
With a config specified at the database connection level, you would have the ability to specify a conversion strategy for Drizzle
In this case, you can define your Drizzle schema like this, and it will automatically translate all camelCase columns in code to snake_case columns in the database
This discussion is created to understand the best strategy for camelCase to snake_case mapping. There may be some cases we haven't handled yet but should address before releasing.
We have checked other libraries and ORMs to see what strategies they are using, and here's what we've found. Almost all of them, except for objection.js, have the same mapping strategy. We plan to use the same approach as Knex, Prisma, Kysely, changecase, and Scule.
Here are the results for all of them. If you find any other cases, please leave a comment so we can add them to our test cases
For
drizzle-kit introspect
, which can now be used asdrizzle-kit pull
by default, we will check if the database column name and the TypeScript-generated keys are the same. If they are, we will remove the database name from the column definition to make the pulled schema easier to read.We will also introduce a new flag to
drizzle.config.ts
:casing: 'camelCase'
. With this flag, all database snake_case columns will be automatically mapped to camelCase, as is currently done. The database name will be removed from the pulled schema, allowing you to useconst db = drizzle(client, { casing: 'camelCase' })
after introspection.Beta Was this translation helpful? Give feedback.
All reactions