Open ffMathy opened 1 month ago
Could you give some examples on how this would be used?
Sure thing!
For instance, we've built a query builder abstraction on top of our generated schema that's fully strong-typed and protects all queries from SQL injection:
const shot = await new QueryBuilder('Shot')
.select(x => x
.property('custom_attributes')
.property('id'))
.where(x => x
.property('id').is("something")
.expandedProperty(x => x.type.name).is("some-type-name"))
.withCacheDuration(60)
.getAll();
This does a few things:
id
, so it knows that .is
is an operator that's available. Same for the type's name. VS Code will suggest this automatically. For number properties for instance, other operators are valid.custom_attributes
and id
properties set as non-optional, so we don't have to null-check it.This is some of the stuff I've been wanting to demo for you for some time, but haven't had the time yet.
Anyway, the problem is that in the above example, I am also looking at an "expanded property" (that's what we call properties that would implicitly trigger a join in Ftrack's end, and are more expensive). In this particular case, we'd love if "is" was strong-typed. "some-type-name" is just a string, but it's actually not a valid type here and will not work at runtime.
This becomes especially useful when querying against "TypedContext", where it will then also suggest valid types.
Another example is if you (for instance) have a Shot, and you'd like to check the type's name somehow.
const someShot: Shot = null as any as Shot;
if(someShot.type.name === "foo-shot") {
//in the above example, VS Code suggests valid type names for shot.
}
Could also imagine a case where it would be a TypedContext
instead, and it would be nice to get suggestions here too of all the types that could be valid.
However, now that I am thinking about it, what is the difference between types and object types?
The types that are valid for an entity, is that decided by the schema? And can one entity have multiple types, but only one object type? What is the cardinality between these type thingies?
Because if the above is right, then I modelled it wrong in the code, and I'll change that. The above is what I want to achieve at least.
Changes
This PR makes type and object type properties of entities strong-typed. This makes it very nice when building up queries that involve checking the type name.
It also refactors some of the code to be (in my opinion) cleaner. It is now easier to see in one place how various overrides to the types are being made. This increases the cohesion of the code quite heavily.
Test
N/A