Back

How to create Batch document

  • 0
  • Databases
  • Flutter
new user
28 Apr, 2023, 21:48

How to create Batch document

TL;DR
The user is asking for an example of how to create a batch document in Dart using the Appwrite SDK. The provided code is in Dart and shows how to create and process multiple documents using the `createDocument` function. The user also mentions an issue with rate limits when using a loop, and a possible solution is mentioned, which is using the Server SDK with an API key that doesn't have rate limits. Another user provides a link to a GitHub issue discussing the issue with rate limits. A solution using a loop is suggested as the best way to create batch documents.
Binyamin
28 Apr, 2023, 21:51

This feature has some issues, you can read about it here https://github.com/appwrite/appwrite/pull/3285#issuecomment-1288129353

The best way would be using a loop with createDocument function.

new user
28 Apr, 2023, 21:52

using loop can give error rate limit exceed

Binyamin
28 Apr, 2023, 21:52

You need it to be for the user side?

Binyamin
28 Apr, 2023, 21:53

If so then create a function, let the user send all the new documents to the function. Then, in the function you'll use Server sdk with API key which don't suffers from rate limits

new user
28 Apr, 2023, 21:54

yes any example if can you provide for bulk creation of document

Binyamin
28 Apr, 2023, 22:02

Maybe something like this

TypeScript
import 'package:dart_appwrite/dart_appwrite.dart';
import 'dart:convert';

Future<void> start(final req, final res) async {
   final client = new Client();
   final database = Databases(client);

  if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
      req.variables['APPWRITE_FUNCTION_API_KEY'] == null) {
    print("Environment variables are not set. Function cannot use Appwrite SDK.");
    } else {
        client
            .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
            .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
            .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
            .setSelfSigned(true);
    }

    try {
        final documents = jsonDecode(req.variables.APPWRITE_FUNCTION_DATA);
        final userID = req.APPWRITE_FUNCTION_USER_ID ?? '';
      
        // check Document structure
        documents.forEach((doc)=>{
        //TODO: Add user id
        //TODO: Add permission
         await database.createDocument('[DATABASE_ID]', '[COLLECTION_ID]', '[DOCUMENT_ID]', doc);
        });
     res.json({areDevelopersAwesome: true,});
    } catch (e) {
     res.json({areDevelopersAwesome: false,});
    }
};
new user
28 Apr, 2023, 22:03

is this is in node js

Binyamin
28 Apr, 2023, 22:03

Yes

new user
28 Apr, 2023, 22:05

any example in dart, if it s possible?

Binyamin
28 Apr, 2023, 22:14

The code is now in dart

new user
28 Apr, 2023, 22:27

ok

Binyamin
28 Apr, 2023, 22:27

It's not a complete working code. But should give you a heads-up

new user
28 Apr, 2023, 22:28

i have posted another post regarding the use of currency api,

new user
28 Apr, 2023, 22:28

ok, thanks

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