SDK
The following functions are provided to help you work with document and asset data inside your Satellite. They are part of the tools available when writing serverless functions in TypeScript and support common tasks such as interacting with the datastore, storage, and custom hook logic.
The SDK is provided by the @junobuild/functions library.
To add it to your project:
- npm
- yarn
- pnpm
npm i @junobuild/functions
yarn add @junobuild/functions
pnpm add @junobuild/functions
You have to follow the pace of the Juno release to ensure compatibility. Refer to the maintenance guide for instructions.
Datastoreβ
The following functions can be used to manage documents within the Datastore from your serverless functions.
setDocStoreβ
Sets a document in a collectionβs of the datastore. Use this to insert or update document data.
function setDocStore(params: SetDocStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing the following fields:caller
: The caller initiating the operation (RawUserId
orUserId
).collection
: The name of the collection where the document will be stored.key
: The key identifying the document.doc
: The document content including:data
: A Uint8Array produced by encodeDocData.description
(optional): A short description linked with the document.version
(optional if new): An expected version number to prevent overwrite.
Returns:β
void
Throws:β
ZodError
if the input schema is invalid.Error
if the operation is rejected by the Satellite (e.g. due to a failed assertion or validation error).
getDocStoreβ
Retrieves a document from the datastore.
function getDocStore(params: GetDocStoreParams): OptionDoc;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The caller requesting the document (RawUserId or UserId).collection
: The collection containing the document.key
: The key identifying the document.
Returns:β
OptionDoc
: The document if found, or undefined.
Throws:β
ZodError
if the input schema is invalid.Error
if retrieval fails.
listDocsStoreβ
Lists documents in a collection using optional filters, pagination, and sorting.
function listDocsStore(params: ListDocsStoreParams): ListResults<Doc>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The caller initiating the request (RawUserId or UserId).collection
: The collection to list documents from.params
: A ListParams object with pagination, filtering, or sorting options.
Returns:β
ListResults<Doc>
: Matching documents and pagination metadata.
Throws:β
ZodError
if the input schema is invalid.Error
if listing fails.
countDocsStoreβ
Counts documents matching filter criteria.
function countDocsStore(params: CountDocsStoreParams): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The caller initiating the operation.collection
: The collection to count documents in.params
: A ListParams object with filter and pagination options.
Returns:β
bigint
: Number of documents matching the filter.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
countCollectionDocsStoreβ
Counts the total number of documents in a collection.
function countCollectionDocsStore(
params: CountCollectionDocsStoreParams
): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:collection
: The collection to count documents in.
Returns:β
bigint
: The total number of documents.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
deleteDocStoreβ
Deletes a document from the datastore.
function deleteDocStore(params: DeleteDocStoreParams): DocContext<OptionDoc>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The caller initiating the deletion (RawUserId or UserId).collection
: The collection where the document is stored.key
: The key identifying the document.doc
: The document deletion metadata including:version
: The expected version of the document.
Returns:β
DocContext<OptionDoc>
: Includes the key, collection, and optionally the previous document data.
Throws:β
ZodError
if the input schema is invalid.Error
if validation fails or the document cannot be deleted.
deleteDocsStoreβ
Deletes all documents in a specific collection.
function deleteDocsStore(params: DeleteDocsStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:collection
: The collection to delete.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
deleteFilteredDocsStoreβ
Deletes documents matching filter criteria.
function deleteFilteredDocsStore(
params: DeleteFilteredDocsStoreParams
): DocContext<OptionDoc>[];
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The caller initiating the deletion.collection
: The collection to target.params
: A ListParams object with filter and pagination options.
Returns:β
DocContext<OptionDoc>[]
: Context for each deleted document.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
Storageβ
The following functions can be used to manage assets within the Storage from your serverless functions.
setAssetHandlerβ
Sets or updates an asset using identity encoding (no compression).
function setAssetHandler(params: SetAssetHandlerParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing: -key
: An object identifying the asset, including: -name
: The asset filename (e.g., logo.png). -full_path
: The asset path (e.g., /images/logo.png). -collection
: The collection it belongs to. -owner
: The caller principal. -token
(optional): An access token if required. -description
(optional): A short description. -content
: A Uint8Array representing the raw content of the asset. -headers
: An array of header fields to store with the asset.
Returns:β
void
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
getAssetStoreβ
Retrieves an asset from the Storage.
function getAssetStore(params: GetAssetStoreParams): OptionAsset;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The identity requesting the asset (RawUserId
orUserId
).collection
: The collection name.full_path
: The asset path.
Returns:β
OptionAsset
: The asset if found, orundefined
.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
listAssetsStoreβ
Lists assets in a collection without their content.
function listAssetsStore(
params: ListAssetsStoreParams
): ListResults<AssetNoContent>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The identity making the request.collection
: The collection name.params
: AListParams
object to filter, sort, or paginate.
Returns:β
ListResults<AssetNoContent>
: Matching assets without content chunks.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
countCollectionAssetsStoreβ
Counts all assets in a collection.
function countCollectionAssetsStore(
params: CountCollectionAssetsStoreParams
): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:collection
: The collection name.
Returns:β
bigint
: Number of assets in the collection.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
countAssetsStoreβ
Counts assets in a collection matching filter criteria.
function countAssetsStore(params: CountAssetsStoreParams): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The identity making the request.collection
: The collection name.params
: AListParams
object to filter the count.
Returns:β
bigint
: Number of matching assets.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
deleteAssetStoreβ
Deletes a single asset from the Storage.
function deleteAssetStore(params: DeleteAssetStoreParams): OptionAsset;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The identity initiating the deletion.collection
: The collection name.full_path
: The asset path to delete.
Returns:β
OptionAsset
: The deleted asset, orundefined
.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
deleteAssetsStoreβ
Deletes all assets in a collection.
function deleteAssetsStore(params: DeleteAssetsStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:collection
: The collection to delete.
Returns:β
void
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
deleteFilteredAssetsStoreβ
Deletes assets matching filters in a collection.
function deleteFilteredAssetsStore(
params: DeleteFilteredAssetsStoreParams
): OptionAsset[];
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:caller
: The identity initiating the operation.collection
: The collection name.params
: AListParams
object to define the filters.
Returns:β
OptionAsset[]
: List of deleted assets.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.
getContentChunksStoreβ
Retrieves a specific chunk of an asset.
function getContentChunksStore(
params: GetContentChunksStoreParams
): Blob | undefined;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params
: An object containing:encoding
: TheAssetEncoding
to retrieve from.chunk_index
: The index of the chunk (starting from 0).memory
: EitherHeap
orStable
.
Returns:β
Blob | undefined
: The content chunk if found.
Throws:β
ZodError
if the input schema is invalid.Error
if the operation fails.