Unable to include sync and import filter functions using backticks and initialize a sync gateway database

I am trying to use the following file for creating a sync gateway database. It is a variation of the example in the official documentation.

{
  "name": "db",
  "bucket": "bucket",
  "scopes": {
    "_default": {
      "collections": {
        "content": {
          "sync": `
              function(doc, oldDoc, meta) {
                  channel(doc.module_identifier)
              }
          `,
          "import_filter": `
            function(doc) {
              return true;
            }
          `
        }
      }
    }
  },
  "num_index_replicas": 0
}

I am basically adding the functions to the collection content in _default scope.

It gives the following error (command run is included in the code block below):

curl -v --location --request PUT 'localhost:4985/main/' --header 'Content-Type: application/json' --header 'Authorization: Basic <auth-token>' --data-raw $(cat sg-create-db-test-1.json)
*   Trying 127.0.0.1:4985...
* Connected to localhost (127.0.0.1) port 4985 (#0)
> PUT /main/ HTTP/1.1
> Host: localhost:4985
> User-Agent: curl/8.1.2
> Accept: */*
> Content-Type: application/json
> Authorization: Basic c3luY19nYXRld2F5OnBhc3N3b3Jk
> Content-Length: 1
>
< HTTP/1.1 500 Internal Server Error
< Content-Type: application/json
< Server: Couchbase Sync Gateway/3.1.1 CE
< Date: Mon, 23 Oct 2023 07:43:09 GMT
< Content-Length: 80
<
* Connection #0 to host localhost left intact
{"error":"Internal Server Error","reason":"Unable to read body: unexpected EOF"}* URL rejected: Port number was not a decimal number between 0 and 65535
* Closing connection -1
curl: (3) URL rejected: Port number was not a decimal number between 0 and 65535
* URL rejected: Bad hostname
* Closing connection -1
curl: (3) URL rejected: Bad hostname
* URL rejected: Port number was not a decimal number between 0 and 65535
* Closing connection -1
curl: (3) URL rejected: Port number was not a decimal number between 0 and 65535
* URL rejected: Bad hostname
* Closing connection -1
curl: (3) URL rejected: Bad hostname
* URL rejected: Port number was not a decimal number between 0 and 65535
* Closing connection -1
curl: (3) URL rejected: Port number was not a decimal number between 0 and 65535
curl: (3) unmatched brace in URL position 1:
{
 ^

Here are the environment details:

  • couchbase/server:community-7.2.2
  • couchbase/sync-gateway:3.1.1-community
  • macOS Sonoma 14.0
  • curl 8.1.2 (x86_64-apple-darwin23.0) libcurl/8.1.2 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.55.1
  • all are docker images

I also tried removing the backticks and having the functions in a single line enclosed in double quotes. That didn’t work either.

I am guessing that this is a minor issue in one of my environment setup.

Any help would be appreciated. Thanks in advance.

In the curl command the --data-raw needs to be quoted:

curl -v --location --request PUT 'localhost:4985/main/' --header 'Content-Type: application/json' --header 'Authorization: Basic <auth-token>' --data-raw '$(cat sg-create-db-test-1.json)'

Getting the following error.

{"error":"Internal Server Error","reason":"Unable to read body: invalid character '$' looking for beginning of value"}

Sorry, this will need double quotes, or it uses $(cat sg-create-db-test-1.json) as the literal string.

Yeah, I am wondering why the following works but doesn’t when you try to cat the file

curl -v --location --request PUT 'localhost:4985/main-content/' --header 'Content-Type: application/json' --header 'Authorization: Basic <token>' \
--data-raw '{
  "name": "main-content",
  "bucket": "main",
  "guest":{"disabled":true},
  "import_docs":true,
  "enable_shared_bucket_access":true,
  "scopes": {
    "_default": {
      "collections": {
        "content": {
          "sync": `
              function(doc, oldDoc, meta) {
                channel(doc.module_identifier);
              }
          `,
          "import_filter": `
            function(doc) {
              return true;
            }
          `
        }
      }
    }
  },
  "num_index_replicas": 0
}'

*   Trying 127.0.0.1:4985...
* Connected to localhost (127.0.0.1) port 4985 (#0)
> PUT /main-content/ HTTP/1.1
> Host: localhost:4985
> User-Agent: curl/8.1.2
> Accept: */*
> Content-Type: application/json
> Authorization: Basic <token>
> Content-Length: 520
>
< HTTP/1.1 201 Created
< Server: Couchbase Sync Gateway/3.1.1 CE
< Date: Mon, 23 Oct 2023 15:26:23 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

I also tried using external files for javascript functions and even that didn’t work. Are there any file encoding stuff (I am not comfortable in that area) that we need to take care of to get this to work?

The reason this doesn’t work when you use cat is because all of the json is interpreted by the shell, as per rules of double quotes in shell. Here’s an alternative:

curl -v --location --request PUT 'localhost:4985/main/' --header 'Content-Type: application/json' --header 'Authorization: Basic <auth-token>' --data @sg-create-db-test-1.json
1 Like

Thank you. It works.

Why doesn’t using external files work? Are there any gotchas there?

The original problems may have nothing to do with the inline javascript or files, but rather shell quoting. I do not expect any problems with javascript in a file.

yeah yeah. original problem doesn’t have anything to do with external files. i will try again and create a separate post if it exists.