Skip to content

Commit

Permalink
Add embedded document notation to defaultKey #60
Browse files Browse the repository at this point in the history
  • Loading branch information
cashpw committed Nov 19, 2015
1 parent ec8c771 commit c68e287
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions filters.js
Expand Up @@ -29,3 +29,7 @@ exports.convertBytes = function(input) {
exports.to_string = function (input) {
return input !== null ? input.toString() : '';
};

exports.is_embeddedDocumentNotation = function (input) {
return /^(?:[a-zA-Z0-9_]+\.)+[a-zA-Z0-9_]+/.test(input);
}
13 changes: 12 additions & 1 deletion routes/collection.js
Expand Up @@ -30,6 +30,16 @@ var routes = function(config) {
var defaultKey = (config.defaultKeyNames && config.defaultKeyNames[dbName] && config.defaultKeyNames[dbName][collectionName]) ?
config.defaultKeyNames[dbName][collectionName] :
'_id';
var edKey = function (doc, defaultKey) {
var defaultKeyAsArray = defaultKey.split('.');
var val = doc;
for (var i = 0; i < defaultKeyAsArray.length; i++) {
if (val[defaultKeyAsArray[i]]) {
val = val[defaultKeyAsArray[i]];
}
}
return val;
};

if (key && value) {
// If type == J, convert value as json document
Expand Down Expand Up @@ -115,7 +125,8 @@ var routes = function(config) {
type: type,
query: jsonQuery,
fields: jsonFields,
defaultKey: defaultKey
defaultKey: defaultKey,
edKey: edKey
};

res.render('collection', ctx);
Expand Down
10 changes: 9 additions & 1 deletion views/collection.html
Expand Up @@ -174,7 +174,15 @@ <h3>Add Document</h3>
{% if document[defaultKey] == "" %}
{{defaultKey}} is empty for {{document["_id"]|to_string}}
{% else %}
{{document[defaultKey] |to_string}}
{% if !document[defaultKey] %}
{% if defaultKey|is_embeddedDocumentNotation %}
{{edKey(document, defaultKey)}}
{% else %}
{{defaultKey}} is undefined for {{document["_id"]|to_string}}
{% endif %}
{% else %}
{{document[defaultKey] |to_string}}
{% endif %}
{% endif %}
</a>
</div>
Expand Down

0 comments on commit c68e287

Please sign in to comment.