general table history

Thanks. I might dive into that tomorrow when I return to this. The other problem is that I'm trying to make the components for this history display as generic as possible. I'm going to have dozens of tables once this is all done, so I'm hoping to find/create a way to allow for easily delving into linked documents without too much custom setup for each table.
5 Replies
lee
lee4w ago
you can do this too with the schema parsing if you want like if you're trying to display a string in table tableName and at field path fieldPath (which is an array of strings) you can do
const tableValidator = schema.tables[tableName].validator;
let itemValidator = tableValidator;
for (const fieldName of fieldPath) {
itemValidator = itemValidator.fields[fieldName];
}
let referencedTableName = null;
if (itemValidator.kind === 'id') {
referencedTableName = itemValidator.tableName;
}
const tableValidator = schema.tables[tableName].validator;
let itemValidator = tableValidator;
for (const fieldName of fieldPath) {
itemValidator = itemValidator.fields[fieldName];
}
let referencedTableName = null;
if (itemValidator.kind === 'id') {
referencedTableName = itemValidator.tableName;
}
Clever Tagline
Clever TaglineOP4w ago
Thank you! This works perfectly even though the type checker indicates an error with schema.tables[tableName]:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ afterHours: TableDefinition<VObject<{ number: string; }, { number: VString<string, "required">; }, "required", "number">, { by_number: ["number", "_creationTime"]; }, {}, {}>; ... 20 more ...; links: TableDefinition<...>; }'.
No index signature with a parameter of type 'string' was found on type '{ afterHours: TableDefinition<VObject<{ number: string; }, { number: VString<string, "required">; }, "required", "number">, { by_number: ["number", "_creationTime"]; }, {}, {}>; ... 20 more ...; links: TableDefinition<...>; }'.ts(7053)
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ afterHours: TableDefinition<VObject<{ number: string; }, { number: VString<string, "required">; }, "required", "number">, { by_number: ["number", "_creationTime"]; }, {}, {}>; ... 20 more ...; links: TableDefinition<...>; }'.
No index signature with a parameter of type 'string' was found on type '{ afterHours: TableDefinition<VObject<{ number: string; }, { number: VString<string, "required">; }, "required", "number">, { by_number: ["number", "_creationTime"]; }, {}, {}>; ... 20 more ...; links: TableDefinition<...>; }'.ts(7053)
I can add a @ts-ignore comment above that line for now, but I'm curious if there's another way to satisfy the type checker for that line.
lee
lee4w ago
Making it typecheck sounds like more trouble than it's worth
Clever Tagline
Clever TaglineOP4w ago
Yeah, that's kinda what I figured. As a side question, will that logic with the field path also work if the ID is in an array, or inside an object that's part of an array? Do I need to adjust how I'm building the path array to account for cases like that? For example, I'm running all of these early tests on my contacts table. One of the fields in that table is:
deals: v.optional(v.array(v.id("deals")))
deals: v.optional(v.array(v.id("deals")))
lee
lee4w ago
Arrays would require more steps. I recommend exploring your schema -- just start typing the schema.tables.contacts.validator.fields. and see what autocomplete gives you

Did you find this page helpful?