gabrielwG
Convex Community13mo ago
24 replies
gabrielw

Hey all, does anyone know if there is a Table helper equivalent for convex ENTs?

Trying to see if it's possible to collocate validators with schema definitions using the ENTs abstraction.

For example, I am currently using the defineEnts method to define a table in my schema:

events: defineEnt({
      name: v.string(),
      event_img_storage_id: v.optional(v.id("_storage")),
      event_img_url: v.optional(v.string()),
    })
      .field("start_at", v.string(), { index: true })
      .field("timezone", v.string())
      .field("end_at", v.optional(v.string()))
      .field("geo_address_json", v.optional(geo_address_json_validator))
      .field("geo_latitude", v.optional(v.string()))
      .field("geo_longitude", v.optional(v.string()))
      .field("luma_event_id", v.optional(v.string()))
      .edges("teams"),...

And I am exporting a separate obj in order to infer argument validation for the corresponding create mutation:
// supplied for frontend mutation
export const eventFields = {
  name: v.string(),
  start_at: v.string(),
  timezone: v.string(),
  end_at: v.optional(v.string()),
  geo_address_json: v.optional(geo_address_json_validator),
  geo_latitude: v.optional(v.string()),
  geo_longitude: v.optional(v.string()),
  team_ids: v.array(v.id("teams")),
  event_img_storage_id: v.optional(v.id("_storage")),
};


Just wondering if there is a way to declare the validator once and feed it into the schema with the ENT-way of doing things. As outlined in the best practices of not redefining the shape in vanilla convex:

// in convex/schema.ts
// ...
export const recipeFields = {
  name: v.string(),
  course: courseValidator,
  ingredients: v.array(v.string()),
  steps: v.array(v.string()),
};

export default defineSchema({
  recipes: defineTable(recipeFields)
    .index("by_course", ["course"]),
});
Was this page helpful?