Somewhere along the way, in every project we come across a need to create our own utils.js
to serve the business codebase.
This function is designed to update a specific document in a Squid database collection. The function takes an object as an argument, which includes the following properties: docRefName
, docProp
, valueToPush
, and collectionName
.
Here's a step-by-step breakdown of what the function does:
-
The function begins by calling the
selectCollection
function withcollectionName
as an argument. This function is not shown in the provided code, but it presumably returns a reference to a Squid collection. -
Next, it creates a document reference,
docRef
, by calling thedoc
method on theportfolioCollection
object withdocRefName
as an argument. This reference points to a specific document within the Squid collection. -
The function then retrieves the current state of the document by calling the
snapshot
method ondocRef
and awaits its result. The current state of the document is stored in thecurrent
variable. -
A copy of the
current
object is made and stored intempDocProps
. This is done to avoid directly mutating thecurrent
object. -
The function then checks if the property of the document specified by
docProp
is a string. If it is, the function updates thedocRefName
property oftempDocProps
withvalueToPush
and then updates the document in Squid with the updatedtempDocProps
object. -
If the
docProp
property of the document is an array, the function pushesvalueToPush
into this array, updates thedocProp
property oftempDocProps
with the updated array, and then updates the document in Squid with the updatedtempDocProps
object. -
Finally, the function logs the updated state of the document to the console.
This function is useful for updating specific properties of a document in a Squid collection, whether those properties are strings or arrays.
This function is designed to interact with both a Squid database collection and the browser's local storage. The function takes an object as an argument, which includes the following properties: docRefName
, collectionName
, and refresh
.
Here's a step-by-step breakdown of what the function does:
-
The function begins by calling the
selectCollection
function withcollectionName
as an argument. This function is not shown in the provided code, but it presumably returns a reference to a Squid collection. -
The function then checks if there is an item named "cacheResponse" in the local storage or if the
refresh
parameter is true. If either of these conditions is met, the function proceeds to the next steps. Otherwise, it skips to step 4. -
The function creates a document reference,
docRef
, by calling thedoc
method on theportfolioCollection
object withdocRefName
as an argument. This reference points to a specific document within the Squid collection. The function then retrieves the current state of the document by calling thesnapshot
method ondocRef
and awaits its result. The current state of the document is then stored in the local storage under the key "cacheResponse". -
The function retrieves the "cacheResponse" item from the local storage and stores it in the
storedData
variable. -
Finally, the function returns the parsed JSON of
storedData
.
This function is useful for caching Squid document data in the local storage and retrieving it. The refresh
parameter allows for the cache to be updated with the latest data from Squid when needed.