Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FileDB: Lame metadata implementation, POC.

  • Loading branch information...
commit f7ba73b09884d9f32cf3f1be1587ee192ff4b9ac 1 parent 6036ef4
goliatone authored
Showing with 57 additions and 10 deletions.
  1. +57 −10 libs/filedb.js
67 libs/filedb.js
View
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.