Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wildcard queries #1

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

@mxstbr mxstbr Jan 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work I'm pretty sure, since options.wildcard comes from the query param it'll be "false", a string, not false, a boolean. (also this should be opt-out I thought, not opt-in)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You wouldn't have known this because I haven't don't the analytics side, but I did not plan on sending the query param down. I planned on sending the bool value down to it based on the query param. As a practice, we should always send down actual bool values instead of having any type of adapter have to worry about that nuance

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you're right on the opt-out part. That was a last minute swap

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also going to change it to ignoreWildcard as that makes more sense

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 you're right, good call on sending down the booleans!

})

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