Created
May 20, 2015 09:05
-
-
Save annoyingmouse/db0c4e53b71a5dfe18f2 to your computer and use it in GitHub Desktop.
lokiFileSystemAdaptor_new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="lokijs.js"></script> | |
<script src="lokiFileSystemAdapter_new.js"></script> | |
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script> | |
</head> | |
<body> | |
<table> | |
<thead> | |
<tr> | |
<th>Person</th> | |
<th>Age</th> | |
</tr> | |
</thead> | |
<tbody id="peopleTable"> | |
</tbody> | |
</table> | |
<script> | |
var requestedBytes = 1025*1024*10 // 10MB | |
window.fs = null; | |
window.db = null; | |
(function(){ | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
navigator.webkitPersistentStorage.requestQuota ( | |
requestedBytes, | |
function(grantedBytes) { | |
window.requestFileSystem( | |
PERSISTENT, | |
requestedBytes, | |
function(FileSystem){ | |
fs = FileSystem; | |
fs.root.getDirectory( | |
'test_directory', | |
{ | |
create: true | |
}, | |
function(dirEntry) { | |
var fsAdapter = new FileSystemAdapter({ | |
"base_dir": "test_directory" | |
}); | |
db = new loki('db.json', {"adapter": fsAdapter}); | |
db.loadDatabase( | |
db.filename, | |
function(){ | |
try{ | |
populateTable(db.getCollection("users").chain().find().data()) | |
}catch(e){ | |
console.log(e); | |
var users = db.addCollection('users', {indices: ['email']}); | |
users.insert({ | |
name: 'odin', | |
email: 'odin.soap@lokijs.org', | |
age: 38}); | |
users.insert({ | |
name: 'thor', | |
email: 'thor.soap@lokijs.org', | |
age: 25}); | |
users.insert({ | |
name: 'stan', | |
email: 'stan.soap@lokijs.org', | |
age: 29}); | |
users.insert({ | |
name: 'oliver', | |
email: 'oliver.soap@lokijs.org', | |
age: 31}); | |
users.insert({ | |
name: 'hector', | |
email: 'hector.soap@lokijs.org', | |
age: 15}); | |
users.insert({ | |
name: 'achilles', | |
email: 'achilles.soap@lokijs.org', | |
age: 31}); | |
populateTable(db.getCollection("users").chain().find().data()) | |
db.saveDatabase(); | |
} | |
} | |
); | |
}, | |
function(e) { | |
console.error(e); | |
} | |
); | |
}, | |
function(e) { | |
console.error(e); | |
} | |
); | |
}, | |
function(e) { | |
console.error(e); | |
} | |
); | |
function populateTable(arr){ | |
$.each( | |
arr, | |
function(k, v){ | |
$("#peopleTable") | |
.append($("<tr></tr>") | |
.append($("<td></td>") | |
.append($("<a></a>",{ | |
"href": "mailto:" + v.email, | |
"text": v.name}))) | |
.append($("<td></td>",{ | |
"text": v.age}))) | |
}); | |
} | |
})(); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FileSystemAdapterError() {} | |
FileSystemAdapterError.prototype = new Error(); | |
function FileSystemAdapter(options) { | |
this.options = options; | |
} | |
FileSystemAdapter.prototype.saveDatabase = function (name, data, callback) { | |
fs.root.getFile( | |
this.options.base_dir + '/' + name, | |
{ | |
create: true | |
}, | |
function(fileEntry) { | |
fileEntry.createWriter( | |
function(fileWriter) { | |
fileWriter.onwriteend = function() { | |
if (fileWriter.length === 0) { | |
var blob = new Blob( | |
[data], | |
{ | |
type: 'text/plain' | |
} | |
); | |
fileWriter.write(blob); | |
} | |
}; | |
fileWriter.truncate(0); | |
}, | |
function(err){ | |
throw new FileSystemAdapterError("Unable to write file" + JSON.stringify(err)); | |
} | |
); | |
}, | |
function(err){ | |
throw new FileSystemAdapterError("Unable to get file" + JSON.stringify(err)); | |
} | |
); | |
}; | |
FileSystemAdapter.prototype.loadDatabase = function (name, callback) { | |
fs.root.getFile( | |
this.options.base_dir + '/' + name, | |
{ | |
create: false | |
}, | |
function(fileEntry){ | |
fileEntry.file(function(file) { | |
var reader = new FileReader(); | |
reader.onloadend = function(event) { | |
var contents = event.target.result; | |
callback(contents); | |
}; | |
reader.readAsText(file); | |
}, function(err){ | |
callback(FileSystemAdapterError("Unable to read file" + err.message)); | |
}); | |
}, | |
function(err){ | |
callback(new FileSystemAdapterError("Unable to get file: " + err.message)) | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment