appwrite / templates

Templates for Appwrite Functions ⚡️🌩️
https://appwrite.io
MIT License
106 stars 82 forks source link

🚀 Feature: Add Sync with Meilisearch for Dart #188

Closed gewenyu99 closed 8 months ago

gewenyu99 commented 9 months ago

Appwrite Functions allow you to extend and customize your Appwrite server functionality by executing your custom code. 🤩 You can learn more at our official Appwrite Functions docs.

Your task is to implement the Sync with Meilisearch function in Dart. You can look at the existing Appwrite Functions Templates in another coding language.

Your function should behave and be implemented similarly to existing Sync with Meilisearch Node.js template.

Tasks summary:

If you need any help, contact us on our Discord server.

Are you ready to work on this issue? 🤔 Let us know, and we will assign it to you 😊

Happy Appwriting!

notpritam commented 9 months ago

can you please assign this to me.. i would love to contribute on this...

Haimantika commented 9 months ago

can you please assign this to me.. i would love to contribute on this...

Hi @notpritam have assigned the issue to you! Thank you for showing interest in contributing to Appwrite! Happy Hacktoberfest 🎃

Notes:

  1. Please update us with your progress every 3 days, so that we know that you are working on it.
  2. Join us on Discord - https://appwrite.io/discord to chat about Hacktoberfest and Appwrite!
notpritam commented 9 months ago

Here is the Progress , i am about to complete this , i am new to both appwrite and meilisearch so having a little trouble,

import 'dart:convert';
import 'dart:io';
import 'utils.dart';

import 'package:http/http.dart' as http;
import 'package:meilisearch/meilisearch.dart';
import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> main(final context) async {

  final requiredEnvVars = [
    'APPWRITE_API_KEY',
    'APPWRITE_DATABASE_ID',
    'APPWRITE_COLLECTION_ID',
    'MEILISEARCH_ENDPOINT',
    'MEILISEARCH_INDEX_NAME',
    'MEILISEARCH_ADMIN_API_KEY',
    'MEILISEARCH_SEARCH_API_KEY',
  ];

  throwIfMissing(Platform.environment, requiredEnvVars);

    if (context.method == 'GET') {
      final html = interpolate(await getStaticFile('index.html'), {
        'MEILISEARCH_ENDPOINT': Platform.environment['MEILISEARCH_ENDPOINT'] as String,
        'MEILISEARCH_INDEX_NAME': Platform.environment['MEILISEARCH_INDEX_NAME'] as String,
        'MEILISEARCH_SEARCH_API_KEY':
            Platform.environment['MEILISEARCH_SEARCH_API_KEY'] as String,
      });

      context.req.response
        ..headers.contentType = ContentType.html
        ..write(html)
        ..close();
    } else {

      final client = Client()
     .setEndpoint('https://cloud.appwrite.io/v1')
     .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'])
     .setKey(Platform.environment['APPWRITE_API_KEY']);

      final databases = Databases(client);

      final meilisearch = MeiliSearchClient('http://127.0.0.1:7700', Platform.environment['MEILISEARCH_ADMIN_API_KEY'] as String);

      final index = meilisearch.index(Platform.environment['MEILISEARCH_INDEX_NAME'] as String);

      String? cursor;

      do {
        final queries = [Query.limit(100)];

        if (cursor != null) {
          queries.add(Query.cursorAfter(cursor!));
        }

        final response = await databases.listDocuments(
         databaseId: Platform.environment['APPWRITE_DATABASE_ID'] as String,
         collectionId: Platform.environment['APPWRITE_COLLECTION_ID'] as String,
        queries: queries,
        );

        final documents = response['documents'];

        if (documents.isNotEmpty) {
          cursor = documents[documents.length - 1]['\$id'];
        } else {
          print('No more documents found.');
          cursor = null;
          break;
        }

        print('Syncing chunk of ${documents.length} documents ...');
        await index.addDocuments(documents, primaryKey: '\$id');
      } while (cursor != null);

      print('Sync finished.');

      context.req.response
        ..write('Sync finished.')
        ..close();
    }

}

String interpolate(String content, Map<String, String> variables) {
  for (final entry in variables.entries) {
    content = content.replaceAll('{{${entry.key}}}', entry.value);
  }
  return content;
}
gewenyu99 commented 9 months ago

@notpritam Thanks for the update!

Haimantika commented 9 months ago

Hi @notpritam any update?

himanshubalani commented 9 months ago

Hey @gewenyu99 @Haimantika , can I work on the issue if the previous assignee is inactive?

notpritam commented 9 months ago

Hi @notpritam any update?

I am almost done with issue, i was having some problem with meilisearch, I will create a pr tomorrow after completing it

Haimantika commented 9 months ago

Hi @notpritam any update?

I am almost done with issue, i was having some problem with meilisearch, I will create a pr tomorrow after completing it

Hi, is the issue with Meilisearch solved? If not, you can raise a support thread on our Discord server and we can help you

tessamero commented 8 months ago

Closing issue due to inactivity and hacktoberfest being over, thank you for taking the time to be part of hacktoberfest this year :)