Created
February 10, 2018 02:22
-
-
Save scriptmaster/d4a7cca6577e2a64e98fc1bb56c76e1b to your computer and use it in GitHub Desktop.
couchdb filters design document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "_id": "_design/filters", | |
| "_rev": "10-82eb7646e9ac6bc442fbf9ec383dfc3e", | |
| "filters": { | |
| "type": "function (doc, req) { return doc.type === req.query.type; }", | |
| "prop": "function (doc, req) { return doc[req.query.prop] === req.query.value; }", | |
| "name": "function (doc, req) { return doc.name && typeof(req.query.name)!='undefined'? doc.name.toLowerCase() == req.query.name.toLowerCase(): false }" | |
| }, | |
| "language": "javascript", | |
| "views": { | |
| "by_type": { | |
| "map": "function (doc) {\n emit(doc.type, null);\n}", | |
| "reduce": "_count" | |
| } | |
| } | |
| } | |
| { | |
| _id: '_design/filters' | |
| , | |
| filters: { | |
| type: function (doc, req) { | |
| return doc.type === req.query.type; | |
| }.toString() | |
| , | |
| prop: function (doc, req) { | |
| return doc[req.query.prop] === req.query.data; | |
| }.toString() | |
| } | |
| } | |
| { | |
| "_id": "_design/filters", | |
| "_rev": "3-fdd5c251e3f53bac2418ff746c43a71e", | |
| "filters": { | |
| "type": "function (doc, req) { return doc.type === req.query.type; }", | |
| "prop": "function (doc, req) { return doc[req.query.prop] === req.query.value; }" | |
| }, | |
| "language": "javascript", | |
| "views": { | |
| "by_type": { | |
| "map": "function (doc) {\n emit(doc.type, doc.name);\n}" | |
| } | |
| } | |
| } | |
| filters = await rdb.get('_design/filters') | |
| filters.filters.name = function(doc, req) { return doc.name.toLowerCase() == req.query.name.toLowerCase() }.toString() | |
| filters.filters.regex = function(doc, req) { | |
| if(!req.query.regex) return false; | |
| if(!doc[req.query.prop]) return false; | |
| return new RegExp(req.query.regex, req.query.regexOpts||'i').test(doc[req.query.prop]) | |
| }.toString() | |
| (const validate = function(){return false};) | |
| await rdb.put(filters) | |
| c2 = await rdb.changes({ | |
| filter: 'filters/name', | |
| query_params: {name: 'Todo 1'} | |
| }) | |
| /* | |
| db.changes({ | |
| filter: 'filters/type' | |
| }); | |
| { | |
| _id: '_design/filters', | |
| filters: { | |
| type: function (doc, req) { | |
| return doc.type === req.query.type; | |
| }.toString() | |
| } | |
| } | |
| db.changes({ | |
| filter: 'filters/type', | |
| query_params: {type: 'company'} | |
| }); | |
| db.changes({ | |
| filter: 'filters/type', | |
| query_params: {type: 'todo'} | |
| }); | |
| db.changes({ | |
| filter: 'filters/prop', | |
| query_params: {prop: name, type: 'todo'} | |
| }); | |
| { | |
| _id: '_design/views', | |
| views: { | |
| myview: function (doc) { | |
| if (doc.type === 'marsupial') { | |
| emit(doc._id); | |
| } | |
| }.toString() | |
| } | |
| } | |
| db.changes({ | |
| filter: '_view', | |
| view: 'mydesign/myview' | |
| }); | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment