whoami
whoami2y ago

How to do convert between Id in string format and `Id<'...'>`?

Question is in the title
19 Replies
ian
ian2y ago
Currently the id is an object, but it will likely be just a string in the future. Are you talking about how to make the string part (like doc._id.id) into an Id object? That is by new Id(“mytable”, stringId) You probably shouldn’t cast a regular string to the ID type, because it won’t work as an argument in any of the functions that expect an id.
whoami
whoamiOP2y ago
export const get = query({
args: {
id: v.id('chats'),
},

handler: async ({ db, auth }, { id }) => {
const identity = await auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call')
}

const chat = await db.get(id)
if (!chat) {
throw new Error('Chat not found')
}

const tools = chat.tools.map((tool) => {
db.get(tool)
})

const messages = db
.query('messages')
.filter((q) => q.eq(q.field('chat'), id))
.order('asc')

return { id: chat._id, title: chat.title, messages, tools }
},
})
export const get = query({
args: {
id: v.id('chats'),
},

handler: async ({ db, auth }, { id }) => {
const identity = await auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call')
}

const chat = await db.get(id)
if (!chat) {
throw new Error('Chat not found')
}

const tools = chat.tools.map((tool) => {
db.get(tool)
})

const messages = db
.query('messages')
.filter((q) => q.eq(q.field('chat'), id))
.order('asc')

return { id: chat._id, title: chat.title, messages, tools }
},
})
This is my function, what is the best workaround now? I tried to call it via
const data = useQuery('chats:get', { id: new Id('chats', 'ErKzUFoAJrG5JmpiThZMGg')
})
const data = useQuery('chats:get', { id: new Id('chats', 'ErKzUFoAJrG5JmpiThZMGg')
})
and got a weird error Expected 1 arguments, but got 2.ts(2554)
ian
ian2y ago
This all looks correct. If you move the Id creation to the previous line, is the error still in new Id(...)? and you're importing Id from something like import { Id } from "../convex/_generated/dataModel";?
whoami
whoamiOP2y ago
No description
whoami
whoamiOP2y ago
I think so
ian
ian2y ago
One thing you'll likely run into: you need to await some of your results. e.g. you need to do const tools = await Promise.all(chat.tools.map...); and const messages = await db.query... and the query needs to call .collect()
whoami
whoamiOP2y ago
oh let me fix them Does it look correct now? I put await on every line and collect in the end
export const get = query({
args: {
id: v.id('chats'),
},

handler: async ({ db, auth }, { id }) => {
const identity = await auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call to query')
}

const chat = await db.get(id)
if (!chat) {
throw new Error('Chat not found')
}

const tools = await chat.tools.map((t) => db.get(t))
const messages = await db
.query('messages')
.filter((q) => q.eq(q.field('chat'), id))
.order('asc')
.collect()

return { id: chat._id, title: chat.title, messages, tools }
},
})
export const get = query({
args: {
id: v.id('chats'),
},

handler: async ({ db, auth }, { id }) => {
const identity = await auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call to query')
}

const chat = await db.get(id)
if (!chat) {
throw new Error('Chat not found')
}

const tools = await chat.tools.map((t) => db.get(t))
const messages = await db
.query('messages')
.filter((q) => q.eq(q.field('chat'), id))
.order('asc')
.collect()

return { id: chat._id, title: chat.title, messages, tools }
},
})
ian
ian2y ago
tools will return an array of promises, not a promise itself, so you need to wrap the array of promises with Promise.all() to make it a promise that awaits all the promises in the list And your console.log shows id as a string and not as undefined? if it's undefined, the Id constructor might think it's only being called with one parameter
whoami
whoamiOP2y ago
yea Id is a string
No description
ian
ian2y ago
and what's the type error on the new Id(...)? maybe move that to the line above, like const chatId = new Id(...) to figure out which part is causing the error. I'm assuming you're running convex dev / your functions are deployed?
whoami
whoamiOP2y ago
yea I am running npx convex dev
whoami
whoamiOP2y ago
No description
whoami
whoamiOP2y ago
that line looks fine ah maybe it is my problem having two generated folder
ian
ian2y ago
how did you get two? i'm curious
whoami
whoamiOP2y ago
I moved the folders around, but anyways I don't think that's the problem
ian
ian2y ago
I've got to run now, but it sounds like the Id generation isn't the issue anymore, so I'll let you figure it out from here. holler if you need more help
whoami
whoamiOP2y ago
I was importing the tanstack useQuery 😂 , dumb mistake thanks Ian
jamwt
jamwt2y ago
Happens to the best of us!
ian
ian2y ago
As an update, IDs are now strings - so new Id isn't necessary

Did you find this page helpful?