将CouchDB设置为只读模式

通过CouchDB的validation function可以将 CouchDB设置为只读模式

validate_doc_update 设置为如下函数

function(newDoc, oldDoc, userCtx) {
    if( (newDoc && newDoc._id == '_design/readonly-database') ||
        (oldDoc && oldDoc._id == '_design/readonly-database') ){
    }
    else {
        throw({forbidden : 'Database is running in read-only mode since it is being maintained. Please try again a moment later.'});
    }
}

将此函数作为设计文档加入需要设置只读的数据库

{
    "_id": "_design/readonly-database",
    "language": "javascript",
    "validate_doc_update" : "function(newDoc, oldDoc, userCtx) { if( (newDoc && newDoc._id == '_design/readonly-database') || (oldDoc && oldDoc._id == '_design/readonly-database') ){ } else { throw({forbidden : 'Database is running in read-only mode since it is being maintained. Please try again a moment later.'}); } }" }

这样就完成了, 如果要恢复,删除此设计文档即可

你可能感兴趣的:(CouchDB)