Mongoose (MongoDB) → Drizzle ORM (PostgreSQL)
hardMove from MongoDB/Mongoose document model to relational PostgreSQL with Drizzle. Involves rethinking data structure, not just swapping libraries.
List all Mongoose schemas. Identify embedded documents (→ related tables), arrays (→ junction tables), mixed types (→ JSONB columns).
Create Drizzle schema file. Embedded docs → separate tables with FK. Arrays of primitives → separate table or JSONB. Mixed → JSONB.
npm install drizzle-orm postgres && npm install -D drizzle-kit
Use mongoexport to export collections as JSON.
mongoexport --db mydb --collection users --out users.json
Write a Node.js script to read exported JSON and insert into PostgreSQL using Drizzle. Handle nested objects and arrays.
Model.find(filter) → db.select().from(table).where(). Model.create() → db.insert(). Model.updateOne() → db.update().
Recreate Mongoose indexes as PostgreSQL indexes in Drizzle schema. Unique constraints, compound indexes.