From e04236546cc0978c892bbb360ff909b3460d67a6 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 3 Apr 2015 12:40:03 -0400 Subject: Queries now properly backed by resources --- app/index.js | 21 ++++++++------- app/node_modules/okquery/index.js | 54 +++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 36 deletions(-) (limited to 'app') diff --git a/app/index.js b/app/index.js index 6d057d3..8ab7743 100644 --- a/app/index.js +++ b/app/index.js @@ -25,7 +25,6 @@ function OKCMS(options) { var db = new OKDB(options.db || 'fs'); var templateCache = this._templateCache = new OKTemplate({root: root}); var server = this._server = new OKServer({root: root}); - // Special query for getting meta info var meta = this._meta = { name: 'meta', get: function() { @@ -36,7 +35,7 @@ function OKCMS(options) { var resources = this._resources = this._createResources(resourceConfig, db, schemas); var views = this._views = - this._createViews(viewConfig, db, meta, templateCache); + this._createViews(viewConfig, db, meta, resources, templateCache); this._initViews(server, views); } @@ -75,7 +74,7 @@ OKCMS.prototype._createResources = function(resourceConfig, db, schemaCache) { }; OKCMS.prototype._createViews = function(viewConfig, db, - meta, templateCache) { + meta, resourceCache, templateCache) { viewConfig = viewConfig || {}; var self = this; var createQueries = this._createQueries.bind(this); @@ -86,7 +85,7 @@ OKCMS.prototype._createViews = function(viewConfig, db, if (!template) throw new Error(format('No template named "%s" found', templateName)); var queryConfig = config.data || []; - var queries = createQueries(queryConfig, db); + var queries = createQueries(queryConfig, resourceCache); // Instantiate! cache[route] = OKView({ route: route, @@ -113,16 +112,20 @@ OKCMS.prototype._createViews = function(viewConfig, db, } } -OKCMS.prototype._createQueries = function(queryConfig, db) { +OKCMS.prototype._createQueries = function(queryConfig, resourceCache) { queryConfig = queryConfig || {}; if (!queryConfig.length) queryConfig = [queryConfig]; return queryConfig.map(function(config) { + var type = config.type; + var resource = resourceCache[type]; + if (!resource) + throw new Error('Query configured with nonexistent resource'); // Default to "select all" query - var id = config.id || '*'; - return new OKQuery(db, { - type: config.type, - id: id + var query = config.query || '*'; + return new OKQuery({ + resource: resource, + query: query }); }); }; diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index f5db3f0..cfc8d0b 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -1,59 +1,59 @@ /** * OKQuery! - * Takes configuration and gives you something that can run a DB query - * based on the configurations. + * Takes a query spec for a resource and maps it the proper read + * methods of that resource. This is simply to separate the notions + * of queries and resources. */ -function OKQuery(db, options) { +function OKQuery(options) { if (!(this instanceof OKQuery)) return new OKQuery(db, options); options = options || {}; - if (!db) - throw new Error('No DB provided to query.'); - if (!options.type) - throw new Error('No resource type provided to query'); - var type = options.type; - var id = options.id || '*'; + if (!options.resource) + throw new Error('No resource provided to query'); + var resource = options.resource; + var type = resource.type; + var query = options.query || '*'; Object.defineProperty(this, 'type', { - value: type, + value: resource.type, writable: false }); - this.get = createQuery(db, type, id); + this.get = createQuery(resource, query); } -function createQuery(db, type, id) { - if (isDynamic(id)) { - return queryDynamic(db, type); - } else if (isSet(id)) { - return queryAll(db, type); +function createQuery(resource, query) { + if (isDynamic(query)) { + return queryDynamic(resource); + } else if (isSet(query)) { + return queryAll(resource); } else { - return querySingle(db, type, id); + return querySingle(resource, query); } } -function queryDynamic(db, type) { +function queryDynamic(resource) { return function(options) { options = options || {}; - return db.get(type, options.id); + return resource.get(options.id); } } -function queryAll(db, type) { +function queryAll(resource) { return function() { - return db.getAll(type); + return resource.all(); } } -function querySingle(db, type, id) { +function querySingle(resource, id) { return function() { - return db.get(type, id); + return resource.get(id); } } -function isDynamic(id) { - return id && id.charAt(0) === ':'; +function isDynamic(query) { + return query && query.charAt(0) === ':'; } -function isSet(id) { - return id && id === '*'; +function isSet(query) { + return query && query === '*'; } module.exports = OKQuery; -- cgit v1.2.3-70-g09d2