Add support for Drizzle ORM schema parsing
Problem
Liam ERD currently lacks support for Drizzle ORM, a popular TypeScript-first ORM for SQL databases. This limitation prevents users who use Drizzle in their projects from easily creating ER diagrams from their schema files.
- ref: https://github.com/liam-hq/liam/discussions/364
Current Behavior
Currently, Liam ERD supports PostgreSQL, Rails schema.rb, Prisma, and tbls formats, but has no native Drizzle support. Users with Drizzle schemas have to resort to workarounds like using pg_dump or tbls as intermediaries, which adds unnecessary complexity.
Proposed Solution
Add native support for Drizzle schema files by implementing a new parser in the db-structure package. The parser would extract database structure information from Drizzle schema files and convert it to Liam's internal DBStructure format.
Technical Considerations
-
Schema Extraction Method:
- Similar to how we use DMMF for Prisma schemas, we can use existing Drizzle tooling like
drizzle-kitordrizzle-dbml-generatorto understand schema structure. - Alternatively, we could analyze the TypeScript AST directly using the TypeScript compiler API, as Drizzle schemas are defined in TypeScript.
- Similar to how we use DMMF for Prisma schemas, we can use existing Drizzle tooling like
-
Integration with Existing Infrastructure:
- Update the
SupportedFormattype to include 'drizzle' - Create a new processor function in
frontend/packages/db-structure/src/parser/drizzle/ - Update the format detection logic to recognize .ts files with Drizzle schema patterns
- Update the
-
Runtime vs Static Analysis:
- Static analysis would be consistent with our existing approach for other parsers
- We should avoid runtime execution of schema files, following the principles established in ADR 20241206
Implementation Details
- Add ADR(Architecture Decision Records)
Please refer to the following as you proceed:
- https://github.com/liam-hq/liam/blob/932562daf69a2c7cf3d535b8381b56a604e3a279/frontend/apps/docs/content/docs/contributing/adr/20241206-node-js-based-unified-db-schema-parsing.mdx
- https://github.com/liam-hq/liam/blob/932562daf69a2c7cf3d535b8381b56a604e3a279/frontend/apps/docs/content/docs/contributing/adr/20250116-use-dmmf-for-prisma-schema-parsing.mdx
-
Create a new Drizzle parser module:
frontend/packages/db-structure/src/parser/drizzle/ ├── index.ts # Exports the processor function ├── parser.ts # Contains the main parsing logic ├── converter.ts # Converts Drizzle schema to DBStructure └── types.ts # Drizzle-specific types -
Update format detection:
- Add 'drizzle' to
supportedFormatSchemainsupportedFormat/schema.ts - Update
detectFormat.tsto detect Drizzle schema files (looking for typical Drizzle import patterns)
- Add 'drizzle' to
-
Update the main parser switch in
index.ts:export const parse = ( str: string, format: SupportedFormat, ): Promise<ProcessResult> => { switch (format) { // ... existing cases case 'drizzle': return drizzleProcessor(str) } } -
Update documentation to include Drizzle in the supported formats list
Related Files
- https://github.com/liam-hq/liam/blob/main/frontend/packages/db-structure/src/parser/index.ts
- https://github.com/liam-hq/liam/blob/main/frontend/packages/db-structure/src/parser/supportedFormat/schema.ts
- https://github.com/liam-hq/liam/blob/main/frontend/packages/db-structure/src/parser/supportedFormat/detectFormat.ts
- https://github.com/liam-hq/liam/blob/main/frontend/apps/docs/content/docs/parser/supported-formats/index.mdx
- https://github.com/liam-hq/liam/blob/main/frontend/apps/docs/content/docs/parser/supported-formats/drizzle.mdx
Hi @prakha, I want to entrust this issue to you! Let me know if you have any questions.
is there any news about this ✨👀
@MH4GF i will start working on this from Thursday.
oooh cool, i really appreciate your amazing work, really one of the best open source projects ever and i'm so proud of the team and i choose to use it in our startup and when its done i'm mentioning liam for sure
also two questions do you save the positions of the tables is there any plans for making schema from liam or is it just a visualization layer or like queries
thank you so much again
@YoussefRabeiii Thank you! I'd love to try it out and get your feedback.
is there any plans for making schema from liam or is it just a visualization layer or like queries
With those two, this one is in development. We're looking to visually extend the existing schema and take out the migration code! We'd love for you to share your use cases and interests with us! Comment on the issues and roadmap below.
- ref: https://github.com/liam-hq/liam/issues/510
- ref: https://github.com/orgs/liam-hq/projects/1/views/1
hi, is there any updates related to this one, @prakha , @MH4GF
hi, is there any updates related to this one, @prakha , @MH4GF
@prakha Have you started to respond yet?
@Youssefrabei-dev Are you also interested in implementing this issue?
No I will start working on this as soon as possible .
@MH4GF yes i'm very interested in this feature and the liam to drizzle too but the visualizer is a key feature in our startup and i proposed we use liam and mention your product in our tech stack and maybe we could go abit further and collab togther in the near future hopefully
@MH4GF @prakha sorry to bother you but any updates on this
@Youssefrabei-dev Thanks for the multiple reminders. @prakha seems to be busy. I would love for you to work on it!
hi any updates on this even if its gonna be postponed or something like that and where can we know whats new in liam as in other features or progress in general
@Youssefrabei-dev Release notes or X for the latest information.
- https://github.com/liam-hq/liam/releases/tag/%40liam-hq%2Fcli%400.5.7
- https://x.com/liam_app
Hi, @Youssefrabei-dev
Just wanted to share a quick update — we’ve added support for Drizzle (PostgreSQL) in the CLI as of @liam-hq/[email protected] 🎉
This is still an experimental release, so there are a few limitations (e.g., multi-schema support is not yet available), but we’d love for you to try it out in your project and let us know if you run into anything or have feedback!
@FunamaYukina hey i just tried out the cli with drizzle and first of all AMAZING WORK for real, but with our schema and it works but there is a weird behavior which is the references uses the table sql name and not the typescript name and this is not the desired way of behaving imo
@Youssefrabei-dev Thank you for the feedback and glad you're enjoying the CLI! 🎉 You're absolutely right about this issue. Currently, the CLI is incorrectly using the SQL table name instead of the TypeScript variable name in references. For example:
// TypeScript name: users
// SQL name: user_accounts
export const users = pgTable('user_accounts', {
id: serial('id').primaryKey(),
name: text('name')
});
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
// Currently generating: user_accounts.id
// Should be: users.id
authorId: integer('author_id').references(() => users.id)
});
We’re planning to fix this to make sure TypeScript variable names are used properly in references. Thanks for pointing it out!
@Youssefrabei-dev cc @FunamaYukina
Thanks for the feedback 😄
Liam ERD implements all parsers with the policy of displaying the actual SQL table names. For example, in Ruby on Rails, the User class corresponds to the users table, so Liam ERD displays users.
This is a decision made because there are more use cases for SQL table names than implementation names; ER-Diagrams are often intended for sharing and are also viewed by non-engineers, PMs and data analysts are not aware of the Drizzle implementation and should be doing their own work with the SQL table names.
If you have any comments on this policy, we would love to hear them, and there may be an option to annex the name on the implementation in the TableDetail.
Oh i didnt mean the display name I meant the type safety when referencing a foreign key One of the amazing benefits of using drizzle is that the database layer is fully typesafe and i wanted that type saftey with liam too since the main file schema.ts is but i get the PM uses too and the others uses liam erd not just coders
@Youssefrabei-dev cc @FunamaYukina Oh! That may have been my misconception. My apologies. So maybe there is a mistake in the information displayed? May I ask for more information on the situation?
Yes i made a quick demo to test it out with different scenarios do we discuss it here or like on discord or a quick google meet call
@Youssefrabei-dev Thank you! I’d be happy to discuss it here, if that works for you. 🌟
so after i wrote here the test schema with 3 scenarios i just tested them and now all of the "correct" ones worked and added 2 more tests and another one worked
so as in now
- referencing the typescript variable works
- referencing the sql name works (but its case-sensitive) but not type safe and the linter will tell its not valid but liam displays it just fine but i drizzle wont work with it since it must be a defined variable (same or different name from the sql but it has to be a js variable)
so amazing work for real and will try and deploy it to our staging env rightnow didnt get to this part before so will let you know how it goes and if there is any feedback
@Youssefrabei-dev I see! So for example, even writing styles like the one below that wouldn't work in Drizzle ORM itself are still functioning in Liam ERD?
// ⚠️ Problematic approach - SQL name string reference
const posts = pgTable('posts', {
userId: integer('user_id').references(() => "users.id") // string reference
});
That's really helpful to understand the current behavior. It sounds like the parser is quite flexible in what it can interpret for ERD display purposes, even if some patterns wouldn't be valid for actual Drizzle usage.
Thank you for clarifying this! Looking forward to hearing how the staging deployment goes.😄
and yeah the "string reference" wont work in drizzle the linter itself wont allow it also wont work in liam if its string and i meant the sqlName.id not as string its as a undefined variable it wont allow it but liam will parse it correctly even when drizzle and typescript dont like it
so string reference wont work in liam or drizzle variable reference (table sql name) wont work in drizzle but work in liam correct variable reference will work in both
and the deployment took abit of time i would love if there was a quick small guide on how to deploy it cause tbh i didnt vite directly in many years so it was a bit chaotic imo
and you intend to support multi file schema soon i had to literally copy all of our schema into one file for now (drizzle have their way to make it like a bit schema variable to use it in their client so maybe it would be smart to parse that so it handle multiple file schema)
and also is it planned to save the tables places (drizzle visualization vscode extension do it in json i think) would be cool to have our schema position saved since it would make it easier to remember
@Youssefrabei-dev
Thanks so much for the detailed feedback🌟. I really appreciate the time you took to share your thoughts. Let me go through each point:
so string reference wont work in liam or drizzle variable reference (table sql name) wont work in drizzle but work in liam correct variable reference will work in both
Thanks for breaking this down 🙌 that really helped me understand how Liam’s parser handles things differently from Drizzle’s validation logic, especially when it comes to string references vs. variable references.
and the deployment took abit of time i would love if there was a quick small guide on how to deploy it cause tbh i didnt vite directly in many years so it was a bit chaotic imo
We primarily designed our deployment documentation for CI/CD pipelines. You can find it at https://liambx.com/docs/cli/ci-cd, though it might not cover the direct Vite deployment scenario you're looking for.
and you intend to support multi file schema soon i had to literally copy all of our schema into one file for now (drizzle have their way to make it like a bit schema variable to use it in their client so maybe it would be smart to parse that so it handle multiple file schema)
Yeah, having to merge everything into one file isn't ideal. I get that. Supporting multi-file schema structures (like Drizzle's schema object approach) is definitely something we want to improve. I’ll bring it up with the team so we can figure out how best to handle this in the future.
and also is it planned to save the tables places (drizzle visualization vscode extension do it in json i think) would be cool to have our schema position saved since it would make it easier to remember
There is already a discussion about this feature, and we agree it's something we need to consider for the future. Being able to persist the ERD layout would definitely improve the user experience.
- https://github.com/liam-hq/liam/discussions/784
Thanks again for your thoughtful feedback and for giving all of this a try. It really helps us understand how people are using Liam in real projects, and what we should focus on next!
hey just checkin is there any news regarding liam and drizzle or the features we talked about