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

feat: Replace primsa.$queryRaw() with calls to prisma models #19

Merged
merged 6 commits into from
Aug 16, 2024

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Jul 26, 2024

What does this PR do?

  • Removes all instances of primsa.$queryRaw() and replaces them with calls to the Prisma models
  • Remove in-code calculations (e.g. count and average) to aggregate queries
  • Renames a few variables to make things a little easier to read

What's not in this PR

  • A wider refactor and reorganisation - I've tried to keep this as close as I can to swap of one thing for another. This should keep things more controlled and atomic

Next steps...

It's worth reorganising the code in route.ts to a three-tier architecture with separation of handler (http requests) → service layer (business logic) → data layer (prisma & db).

graph TD
    A[Client] --> B[Handler Layer]
    B[Handler Layer] --> C[Service / Business logic Layer]
    C[Service / Business logic Layer] --> D[Data Access Layer]
    D[Data Access Layer] --> E[Database]

Loading

Copy link

vercel bot commented Jul 26, 2024

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

Name Status Preview Comments Updated (UTC)
fairhold-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 16, 2024 9:39am

@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from 5e51637 to 53a675d Compare July 26, 2024 16:07
Base automatically changed from dp/prisma-dx to main July 26, 2024 16:42
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from 53a675d to 6f30820 Compare July 26, 2024 16:47
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from 32512fd to bc8fedb Compare July 26, 2024 16:54
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from bc8fedb to 691aade Compare August 2, 2024 08:06
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch 2 times, most recently from 809b5a9 to a7184d6 Compare August 2, 2024 12:11
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from 8148158 to 6ed6450 Compare August 2, 2024 15:40
Comment on lines +114 to +115
// TODO: Make columns non-nullable
if (!buildPrice) throw Error("Missing buildPrice");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a few lines like this, because the db columns are missing a NOT NULL constraint. This is annotated by question marks in the Prisma schema, e.g. Float?

model BuildPrices {
  id                   Int     @id @default(autoincrement())
  houseTypeDescription String? @map("housetypedescription") @db.VarChar(250)
  houseType            String? @map("housetype") @db.VarChar(1)
  priceRange           String? @map("pricerange") @db.VarChar(50)
  priceMid             Float?  @map("pricemid")

  @@map("buildprices")
}

I'll pick this up next - we should be able to update the Prisma schema to generate a migration to do this 🤞

Comment on lines +5 to +6
// Type not exported by postcode lib directly
type ValidPostcode = Extract<ReturnType<typeof parsePostcode>, { valid: true }>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was getting build errors without this - this type guard now narrows postcode down from the union of InvalidPostcode | ValidPostcode to just ValidPostcode

Comment on lines +69 to +84
const pricesPaidArea = await prisma.pricesPaid.aggregate({
where: {
propertyType: {
equals: input.houseType,
},
postcode: {
startsWith: postcodeArea,
},
},
_count: {
id: true,
},
_avg: {
price: true,
},
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hopefully this is a nice demonstration of the benefits of this approach -

  • Type safe queries (no room for typos in raw SQL)
  • Safer (relevant XKCD)
  • More discoverable code as it's linked by models

Copy link
Collaborator

Choose a reason for hiding this comment

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

Brilliant, that makes a lot of sense, thanks for explaining

Copy link
Collaborator

Choose a reason for hiding this comment

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

Much friendlier code, makes a lot of sense--cheers Daf!

@DafyddLlyr DafyddLlyr marked this pull request as ready for review August 3, 2024 10:23
@DafyddLlyr DafyddLlyr requested a review from a team August 3, 2024 10:23
@DafyddLlyr DafyddLlyr force-pushed the dp/remove-prisma-queryRaw branch from 427405c to 398812b Compare August 16, 2024 09:38
@DafyddLlyr DafyddLlyr merged commit b45b76d into main Aug 16, 2024
3 checks passed
@DafyddLlyr DafyddLlyr deleted the dp/remove-prisma-queryRaw branch August 16, 2024 09:39
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