You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: callable functions (also called custom functions) (#667)
* docs: callable functions
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
* feat: link
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
* 📄 Update LLMs.txt snapshot for PR review
---------
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Functions are a set of features enabling developers to extend the native capabilities of [Satellites](/docs/terminology.md#satellite) using Rust or TypeScript. They let you define serverless behaviors directly within your containerized environment.
2274
2274
2275
-
Triggered by events like document and asset operations, they allow you to automatically run backend code or assertions. These functions are shipped with your container and don’t require you to manage or scale your own servers.
2275
+
There are two types of functions:
2276
2276
2277
-
---
2278
-
2279
-
## How does it work?
2280
-
2281
-
Functions are defined using hooks that automatically respond to document and asset events such as create, update, or delete. This allows you to embed dynamic behavior directly into your container.
2277
+
* ([Event-driven functions](#event-driven-functions)): triggered automatically in response to actions such as a document being created or an asset being uploaded.
2278
+
* ([Callable functions](#callable-functions)): explicitly invoked from your frontend or from other services.
2282
2279
2283
-
A naive schema representation of a hook that is triggered when a document is set:
When a Function is triggered, it spawns hooks asynchronously, operating independently of the caller's action. This means that the execution of the hooks is initiated without waiting for a synchronous response, ensuring that the flow of update calls to the Satellite remains unhindered. Consequently, callers may receive feedback on their actions before the corresponding hooks have commenced their execution.
2282
+
## Event-driven Functions
2290
2283
2291
-
### Error-Resilient Execution
2284
+
Event-driven functions respond to actions occurring in your Satellite. They are triggered automatically and never invoked directly.
2292
2285
2293
-
Hooks are initiated only when there are no errors in the preceding operations. This ensures a robust and dependable execution flow, promoting reliability and consistency in the functioning of Functions.
2286
+
### Hooks
2294
2287
2295
-
### Optional
2288
+
Hooks respond to specific actions within your Satellite. They run asynchronously, independently of the caller's request - meaning the caller may receive a response before the hook has finished executing.
2296
2289
2297
-
Custom hooks are not active by default. You need to opt in to enable event-driven execution of your own logic.
Hooks are only initiated when the preceding operation completes without errors.
2302
2295
2303
2296
| Hook | Provider | Description |
2304
2297
| --- | --- | --- |
@@ -2314,11 +2307,9 @@ Custom hooks are not active by default. You need to opt in to enable event-drive
2314
2307
| `on_init` | Satellite | Called during the initialization of the Satellite. |
2315
2308
| `on_post_upgrade` | Satellite | Invoked after the Satellite has been upgraded to a new version. |
2316
2309
2317
-
---
2318
-
2319
-
## Assertions
2310
+
### Assertions
2320
2311
2321
-
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.
2312
+
Assertions run synchronously before an operation is executed. They allow you to validate or reject actions before any data is written.
2322
2313
2323
2314
| Assertion | Provider | Description |
2324
2315
| --- | --- | --- |
@@ -2329,6 +2320,21 @@ In addition to hooks, developers have the option to expand the native rule set o
2329
2320
2330
2321
---
2331
2322
2323
+
## Callable Functions
2324
+
2325
+
Callable functions are explicitly invoked — from your frontend or from other services. They expose query and update endpoints directly from your Satellite.
2326
+
2327
+
**Tip:**
2328
+
2329
+
This is conceptually similar to exposing your own endpoints on an API.
2330
+
2331
+
| Type | Description |
2332
+
| --- | --- |
2333
+
| `query` | A read-only function that returns data without modifying state. |
2334
+
| `update` | A function that can read and write state. |
2335
+
2336
+
---
2337
+
2332
2338
## Rust vs. TypeScript
2333
2339
2334
2340
You can write serverless functions in either [Rust](/docs/build/functions/development/rust.md) or [TypeScript](/docs/build/functions/development/typescript.md), depending on your needs and project goals.
@@ -2569,11 +2575,17 @@ When you're ready to implement Functions within your Juno Satellite, you'll have
2569
2575
2570
2576
---
2571
2577
2572
-
## Hooks
2578
+
## Event-driven Functions
2579
+
2580
+
Event-driven functions respond to actions occurring in your Satellite. They are triggered automatically and never invoked directly.
2581
+
2582
+
---
2583
+
2584
+
### Hooks
2573
2585
2574
2586
Hooks allow you to define event-driven logic that responds to specific actions within your Satellite. The following is a list of available hooks and their respective use cases.
2575
2587
2576
-
### on\_set\_doc
2588
+
#### on\_set\_doc
2577
2589
2578
2590
Triggered when a document is created or updated in the datastore.
2579
2591
@@ -2588,7 +2600,7 @@ Triggered when a document is created or updated in the datastore.
Used for operations that involve deleting multiple assets in a batch.
2684
2696
@@ -2693,7 +2705,7 @@ Used for operations that involve deleting multiple assets in a batch.
2693
2705
export const onDeleteManyAssets = defineHook<OnDeleteManyAssets>({ collections: [], run: async (context) => { // Custom logic for handling the deletion of multiple assets }});
2694
2706
```
2695
2707
2696
-
### on\_delete\_filtered\_assets
2708
+
#### on\_delete\_filtered\_assets
2697
2709
2698
2710
Invoked when assets are deleted according to specified filters in the storage.
2699
2711
@@ -2708,7 +2720,7 @@ Invoked when assets are deleted according to specified filters in the storage.
2708
2720
export const onDeleteFilteredAssets = defineHook<OnDeleteFilteredAssets>({ collections: [], run: async (context) => { // Custom logic for handling the deletion of filtered assets }});
2709
2721
```
2710
2722
2711
-
### on\_init
2723
+
#### on\_init
2712
2724
2713
2725
* Rust
2714
2726
* TypeScript
@@ -2729,7 +2741,7 @@ This feature is not enabled by default. To use it, you’ll need to opt in by up
2729
2741
2730
2742
This hook is not available when writing functions in TypeScript.
2731
2743
2732
-
### on\_post\_upgrade
2744
+
#### on\_post\_upgrade
2733
2745
2734
2746
* Rust
2735
2747
* TypeScript
@@ -2752,11 +2764,11 @@ This hook is not available when writing functions in TypeScript.
2752
2764
2753
2765
---
2754
2766
2755
-
## Assertions
2767
+
### Assertions
2756
2768
2757
2769
Assertions enable validation checks before specific actions are executed within your Satellite. The following is a list of available assertions and their functionalities.
2758
2770
2759
-
### assert\_set\_doc
2771
+
#### assert\_set\_doc
2760
2772
2761
2773
Ensures a document can be created or updated.
2762
2774
@@ -2771,7 +2783,7 @@ Ensures a document can be created or updated.
2771
2783
export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: [], assert: (context) => { // Custom logic for asserting a document's creation or update is possible // Throw an error if the condition fails to prevent the action }});
2772
2784
```
2773
2785
2774
-
### assert\_delete\_doc
2786
+
#### assert\_delete\_doc
2775
2787
2776
2788
Verifies that a document can be deleted.
2777
2789
@@ -2786,7 +2798,7 @@ Verifies that a document can be deleted.
2786
2798
export const assertDeleteDoc = defineAssert<AssertDeleteDoc>({ collections: [], assert: (context) => { // Custom logic for asserting a document can be deleted // Throw an error if the condition fails to prevent the action }});
2787
2799
```
2788
2800
2789
-
### assert\_upload\_asset
2801
+
#### assert\_upload\_asset
2790
2802
2791
2803
Confirms an asset upload can be committed.
2792
2804
@@ -2801,7 +2813,7 @@ Confirms an asset upload can be committed.
2801
2813
export const assertUploadAsset = defineAssert<AssertUploadAsset>({ collections: [], assert: (context) => { // Custom logic for asserting an asset upload is possible // Throw an error if the condition fails to prevent the action }});
0 commit comments