Scopes and Collections Configuration for Sync Gateway
This section explains how to configure Scopes and Collections for Sync Gateway. It describes configuration options and provides example configuration code.
Understanding Buckets, Scopes, and Collections
You can define 1 custom scope per database with up to 1000 custom collections. If you don’t specify a custom scope and collection, any documents you create will be saved in the default scope and collection. For more information, examples and use cases, see Scopes and Collections Support in Couchbase Mobile for Edge Applications.
Configuration Options
When upgrading to Sync Gateway 3.1, it’s not necessary to include scopes
and collections
in the configuration.
If you choose not to specify them, your previous data set will still be compatible.
Keep in mind that the provided examples are optional.
You have these options to configure Scopes and Collections Sync for Gateway:
-
custom scope/custom collection (the option we cover here)
You can define 1 custom scope per database with up to 1000 custom collections.If you don’t specify a custom scope and collection, any documents you create will be saved in the default scope and collection.
-
default scope/custom collection
You can choose the default scope with up to 1000 custom collections.If you don’t specify a custom scope and collection, any documents you create will be saved in the default scope and collection.
-
default scope/default collection
You can choose the default scope with default collection.If you don’t specify a custom scope and collection, any documents you create will be saved in the default scope and collection.
When you import a dataset created in previous versions of Couchbase Server, the documents will automatically be saved in the
_default
scope and_default
collection.
Configuration Example: custom scope/custom collection
This example covers a configuration with 1 custom Scope and 2 custom Collections:
{
"name": "db",
"bucket": "bucket",
"scopes" : {
"scope1": {
"collections": {
"collection1" : {
"sync": `
function(doc, oldDoc, meta) {
if (doc.type != "default") {
throw({forbidden : "Rejected document"})
}
channel("legacy")
}
`,
"import_filter": `
function(doc) {
return doc.type == "mobile"
}
`
},
"collection2" : {
"sync": `
function(doc, oldDoc, meta) {
channel("collection1")
}
`,
"import_filter": `
function(doc) {
return doc.type == "mobile"
}
`
}
}
}
},
"num_index_replicas": 0
}
Field | Description |
---|---|
name |
The name of the database. |
bucket |
The name of the bucket associated with the database. |
scopes |
An object representing different scopes within the database. Each scope contains collections with their respective sync and import filter functions. |
scope1 |
The name of the first scope in the database. |
collections |
An object representing collections within the |
collection1 |
The name of the first collection within |
sync |
The sync function associated with |
The sync function checks if the |
|
After the rejection, it sends a message to the |
|
import_filter |
The import filter function associated with |
The import filter checks if the |
|
collection2 |
The name of the second collection within |
sync |
The sync function associated with |
The sync function sends a message to the |
|
import_filter |
The import filter function associated with |
The import filter checks if the |
|
num_index_replicas |
The number of index replicas. In this case, it is set to 0, meaning no replicas of indexes will be created for this database. |
Configuration Example: Dynamic Access Grant
function(doc, oldDoc, meta) {
if (doc.userId) {
channelId = "channel.profile:"+ doc.userId;
// Assign document to channel
channel(channelId);
// Grant user access to channel
access(doc.userId, channelId);
}
if (doc.providerId) {
// Grant provider user access to channel
access(doc.providerId, channelId);
}
}
Field | Explanation |
---|---|
doc |
The |
oldDoc |
The |
meta |
The |
doc.userId |
Checks if the |
channelId |
The string that represents the channel where the document is assigned. |
channel() |
A function used to assign the document to a channel represented by the |
access() |
A function used to grant access to a user for a specific channel. |
doc.providerId |
Checks if the |
Configuration Example: Custom Sync Function
function(doc, oldDoc, meta) {
// Only users with provider role can update document
requireRole("provider");
if (doc.userId) {
channelId = "channel.medication:"+ doc.userId;
// Assign document to channel
channel(channelId);
// Grant user access to channel
access(doc.userId, channelId);
}
}
Field | Explanation |
---|---|
function |
The function starts with |
requireRole |
The |
if (doc.userId) |
A conditional check: |
channelId |
If the |
channel(channelId) |
The |
access(doc.userId, channelId) |
The |