#How to best model an eventually complete record in the schema?

2 messages · Page 1 of 1 (latest)

stable garden
#

We have a table in which some fields are not ready at creation, but are eventually filled out by scheduled functions. What would be the best way to model the schema for this in Convex and get type safety?

Here's a complex approach that we're trying (that would obviously not scale well for more fields):

const commonFields = v.object({
    createdBy: v.id("users"),
    displayName: v.string(),
  });
  
  const eventuallyCompleteField1 = v.object({
    projectSlug: v.string(),
    teamSlug: v.string()
  });
  
  const eventuallyCompleteField2 = v.string();

  const tableReadyVersion = v.object({
    ...commonFields.fields,
    eventuallyComplete1: eventuallyCompleteField1,
    eventuallyComplete2: eventuallyCompleteField2
  });
  
  const tableNotReadyVersion = v.object({
    ...commonFields.fields,
    eventuallyComplete1: v.null(),
    eventuallyComplete2: v.null()
  });
  
  const ev1NotReadyVersion = v.object({
    ...commonFields.fields,
    eventuallyComplete1: v.null(),
    eventuallyComplete2: eventuallyCompleteField2
  });
  
  const ev2NotReadyVersion = v.object({
    ...commonFields.fields,
    eventuallyComplete1: eventuallyCompleteField1,
    eventuallyComplete2: v.null()
  });
  
  export const vMyTable = v.union(
    tableReadyVersion,
    tableNotReadyVersion,
    ev1NotReadyVersion,
    ev2NotReadyVersion,
  );

Thank you!

midnight heronBOT
#

Thanks for posting in #1088161997662724167.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.

    - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
    - Use [search.convex.dev](https://search.convex.dev) to search Docs, Stack, and Discord all at once.
    - Additionally, you can post your questions in the Convex Community's #1228095053885476985 channel to receive a response from AI.
    - Avoid tagging staff unless specifically instructed.

    Thank you!