MichaelM
Convex Community10mo ago
2 replies
Michael

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

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!
Was this page helpful?