Prisma & Drizzle lunch & learn
Swagger does not works with types, so it's not included in this project. You can always create models manually or use a generator to create models from the types/schema
- Docker compose with mysql database
- Bruno schema for the API
bruno.json - Table User, UserInfo, Role & Permission (all types of relationships)
- Logger to log time spent on each request
- Exception filter with custom error response
- Custom decorators for validation with valibot schemas
- Inherit types from schemas
- Satifies schemas types with DB types
- Auto logging of queries
- Seeder to seed the database with data
- Migrations
- CRUD operations
- Transactions
findAll() - Raw query
findAllWithMaturity() - Cte query
findAllWithPermissionsCount() - Exclude fields from response
findOne() - Add fields to response
findOne() - "Cascade" delete
delete()
- Automatically omit password field from User
- Generic search in
findAll()with schemas validationPrismaSearchQuerySchema- Includes pagination
- Includes sorting (keys are not validated)
- Includes search (not validated JSON string, follow prisma where clause and must be encoded)
- Omit password field from User
findAll()only include paginations, too hard to make it work with generic search/sorting, probably feasible with a lot of work- Both core and relational queries
- Documentation
npx prisma formatwill format the schema filenpx prisma studiowill open the studio to see the data in the databasenpx prisma db seedwill seed the database with the data in the seed file specified in package.jsonnpx prisma generatewill generate the client innode_modules/@prisma/clientand can be imported in the code for type safetynpx prisma migrate devwill create a migration and apply it to the database, you pass the--nameflag to give a name to the migration (or it will be prompted). Will also generate the clientnpx prisma migrate deploywill apply the migration to the database without creating a migration file (qa, staging, production)npx prisma migrate resetwill reset the database, apply all migrations and seed datanpx prisma db pushwill apply the schema to the database without migration, useful for prototyping locally before using migrations docs- No automatic down migrations. How to do down migrations docs
- Lots of community generators here
- Really mature with lots of feature/documentation, but some keys one are still mising (like cte queries)
- Hard to do stuffs outside of the "basic" workflow (need to use raw queries)
- Really easy to do simple "CRUD" operations
- Schema definition is simple but lacks "raw" sql options
- Documentation
npx drizzle-kit studiowill open the studio to see the data in the database- No build-in seed command, use .ts file to seed the database (
npm run drizzle:seedin this project) npx drizzle-kit generatewill generate migrations based on the schema filenpx drizzle-kit migratewill apply the migrations to the databasenpx drizzle-kit dropwill drop the selected migration (CLI prompt to select)npx drizzle-kit pushwill apply the schema to the database without migration, useful for prototyping locally before using migrations- There is not reset command
- There is no down migrations but it is in the work
- Drizzle has a config file
drizzle.config.tsto configure some options
- A plugin exist to create validation schemas from the database schema, but the one for valibot is not up to date pr
- Migrations are not the best, clearly designed for
pushworkflow (for exemple, if a migration fails midway, you need to manually revert and apply the migrations), update is in the work - Schema definitions is verbose but similar to SQL
- Core queries (sql likes)
- More control over the queries, but more verbose
- Easy to do stuff outside of the "basic" workflow
- More verbose to do "CRUD" operations
- Typing is exceptional, but doing generic things while keeping typesafety is harder/impossible
- They're is a way to return information from the query, but not supported in mysql
- Relationnal queries (v2 on the way)
- Only for querying, not for creating/updating
- Schema relations are applications level only, not enforced by the database
- Aggregations are not supported in 'extras' fields for now
- No way to do CTE queries
npm ito install dependencies- Copy the
.env.exampleto.env docker-compose upto start the database- Import the
bruno.jsonfile in Bruno npm run prisma:generateto generate the Prisma clientnpm run prisma:migrate-deployto apply the migrationsnpm run prisma:seedto seed the databasenpm run drizzle:migrateto apply the Drizzle migrationsnpm run drizzle:seedto seed the databasenpm run devto start the server
You can import the bruno file to have api endpoints