Permalink
Browse files

Merge branch 'master' into FAKER

  • Loading branch information...
2 parents 155dcfd + 9d3cbaa commit e82e0d4da3f9371a3f028f4e19728a09abc61191 @goliatone committed Jul 23, 2014
Showing with 54 additions and 18 deletions.
  1. +12 −1 README.md
  2. +9 −8 libs/filedb.js
  3. +29 −6 routes/restful.js
  4. +2 −2 routes/uploader.js
  5. +2 −1 views/restful/result.jtpl
View
@@ -2,7 +2,8 @@
- DONE ~~Output JSON templates~~
- DONE ~~FileDB metadata: indexing, count~~
- TODO Resources: list and details
-- TODO Routes: restful list, take in where/params
+- TODO ~~Routes: restful list, take in where/params~~
+ - TODO: Implement optional resource schema to handle type in queries
- DONE ~~Sanitize output file name~~
- TODO Take in filename parameter!
- DONE ~~Files: Format conversion library. Support CSV, JSON~~
@@ -21,6 +22,16 @@ Context, gathered from FileDB metadata and results.
* Result data: Array|Object
* success: boolean
+## Restful:
+*NOTE* Right now we do not type check resource objects, so conversions to JSON are left to chance, ie a field containing a number might get parsed as a string. This is OK most of the time, but it could break your `where` queries on the `list` endpoint:
+
+```
+http://localhost:9000/api/athletes?where={"age":23}
+```
+
+```
+http://localhost:9000/api/athletes?where={"age":"23"}
+```
## Resources
- [Browser library][1]
View
@@ -29,9 +29,11 @@ JsonDB.metadata = {};
JsonDB.loadMetadata = function() {
if (this.loaded) return;
- if (fs.existsSync(file)) return;
+ var dirname = JsonDB.resourcesPath + JsonDB.metadataFile;
- this.load(JsonDB.resourcesPath + JsonDB.metadataFile, function(err, data) {
+ if (fs.existsSync(dirname)) return;
+
+ this.load(dirname, function(err, data) {
try {
JsonDB.metadata = JSON.parse(data);
} catch (err) {
@@ -69,7 +71,6 @@ JsonDB.indexMetadata = function() {
JsonDB.updateMetadata(resource, file, items);
count++;
if (total === count) callback(null, JsonDB.metadata);
-
});
} catch (e) {
@@ -118,11 +119,11 @@ var FileDB = function(file, config) {
* @return {void}
* @throws {Error} If file does not exist.
*/
-FileDB.prototype.find = function(attrs, cb) {
+FileDB.prototype.find = function(options, cb) {
//predicate is optional
- if (_.isFunction(attrs)) {
+ if (_.isFunction(options)) {
cb = attrs;
- attrs = undefined;
+ options = undefined;
}
_exists(this.file, cb);
@@ -140,9 +141,9 @@ FileDB.prototype.find = function(attrs, cb) {
return;
}
- attrs = _convertValueToFilter(attrs, this.idAttr);
+ options = _convertValueToFilter(options, this.idAttr);
- if (attrs) docs = _.where(docs, attrs);
+ if (options) docs = _.where(docs, options);
cb(null, docs);
}).bind(this));
View
@@ -40,6 +40,7 @@ Routes.resultHandler = function(action, resource, res, err, result) {
resource: resource,
message: Routes.messages[action],
count: result.length || 1,
+ total: 9999,
data: result
});
@@ -51,17 +52,39 @@ Routes.resultHandler = function(action, resource, res, err, result) {
Routes.list = function(req, res, next) {
// var where = JSON.parse(req.param('where'));
- // console.log(where);
- // console.log('HERE')
+ var where = req.param('where', false);
+ if (where) {
+ where = JSON.parse(where);
+ } else where = undefined;
+
var resource = req.params.resource;
- db(resource).find(Routes.resultHandler.bind(null, 'list', resource, res));
+ db(resource).find(where, function(err, items) {
+
+ var offset = req.param('offset', false),
+ limit = req.param('limit', 30),
+ output = items;
+
+ if (offset) {
+ //TODO: Check boundaries!!!
+ //TODO: Check if isNaN after parseInt!
+ offset = parseInt(offset),
+ limit = parseInt(limit);
+
+ offset = offset > 0 ? offset - 1 : 0;
+
+ output = items.slice(offset, offset + limit);
+ }
+
+ Routes.resultHandler('list', resource, res, null, output);
+
+ });
};
// POST /:resource
Routes.create = function(req, res, next) {
- db(req.params.resource).insert(req.body, function(err, resource) {
- res.jsonp(resource);
- });
+ var resource = req.params.resource,
+ attributes = req.body;
+ db(resource).insert(attributes, Routes.resultHandler.bind(null, 'create', resource, res));
};
// GET /:resource/:id
View
@@ -84,8 +84,8 @@ function _getFile(req, options) {
function _outputPath(req, file, options) {
filename = req.param(options.filenameParam, file.originalname);
- filename = filename.replace(file.extension, options.outputExtension);
- filename = options.sanitizeFilename(filename);
+ filename = filename.replace('.' + file.extension, '');
+ filename = options.sanitizeFilename(filename) + '.' + options.outputExtension;
//TODO: WATCH OUT FOR MALFORMED PATHS!!!!!
return options.outputDir + '/' + filename;
}
@@ -1,8 +1,9 @@
{
"succes": true,
- "count": {{count}},
"message": "{{message}}",
"data": {
+ "total": {{total}},
+ "count": {{count}},
"{{resource}}": {{{data}}}
}
}

0 comments on commit e82e0d4

Please sign in to comment.