Gozala / datalogia

Library for querying in-memory facts using datalog
5 stars 1 forks source link

Support for optional attributes #13

Open Gozala opened 6 months ago

Gozala commented 6 months ago

I'm finding a need for optional attributes, more specifically I don't want entity to be filtered out even if it does not have that attribute. Here is an example of what I'm finding myself doing

{
  select: { ...vars },
  where: [
    ...conditions,
    match([ucan, 'ucan/expiration', expiration])
     .and(expiration.conforms($ => $ > time))
     .or(match([ucan, 'ucan/issuer', _])
  ]
}

Basically I introduce an or clause that always succeeds. In fact now that I'm writing I'm realizing it's not even doing the right thing because it will succeed even if expiration < time.

Gozala commented 6 months ago

After trying to learn what datomic does here I found that I could write query like this

{
  select: { ucan },
  where: [
    ...conditions,
   not(match([ucan, 'ucan/expiration', _])
   .or(match([ucan, 'ucan/expiration', expiration])
       .and(expiration.conforms($ => $ > time)))
  ]
}

perhaps this is good enough and we could instead add sugar for the implicit support e.g. something like

const expiration = attribute('ucan/expiration').implicit(Number.MAX_SAFE_INTEGER)
const query = {
  select: { ucan },
  where: [
    ...conditions,
    expiration(ucan).conforms($ => $ > time)
  ]
}