Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Kysely with Prisma for database interaction #1168

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open

Conversation

jotjern
Copy link
Member

@jotjern jotjern commented Feb 12, 2025

This pull request replaces Kysely with Prisma. This is done for a couple reasons:

  • Automatic migrations: It currently takes considerable effort to do minor changes to the database structure
  • Improved typescript performance: kysely has poor typescript performance compared to prisma
  • Generated types: currently we define all of our types doubly, both in the types module and in the migrations. This PR still allows deviation from the db schema by extending the generated schema, but they are based on the db schemas.

Ref some points from earlier: #235

  1. No schema splitting
    This is now possible, but I don't see why this is a big issue anyways, having a large file is fine.
  2. No programmatic access to CLI
    We can wrap it, it works just fine
  3. Lambda cold start
    The Prisma team have rewritten the rust part of the codebase, so the large binaries shouldn't be a problem anymore.

This change drastically reduces the number of steps necessary to make minor changes by simply using schema.prisma as source of truth, and then generating migrations and domain object attributes based on those.

Prisma is a very well maintained project, and is in my opinion well suited for monoweb.

The pull request is quite large and ugly because of all of the repository changes, those are most of the changes. The only non-trivial service change is job-listing-service which i refactored because the current solution was very much broken.

Copy link

vercel bot commented Feb 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
dashboard ❌ Failed (Inspect) Feb 12, 2025 7:42pm
1 Skipped Deployment
Name Status Preview Updated (UTC)
web ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 7:42pm

doppler.yaml Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
packages/core/package.json Outdated Show resolved Hide resolved
packages/core/src/modules/company/company-service.ts Outdated Show resolved Hide resolved
packages/core/src/modules/job-listing/job-listing-error.ts Outdated Show resolved Hide resolved
packages/db/eu-north-1-bundle.pem Outdated Show resolved Hide resolved
@henrikskog
Copy link
Contributor

😱

@@ -57,13 +57,13 @@ const Landing: NextPage<{ user: User }> = ({ user }) => {
<TextInput
width="flex-1 mb-2 mx-1"
placeholder="Fornavn"
defaultValue={user.firstName}
defaultValue={user.firstName ?? ""}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questionable, should it not be ?? undefined?

"@types/node": "^22.0.0",
"@vitest/ui": "^1.3.1",
"testcontainers": "^10.13.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

return this.parseExtras(attendance)
}

// Prisma requires distinction between database null and json null, so here we choose database null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use JSDoc comments if above function definition, or move into body for regular comment

return { ...data, extras: data.extras === null ? Prisma.DbNull : data.extras }
}

// Takes an object with unparsed JSON value yearCriteria and returns it with yearCriteria parsed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume you merged this with job-listing-repository?

provider = "zod-prisma-types"
output = "../src/generatedSchemas"

useMultipleFiles = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably prefer to have all the codegen in a single file. It's less files to search through in text editors

Comment on lines +390 to +391
longDescription String @default("")
joinInfo String @default("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make them nullable. Also why is joinInfo a different field? I think it should all just be one MDX blob

import { getProductFixtures } from "./fixtures/product"
import { getProductPaymentProviderFixtures } from "./fixtures/product-payment-provider"

if (process.env.DATABASE_URL === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QOL: Add a if DATABASE_URL is pointing to AWS, then abort

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated schemas should not be commited to version control.


export { createMigrator } from "./migrator"
const logger = getLogger("@dotkomonline/db")
export * as dbSchemas from "./generatedSchemas"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if it's just named schemas. I think it's quite unmistakenable what schemas we're talking about here

Suggested change
export * as dbSchemas from "./generatedSchemas"
export * as schemas from "./generatedSchemas"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants