-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
5e51637
to
53a675d
Compare
53a675d
to
6f30820
Compare
32512fd
to
bc8fedb
Compare
bc8fedb
to
691aade
Compare
809b5a9
to
a7184d6
Compare
8148158
to
6ed6450
Compare
// TODO: Make columns non-nullable | ||
if (!buildPrice) throw Error("Missing buildPrice"); |
There was a problem hiding this comment.
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 🤞
6ed6450
to
d8970ac
Compare
d8970ac
to
427405c
Compare
// Type not exported by postcode lib directly | ||
type ValidPostcode = Extract<ReturnType<typeof parsePostcode>, { valid: true }>; |
There was a problem hiding this comment.
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
const pricesPaidArea = await prisma.pricesPaid.aggregate({ | ||
where: { | ||
propertyType: { | ||
equals: input.houseType, | ||
}, | ||
postcode: { | ||
startsWith: postcodeArea, | ||
}, | ||
}, | ||
_count: { | ||
id: true, | ||
}, | ||
_avg: { | ||
price: true, | ||
}, | ||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
427405c
to
398812b
Compare
What does this PR do?
primsa.$queryRaw()
and replaces them with calls to the Prisma modelsWhat's not in this PR
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).