Migrating to couchbase with sync gateway

We already have an existing mobile application which is based on a relational database. Let’s say the database structure goes as


where One Organization has Many Employees who have many Attendance Records, Mark Records etc.

There was an existing rest service to authenticate the user.
Another thing to note is that the existing mobile application works online only.

We are planning to rewrite this application using ionic (hybrid framework on top of cordova) and pouchdb
Initially we looked at couchdb, we looked at creating a database per organization where we store all the data related to that org in its own database.

If I give an overview of the architecture we thought about with couchdb it goes like this

User logs in to the system through a rest API, in that rest API it calls another external REST API to authenticate, after authentication it checks if any roles necessary to use the app are available, if so it checks if there is a database already created for the organization, if not create it. Return a token to the user with the organization Id. The user connects to the database using the organization Id and normal sync resumes.

If we were to use the sync gateway how would this be done.

How could we handle authentication from a separate custom service? Should we create a new user locally on couchbase if it does not exist, etc?

Do we create a channel for every organization? If so we have multiple document types, does this mean EACH record should have a organization id even though some entities are part of the organization. (e.g. - Employee belongs to only one organization and Marks belongs to one employee, using this we can trace the orgaization Marks belongs to but how without having redundant organizationIds)? (Note that in documents per organization database structure did not have this problem because everything was already isolated by organization)

We also have roles on top of limiting data per organization, how would this happen?

We might have a scenario in the future where there is a backend calculation that happens. In one of our other apps it goes like this, user saves entity, in the entity backend save service the values of the entity are used to do a calculation and adds that value to the entity before it is saved. NOW this can be done by exposing a rest web service to the calculation but what happens when the user is offline? The web service won’t be called, we can make it so that when the user comes back online the rest service is called, but what if its down at that time, do we have to then implement retry logic? Sounds too much to me, managing state, IF service was called, etc. Is it a good practice to do this in the Sync function?

Essentially what we are looking for is what would be the RECOMMENDED general architecture for this.


I think so. you can use organizationIds as part of channel,the (changed) document is associated with a set of channels. so the document should have a organization id property.


If you used Couchbase Lite, No.
The user can even modify data while offline, and it’ll be synced to the server as soon as possible.

So lets say I have an entity relationship structure of Organization -> Employee -> Attendance -> Times

Where Employee entity ONLY has OrgId. If we are to use couchbase is it that Attendance and Times also have to have OrgId even though they are by nature a sub part of Employee? Is it there a better way than duplicating the OrgId.

In couchdb it was recommended to have a DB per Organization so we don’t need to duplicate this data, in this case we don’t need to specify the orgId anywhere as the database connecting to will be correct. However in couchbase it seems channels are the recommended way of doing things?? There also seems to be a 10 or so bucket restriction on Couchbase?? What’s the optimal solution for this?

What I actually meant is (sorry I guess the paragraph was large) in a system which had a web server to handle the request (instead of sync gateway). I would edit the entity passed from the mobile device before saving it (Let’s say I call a separate web service and append that value before saving the entity). How would I do such a thing in couchbase where the sync gateway is called rather than a traditional web service. (keeping in mind lots of entities go through the same gateway for lot of tasks, replication, etc)/

if you entity is looks like this, you don’t need put orgId in all docs.

user -> channel -> documents

when Attendance is created, userId should be a property of Attendance entity,so the Attendance doc can be routed to the user.
And before this, when the user is created, the user should be assigned a user channel
for example, user1 is created,so user1 can assigned u-user1 channel ;
so when Attendance is created, Attendance can be routed to u-user1 channel ;

you should only change doc by Couchbase lite or by Sync Gateway. never change doc by Couchbase directly.

@atom_yang thanks for your help so far

So yes that works, let’s say I need to route “Times” which is a subentity of Attendance, (Times could also have subentities, Attendance also other subentities).

Organization -> Employee -> Attendance -> Times -> Other Entity

Now in this scenario “Times” will have “attendanceId” as an FK, OtherEntity will have “timeId” as an FK. now if I need to get all the time docs and otherentity docs I would need for the logged in user’s organization then what should I do. I cant simply say channels(doc.organizationId) because all channels won’t have organizationId explicitly though they would be implicitly related to organizationId via relatiohsips. The other option seems to be manually traversing the relationsips but I feel this might be a bad practice as we need to do this for EACH entity (100 entities???)? What would you suggest?

So if we are to call a external web service or such or any other business logic it is ok to do that in the sync gateway?

Shouldn’t sync gateway be more of a configuration setup? This means each time we need to write some business logic that requires the restricting of data we need to edit the sync gateway? Is this the normal practice?

so you need put orgId as a property in the docs. so the docs can be routed to the org by or
gId’s channel.

I think so, you can update the config by ADMIN REST API

Integrating External Stores or Webhooks and changes feed FYI