Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 Feature: The ability to upsert documents #3502

Open
2 tasks done
p4-k4 opened this issue Jul 5, 2022 · 9 comments
Open
2 tasks done

馃殌 Feature: The ability to upsert documents #3502

p4-k4 opened this issue Jul 5, 2022 · 9 comments
Labels
enhancement New feature or request product / databases Fixes and upgrades for the Appwrite Database.

Comments

@p4-k4
Copy link

p4-k4 commented Jul 5, 2022

馃敄 Feature description

The ability to upsert documents. That is when performing an update, to insert it if it does not already exist.

It was explained that it's not so common but at least for me that existed coming from Firebase, and a few rest API's that supported mongoDB. Regardless of this, it's still far more economic to perform an upsert operation than to check existence by hand before deciding upon a create or update.

馃帳 Pitch

Overall, it's just far more economic to have the option to upsert a document. Saves on roundtrips, saves on code, saves on time, saves on the client doing work.

There's two ways an SDK could implement it (using dart as an example).

database.upsert(collectionId: 'myCollection', documentId: 'myDocumentId');

Or...

database.update(collectionId: 'myCollection', documentId: 'myDocumentId', upsert: true);

馃憖 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

馃彚 Have you read the Code of Conduct?

@p4-k4 p4-k4 added the feature label Jul 5, 2022
@p4-k4
Copy link
Author

p4-k4 commented Jul 5, 2022

A temporary and not so optimal solution for flutter users:

final database = Databases(client, databaseId: databaseId);
await database.upsert(collectionId: '', documentId: '', data: {});

Via this package:
https://pub.dev/packages/appwrite_extensions/
Append to your pub spec.yaml dependencies:

dependencies:
  appwrite_extensions: ^0.0.1

Note This is merely syntactic sugar for checking if a document exists, if not, insert it, otherwise update it. But doesn't address the real concern of performance or reducing roundtrips etc.

@frmatthew
Copy link

I would be grateful for this feature to be integrated into the core too. In many use cases it is irrelevant to the client whether the record needs to be created for the first time or simply updated; but at present, clients must manage whether certain documents have been inserted already or not, which can become tedious. Thx.

@weepy
Copy link

weepy commented Jul 21, 2022

+1

@stnguyen90 stnguyen90 added the product / databases Fixes and upgrades for the Appwrite Database. label Jul 21, 2022
@stnguyen90
Copy link
Contributor

Thank you for raising this issue! 馃檹 We鈥檒l dig into this and get back to you as soon as we can. 馃槉

@a-l-e-c
Copy link

a-l-e-c commented Oct 23, 2022

When attempting to insert a new Document; the Exception with message Document with the requested ID already exists. should probably also include the actual $id of the existing document so we can easily prep another batch update.

Same goes for the 409 response (with status=rejected). Would be very helpful to receive the $id in that response.

Only workaround I could think of is to do something like:

    let promises = [];
    let update_promises = [];
    
    console.time("insertdocuments");
    for (let i = 0; i < documents.length; i++) {
        let createdDocument = database.createDocument(databaseID, collectionID, documents[i][pk_Columns[collectionID]].toString(), documents[i]);
        promises.push(createdDocument);
    }

    Promise.allSettled(promises)
        .then((values) => {
            for (let i = 0; i < values.length; i++) {
                if(values[i].status == 'rejected' && values[i].reason.code == 409){
                    let updateDocument = database.updateDocument(databaseID, collectionID, documents[i][pk_Columns[collectionID]].toString(), documents[i]);
                    update_promises.push(updateDocument);
                }
            }
        })
        .then(() => {
            Promise.allSettled(update_promises)
                .then((values) => {
                    console.timeEnd("insertdocuments");
                }
            );
        }
    );

@SMARTMICROWORLD
Copy link

Thank you for raising this issue! 馃檹 We鈥檒l dig into this and get back to you as soon as we can. 馃槉

more than one years still no update馃槬

@stnguyen90 stnguyen90 added enhancement New feature or request and removed feature labels Mar 20, 2024
@tripolskypetr
Copy link

Still waiting for resolvement of that issue. It's quire critical for IP telephony cause you need to rapidly update call status by webhook subscription. Right now it throws a lot of exceptions on create/update switch so logs are ugly

@tripolskypetr
Copy link

馃憖

@jonleesmith
Copy link

+1 for requesting this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request product / databases Fixes and upgrades for the Appwrite Database.
Projects
None yet
Development

No branches or pull requests

8 participants