K
Kysely•2y ago
NazCodeland

Best practice around building query functions

Hey, I've looked around the docs somewhat but still not confident if I should do this:
export async function createUser(user: NewUser) {
return await db
.insertInto('User')
.values({
email: user.email,
role: user.role
})
.returning('User.id')
.execute();
}

export async function createProfile(profile: NewProfile) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId: // user[0].id
})
.returningAll()
.execute();
}
export async function createUser(user: NewUser) {
return await db
.insertInto('User')
.values({
email: user.email,
role: user.role
})
.returning('User.id')
.execute();
}

export async function createProfile(profile: NewProfile) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId: // user[0].id
})
.returningAll()
.execute();
}
Say, I have a createUser function, and then I need access to that returned user object within createProfile. I could also create a findUserById() function like in the docs,
export async function findUserById(id: NewUser['id']) {
return await db.selectFrom('User')
.where('id', '=', id)
.selectAll()
.executeTakeFirst()
}
export async function findUserById(id: NewUser['id']) {
return await db.selectFrom('User')
.where('id', '=', id)
.selectAll()
.executeTakeFirst()
}
and call findUserById() within createProfile() but then I would have to pass the userObject or user.id to createProfile(), which is fine, but before I start implementing it this way, I figure I should ask if this is the ideal way or is there a better way
11 Replies
NazCodeland
NazCodelandOP•2y ago
If I did do it this way
export async function createProfile(profile: NewProfile, userId: string) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId
})
.execute();
}
export async function createProfile(profile: NewProfile, userId: string) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId
})
.execute();
}
the userId parameter can not be of type NewUser['id] because it wouldn't match .insertInto('Profile') type I don't know if doing userId: string is correct in this case but get's rid of the warning
koskimas
koskimas•2y ago
I'm not following.. What's the question here? Doesn't NewProfile already contain the userId? You don't need to use id: NewUser['id']. You can just use the id's type. string if it's a string, number if it's a number and so on
NazCodeland
NazCodelandOP•2y ago
ok NewProfile contains the fields but it doesn't contain the value beacuse the record hasn't been created yet hmm wait a minute oops, I think I'm confused, ya ya... when I do const user = await createUser(userObject) the returned user will be passed to createProfile, so it will contain it my apologies for being so newb 😅 hmm, why is this saying
Type 'string | undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Type 'undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Type 'string | undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Type 'undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
when userId is of type string
(alias) type NewProfile = {
name: string;
userId: string;
} & {
id?: string | undefined;
addressId?: string | null | undefined;
image?: string | null | undefined;
phone?: string | null | undefined;
location?: Point | null | undefined;
}
import NewProfile
(alias) type NewProfile = {
name: string;
userId: string;
} & {
id?: string | undefined;
addressId?: string | null | undefined;
image?: string | null | undefined;
phone?: string | null | undefined;
location?: Point | null | undefined;
}
import NewProfile
export async function createProfile(profile: NewProfile) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId: profile.id
})
.returningAll()
.execute();
}
export async function createProfile(profile: NewProfile) {
return await db
.insertInto('Profile')
.values({
name: profile.name,
userId: profile.id
})
.returningAll()
.execute();
}
koskimas
koskimas•2y ago
profile.id is optional. You can't assign it to a required property
NazCodeland
NazCodelandOP•2y ago
hmm ok
koskimas
koskimas•2y ago
But how's profile.id and profile.userId the same value?
NazCodeland
NazCodelandOP•2y ago
damnit I feel like an idiot ya it should be userId and not id super embarrased sorry
koskimas
koskimas•2y ago
No worries 😄
NazCodeland
NazCodelandOP•2y ago
btw, I don't know if this is just my editor or the library if it is perhaps this is good feedback
No overload matches this call.
Overload 1 of 2, '(insert: InsertObjectOrList<DB, "Profile">): InsertQueryBuilder<DB, "Profile", InsertResult>', gave the following error.
Type 'string | undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Type 'undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Overload 2 of 2, '(insert: InsertObjectOrListFactory<DB, "Profile">): InsertQueryBuilder<DB, "Profile", InsertResult>', gave the following error.
Argument of type '{ name: string; userId: string | undefined; }' is not assignable to parameter of type
'InsertObjectOrListFactory<DB, "Profile">'.
Object literal may only specify known properties, and 'name' does not exist in type 'InsertObjectOrListFactory<DB, "Profile">'.ts(2769)
(property) name: string
No overload matches this call.
Overload 1 of 2, '(insert: InsertObjectOrList<DB, "Profile">): InsertQueryBuilder<DB, "Profile", InsertResult>', gave the following error.
Type 'string | undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Type 'undefined' is not assignable to type 'ValueExpression<DB, "Profile", string>'.
Overload 2 of 2, '(insert: InsertObjectOrListFactory<DB, "Profile">): InsertQueryBuilder<DB, "Profile", InsertResult>', gave the following error.
Argument of type '{ name: string; userId: string | undefined; }' is not assignable to parameter of type
'InsertObjectOrListFactory<DB, "Profile">'.
Object literal may only specify known properties, and 'name' does not exist in type 'InsertObjectOrListFactory<DB, "Profile">'.ts(2769)
(property) name: string
I get that as an error in the case of using userId: profile.id I've noticed my errors are huge like that whenever I make a mistake and it says things like 'name' does not exist in type 'InsertObjectOrListFactory<DB, "Profile">'.ts(2769)
koskimas
koskimas•2y ago
Those are typescript compilation errors. We have no control over them
NazCodeland
NazCodelandOP•2y ago
ok thank you again koskimas!
Want results from more Discord servers?
Add your server