diff options
Diffstat (limited to 'app/node_modules/okquery/index.js')
| -rw-r--r-- | app/node_modules/okquery/index.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js new file mode 100644 index 0000000..22dd74f --- /dev/null +++ b/app/node_modules/okquery/index.js @@ -0,0 +1,56 @@ +/** + * OKQuery! + * Takes configuration and gives you something that can run a DB query + * based on the configurations. + */ +function OKQuery(db, options) { + if (!(this instanceof OKQuery)) return new OKQuery(db, options); + options = options || {}; + if (!db) + throw new Error('No DB provided to query.'); + if (!options.name) + throw new Error('No name type provided to query'); + this.name = options.name; + this.get = createQuery(db, options); +} + +function createQuery(db, config) { + var name = config.name; + var id = config.id || '*'; + if (isDynamic(id)) { + return queryDynamic(db, name); + } else if (isSet(id)) { + return queryAll(db, name); + } else { + return querySingle(db, name, id); + } +} + +function queryDynamic(db, name) { + return function(options) { + options = options || {}; + return db.get(name, options.id); + } +} + +function queryAll(db, name) { + return function() { + return db.getAll(name); + } +} + +function querySingle(db, name, id) { + return function() { + return db.get(name, id); + } +} + +function isDynamic(id) { + return id && id.charAt(0) === ':'; +} + +function isSet(id) { + return id && id === '*'; +} + +module.exports = OKQuery; |
