Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: goliatone/fakeful
base: 0da70199e1
...
head fork: goliatone/fakeful
compare: d54c5d4887
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 68 additions and 13 deletions.
  1. +3 −3 README.md
  2. +57 −10 libs/filedb.js
  3. +8 −0 routes/restful.js
View
6 README.md
@@ -1,9 +1,9 @@
## TODO:
-- TODO Output JSON templates
-- TODO FileDB metadata: indexing, count
+- DONE ~~Output JSON templates~~
+- DONE ~~FileDB metadata: indexing, count~~
- TODO Resources: list and details
- TODO Routes: restful list, take in where/params
-- TODO Sanitize output file name
+- DONE ~~Sanitize output file name~~
- TODO Take in filename parameter!
- DONE ~~Files: Format conversion library. Support CSV, JSON~~
- TODO Error handling
View
67 libs/filedb.js
@@ -3,6 +3,9 @@ var fs = require('fs'),
var JsonDB = function(resource, idAttribute) {
+
+ JsonDB.loadMetadata();
+
var idAttr = idAttribute || JsonDB.idAttribute;
var file = resource.indexOf('.json') === -1 ? resource + '.json' : resource;
if (!file) {
@@ -17,8 +20,62 @@ var JsonDB = function(resource, idAttribute) {
return db;
};
+var EXT = JsonDB.EXT = '.json';
JsonDB.resourcesPath = '';
JsonDB.idAttribute = 'id';
+JsonDB.metadataFile = '.jsondb';
+JsonDB.metadata = {};
+
+JsonDB.loadMetadata = function() {
+ if (this.loaded) return;
+
+ if (fs.existsSync(file)) return;
+
+ this.load(JsonDB.resourcesPath + JsonDB.metadataFile, function(err, data) {
+ try {
+ JsonDB.metadata = JSON.parse(data);
+ } catch (err) {
+ console.log(err);
+ throw new Error('Unable to load metadata');
+ return;
+ }
+ });
+};
+
+JsonDB.updateMetadata = function(resource, file, items) {
+ JsonDB.metadata[resource] = {
+ file: file,
+ count: items.length || 0
+ };
+ var dirname = JsonDB.resourcesPath + JsonDB.metadataFile;
+ fs.writeFile(dirname, JSON.stringify(JsonDB.metadata, null, " "));
+};
+
+JsonDB.indexMetadata = function() {
+ var dirname = JsonDB.resourcesPath,
+ resource,
+ items,
+ files,
+ total,
+ count;
+
+ files = fs.readdirSync(dirname);
+ total = files.length;
+ files.forEach(function(file) {
+ try {
+ _load(dirname + file, function(err, data) {
+ resource = file.replace(EXT, '');
+ items = JSON.parse(data);
+ JsonDB.updateMetadata(resource, file, items);
+ count++;
+ if (total === count) callback(null, JsonDB.metadata);
+
+ });
+ } catch (e) {
+
+ }
+ });
+};
var _convertValueToFilter = function(attrs, idAttr) {
// if a value was provided instead of an object, use the value as an id
@@ -47,16 +104,6 @@ var _exists = JsonDB.exists = function(file, callback) {
return;
};
-JsonDB.updateMetadata = function(resource, file, items) {
- console.log('UPDATE METADATA', resource, file, items);
- console.log('UPDATE', JsonDB.resourcesPath, resource);
- // fs.writeFile(file, JSON.stringify(docs, null, " "), 'utf-8', function() {
- // JsonDB.updateMetadata(resource, file, docs);
- // });
-};
-
-
-
var FileDB = function(file, config) {
this.file = file;
console.log('FileDB', file)
View
8 routes/restful.js
@@ -89,6 +89,12 @@ Routes.destroy = function(req, res, next) {
// GET /resources
Routes.listResources = function(req, res) {
+ /*
+ db.indexMetadata(function(err, data) {
+ res.jsonp(data);
+ });
+ return;
+*/
var _resources = db.resourcesPath;
fs.readdir(_resources, function(err, files) {
if (err) throw err;
@@ -106,6 +112,8 @@ Routes.listResources = function(req, res) {
});
};
+
+
router.get('/resources', Routes.listResources);
router.get('/:resource', Routes.list);

No commit comments for this range

Something went wrong with that request. Please try again.