Skip to content

Commit b927f65

Browse files
collection_wiper
1 parent 29e0782 commit b927f65

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.Map;
2+
import java.util.HashMap;
3+
import java.util.*;
4+
import io.openruntimes.java.*;
5+
import com.google.gson.Gson;
6+
import io.appwrite.Client;
7+
import io.appwrite.exceptions.AppwriteException;
8+
import io.appwrite.services.Databases;
9+
import kotlin.Result;
10+
import kotlin.coroutines.Continuation;
11+
import kotlin.coroutines.CoroutineContext;
12+
import kotlin.coroutines.EmptyCoroutineContext;
13+
import org.jetbrains.annotations.NotNull;
14+
import okhttp3.Response;
15+
import com.google.gson.JsonParser;
16+
import com.google.gson.JsonObject;
17+
18+
final Gson gson = new Gson();
19+
20+
public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {
21+
// Initialize the Appwrite client and databases service
22+
Client client = new Client();
23+
var variables = req.getVariables();
24+
String payloadString = req.getPayload().toString();
25+
Map<String, Object> responseData = new HashMap<>();
26+
27+
// Check if the required environment variables are set
28+
if (variables == null
29+
|| !variables.containsKey("APPWRITE_FUNCTION_ENDPOINT")
30+
|| !variables.containsKey("APPWRITE_FUNCTION_API_KEY")
31+
|| !variables.containsKey("APPWRITE_FUNCTION_PROJECT_ID")
32+
|| variables.get("APPWRITE_FUNCTION_ENDPOINT") == null
33+
|| variables.get("APPWRITE_FUNCTION_API_KEY") == null
34+
|| variables.get("APPWRITE_FUNCTION_PROJECT_ID") == null) {
35+
return res.json(Map.of("Environment variables are not set. Function cannot use Appwrite SDK.", false));
36+
} else {
37+
// Set the Appwrite client properties
38+
client
39+
.setEndpoint(variables.get("APPWRITE_FUNCTION_ENDPOINT"))
40+
.setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
41+
.setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
42+
}
43+
44+
try {
45+
// Parse the payload data into a Map
46+
Databases databases = new Databases(client);
47+
Map<String, String> payload = gson.fromJson(payloadString, Map.class);
48+
String databaseId = (String) payload.get("databaseId");
49+
String collectionId = (String) payload.get("collectionId");
50+
51+
if (databaseId == null || collectionId == null) {
52+
return res.json(Map.of("Invalid payload.", false));
53+
}
54+
55+
databases.deleteCollection(
56+
databaseId,
57+
collectionId,
58+
new Continuation<Object>() {
59+
@NotNull
60+
@Override
61+
public CoroutineContext getContext() {
62+
return EmptyCoroutineContext.INSTANCE;
63+
}
64+
65+
@Override
66+
public void resumeWith(@NotNull Object o) {
67+
String json = "";
68+
try {
69+
if (o instanceof Result.Failure) {
70+
Result.Failure failure = (Result.Failure) o;
71+
throw failure.exception;
72+
} else {
73+
Response response = (Response) o;
74+
}
75+
} catch (Throwable th) {
76+
System.out.print("ERROR: " + th.toString());
77+
}
78+
}
79+
}
80+
);
81+
82+
return res.json(Map.of("success", true));
83+
} catch (AppwriteException e) {
84+
return res.json(Map.of("Collection not found.", false));
85+
}
86+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Appwrite Collection Wiper
2+
3+
Welcome to the documentation of this function 👋 We strongly recommend keeping this file in sync with your function's logic to make sure anyone can easily understand your function in the future. If you don't need documentation, you can remove this file.
4+
5+
## 🤖 Documentation
6+
7+
A Java Cloud Function that wipes all the documents inside a collection.
8+
9+
_Example input:_
10+
11+
```json
12+
{
13+
"databaseId": "stage",
14+
"collectionId": "profiles"
15+
}
16+
```
17+
18+
_Example output (_sucess_):_
19+
20+
```json
21+
{
22+
"success": true,
23+
}
24+
```
25+
26+
_Example output (_failure_):_
27+
28+
```json
29+
{
30+
"success": false,
31+
"message": "Collection not found."
32+
}
33+
```
34+
35+
## 📝 Environment Variables
36+
37+
APPWRITE_FUNCTION_ENDPOINT - Endpoint of your Appwrite server
38+
APPWRITE_FUNCTION_API_KEY - Appwrite API Key
39+
APPWRITE_FUNCTION_PROJECT_ID - Appwrite project ID. If running on Appwrite, this variable is provided automatically.
40+
41+
## 🚀 Deployment
42+
43+
1. Clone this repository, and enter this function folder:
44+
45+
$ git clone https://github.com/open-runtimes/examples.git && cd examples
46+
$ cd java/wipe_appwrite_collection
47+
48+
2. Enter this function folder and build the code:
49+
50+
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=Index.java --rm --interactive --tty --volume $PWD:/usr/code openruntimes/java:v2-18.0 sh /usr/local/src/build.sh
51+
As a result, a code.tar.gz file will be generated.
52+
53+
3. Start the Open Runtime:
54+
55+
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/java:v2-18.0 sh /usr/local/src/start.sh
56+
Your function is now listening on port 3000, and you can execute it by sending POST request with appropriate authorization headers. To learn more about runtime, you can visit Java runtime README.
57+
58+
4. Execute function:
59+
60+
curl http://localhost:3000/ -d '{"variables":{"APPWRITE_FUNCTION_ENDPOINT":"YOUR_ENDPOINT","APPWRITE_FUNCTION_PROJECT_ID":"YOUR_PROJECT_ID","APPWRITE_FUNCTION_API_KEY":"YOUR_API_KEY"},"payload":"{\"databaseId\":\"stage\",\"collectionId\":\"profiles\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
61+
62+
### Using CLI
63+
64+
Make sure you have [Appwrite CLI](https://appwrite.io/docs/command-line#installation) installed, and you have successfully logged into your Appwrite server. To make sure Appwrite CLI is ready, you can use the command `appwrite client --debug` and it should respond with green text `✓ Success`.
65+
66+
Make sure you are in the same folder as your `appwrite.json` file and run `appwrite deploy function` to deploy your function. You will be prompted to select which functions you want to deploy.
67+
68+
### Manual using tar.gz
69+
70+
Manual deployment has no requirements and uses Appwrite Console to deploy the tag. First, enter the folder of your function. Then, create a tarball of the whole folder and gzip it. After creating `.tar.gz` file, visit Appwrite Console, click on the `Deploy Tag` button and switch to the `Manual` tab. There, set the `entrypoint` to `src/Index.java`, and upload the file we just generated.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies {
2+
implementation 'com.google.code.gson:gson:2.9.0'
3+
implementation 'io.appwrite:sdk-for-kotlin:1.2.0'
4+
}

0 commit comments

Comments
 (0)