Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@cranq/document-utils
Advanced tools
Readme
Utilities for manipulating documents in a document store.
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"
}
}
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 collectionExample:
const buddyRef: Reference<Dog> = {
__collectionId: "dogs",
__documentId: "buddy"
}
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"
}
}
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>
.
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"
}
}
}
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"
}
}
}
Example:
const buddyTruncated = getDocument(documentStore, "dogs", "buddy");
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");
Example:
const buddyName = getField(documentStore, "dogs", "buddy", "name");
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 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 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()
.
FAQs
Document store manipulation utilities
The npm package @cranq/document-utils receives a total of 1 weekly downloads. As such, @cranq/document-utils popularity was classified as not popular.
We found that @cranq/document-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.