CouchBase lite and php

Hello all,

I am new to couchbase, but I have successfully created my first app using couchbase lite. Unfortunately, for a couple reasons sync gateway is not the solution I need to get my app synced with a server. The main reason is I must upload all data through php. Another is that the nature of my app requires me to mainly push data up to the server and then delete uploaded data from device storage. This app is logging large amounts of data and it is inappropriate to keep the data stored on the device. I am instead trying to insert the data into the database through php and then once the data is confirmed to be uploaded, the device safely deletes the data.

Has anyone created something like this or know of an example? I am a little lost on the implementation. Specifically I am unsure if I should just upload the document to php as a file, then use the php-couchbase-sdk to upload it to the server. Or if there is a way I can upload the document and have it inserted directly into the database though php.

Any feedback and thoughts are appreciated.

You can do this, but you’d be using Couchbase Lite just as a database, not for its sync capabilities. Or rather, you’d be implementing your own push replicator instead of using ours.

The basic approach is:

  1. Keep track (per-database) of the latest sequence number you’ve pushed. Initially this is 0.
  2. Run a query for the IDs of docs with sequences greater than the last-sequence-number-pushed.
  3. Iterate over those docs, in order by sequence, reading each from the database and uploading it to your PHP script.
  4. When a doc is successfully uploaded, update your last-sequence-pushed to its sequence number, and purge (not delete!) the document.
1 Like

Hi @mickcoyne1999,

This is related to this Stack Overflow question?

Can I ask why you need to upload through PHP? Is it transforming the data somehow? It just seems like you’re losing some nice features of the sync mechanism. Letting CBL manage the sync and then purging docs seems to fit otherwise.

1 Like

Thank you for the suggested approach! I will be using it, or a variation of it. Considering multiple devices will be uploading data at once from independent couchbase lite databases I will need to figure a way to ensure the documents have unique IDs. I imagine I will define each user to have a UUID and each doc will be that UUID + an increment.

And yes I am using couchbase lite just as a database. It is easier since the server database is couchbase. It is unfortunate that my project requires me to use PHP.

Yes, and I just marked your answer as a solution. I appreciate the time you took to explain how I could use the syncing functionalities for this project. Unfortunately, my project requirements were updated such that I must upload all the data through PHP. It is unfortunate as I am now unable to take full advantage of couchbase lite. This project could be completed without couchbase lite at this point really, but I might as well keep using it.

This might be a silly question, but since I have the document object on mobile, can I upload it to php with post then upsert that document in php? Or do I have to some how break the values of the document down and rebuild the document with the php sdk. I know this isn’t how it was intended to be used but I am in too deep now.

Yeah, you can receive the JSON body of the doc in PHP and then directly write that to the bucket without needing to parse it.

(This will not work if you’re also using Couchbase’s replicator, but if your PHP script is the only connection between mobile and the bucket, you’re OK.)