Does Cloudant support rewrites as functions?

喜你入骨 提交于 2019-12-08 08:17:31

问题


I have a Cloudant database, and I want to make pretty URLs for my slash-containing documents. So I define a rewrite function like so:

{
  "_id": "_design/myRewrites",
  "rewrites": "function (req2) {\n    return {path: \"../../../\" + req2.path.slice(4).join(\"%2F\")};\n}"
}

Rewrite function formatted more nicely:

function (req2) {
  return {path: "../../../" + req2.path.slice(4).join("%2F")};
}

According to the CouchDB docs, CouchDB has supported this kind of rewriting (as stringified functions) since CouchDB 1.7, but Cloudant's documentation doesn't speak about this particular functionality (only rewrites from arrays).

This is reflected in my experience when I try it out https://myAccount.cloudant.com/myDb/_design/myRewrites/_rewrite/hello/world/, I get the following response:

{"error":"unknown_command","reason":"unknown ddoc command 'rewrites'"}

However I read somewhere that Cloudant and CouchDB match their source code since 2.0, so I would expect Cloudant to support all CouchDB features. What's the deal?

Also see following tweet about this, in which IBM asks me to make a question on StackOverflow and suggests I might be on an outdated cluster: https://twitter.com/digitalheir/status/845910843934085120

My data location says "Porter, London". Could it help if I changed this?


回答1:


tl;dr: Sorry, but no. Cloudant doesn't support rewrites as functions :(

We tried your example and got the same results. Digging deeper, I can now confirm that Cloudant does not support URL rewrites via stringified functions. The service only supports rewrites using the array approach.

I can't say for sure, but I suspect that the team overlooked this feature. That said, it's unlikely that Cloudant will support rewrites as JS functions anytime soon because the current approach does not scale well, as it can bog down the database if views are frequently updated. It's similar to the reason that Cloudant recommends people use the built-in reduce functions (which are implemented in Erlang), rather than writing their own custom JavaScript reduces.

Rewrites as arrays, however, does scale. But this approach obviously won't work if you're dynamically generating URLs. In this case, we suggest moving the URL rewrite functionality to an app server. Unfortunately, this all might be a moot point if you're building a CouchApp :/

This was confusing, so thank you for pointing it out. I'm going to ask the Cloudant team to note this difference in the documentation. Hope this at least helps provide some closure. You weren't wrong for expecting it to work.



来源:https://stackoverflow.com/questions/43125155/does-cloudant-support-rewrites-as-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!