Skip to content

Commit

Permalink
saves a new version of a record if it already exists - #17
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jan 10, 2015
1 parent e1aa706 commit de31939
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
39 changes: 32 additions & 7 deletions lib/fs.js
Expand Up @@ -30,9 +30,11 @@ function createDataDirIfNotExists (callback) {
});
}

function recordName(record) {
return dataDir + '/' + record.index
+ '_' + record.type + '_' + record.id + '.json';
function recordName(record, count) {
console.log('COUNT: '+count);
var count = (count === 0 || count === undefined) ? '' : '-'+count;
return dataDir + '/' + record.index + '_'
+ record.type + '_' + record.id +count +'.json';
}

function fileExists(record, callback) {
Expand All @@ -42,12 +44,35 @@ function fileExists(record, callback) {
});
}

// recursive filename generator
function fileNameGenerator(record, count, callback) {
var filename;

if(typeof count === 'function') {
callback = count;
count = 0;
filename = recordName(record);
} else {
var filename = recordName(record, count);
}

fs.exists(filename, function (exists) {
if(exists){
fileNameGenerator(record, count+1, callback);
} else {
callback(filename);
}
});
}

function saveFile(record, callback) {
createDataDirIfNotExists( function() {
var file = recordName(record);
record = JSON.stringify(record);
fs.writeFile(file, record, function(err) {
callback(err, file);
// ensure we NEVER over-write an existing version
fileNameGenerator(record, function(filename) {
record = JSON.stringify(record);
fs.writeFile(filename, record, function(err) {
callback(err, filename);
});
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion test/e2e.js
Expand Up @@ -20,7 +20,8 @@ test(chalk.cyan('E2E CREATE, READ & DELETE a Record'), function (t) {
}
ES.CREATE(record, function(err, res) {
ES.DELETE(rec, function(err3, res3) {
t.equal(res3.found, true, chalk.green("✓ Record Exists - Lets Delete it!"));
t.equal(res3.found, true, chalk.green("✓ Record Exists - Lets Delete it!"));
// should we check for backed up record here...?
// attempt to read record - it should fail
ES.READ(rec, function(err4, res4){
t.equal(res4.found, false, chalk.green("✓ Record Deleted"));
Expand Down
24 changes: 24 additions & 0 deletions test/fs.js
Expand Up @@ -73,3 +73,27 @@ test(chalk.cyan('Create a FILE (record)'), function (t) {
});
});
});

test(chalk.cyan('Create a NEW Version of a record'), function (t) {
record.id = '987654';
FS.fileExists(record, function (err, exists) {
// t.equal(exists, false, chalk.green("✓ ") + chalk.red('record did not exists'));
FS.saveFile(record, function (err) {
// attempt to save it a second time to see if it creates a new version
FS.saveFile(record, function (err2) {
record.id = '987654-1'; // revision 1
FS.fileExists(record, function(err, exists) {
t.equal(exists, true, chalk.green("✓ revision 1 exists"));
record.id = '987654'; // reset id back to original
FS.saveFile(record, function (err2) {
record.id = '987654-2'; // revision 2
FS.fileExists(record, function(err, exists) {
t.equal(exists, true, chalk.green("✓ second revision exists"));
t.end();
});
});
});
});
});
});
});
19 changes: 10 additions & 9 deletions test/z_teardown.js
Expand Up @@ -33,12 +33,13 @@ test(chalk.cyan('Create dummy records to exercise ') + chalk.red('deleteDataDir
});
});

test(chalk.cyan('TIDY UP TIME ( delete all files in ') + chalk.red('_data ') + chalk.cyan('directory )'), function (t) {
D.deleteDataDir(function (err, deleted) {
t.equal(deleted, true, chalk.green("✓ ") + chalk.red('_data DELETED!'));
FS.dataDirExists(function (err, exists) {
t.equal(exists, false, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir should no longer exist!"));
t.end();
});
});
});
//
// test(chalk.cyan('TIDY UP TIME ( delete all files in ') + chalk.red('_data ') + chalk.cyan('directory )'), function (t) {
// D.deleteDataDir(function (err, deleted) {
// t.equal(deleted, true, chalk.green("✓ ") + chalk.red('_data DELETED!'));
// FS.dataDirExists(function (err, exists) {
// t.equal(exists, false, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir should no longer exist!"));
// t.end();
// });
// });
// });

0 comments on commit de31939

Please sign in to comment.