-
Notifications
You must be signed in to change notification settings - Fork 1k
/
storage.js
executable file
·95 lines (73 loc) · 2.77 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Way data is stored for this database
* For a Node.js/Node Webkit database it's the file system
* For a browser-side database it's localforage, which uses the best backend available (IndexedDB then WebSQL then localStorage)
*
* This version is the browser version
*/
var localforage = require('localforage')
// Configure localforage to display NeDB name for now. Would be a good idea to let user use his own app name
localforage.config({
name: 'NeDB'
, storeName: 'nedbdata'
});
function exists (filename, callback) {
localforage.getItem(filename, function (err, value) {
if (value !== null) { // Even if value is undefined, localforage returns null
return callback(true);
} else {
return callback(false);
}
});
}
function rename (filename, newFilename, callback) {
localforage.getItem(filename, function (err, value) {
if (value === null) {
localforage.removeItem(newFilename, function () { return callback(); });
} else {
localforage.setItem(newFilename, value, function () {
localforage.removeItem(filename, function () { return callback(); });
});
}
});
}
function writeFile (filename, contents, options, callback) {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
localforage.setItem(filename, contents, function () { return callback(); });
}
function appendFile (filename, toAppend, options, callback) {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
localforage.getItem(filename, function (err, contents) {
contents = contents || '';
contents += toAppend;
localforage.setItem(filename, contents, function () { return callback(); });
});
}
function readFile (filename, options, callback) {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
localforage.getItem(filename, function (err, contents) { return callback(null, contents || ''); });
}
function unlink (filename, callback) {
localforage.removeItem(filename, function () { return callback(); });
}
// Nothing to do, no directories will be used on the browser
function mkdirp (dir, callback) {
return callback();
}
// Nothing to do, no data corruption possible in the brower
function ensureDatafileIntegrity (filename, callback) {
return callback(null);
}
// Interface
module.exports.exists = exists;
module.exports.rename = rename;
module.exports.writeFile = writeFile;
module.exports.crashSafeWriteFile = writeFile; // No need for a crash safe function in the browser
module.exports.appendFile = appendFile;
module.exports.readFile = readFile;
module.exports.unlink = unlink;
module.exports.mkdirp = mkdirp;
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity;