Back

[SOLVED] How functions work

  • 2
  • Functions
XaFigg
12 Aug, 2023, 15:39

Thank you so much ! This information will serve me as sure soon, but unfortunately it does not answer my question ^^

The problem is that I don't execute anything on the client side, the function is automatically triggered with an event (like creating a user)

TL;DR
The user had two questions: 1) How to prevent triggers from being deleted every time a function is deployed via Appwrite CLI. Solution: Currently, there is no way to prevent triggers from being deleted when deploying via Appwrite CLI. 2) Is TypeScript supported? Solution: Yes, TypeScript is supported by Appwrite, but it needs to be transpiled before deploying. The user also had an issue with creating a bucket and received the error "User (role: guests) missing scope (buckets.write)". The issue was resolved by updating the code to include the necessary permissions in the bucket creation request. Additionally, the user asked about
XaFigg
12 Aug, 2023, 15:39

In this case, how can I retrieve the user information that was created?

safwan
12 Aug, 2023, 15:40

Ahhhh okay! In that case, you can use Realtime.

XaFigg
12 Aug, 2023, 15:40

Realtime ?

safwan
12 Aug, 2023, 15:40

Alright one sec

safwan
12 Aug, 2023, 15:40

You can read detailed information about Realtime here: https://appwrite.io/docs/realtime

safwan
12 Aug, 2023, 15:40

But in short, it's a way for you to subscribe to changes being made to anything in your Appwrite project

safwan
12 Aug, 2023, 15:41

There's a lot of channels that you can subscribe to, which can be found here: https://appwrite.io/docs/realtime#channels

XaFigg
12 Aug, 2023, 15:41

But we agree that I don't need to run anything with any SDK?

safwan
12 Aug, 2023, 15:41

Realtime works best with the SDK that appwrite supplies

safwan
12 Aug, 2023, 15:42

Have a look at the Realtime docs. It should help you

safwan
12 Aug, 2023, 15:43

For example, if your function is triggered by a new document creation, you can use the new document's ID (which is returned after the document is created) to subscribe to any changes being made to that document

safwan
12 Aug, 2023, 15:43

I hope that makes sense

XaFigg
12 Aug, 2023, 15:44

I think I understood something. I'll try, but I don't think it's exactly the same thing I want to do. I'll be back in about ten minutes to try something then I'll let you know ^^

XaFigg
12 Aug, 2023, 16:00
TypeScript
const { Client, Storage, Query, Permission } = require("node-appwrite")

module.exports = async function (req, res) {
    const client = new Client()
    const storage = new Storage(client)
    client.setEndpoint(req.variables.APPWRITE_FUNCTION_ENDPOINT)
        .setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
        .setKey(req.variables.APPWRITE_FUNCTION_API_KEY)

    const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
    const { userId } = JSON.parse(user)
    const bucketId = `user-${userId}`

    const list = await storage.listBuckets([Query.equal("$id", [bucketId])])
    if (list.total) {
        res.send(`Bucket ${bucketId} already exists`, 409)
        return
    }

    const bucket = await storage.createBucket(bucketId, bucketId, [
        Permission.read(Role.user(userId)),
        Permission.write(Role.user(userId)),
        Permission.delete(Role.user(userId))
    ], true)
    res.json(bucket, 201)
}

This is the kind of functions that will be triggered by the creation event of a user. For the moment I have a connection refused error when running, but the idea is there ^^

Drake
12 Aug, 2023, 16:18

Look at the event data variable. See https://appwrite.io/docs/functions#functionVariables

XaFigg
12 Aug, 2023, 16:25

Thank you ^^ Indeed I was able to retrieve the data, however I don't understand why I can't create a bucket :/ Here is my error and my code:

TypeScript
const { Client, Storage, Role, Permission } = require("node-appwrite")

module.exports = async function (req, res) {
    const client = new Client()
    const storage = new Storage(client)
    client.setEndpoint("https://back.transferra.cc/v1")
        .setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
        .setKey(req.variables.APPWRITE_FUNCTION_API_KEY)

    const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
    const { userId } = JSON.parse(user)
    const bucketId = `user-${userId}`

    const bucket = await storage.createBucket(bucketId, bucketId, [
        Permission.read(Role.user(userId)),
        Permission.write(Role.user(userId)),
        Permission.delete(Role.user(userId))
    ], true)
    res.json(bucket, 201)
}
TypeScript
Error: User (role: guests) missing scope (buckets.write)
    at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Storage.createBucket (/usr/code-start/node_modules/node-appwrite/lib/services/storage.js:115:16)
    at async module.exports (/usr/code-start/src/index.js:27:20)
    at async /usr/local/src/server.js:68:13

I use an API key with rights to create buckets.

Drake
12 Aug, 2023, 16:32

User guest means API key wasn't set 🧐 you created a variable and put in the API key? Maybe you can update your code to log the Appwrite function API key variable

XaFigg
12 Aug, 2023, 16:36

Indeed error on my part, I had well defined the variable but the content of the key was wrong 🤣 I have two questions and then I think it's all good for me.

When I deploy a function via Appwrite CLI, the triggers get deleted every time and I have to recreate them from the appwrite interface. How to prevent this?

My second question: Is TypeScript supported?

Drake
12 Aug, 2023, 16:41

You will need to put your variables and any other function settings in your appwrite.json.

Drake
12 Aug, 2023, 16:42

For typescript, for now, you'll have to transpile before deploying and deploy the transpiled code.

XaFigg
12 Aug, 2023, 16:46

Oh okay ! That's damn good lol A big thank you to you both steven and safwan ^^

The more I use Appwrite, the more I love it, the team too :'))

XaFigg
12 Aug, 2023, 17:32

It's good ! Everything works great :)

XaFigg
12 Aug, 2023, 17:32

[SOLVED] How functions work

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more