From 938cb38441c957753a4d1df5e1d25fc8fc6f0807 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 29 Mar 2024 17:49:07 +0100 Subject: [PATCH] docs: serverless functions assertions (#111) Signed-off-by: David Dal Busco --- docs/build/functions.md | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/docs/build/functions.md b/docs/build/functions.md index fc3a180e..573a5c9a 100644 --- a/docs/build/functions.md +++ b/docs/build/functions.md @@ -44,6 +44,19 @@ In the stock Satellite, custom hooks are not active by default. Developers shoul --- +## Assertions + +In addition to hooks, developers have the option to expand the native rule set of their Satellites by creating custom assertions. These assertions can be implemented similarly to hooks, with the key difference being that they are synchronous and must return a result indicating the outcome of the assertion. + +| Assertion | Provider | Description | +| --------------------- | --------- | --------------------------------------------- | +| `assert_set_doc` | Datastore | Ensures a document can be created or updated. | +| `assert_delete_doc` | Datastore | Verifies that a document can be deleted. | +| `assert_upload_asset` | Storage | Confirms an asset upload can be committed. | +| `assert_delete_asset` | Storage | Checks that an asset can be deleted. | + +--- + ## Getting Started Incorporating custom Functions into a Juno Satellite requires developers to prepare their project with the necessary Rust environment. This foundational step ensures the capability to develop tailored logic within the Satellite. @@ -206,6 +219,54 @@ async fn on_delete_many_assets(context: OnDeleteManyAssetsContext) -> Result<(), Similarly to [on_set_doc](#on_set_doc), the hook can scope the events to a particular list of collections or be left empty if it should never fire. +### assert_set_doc + +Ensures a document can be created or updated. + +```rust +#[assert_set_doc] +fn assert_set_doc(_context: AssertSetDocContext) -> Result<(), String> { + // Custom logic for asserting a document's creation or update is possible + Ok(()) +} +``` + +### assert_delete_doc + +Verifies that a document can be deleted. + +```rust +#[assert_delete_doc] +fn assert_delete_doc(context: AssertDeleteDocContext) -> Result<(), String> { + // Custom logic for asserting a document can be deleted + Ok(()) +} +``` + +### assert_upload_asset + +Confirms an asset upload can be committed. + +```rust +#[assert_upload_asset] +fn assert_upload_asset(_context: AssertUploadAssetContext) -> Result<(), String> { + // Custom logic for asserting an asset upload is possible + Ok(()) +} +``` + +### assert_delete_asset + +Checks that an asset can be deleted. + +```rust +#[assert_delete_asset] +fn assert_delete_asset(_context: AssertDeleteAssetContext) -> Result<(), String> { + // Custom logic for asserting an asset can be deleted + Ok(()) +} +``` + ### Including the Satellite After defining your Functions, at the very end of your `lib.rs` module, include the Satellite to ensure that your custom logic and the default features or Juno are properly registered and executable within the Juno ecosystem. @@ -214,6 +275,8 @@ After defining your Functions, at the very end of your `lib.rs` module, include include_satellite!(); ``` +--- + ## Additional Notes WebAssembly (Wasm) binaries serve as the compilation target for the Satellites. While Juno's CLI automatically specifies this target for you, manual execution of certain `cargo` commands necessitates explicitly providing this target.