From 6e1f689cfd8f820090c4ab15519114f4d3bf929f Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 21:38:31 -0400 Subject: Overhaul DB impl Make DB schema aware Add autoincrement support Add custom ID field support --- app/node_modules/okadminview/index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'app/node_modules/okadminview/index.js') diff --git a/app/node_modules/okadminview/index.js b/app/node_modules/okadminview/index.js index b8ade49..1668900 100644 --- a/app/node_modules/okadminview/index.js +++ b/app/node_modules/okadminview/index.js @@ -57,13 +57,14 @@ function OKAdminView(options) { var config = resourceConfig[key]; var type = config.type; var staticData = config.static || {}; + // Get parent level resource var resource = resourceCache.get(config.type); if (!resource) throw new Error('Something weird is going on'); - var id = staticData[resource.idField]; + var id = resource.getID(staticData); + // Check to see if there's a more specific instance resource = resourceCache.get(type, id) || resource; if (resource.bound) { - // Resource instances implement the query API return OKQuery({resource: resource});; } else { return OKQuery({resource: resource, query: config.query}) @@ -166,7 +167,7 @@ function OKAdminView(options) { resource.assertValid(data); resource.create(data).then(function(created) { req.flash('success', {action: 'create'}); - res.redirect(303, data[resource.idField]); + res.redirect(303, resource.getID(data)); }).fail(errorHandler(req, res)); } catch (errors) { var templateData = getResourceTemplateData(metadata, resource, data); @@ -190,7 +191,7 @@ function OKAdminView(options) { resource.assertValid(data); resource.update(id, data).then(function(updated) { req.flash('success', {action: 'update'}); - res.redirect(303, '../' + updated[resource.idField]); + res.redirect(303, '../' + resource.getID(updated)); }).fail(errorHandler(req, res)); } catch (errors) { var templateData = getResourceTemplateData(metadata, resource, data); @@ -236,7 +237,7 @@ function getResourceTemplateData(meta, resource, data) { return { meta: meta, resource: { - id: data[resource.idField], + id: resource.getID(data), type: resource.type, spec: spec } @@ -293,14 +294,14 @@ function fetchIndexTemplateData(meta, queries) { } if (result.length) { - result.forEach(addToCache) + result.forEach(addData) } else { - addToCache(result); + addData(result); } - function addToCache(data) { + function addData(data) { // Report id to template under standard name - data.id = data[resource.idField]; + data.id = resource.getID(data); cache[key].data.push(data); } -- cgit v1.2.3-70-g09d2