-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(drizzle): allow to customize the drizzle schema with before / after init hooks #8196
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See feedback.
459702a
to
fa9b0da
Compare
…e-customize-schema
9ecc20d
to
1568cb3
Compare
1568cb3
to
6082424
Compare
…e-customize-schema
…e-customize-schema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! I'm waiting on a response from drizzle regarding https://github.com/payloadcms/payload/pull/8196/files#diff-90eef5f8f537920f6493e6e12d17395f002df894f6cd37db199b6a2b914b5d48R2-R5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drizzle confirmed that they don't intend to change the internal API we are using in this PR. They also said they can add test coverage from their end to make prevent mistaken breaking changes from their end.
🚀 This is included in version v3.0.0-beta.109 |
Adds abillity to customize the generated Drizzle schema with
beforeSchemaInit
andafterSchemaInit
. Could be useful if you want to preserve the existing database schema / override the generated one with features that aren't supported from the Payload config.Docs:
beforeSchemaInit
Runs before the schema is built. You can use this hook to extend your database structure with tables that won't be managed by Payload.
One use case is preserving your existing database structure when migrating to Payload. By default, Payload drops the current database schema, which may not be desirable in this scenario.
To quickly generate the Drizzle schema from your database you can use Drizzle Introspection
You should get the
schema.ts
file which may look like this:You can import them into your config and append to the schema with the
beforeSchemaInit
hook like this:Make sure Payload doesn't overlap table names with its collections. For example, if you already have a collection with slug "users", you should either change the slug or
dbName
to change the table name for this collection.afterSchemaInit
Runs after the Drizzle schema is built. You can use this hook to modify the schema with features that aren't supported by Payload, or if you want to add a column that you don't want to be in the Payload config.
To extend a table, Payload exposes
extendTable
utillity to the args. You can refer to the Drizzle documentation.The following example adds the
extra_integer_column
column and a composite index oncountry
andcity
columns.