Use localforage instead of localStorage

pull/2/head
Louis Chatriot 9 years ago
parent 0b564ba674
commit caeb189326
  1. 58
      browser-version/browser-specific/lib/storage.js
  2. 2854
      browser-version/out/nedb.js
  3. 7
      browser-version/out/nedb.min.js
  4. 11
      browser-version/test/playground.html
  5. 2
      lib/storage.js

@ -1,80 +1,72 @@
/**
* 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 localStorage when supported
* 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) {
// In this specific case this always answers that the file doesn't exist
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
if (localStorage.getItem(filename) !== null) {
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) {
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
if (localStorage.getItem(filename) === null) {
localStorage.removeItem(newFilename);
localforage.getItem(filename, function (err, value) {
if (value === null) {
localforage.removeItem(newFilename, function () { return callback(); });
} else {
localStorage.setItem(newFilename, localStorage.getItem(filename));
localStorage.removeItem(filename);
localforage.setItem(newFilename, value, function () {
localforage.removeItem(filename, function () { return callback(); });
});
}
return callback();
});
}
function writeFile (filename, contents, options, callback) {
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
localStorage.setItem(filename, contents);
return callback();
localforage.setItem(filename, contents, function () { return callback(); });
}
function appendFile (filename, toAppend, options, callback) {
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
var contents = localStorage.getItem(filename) || '';
localforage.getItem(filename, function (err, contents) {
contents = contents || '';
contents += toAppend;
localStorage.setItem(filename, contents);
return callback();
localforage.setItem(filename, contents, function () { return callback(); });
});
}
function readFile (filename, options, callback) {
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options; }
var contents = localStorage.getItem(filename) || '';
return callback(null, contents);
localforage.getItem(filename, function (err, contents) { return callback(null, contents || ''); });
}
function unlink (filename, callback) {
if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); }
localStorage.removeItem(filename);
return callback();
localforage.removeItem(filename, function () { return callback(); });
}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playground for NeDB</title>
</head>
<body>
<script src="../out/nedb.min.js"></script>
</body>
</html>

@ -1,7 +1,7 @@
/**
* 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 localStorage when supported
* For a browser-side database it's localforage which chooses the best option depending on user browser (IndexedDB then WebSQL then localStorage)
*
* This version is the Node.js/Node Webkit version
* It's essentially fs, mkdirp and crash safe write and read functions

Loading…
Cancel
Save