Best practice around building query functions
Hey, I've looked around the docs somewhat but still not confident if I should do this:
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, 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 way11 Replies
If I did do it this way
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 warningI'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 onok
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 when userId is of type string
profile.id
is optional. You can't assign it to a required propertyhmm ok
But how's
profile.id
and profile.userId
the same value?damnit I feel like an idiot
ya it should be userId
and not id
super embarrased sorry
No worries 😄
btw, I don't know if this is just my editor or the library if it is perhaps this is good feedback
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)
Those are typescript compilation errors. We have no control over them
ok
thank you again koskimas!