Back

Best way to modify database from functions?

  • 0
  • Self Hosted
  • Functions
  • Android
Daniel
10 Aug, 2023, 13:57

No but like the code will be in the cloud function can I use localhost because I think that if I use the server's url then it would be slow in terms of performance

TL;DR
The user wants to know how to modify a database from a function. They also ask about the RuntimeRequest and RuntimeResponse objects, and inquire about using the Kotlin server SDK for Java functions. The user is informed about the recommended specifications for the server and the expected requests per second. They are advised to use the internal IP for efficient performance. Two options are provided for initializing the client with the endpoint. The user confirms understanding and mentions they are using a cloud function for Appwrite and want to modify a collection in a database. They ask about using the server's URL or localhost for performance. The response clarifies that localhost cannot be used and
Daniel
10 Aug, 2023, 13:58

Sorry if I am not making sense but I believe it will affect the performance

Binyamin
10 Aug, 2023, 13:58

You're

Daniel
10 Aug, 2023, 13:58

Let me try to make things more clearer

Binyamin
10 Aug, 2023, 13:58

There few ways around it

Daniel
10 Aug, 2023, 14:00

I have made a cloud function for appwrite:

TypeScript
import java.util.Map;
import java.util.HashMap;

public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {

    Map response = new HashMap();
        response.put("message", "Hello from Appwrite!");

    return res.json(response);
}```
and I have built it.

Now I want this function to modify one of the collections inside a database.

As per you we need to use the Server SDK for it. But I need to initialize the client with an endpoint. Does using the server's url which is abc.com (as an example) result in slower performance instead of localhost? And can we even use localhost in this case?
Binyamin
10 Aug, 2023, 14:02

Btw, is this cloud or self-hosted?

Daniel
10 Aug, 2023, 14:02

Self hosted

Binyamin
10 Aug, 2023, 14:02

👍

Daniel
10 Aug, 2023, 14:02

I hope that you understood my goal here?

Binyamin
10 Aug, 2023, 14:02

Yes

Binyamin
10 Aug, 2023, 14:06

You can't use localhost as it will be the function container and not your Appwrite

What you can do

  1. Use the server internal IP. For example, in my server it's 172.19.0.1 so you can insert as the endpoint. you just need to make sure it's not going to change. So you can initliaze the client like so:
TypeScript
        client
          .setEndpoint("http://172.19.0.1/v1")
          .setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
          .setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
  1. Or, you can attach (not much recommend) the main container to the runtimes network by adding this to the yml file like so
TypeScript
  appwrite:
    image: appwrite/appwrite:1.3.8
    container_name: appwrite
    <<: *x-logging
    restart: unless-stopped
    networks:
      - appwrite
      - runtimes
    labels:
      ....

Then run docker compose down && docker compose up -d and you'll be able to intiliaze the client using the container address like so:

TypeScript
        client
          .setEndpoint("http://appwrite/v1")
          .setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
          .setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
Daniel
10 Aug, 2023, 14:09

Okay so I will use the internal IP but will it be efficient in terms of performance?

Binyamin
10 Aug, 2023, 14:09

How many requests you're expecting in any given second?

Daniel
10 Aug, 2023, 14:10

I believe 4-5 currently but it may increase upto 100-120 later on

Binyamin
10 Aug, 2023, 14:10

And what are the specs of that server?

Daniel
10 Aug, 2023, 14:10

2 vCores and 4 GB RAM

Binyamin
10 Aug, 2023, 14:11

It should help, but I don't think that much performance wise

Daniel
10 Aug, 2023, 14:11

Then what should be the recommended specs?

Binyamin
10 Aug, 2023, 14:13

It's not the recommended specs as the overall use of the appwrite containers. This server is great, but when it come to many functions execution per-second in the server you may experience this https://github.com/appwrite/appwrite/issues/5629

You can try the semi-solution which will cover both the performances aspect and the local connection

Daniel
10 Aug, 2023, 14:21

Thank you. It is helpful. I will follow up in case I do need to scale up the power. My next question is that the cloud functions have python, ruby, nodejs and java as the default runtime. The server sdk isnt offered in java but does have kotlin. Can we use that for creating java functions without any bugs ?

Binyamin
10 Aug, 2023, 14:23

Yes, it should work out of the box It's recommend you'll create your function using the appwrite init function cli command

Also, you can add more runtimes by editing the _APP_FUNCTIONS_RUNTIMES variable. https://appwrite.io/docs/environment-variables#functions

Daniel
10 Aug, 2023, 14:24

Also in this sample code:

TypeScript
import java.util.Map;
import java.util.HashMap;

public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {

    Map response = new HashMap();
        response.put("message", "Hello from Appwrite!");

    return res.json(response);
}```

There's a RuntimeResponse object. I didnt find any documentation for it nor RuntimeRequest.
Binyamin
10 Aug, 2023, 14:27
Daniel
10 Aug, 2023, 14:32

Ah I see. Thank you for all your help 🙂

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