typetta icon indicating copy to clipboard operation
typetta copied to clipboard

Live queries ✨

Open edobrb opened this issue 4 years ago • 0 comments

Live queries are needed to support graphql subscriptions.

API proposal:

const user = ... User DAO ...
const lq: LiveQuery<{ name: string } | null> = await user.liveFindOne({ filter: { live: true  }, projection: { name: true }, sorts: [ { name: 'asc' } ], skip: 1 })
const it = lq.asyncIterator()
const firstUser = (await it.next()).value
const secondUser = (await it.next()).value
lq.close()
const user = ... User DAO ...
const lq: LiveQuery<{ name: string }[]> = await user.liveFindAll({ filter: { live: true  }, projection: { name: true }, sorts: [ { name: 'asc' } ], skip: 1, limit: 2 })
const it = lq.asyncIterator()
const firstUsers = (await it.next()).value
const secondUsers = (await it.next()).value
lq.close()
const user = ... User DAO ...
const lq: LiveQuery<boolean> = await user.liveExists({ filter: { live: true  } })
const it = lq.asyncIterator()
const first = (await it.next()).value
const second= (await it.next()).value
lq.close()
const user = ... User DAO ...
const lq: LiveQuery<number> = await user.liveCount({ filter: { live: true  } })
const it = lq.asyncIterator()
const first = (await it.next()).value
const second = (await it.next()).value
lq.close()
export abstract class LiveQuery<T> {
  public abstract close(): void // closes all opened iterators
  public abstract asyncIterator(): AsyncIterator<T> // creates new iterator hooked to the same live query
}

edobrb avatar Apr 20 '22 07:04 edobrb