Skip to content

Conversation

@Rdeisenroth
Copy link
Collaborator

@Rdeisenroth Rdeisenroth commented Nov 6, 2025

Prisma-Orm migration

This PR aims to improve the dev experience and open the many possibilities that come with an ORM by migrating db-queries to Prisma. At the same time, i will try to optimize the queries a bit for performance.

I personally see this as a prequisite before working on #146, since it allows me to reuse many of my existing authentication utils, and will ensure a conflict-free migration path for existing users.

If you are very opposed to prisma for any reason, please let me know why, and we can surely find an alternative (but i won't do the migration work again ;))

Advantages:

  • database independence (you could swap the sqlite db with any supported db, like postgres with redis cache)
  • typesafe queries (will also go a long way against sql injections and so on)
  • easy db migrations
  • lay the foundation for easy integration with verification libraries such as zod (not part of this PR)
  • many more

Drawbacks:

  • more dependencies, probably larger bundle size

TODOs:

  • add prisma dependency
  • migrate database schema to schema.prisma
  • replace all sql queries with prisma queries
  • test functionality still works
  • provide migration path for existing installations
  • provide option to overwrite the db type out of scope for this PR
  • optimize queries -> further optimisations certainly possible, but optimized enough for this PR
  • update documentation -> since all migration happens automagically, i don't see the relevance in updating docs for now. Maybe in another PR
  • adjust docker image

Additional notes:

I did adjust the typescript-internal names to fit in with the prisma naming conventions, but kept the original names for compatibility purposes. Please let me know if you do not like this.

@github-actions github-actions bot added Feature and removed Feature labels Nov 6, 2025
@C4illin
Copy link
Owner

C4illin commented Nov 6, 2025

Looking nice! Just had a quick look on the phone but feel free to change the names if you want I dont know anything about prisma really

@github-actions github-actions bot added Feature and removed Feature labels Nov 7, 2025
@github-actions github-actions bot added Feature and removed Feature labels Nov 8, 2025
@Rdeisenroth Rdeisenroth marked this pull request as ready for review November 8, 2025 15:58
@Rdeisenroth
Copy link
Collaborator Author

@C4illin i think it's ready for review now. Some notes:

  • The migration process currently uses the existing PRAGMA user_version to determine the state of existing databases. In the future, this migration could be deprecated once most users databases have been migrated, since prisma has it's own migration tracking mechanism for further migrations.
  • We should improve the schema in future MRs by adding validators and more indexes, for example, this is how my user model looks with zod:
/// A user of the application
model User {
  /// The unique identifier for the user
  id            Int            @id @default(autoincrement())
  /// The date and time that the user was created (UTC)
  createdUtc    DateTime       @default(now())
  /// The email address of the user
  /// @zod.string.email()
  email         String?        @unique
  /// The username of the user
  /// @zod.string.min(1).max(255)
  username      String         @unique
  /// The hashed password of the user. If not set, user can only login via external providers
  password      String?
  /// The display name of the user
  /// @zod.string.min(1).max(255)
  displayName   String
  /// The files that the user has uploaded
  files         File[]
  /// The presentations that the user has created
  presentations Presentation[]
  /// The provider accounts that the user has linked
  providerAccounts ProviderAccount[]
}

That allows you to have thourough checks on emails and so on and potentially even autogenerate forms.

  • I tried to match the original queries as close as possible, and in my testing, everything seems to work as intended. However i would recommend you check as well since i'm not too familiar with the project yet

Copy link
Owner

@C4illin C4illin left a comment

Choose a reason for hiding this comment

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

Nice work!

if (!fs.existsSync("./data/mydb.sqlite")) {
// run bun prisma migrate deploy with child_process
console.log("Database not found, creating a new one...");
execSync("bun prisma migrate deploy");
Copy link
Owner

Choose a reason for hiding this comment

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

Is this the standard way to do it?

Not that it is anything wrong with it just want to double check

.as(User)
.get(body.email);
if (existingUser && existingUser.id.toString() !== user.id) {
const emailInUse = await prisma.user.findUnique({ where: { email: body.email } });
Copy link
Owner

Choose a reason for hiding this comment

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

nice! :)

@C4illin
Copy link
Owner

C4illin commented Nov 10, 2025

The migration process currently uses the existing PRAGMA user_version to determine the state of existing databases. In the future, this migration could be deprecated once most users databases have been migrated, since prisma has it's own migration tracking mechanism for further migrations.

There shouldn't be any users on the old user_version, the update was a long time ago. But I think your current solution can be kept as is just to be sure, there is no harm in it except some technical dept?

@C4illin
Copy link
Owner

C4illin commented Nov 10, 2025

We should improve the schema in future MRs by adding validators and more indexes, for example, this is how my user model looks with zod:

I don't think much validation is needed, the only user input is username and password. And I think it is quite nice being able to have a short or long password and/or username :) but there is no harm in adding it as long as it is backwards compatible

@C4illin
Copy link
Owner

C4illin commented Nov 10, 2025

Run bun pm trust --all to trust prisma so the post-install scripts work

@C4illin
Copy link
Owner

C4illin commented Nov 10, 2025

Found a bug in the delete action

Invalid `db.job.delete()` invocation in
/var/home/emrik/Dokument/Programmering/node.js/ConvertX/src/pages/deleteJob.tsx:33:18

  30 });
  31 
  32 // delete the job
→ 33 await db.job.delete(
Foreign key constraint violated on the foreign key

@C4illin C4illin self-requested a review November 10, 2025 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants