Migrating from MongoDB to PostgreSQL: How to handle embedded types/objects?
I'm an intermediate developer working with Next.js, Node.js, and React. I'm currently using Prisma with MongoDB for my project, but I'm considering migrating to PostgreSQL.
One of my biggest challenges is figuring out how to handle embedded types/objects that I use extensively in my MongoDB schema. For example, I have structures like:
`
I also have more complex nested structures like:
I know PostgreSQL doesn't support embedded types like MongoDB does. I'm considering using JSON/JSONB fields, but I'm concerned about:
0. Should normalize everything into separate tables, or use JSON fields?
Any advice on maintaining type safety with TypeScript when working with JSON fields in Prisma?
I have tried prisma generators before, and it's a mess (at least it was for me!). I prefer a "manual" approach, and I don't...clearly see how the workflow would be.
Thanks in advance for any insights! 😃
2 Replies
You selected to wait for the human sages. They'll share their wisdom soon.
Grab some tea while you wait, or check out
#ask-ai
if you'd like a quick chat with the bot anyway!Hey!
For your migration, I'd suggest:
Start with JSON fields for complex nested structures that you rarely query by their internal properties.
For fields that you frequently filter, sort, or join on, I would recommend creating tables and normalize it.
You can define TypeScript interfaces that mirror your JSON structures and consider using a validation library like Zod or Joi for runtime validation.