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
Here’s the actual TypeScript logic from `index.ts`:
6563
6563
6564
6564
```
6565
-
import { type AssertSetDoc, defineAssert, defineHook, type OnSetDoc} from "@junobuild/functions";import { PersonData, PersonDataSchema } from "../types";import { decodeDocData, encodeDocData, setDocStore} from "@junobuild/functions/sdk";import { Principal } from "@dfinity/principal";export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: ["demo"], assert: (context) => { // We validate that the data submitted for create or update matches the expected schema. const person = decodeDocData<PersonData>(context.data.data.proposed.data); PersonDataSchema.parse(person); }});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["demo"], run: async (context) => { const { caller, data: { key, collection, data: { after: currentDoc } } } = context; // We decode the new data saved in the Datastore because it holds those as blob. const person = decodeDocData<PersonData>(currentDoc.data); // Some console.log for demo purpose console.log( "[on_set_doc] Caller:", Principal.fromUint8Array(caller).toText() ); console.log("[on_set_doc] Collection:", collection); console.log("[on_set_doc] Data:", person.principal, person.value); // We update the document's data that was saved in the Datastore with the call from the frontend dapp. const { hello, ...rest } = person; const updatePerson = { ...rest, hello: `${hello} checked`, yolo: false }; // We encode the data back to blob. const updateData = encodeDocData(updatePerson); // We save the document for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller: caller, collection, key, doc: { version: currentDoc.version, data: updateData } }); }});
6565
+
import { type AssertSetDoc, defineAssert, defineHook, type OnSetDoc} from "@junobuild/functions";import { PersonData, PersonDataSchema } from "../types";import { decodeDocData, encodeDocData, setDocStore} from "@junobuild/functions/sdk";import { Principal } from "@icp-sdk/core/principal";export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: ["demo"], assert: (context) => { // We validate that the data submitted for create or update matches the expected schema. const person = decodeDocData<PersonData>(context.data.data.proposed.data); PersonDataSchema.parse(person); }});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["demo"], run: async (context) => { const { caller, data: { key, collection, data: { after: currentDoc } } } = context; // We decode the new data saved in the Datastore because it holds those as blob. const person = decodeDocData<PersonData>(currentDoc.data); // Some console.log for demo purpose console.log( "[on_set_doc] Caller:", Principal.fromUint8Array(caller).toText() ); console.log("[on_set_doc] Collection:", collection); console.log("[on_set_doc] Data:", person.principal, person.value); // We update the document's data that was saved in the Datastore with the call from the frontend dapp. const { hello, ...rest } = person; const updatePerson = { ...rest, hello: `${hello} checked`, yolo: false }; // We encode the data back to blob. const updateData = encodeDocData(updatePerson); // We save the document for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller: caller, collection, key, doc: { version: currentDoc.version, data: updateData } }); }});
6566
6566
```
6567
6567
6568
6568
**Explanation:**
@@ -7077,7 +7077,7 @@ juno run --src ./my-task.ts
7077
7077
Suppose you want to fetch a document and export it to a file:
This script retrieves a document using `getDoc`, selects the document ID based on the current `mode` (for example staging or production), and writes the result to `mydoc.json`, using `jsonReplacer` to handle types not supported by JSON such as `BigInt`.
@@ -7437,7 +7437,7 @@ This is useful if you want to:
7437
7437
Here's an example that calls another canister’s method and logs the result:
This example performs a call to the ICP Ledger canister using the `icrc1_balance_of` method to retrieve the token balance for the calling user. The result is printed to the log using `console.log`.
0 commit comments