Skip to content

Commit

Permalink
Support wildcard queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-roberts committed Jan 15, 2017
1 parent 893a758 commit 603f158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const flatfile = require('flat-file-db')
const promisify = require('then-flat-file-db')
const escapeRegexp = require('escape-regex')

const db = promisify(flatfile.sync(process.env.DB_NAME || 'views.db'))

const keyRegex = (str) => {
str = str.split('*').map( s => escapeRegexp(s)).join('*')
return new RegExp('^' + str.replace('*','.*'))
}

module.exports = {
put: db.put.bind(db),
has: (key) => Promise.resolve(db.has(key)),
Expand All @@ -27,7 +33,9 @@ module.exports = {
// Get all values starting with a certain pathname and filter their views
getAll: async function getAll(options) {
const data = {}
const keys = (await module.exports.keys()).filter(key => key.startsWith(options.pathname))
const keys = (await module.exports.keys()).filter((key) => {
return options.wildcard ? key.match(keyRegex(options.pathname)) : key.startsWith(options.pathname)
})

for (let key of keys) {
data[key] = await module.exports.get(key, { before: options.before, after: options.after })
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"homepage": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db#readme",
"dependencies": {
"escape-regex": "^1.0.7",
"flat-file-db": "^1.0.0",
"then-flat-file-db": "^1.0.0"
},
Expand Down

0 comments on commit 603f158

Please sign in to comment.