@sanity/document-store

1.148.1 • Public • Published

Document store

Sanity / gradient document store

Example

// If you are interested in everything that happens with document `lol42`
const subscription = documents.byId("lol42").subscribe(event => {
  console.log('Something happened with document lol42!', event)
})

// After a while, when you are no longer interested in updates fom document `lol42`:
subscription.unsubscribe()

How it works

When you add a subscriber to a document the first thing that happens is that the subscriber function is called with a snapshot of the document's current state. This happens as soon as the snapshot is retrieved. If there is a cached version of it on the client, the snapshot is received immediately (synchronously).

From that moment on, you will receive a new event every time something happens to the document, until you unsubscribe again.

API

  • createDocumentStore({serverConnection: ServerConnection}) : DocumentStore

ServerConnection must be an object with the following api:

  • byId(id : string) : Observable<DocumentEvent>
  • query(query : string) : Observable<QueryResultEvent> (todo, add more)

DocumentStore

Query methods

  • byId(id : string) : Observable<DocumentEvent>
  • byIds(ids : Array<string>) : Observable<QueryResultEvent>
  • query(query : string) : Observable<QueryResultEvent>

Mutation methods

  • update(id : string, spec : UpdateSpec) : Observable<OperationEvent>
  • create(document : Document) : Observable<OperationEvent>
  • replace(documentId : string, document : Document) : Observable<OperationEvent>
  • createOrReplace(document : Document) : Observable<OperationEvent>
  • delete(documentId : string) : : Observable<OperationEvent>

Document

  • _id : string
  • _type : string

DocumentEventType

DocumentEvent

  • type : string either one of snapshot, update
  • snapshot : Document

Package Sidebar

Install

npm i @sanity/document-store

Weekly Downloads

1,082

Version

1.148.1

License

MIT

Unpacked Size

33.5 kB

Total Files

21

Last publish

Collaborators

  • drewsanity
  • refiito
  • sergeisarviro
  • ash
  • indrek.karner
  • cngonzalez-sanity
  • rdunk
  • rneatherway-sanity
  • ricokahler
  • pedro-sanity
  • jonabc
  • kenjonespizza
  • pauloborgesf
  • binoy14
  • simen.svale
  • svirs
  • josh_sanity_io
  • joneidejohnsen
  • nina.andal
  • rankers
  • snorreeb
  • mattcraig
  • vincentquigley
  • stipsan
  • michael-sanity
  • rubioz
  • tonina
  • ritasdias
  • simeonsanity
  • kmelve
  • bjoerge
  • rexxars
  • skogsmaskin
  • robinpyon
  • mariuslundgard
  • sanity-io
  • evenw
  • radhe_sanity
  • rbotten
  • judofyr
  • obliadp
  • dcilke
  • fredcarlsen
  • hermanw
  • sgulseth
  • atombender