Type safety on .set() method?

Hey folks - I was wondering if there is a way to get type safety when updating a row using the .set method? In the example below, updated_at is the table name, not the property name updatedAt. This code runs with no errors, but of course never updates the updated at because it doesn't exist!
const data = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
const data = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
Any tips for getting this to be type-safe ?
5 Replies
rphlmr âš¡
rphlmr ⚡•4mo ago
Hey 👋 Can you share your tsconfig?
No description
bennettcohen
bennettcohen•4mo ago
oh hmm
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"lib": ["es6"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"skipLibCheck": true,
"baseUrl": "./src",
"paths": {
"@routes/*": ["routes/*"],
"@v1/*": ["routes/v1/*"],
"@utils/*": ["lib/utils/*"],
"@middleware/*": ["lib/middleware/*"],
"@schemas/*": ["schemas/*"],
"@db": ["db"],
"@db/*": ["db/*"],
"@services/*": ["lib/services/*"],
"@jobs/*": ["jobs/*"],
}
},
"include": [
"src",
"src/**/*",
"src/*",
"src/*/*/",
"src/**/*",
"global.d.ts"
]
}
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"lib": ["es6"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"skipLibCheck": true,
"baseUrl": "./src",
"paths": {
"@routes/*": ["routes/*"],
"@v1/*": ["routes/v1/*"],
"@utils/*": ["lib/utils/*"],
"@middleware/*": ["lib/middleware/*"],
"@schemas/*": ["schemas/*"],
"@db": ["db"],
"@db/*": ["db/*"],
"@services/*": ["lib/services/*"],
"@jobs/*": ["jobs/*"],
}
},
"include": [
"src",
"src/**/*",
"src/*",
"src/*/*/",
"src/**/*",
"global.d.ts"
]
}
Anything you see? Pretty new to the TS world Thanks @Raphaël M (@rphlmr) ⚡
rphlmr âš¡
rphlmr ⚡•4mo ago
is your schema inside src? your include "doesn’t look common" (but there is so many possibilities haha) "**/*.ts","**/*.tsx", "global.d.ts" should be enough to tell TypeScript that it has to check all of your files
bennettcohen
bennettcohen•4mo ago
Ah was able to get it working...i think. The culprit was how i was initializing the db client. There is some weird hacky stuff around the SSL certificate when connecting to my prod db Thank u! Btw, would you know if there a nice way to do optional singles here? Often I'm retrieving one row and can be annoying to have to do data?.[0] @Raphaël M (@rphlmr) ⚡
rphlmr âš¡
rphlmr ⚡•4mo ago
update always returns an array this should be enough
const [updated] = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
const [updated] = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
Want results from more Discord servers?
Add your server