diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-01 18:27:51 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-01 18:27:51 -0400 |
| commit | 2e041ff6cd1b40c26bf16d37777c2c1c5153a669 (patch) | |
| tree | 1e427659faf343bf42795beae8980000436617b8 /app/node_modules/okquery/index.js | |
| parent | ae24790e8122296b06f88fa883b8e272c6454d46 (diff) | |
Bootstrappin young lad
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; |
