Skip to content

Latest commit

 

History

History

README.md

@flowblade/db-sqlserver

Example of a product database using SQL Server, Prisma and Kysely.

Warning See the why Prisma and kysely FAQ.

Quick start

yarn db-recreate
yarn prisma-db-seed

DDL operations requires a SQL Server instance to be running.

docker compose -f ../../docker/sql-edge/compose.yml up

Environment variables

Check the .env file for the environment variables used in this example.

DB_FLOWBLADE_SQLSERVER_JDBC="sqlserver://localhost:1433;database=flowblade;user=sa;password=FlowbladeSADev123;trustServerCertificate=true;encrypt=false"

Yon can create a './env.local' file to override the default values.

FAQ

Why Prisma and Kysely?

While Prisma has official support for Microsoft SQL Server it does not play well in many scenarios. See some context below:

Globally Prisma won't handle a few edges cases with SQL Server. On top of that the performance of the rust based engine won't match the performance of pure-js tedious driver for large resultsets and/or long running apps. This last note seems true for most prisma engines (see also the currently preview driversAdapters alternative). Or Drizzle benchmarks to get an idea of the performance difference. But at the time of writing there isn't yet a pure-js tedious based driver (only Pgsql, sqlite...).

Another set of features where Prisma lacks (as of 5.11):

  • No query cancellation support: see this. Same for Kysely but it can be implemented by running some queries in tedious directly.
  • A query builder. While Prisma start to improve raw queries support for TypedSql having a query builder offers some advantages in term of composition (cte, parametrization...).

What Prisma has:

  • The schema.prisma dsl is a very nice way to design the database. Thanks to generators and/or scripts, it's relatively easy to generate documentation, kysely types and output a DDL file that can be used to by migration tools. It's often a good idea to apply migrations with the SqlServer DacPac from the generated DDL file rather than using prisma.
  • Prisma studio can be helpful for quick data exploration and debugging.
  • Another very interesting way to simplify N+1 issues is the relationJoins preview feature. Something harder to write with a sql builder (lateral joins, json_agg...). Very nice when working with graphql (ie: pothos...) that by design allows to retrieve nested structures. Unfortunately support for SQL server is not as tested as Postgres...

What Kysely has:

  • The query builder is very nice when having to deal with more complex queries. The fact that we can't mix and match queryRaw and QueryBuilder code is amazing in situations where there's no choice.

So The idea is

  • In development: Use Prisma to maintain the schema, the seeds and kysely types generation.
  • In production: Use Kysely with tedious driver and tarn as a connection pooler.

Schema

schema.png

Local scripts

Name Description
yarn codegen Run codegen (prisma generate...)
yarn db-recreate Reset the database
yarn prisma-db-seed Load seeds into database
yarn prisma-db-reset-push Drop and recreate database
yarn prisma-db-push Attempt to apply schema changes to database
yarn prisma-studio Launch prisma studio (ui admin)
yarn prisma-validate Validate schema.prisma
yarn prisma-format Format schema.prisma
yarn lint Check for lint errors
yarn lint --fix Attempt to run linter auto-fix
yarn test-unit Run unit tests
yarn clean Remove all caches