Expression to check if reference is valid


#1

Let’s say I have a type called ThisType with a reference field otherObj of type OtherType. I’m know that I can use an expression like exists(otherObj) to see if the reference exists, but how can I easily check that the reference exists AND it is valid (i.e. it references an instance of OtherType that actually exists in the database)? Is there an expression function for this?


#2

I was searching for the same question and @romain.juban gave me this answer.

Let’s say you have

Type A {
}

Type B {
  link: B
}

Then the following code will gave you instances with valid references
B.fetch({filter: "exists(link.meta.created)"})


#3

Compact types – types with @db(compact = true) annotation – don’t have meta field populated.

I think filtering by version is more reliable.

B.fetch({filter: "exists(link.version)"})