json-file-object
Advanced tools
Comparing version 1.0.0 to 1.1.0
18
index.js
@@ -11,10 +11,10 @@ // Generated by CoffeeScript 1.9.3 | ||
module.exports = function(obj, arg) { | ||
var error, file, forceNew, interval, ref, saveEverySecs; | ||
if (obj == null) { | ||
obj = {}; | ||
module.exports = function(arg) { | ||
var error, file, forceNew, interval, ref, saveEverySecs, value; | ||
ref = arg != null ? arg : {}, value = ref.value, file = ref.file, saveEverySecs = ref.saveEverySecs, forceNew = ref.forceNew; | ||
if (value == null) { | ||
value = {}; | ||
} | ||
ref = arg != null ? arg : {}, file = ref.file, saveEverySecs = ref.saveEverySecs, forceNew = ref.forceNew; | ||
if (file == null) { | ||
file = "file_object.json"; | ||
file = "json_file_object.json"; | ||
} | ||
@@ -30,3 +30,3 @@ if (saveEverySecs == null) { | ||
try { | ||
obj = JSON.parse(fs.readFileSync(file, 'utf8')); | ||
value = JSON.parse(fs.readFileSync(file, 'utf8')); | ||
} catch (_error) { | ||
@@ -38,6 +38,6 @@ error = _error; | ||
interval = setInterval(function() { | ||
return writeFile(file, obj); | ||
return writeFile(file, value); | ||
}, saveEverySecs * 1000); | ||
created_db[file] = { | ||
obj: obj, | ||
obj: value, | ||
interval: interval | ||
@@ -44,0 +44,0 @@ }; |
{ | ||
"name": "json-file-object", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "periodically persisting JSON-able objects into files (one per object)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
// Generated by CoffeeScript 1.9.3 | ||
(function() { | ||
var file_object, my_obj; | ||
var json_file_object, my_obj; | ||
file_object = require("./"); | ||
json_file_object = require("./"); | ||
my_obj = file_object({}, { | ||
my_obj = json_file_object({ | ||
file: "test.json" | ||
@@ -16,4 +16,4 @@ }); | ||
setTimeout(function() { | ||
return file_object.stop(my_obj); | ||
}, 10000); | ||
return json_file_object.stop(my_obj); | ||
}, 3000); | ||
@@ -20,0 +20,0 @@ console.log("The program will end in seconds, once we `stop` backing up ..."); |
@@ -1,34 +0,47 @@ | ||
file-object | ||
=========== | ||
json-file-object | ||
================ | ||
A mockup of a db for a single "JSON"-able object, periodically stored | ||
in a file so it can be recover after restart. | ||
in a file so it can be recovered after restart. | ||
For example: | ||
To install, do | ||
var my_obj = require("file-object")(); | ||
> npm install json-file-object | ||
The variable `my_obj` is saved to the file `file_object.json` every 5 | ||
secs. If the file exists, the object is initialized with the JSON | ||
parsed content of the file, if not it is initialized to `{}`. | ||
Example | ||
------- | ||
The above defaults (for file name, interval, or initial value) can be | ||
var json_file_object = require("json-file-object"); | ||
var my_obj = json_file_object(); | ||
The reference stored in variable `my_obj` is saved to the file | ||
`file_object.json` every 5 secs. If the file exists, the object under | ||
the reference is initialized with the JSON parsed content of the file, | ||
if not it is initialized to `{}`. | ||
The above defaults (for the file name, the interval, or the initial value) can be | ||
specified by the parameters passed to the constructor function | ||
returned by `file_object = require("file-object")`. I.e., | ||
`file_object()` really means: | ||
returned by `json_file_object = require("json-file-object")`. I.e., | ||
`var my_obj = json_file_object()` really means: | ||
* `file_object({}, {file: "file_object.json", saveEverySecs: 5, forceNew: false})` | ||
var my_obj = json_file_object({value:{}, file:"json_file_object.json", saveEverySecs:5, forceNew:false}); | ||
If flag `forceNew` is set, the old content of the file is ignored and the initial value of the returned object is set by the first argument. | ||
The generator function returned by `require("file-object")`, called | ||
here `file_object`, has following methods: | ||
The generator function returned by `require("json-file-object")`, called | ||
here `json_file_object`, has following methods: | ||
* `file_object.stop(obj)` - stops recording `obj` to its file | ||
* `file_object.stopAll()` - stops all recordings | ||
* `file_object.erase(obj)` - stops recording `obj` and erases the corresponding file | ||
* `file_object.eraseAll()` - stops all recordings and erases all corresponding files | ||
* `json_file_object.stop(obj)` - stops recording `obj` to its file | ||
* `json_file_object.stopAll()` - stops all recordings | ||
* `json_file_object.erase(obj)` - stops recording `obj` and erases the corresponding file | ||
* `json_file_object.eraseAll()` - stops all recordings and erases all corresponding files | ||
Warning! | ||
------- | ||
For more info, look into the code `index.coffee` and tests | ||
`test.coffee`, `re_test.coffee`, which are probably shorter than that | ||
document. | ||
Warnings! | ||
-------- | ||
You ___can not___ reassign a new reference to `my_obj` and hope that the new value will be persisted. For example, if you do | ||
@@ -43,1 +56,5 @@ | ||
will not be persisted (because the value under the old reference is being persisted). The logic here is somehow similar to `exports` and `module.exports`. | ||
Also, notice that in order to properly terminate the `node` process | ||
using `json-file-object` you will need to stop "persisting", e.g., by | ||
calling `json_file_object.stopAll()`. |
13
test.js
// Generated by CoffeeScript 1.9.3 | ||
(function() { | ||
var counter, file_object, ld, my_obj, test; | ||
var counter, json_file_object, ld, my_obj, test; | ||
ld = require('lodash'); | ||
file_object = require("./"); | ||
json_file_object = require("./"); | ||
counter = 3; | ||
my_obj = file_object({ | ||
hey: "Start" | ||
}, { | ||
my_obj = json_file_object({ | ||
value: { | ||
hey: "Start" | ||
}, | ||
file: "test.json", | ||
@@ -26,3 +27,3 @@ saveEverySecs: 1, | ||
} else { | ||
require("./").stopAll(); | ||
json_file_object.stopAll(); | ||
return console.log("The file 'test.json' was created and contains the content of 'my_obj'"); | ||
@@ -29,0 +30,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9258
60
10
188