Couchbase-Ecosystem / cbl-ionic

Ionic Capacitor plugin for Couchbase Lite Enterprise >= (3.2)
https://cbl-ionic.dev/
Apache License 2.0
3 stars 1 forks source link

Android doesn't properly save documents with no DocumentId #28

Closed biozal closed 5 months ago

biozal commented 5 months ago

Thanks for taking the time to fill out this issue! Please make sure to add as much detail as you can.

Describe the bug When creating a new document in Typescript using a blank constructor on MutableDocument, the document doesn't save properly.

Expected behavior I would expect when saving a document without a documentId that the system would auto generate a documentId

Current behavior The system doesn't generate the document.

Link to publc reproduction project repository N/A

To Reproduce Steps to reproduce the behavior: Here is the code in Typescript:

      for (const hotel of hotelFile.hotelData) {
        const doc = new MutableDocument()
          .setNumber('id', hotel.id)
          .setString('name', hotel.name)
          .setString('address', hotel.address)
          .setString('phone', hotel.phone)
          .setString('type', this.DOC_TYPE_HOTEL);

        //old way of saving
        //this.database.save(doc);

        //we save documents to collections now and this is an async operation
        await this.collection.save(doc);

I would expect that code to work and it does in iOS. This is because in Android, in our CollectionManager code we don't check the documentId to see if the documentId is blank and if it is so create the MutableDocument without a blank ID so that it get's properly created:

 val col = this.getCollection(collectionName, scopeName, databaseName)
        col?.let { collection ->
            concurrencyControl?.let {
                val mutableDocument = MutableDocument(documentId, document)
                val result = collection.save(mutableDocument, it)
                if (result) {
                    return Pair(mutableDocument.id, true)
                } else {
                    return Pair(mutableDocument.id, false)
                }
            }
            val mutableDocument = MutableDocument(documentId, document)
            collection.save(mutableDocument)
            return Pair(mutableDocument.id, null)
        }

In Swift, this is handle this properly

let mutableDocument: MutableDocument
        if !documentId.isEmpty {
            mutableDocument = MutableDocument(id: documentId, data: MapHelper.toMap(document))
        } else {
            mutableDocument = MutableDocument(data: MapHelper.toMap(document))
        }

Plugin Version 1.0 beta

Affected Platforms (please complete the following information):

Do you have a workaround? [ ] Yes [X] No

Provide and details of the work around below:

Additional Information The bug is in CollectionManager.kt in the Ionic plugin and needs to check the documentId passed in to see if it's empty or not and if so create the document without sending the Id so that an automated UUID is used.