TanStackT
TanStack5mo ago
45 replies
scattered-teal

[Query collection] how to insert with defaults

hi guys.

i have this query collection its query function is a server function that fetches from my database:
export const messageSchema = z.object({
  message: z.string(),
  chatId: z.string(),
  senderId: z.string().nullable(),
})

export const messageCollection = createCollection(
  queryCollectionOptions({
    getKey: (message) => message.id,
    queryClient: getContext().queryClient,
    schema: messageSchema,
    queryFn: () => getMessages(),
    queryKey: ['messageCollection'],
  }),
)

i only need to fill the fields from the message schema since fields like
id
are auto incrementing and dates has defaults set but when i make an insert, the collection makes me provide all fields.

is there any way to handle this?
thank u so much in advance!
Was this page helpful?