npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cranq/document-utils

v2.0.3

Published

Document store manipulation utilities

Downloads

13

Readme

@cranq/document-utils

Utilities for manipulating documents in a document store.

Terms

Document

A document is any record (JS object) that has a __collectionId property. Documents may contain other documents, or references to documents.

Documents extend the Document type.

Example:

type Dog = {
  __collectionId: "dogs",
  name: string;
  owner: Person;
};
type Person = {
  __collectionId: "people",
  name: string;
}
const buddy: Dog = {
  __collectionId: "dogs",
  name: "Buddy",
  owner: { // sub-document
    __collectionId: "people",
    name: "Jane Doe"
  }
}

Reference

A reference to a document identifies a document in a document store, and is a record (JS object) with exactly two properties:

References are of the Reference type.

  • __collectionId - same as the collection ID of the referenced document
  • __documentId - identifies the referenced document in its collection

Example:

const buddyRef: Reference<Dog> = {
  __collectionId: "dogs",
  __documentId: "buddy"
}

Truncated document

A truncated document is a document with all its sub-documents replaced with references.

Truncated documents satisfy the TruncatedDocument<D> type, where D extends Document.

Example:

const buddyTruncated: TruncateDocument<Dog> = {
  __collectionId: "dogs",
  name: "Buddy",
  owner: { // reference to sub-document
    __collectionId: "people",
    __documentId: "jane-doe"
  }
}

Restored document

A restored document is a document restored from a truncated document.

Restored documents are ordinary documents, but their restored types can be calculated using the RestoreDocument generic. RestoreDocument<TR> will be the same as D, given that TR = TruncateDocument<D>.

Document collection

A document collection is a dictionary (JS object) of truncated documents of the same collection ID, indexed by their document IDs.

The type of a document collection is based on type of the documents it contains. Eg: DocumentCollection<D>.

Example:

const dogs: DocumentCollection<Dog> = {
  "buddy": {
    __collectionId: "dogs",
    name: "Buddy",
    owner: {
      __collectionId: "people",
      __documentId: "jane-doe"
    }
  }
}

Document store

A document store is a dictionary (JS object) of document collections, indexed by collection IDs.

The type of a document store is based on the types of all the documents it can store. Eg: DocumentStore<Dog | Person>

Example:

const documentStore: DocumentStore<Dog | Person> = {
  "dogs": {
    "buddy": {
      __collectionId: "dogs",
      name: "Buddy",
      owner: {
        __collectionId: "people",
        __documentId: "jane-doe"
      }
    }
  },
  "people": {
    "jane-doe": {
      __collectionId: "people",
      name: "Jane Doe"
    }
  }
}

Common usage

Retrieving (truncated) document from the store

Example:

const buddyTruncated = getDocument(documentStore, "dogs", "buddy");

Retrieving (deep) document from the store

Retrieving a deep document out of the store, when compared to getDocument(), incurs some performance penalty proportional to the complexity of the document being retrieved, as it needs to be assembled on the fly.

Example:

const buddy = getDeepDocument(documentStore, "dogs", "buddy");

Retrieving field from the store

Example:

const buddyName = getField(documentStore, "dogs", "buddy", "name");

Writing (truncated) document to the store

Example:

documentStore = setDocument(documentStore, "dogs", "buddy", {
  __collectionId: "dogs",
  name: "Buddy",
  owner: {
    __collectionId: "people",
    __documentId: "jane-doe"
  }
})

A mutable version exists for setDocument(), with identical arguments, and void return type: setDocumentMutable().

Appending (truncated) document in the store

Appending a document changes the value of specified field, leaving other fields unaffected. Fields values may be specified either as a value, or as a function - receiving current value and returning new value.

Example:

documentStore = appendDocument(documentStore, "dogs", "buddy", {
  name: "Fido"
})
documentStore = appendDocument(documentStore, "people", "jane.doe", {
  name: (name) => `${name} Jr.`
})

A mutable version exists for appendDocument(), with identical arguments, and void return type: appendDocumentMutable().

Writing (deep) document to the store

Writing a deep document into the store, when compared to setDocument(), incurs some performance penalty proportional to the complexity of the document being written, as it needs to be broken down on the fly.

Example:

documentStore = setDeepDocument(documentStore, "dogs", "buddy", {
  __collectionId: "dogs",
  name: "Buddy",
  owner: {
    __collectionId: "people",
    name: "Jane Doe"
  }
})

A mutable version exists for setDeepDocument(), with identical arguments, and void return type: setDeepDocumentMutable().