-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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(db-postgres, db-sqlite): drizzle schema generation #9953
Conversation
Soooo happy for this! Great work team. |
6eb83ea
to
9ed9bf2
Compare
This is awesome @r1tsuu. I love that this PR is close. QQs:
|
Also - one big thing I've noticed from using Drizzle introspection is that the output doesn't work well when entities have either self or circular relationships. For example, tables with circular foreign key references such as the one below create an error on foreignKey definitions, resulting in an // Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
export const mediaInPayload = payload.table("media", {
ownerId: varchar("owner_id"),
}, (table) => {
return {
mediaOwnerIdUsersIdFk: foreignKey({
columns: [table.ownerId],
foreignColumns: [usersInPayload.id],
name: "media_owner_id_users_id_fk"
}).onDelete("set null"),
}
});
// Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
export const usersInPayload = payload.table("users", {
avatarId: varchar("avatar_id"),
}, (table) => {
return {
usersAvatarIdMediaIdFk: foreignKey({
columns: [table.avatarId],
foreignColumns: [mediaInPayload.id],
name: "users_avatar_id_media_id_fk"
}).onDelete("set null"),
}
}); Issue refs can be found in this Discord thread & these docs. Is this an issue that may pose problems? |
09bb841
to
11483a5
Compare
473ad46
to
0f55ee8
Compare
### What? Abstracts SQL schema building, significantly reducing code duplication for SQLite / Postgres db-sqlite lines count From: ```sh wc -l **/*.ts 62 src/connect.ts 32 src/countDistinct.ts 9 src/createJSONQuery/convertPathToJSONTraversal.ts 86 src/createJSONQuery/index.ts 15 src/defaultSnapshot.ts 6 src/deleteWhere.ts 21 src/dropDatabase.ts 15 src/execute.ts 178 src/index.ts 139 src/init.ts 19 src/insert.ts 19 src/requireDrizzleKit.ts 544 src/schema/build.ts 27 src/schema/createIndex.ts 38 src/schema/getIDColumn.ts 13 src/schema/idToUUID.ts 28 src/schema/setColumnID.ts 787 src/schema/traverseFields.ts 18 src/schema/withDefault.ts 248 src/types.ts 2304 total ``` To: ```sh wc -l **/*.ts 62 src/connect.ts 32 src/countDistinct.ts 9 src/createJSONQuery/convertPathToJSONTraversal.ts 86 src/createJSONQuery/index.ts 15 src/defaultSnapshot.ts 6 src/deleteWhere.ts 21 src/dropDatabase.ts 15 src/execute.ts 180 src/index.ts 39 src/init.ts 19 src/insert.ts 19 src/requireDrizzleKit.ts 149 src/schema/buildDrizzleTable.ts 32 src/schema/setColumnID.ts 258 src/types.ts 942 total ``` Builds abstract schema in shared drizzle package that later gets converted by SQLite/ Postgres specific implementation into drizzle schema. This, apparently can also help here #9953. It should be very trivial to implement a MySQL adapter with this as well.
2ce70a3
to
dba8c96
Compare
Well this give us access to the eq function from drizzle? |
e0273a5
to
b8915be
Compare
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.
Looks good! You will need to add this feature to the docs. Otherwise, I'm ready to approve.
…h of follwing: @r1tsuu custom code gen for drizzle schema @r1tsuu codegen with custom pg schema 8e91f92 @r1tsuu jsonb numeric default values serialize e79be1e @r1tsuu sqlite column to code converter 025e3ff @r1tsuu add prettify and log args 13c0033 @r1tsuu add do not modify by hand b1b4993 @r1tsuu beautify property access output a7f8738 @r1tsuu fix uuid default random SQLite fix columnToCodeConverter regression fix: escape default values properly in generated code revert jest setup
2fceb0f
to
65e1988
Compare
@willviles
If you added them by changing You can mutate as well
We don't use introspection anymore as I said, I included the workaround with
Yes! You can import it from the adapter package directly: import { eq, sql, and } from '@payloadcms/db-postgres/drizzle' |
…ll refactor later other tests 3. re-export geoemtryColumn from payloadcms/db-postgres directly 4. fix SQLite numeric default typescript issue 5. fix: include : AnyPgColumn to generated references if to itself see https://orm.drizzle.team/docs/indexes-constraints#foreign-key
9ca4da6
to
8f3221f
Compare
🚀 This is included in version v3.10.0 |
This PR allows to have full type safety on
payload.drizzle
with a single commandWhich generates TypeScript code with Drizzle declarations based on the current database schema.
Example of generated file with the website template:
https://gist.github.com/r1tsuu/b8687f211b51d9a3a7e78ba41e8fbf03
Video that shows the power:
Screen.Recording.2024-12-13.at.08.44.52.mov
We also now proxy drizzle package the same way we do for Lexical so you don't have to install it (and you shouldn't because you may have version mismatch).
Instead, you can import from Drizzle like this:
Fixes #4318
In the future we can also support types generation for mongoose / raw mongodb results.