liam icon indicating copy to clipboard operation
liam copied to clipboard

Add support for Drizzle ORM schema parsing

Open MH4GF opened this issue 9 months ago • 10 comments

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

  1. Schema Extraction Method:

    • Similar to how we use DMMF for Prisma schemas, we can use existing Drizzle tooling like drizzle-kit or drizzle-dbml-generator to understand schema structure.
    • Alternatively, we could analyze the TypeScript AST directly using the TypeScript compiler API, as Drizzle schemas are defined in TypeScript.
  2. Integration with Existing Infrastructure:

    • Update the SupportedFormat type 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
  3. 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

  1. 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
  1. 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
    
  2. Update format detection:

    • Add 'drizzle' to supportedFormatSchema in supportedFormat/schema.ts
    • Update detectFormat.ts to detect Drizzle schema files (looking for typical Drizzle import patterns)
  3. 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)
      }
    }
    
  4. 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

MH4GF avatar Apr 10 '25 07:04 MH4GF

Hi @prakha, I want to entrust this issue to you! Let me know if you have any questions.

MH4GF avatar Apr 10 '25 07:04 MH4GF

is there any news about this ✨👀

YoussefRabeiii avatar Apr 22 '25 13:04 YoussefRabeiii

@MH4GF i will start working on this from Thursday.

prakha avatar Apr 22 '25 13:04 prakha

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 avatar Apr 22 '25 13:04 YoussefRabeiii

@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

MH4GF avatar Apr 23 '25 11:04 MH4GF

hi, is there any updates related to this one, @prakha , @MH4GF

Youssefrabei-dev avatar Apr 30 '25 13:04 Youssefrabei-dev

hi, is there any updates related to this one, @prakha , @MH4GF

Youssefrabei-dev avatar May 05 '25 18:05 Youssefrabei-dev

@prakha Have you started to respond yet?

@Youssefrabei-dev Are you also interested in implementing this issue?

MH4GF avatar May 08 '25 04:05 MH4GF

No I will start working on this as soon as possible .

prakha avatar May 08 '25 05:05 prakha

@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

Youssefrabei-dev avatar May 08 '25 16:05 Youssefrabei-dev

@MH4GF @prakha sorry to bother you but any updates on this

Youssefrabei-dev avatar Jun 01 '25 18:06 Youssefrabei-dev

@Youssefrabei-dev Thanks for the multiple reminders. @prakha seems to be busy. I would love for you to work on it!

MH4GF avatar Jun 02 '25 01:06 MH4GF

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 avatar Jun 22 '25 13:06 Youssefrabei-dev

@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

MH4GF avatar Jun 23 '25 02:06 MH4GF

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 avatar Jul 08 '25 09:07 FunamaYukina

@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 avatar Jul 09 '25 19:07 Youssefrabei-dev

@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!

FunamaYukina avatar Jul 10 '25 01:07 FunamaYukina

@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.

MH4GF avatar Jul 10 '25 02:07 MH4GF

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 avatar Jul 10 '25 08:07 Youssefrabei-dev

@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?

MH4GF avatar Jul 10 '25 08:07 MH4GF

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 avatar Jul 10 '25 08:07 Youssefrabei-dev

@Youssefrabei-dev Thank you! I’d be happy to discuss it here, if that works for you. 🌟

FunamaYukina avatar Jul 10 '25 09:07 FunamaYukina

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)

Youssefrabei-dev avatar Jul 10 '25 13:07 Youssefrabei-dev

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 avatar Jul 10 '25 13:07 Youssefrabei-dev

@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.😄

FunamaYukina avatar Jul 11 '25 02:07 FunamaYukina

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

Youssefrabei-dev avatar Jul 11 '25 22:07 Youssefrabei-dev

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

Youssefrabei-dev avatar Jul 11 '25 22:07 Youssefrabei-dev

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 avatar Jul 11 '25 22:07 Youssefrabei-dev

@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!

FunamaYukina avatar Jul 15 '25 06:07 FunamaYukina

hey just checkin is there any news regarding liam and drizzle or the features we talked about

Youssefrabei-dev avatar Jul 28 '25 06:07 Youssefrabei-dev