Sync Gateway - Create/Update new property upon replication

Hello and good day.

We are working on a mobile application for iOS that allows to create customers for the currently logged in user.
A customer should have a backend generated unique identifier property, apart from UUID. This property should be incremental. For example Last customer ID was SA01, then the next created customer ID should be SA02.

In our previous implementation, here’s how we do it:

  1. Create new customer on mobile app
  2. Mobile app to display NO CUSTOMER ID (meaning, this customer hasn’t been manually synced yet)
  3. User to manually sync customer data
  4. API will return the unique identifier property for this newly created customer
  5. Mobile app to now display SA02

The question is how to replicate this using sync gateway without using any APIs?

Thank you

(Looks like I am missing something, I do not get what the description of post has to do with the subject line of the post )

Since you need a globally unique ID , you will need a web backend service that is responsible for creating the unique Id and the corresponding user on Sync Gateway (via the _user REST endpoint). Sync Gateway _user REST endpoint for creating users is a admin interface and myst not be exposed to clients over the Internet.
So typical flow for custom authentication would be as follows

  • User signs up on mobile app.
  • Mobile app calls “signup” API on your web backend service
  • web backend generates a unique Id and creates user on Sync Gateway via _user REST endpoint.
  • A session for the user is created on Sync Gateway by the web backend
  • Web service returns the generated session Id to mobile app that then starts the replication in context of the user

It is also quite common to use OAuth for user authentication with mobile apps. Not sure is that is a consideration but we have OIDC integration supported with Sync Gateway

Hi Priya,

Thank you for your response.

In the example I have given, the mobile app creates a customer (or can be any other data, like sales transaction)
Here is the scenario that we are currently doing. This is using an API.

  1. User is offline
  2. User creates a new customer record. Sample schema would be below

{
“customer_name”: “Vikko”,
“UUID”: “f690f84d-e29b-4bce-9c99-ad25cacf20d9”,
“customer_id”: “”
}

  1. User is now online
  2. Upon doing manual sync, the property customer_id would be updated. An API would return the updated values for the customer_id property

{
“customer_name”: “Vikko”,
“UUID”: “f690f84d-e29b-4bce-9c99-ad25cacf20d9”,
“customer_id”: “SA02”
}

The question is if it is possible for the above scenario to be replicated using Couchbase Sync Gateway, without using any APIs.

For example, while using Couchbase Mobile, is this scenario possible

  1. User creates a new customer record. Sample schema would be below

{
“customer_name”: “Vikko”,
“UUID”: “f690f84d-e29b-4bce-9c99-ad25cacf20d9”,
“customer_id”: “”
}

  1. Replicator starts and completes, the data now will be

{
“customer_name”: “Vikko”,
“UUID”: “f690f84d-e29b-4bce-9c99-ad25cacf20d9”,
“customer_id”: “SA02”
}

I hope I have given a better explanation and context about the question.
Again thank you for your response.

Best regards.

Setup bi-directional replication. When the document replicates to the couchbase server backend (via Sync Gateway) in Step 2, you will need to implement server side logic to generate and update the document with customer_id . When the document is updated in the couchbase server bucket, that change will be automatically pulled down by the client (via Sync Gateway).
On Sync Gateway, you can use any of these options for being notified of a document change that can in turn call your backend web service that will generate the unique Id and update the docs in bucket… You can also use our eventing service for listening to document changes and calling into the backend service.
For implementing a backend service that talks to the couchbase server , you can use any one of our server side SDKs .or you can write a web client that uses the Sync Gateway REST API to update the document in the bucket .

Hi Priya

Sorry for the late response.

We’ll definitely take a look at this. What you mentioned seems like a viable solution.

Thank you so much.

1 Like