summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-03 12:40:03 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-03 12:40:03 -0400
commite04236546cc0978c892bbb360ff909b3460d67a6 (patch)
treecb0d8eedece34ca8d407dc8b1d07a1223c88164f
parent7d15bc8a49121dd85171d7837c4f6a97a7e096dc (diff)
Queries now properly backed by resources
-rw-r--r--app/index.js21
-rw-r--r--app/node_modules/okquery/index.js54
-rw-r--r--examples/index.js10
3 files changed, 44 insertions, 41 deletions
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;
diff --git a/examples/index.js b/examples/index.js
index 1e74968..7e0c6f9 100644
--- a/examples/index.js
+++ b/examples/index.js
@@ -28,18 +28,18 @@ var app = okcms.createApp({
views: {
'/': {
data: [
- {type: 'project', id: '*'},
- {type: 'page', id: '*'}
+ {type: 'project', query: '*'},
+ {type: 'page', query: '*'}
]
},
'/about': {
- data: {type: 'page', id: 'about'}
+ data: {type: 'page', query: 'about'}
},
'/contact': {
- data: {type: 'page', id: 'contact'}
+ data: {type: 'page', query: 'contact'}
},
'/:id': {
- data: {type: 'project', id: ':id'}
+ data: {type: 'project', query: ':id'}
}
}