├── src ├── .eslintrc └── loki-cordova-fs-adapter.es6 ├── bower.json ├── README.md ├── package.json ├── LICENSE └── bin └── loki-cordova-fs-adapter.js /src/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": 1, 4 | }, 5 | "globals": { 6 | "require": 1, 7 | "module": 1, 8 | "cordova": 1, 9 | }, 10 | "ecmaFeatures": { 11 | "arrowFunctions": 1, 12 | "blockBindings": 1, 13 | "jsx": 1, 14 | "classes": 1, 15 | "templateStrings": 1, 16 | "destructuring": 1, 17 | "modules": 1, 18 | "forOf": 1, 19 | }, 20 | "rules": { 21 | "global-strict": 0, 22 | "no-console": 0, 23 | "comma-dangle": 0, 24 | "no-underscore-dangle": 0, 25 | "camelcase": 0, 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "loki-cordova-fs-adapter", 3 | "homepage": "https://github.com/cosmith/loki-cordova-fs-adapter", 4 | "authors": [ 5 | "Corentin Smith " 6 | ], 7 | "description": "A FileSystem adapter for LokiJS database persistence on Cordova", 8 | "main": "bin/loki-cordova-fs-adapter.js", 9 | "moduleType": [], 10 | "keywords": [ 11 | "loki", 12 | "lokijs", 13 | "cordova", 14 | "adapter", 15 | "filesystem", 16 | "fs", 17 | "persistence" 18 | ], 19 | "license": "MIT", 20 | "ignore": [ 21 | "**/.*", 22 | "node_modules", 23 | "bower_components", 24 | "test", 25 | "tests" 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # loki-cordova-fs-adapter [![npm version](https://badge.fury.io/js/loki-cordova-fs-adapter.svg)](https://badge.fury.io/js/loki-cordova-fs-adapter) 2 | 3 | ### NOTE: I don't use this plugin anymore since I moved to React Native. 4 | 5 | FileSystem adapter for LokiJS database persistence on Cordova. 6 | 7 | Dependency: https://github.com/apache/cordova-plugin-file 8 | 9 | Based on the code from https://github.com/annoyingmouse/lokiFileSystemAdapter 10 | 11 | 12 | ```js 13 | var LokiCordovaFSAdapter = require("./cordova-file-system-adapter"); 14 | 15 | var adapter = new LokiCordovaFSAdapter({"prefix": "loki"}); 16 | 17 | var db = new loki("dbname", {adapter: adapter}); 18 | ``` 19 | 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "loki-cordova-fs-adapter", 3 | "version": "1.0.2", 4 | "description": "FileSystem adapter for LokiJS database persistence on Cordova.", 5 | "files": "bin/", 6 | "main": "bin/loki-cordova-fs-adapter.js", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/cosmith/loki-cordova-fs-adapter.git" 13 | }, 14 | "keywords": [ 15 | "loki", 16 | "cordova", 17 | "filesystem", 18 | "adapter" 19 | ], 20 | "author": "Corentin Smith", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/cosmith/loki-cordova-fs-adapter/issues" 24 | }, 25 | "homepage": "https://github.com/cosmith/loki-cordova-fs-adapter#readme" 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Corentin Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/loki-cordova-fs-adapter.es6: -------------------------------------------------------------------------------- 1 | class LokiCordovaFSAdapterError extends Error {} 2 | 3 | const TAG = "[LokiCordovaFSAdapter]"; 4 | 5 | class LokiCordovaFSAdapter { 6 | constructor(options) { 7 | this.options = options; 8 | } 9 | 10 | saveDatabase(dbname, dbstring, callback) { 11 | console.log(TAG, "saving database"); 12 | this._getFile(dbname, 13 | (fileEntry) => { 14 | fileEntry.createWriter( 15 | (fileWriter) => { 16 | fileWriter.onwriteend = () => { 17 | if (fileWriter.length === 0) { 18 | var blob = this._createBlob(dbstring, "text/plain"); 19 | fileWriter.write(blob); 20 | callback(); 21 | } 22 | }; 23 | fileWriter.truncate(0); 24 | 25 | }, 26 | (err) => { 27 | console.error(TAG, "error writing file", err); 28 | throw new LokiCordovaFSAdapterError("Unable to write file" + JSON.stringify(err)); 29 | } 30 | ); 31 | }, 32 | (err) => { 33 | console.error(TAG, "error getting file", err); 34 | throw new LokiCordovaFSAdapterError("Unable to get file" + JSON.stringify(err)); 35 | } 36 | ); 37 | } 38 | 39 | loadDatabase(dbname, callback) { 40 | console.log(TAG, "loading database"); 41 | this._getFile(dbname, 42 | (fileEntry) => { 43 | fileEntry.file((file) => { 44 | var reader = new FileReader(); 45 | reader.onloadend = (event) => { 46 | var contents = event.target.result; 47 | if (contents.length === 0) { 48 | console.warn(TAG, "couldn't find database"); 49 | callback(null); 50 | } 51 | else { 52 | callback(contents); 53 | } 54 | }; 55 | reader.readAsText(file); 56 | }, (err) => { 57 | console.error(TAG, "error reading file", err); 58 | callback(new LokiCordovaFSAdapterError("Unable to read file" + err.message)); 59 | }); 60 | }, 61 | (err) => { 62 | console.error(TAG, "error getting file", err); 63 | callback(new LokiCordovaFSAdapterError("Unable to get file: " + err.message)); 64 | } 65 | ); 66 | } 67 | 68 | deleteDatabase(dbname, callback) { 69 | window.resolveLocalFileSystemURL(cordova.file.dataDirectory, 70 | (dir) => { 71 | let fileName = this.options.prefix + "__" + dbname; 72 | dir.getFile(fileName, {create: true}, 73 | (fileEntry) => { 74 | fileEntry.remove( 75 | () => { 76 | callback(); 77 | }, 78 | (err) => { 79 | console.error(TAG, "error delete file", err); 80 | throw new LokiCordovaFSAdapterError("Unable delete file" + JSON.stringify(err)); 81 | } 82 | ); 83 | }, 84 | (err) => { 85 | console.error(TAG, "error delete database", err); 86 | throw new LokiCordovaFSAdapterError( 87 | "Unable delete database" + JSON.stringify(err) 88 | ); 89 | } 90 | ); 91 | }, 92 | (err) => { 93 | throw new LokiCordovaFSAdapterError( 94 | "Unable to resolve local file system URL" + JSON.stringify(err) 95 | ); 96 | } 97 | ); 98 | } 99 | 100 | _getFile(name, handleSuccess, handleError) { 101 | window.resolveLocalFileSystemURL(cordova.file.dataDirectory, 102 | (dir) => { 103 | let fileName = this.options.prefix + "__" + name; 104 | dir.getFile(fileName, {create: true}, handleSuccess, handleError); 105 | }, 106 | (err) => { 107 | throw new LokiCordovaFSAdapterError( 108 | "Unable to resolve local file system URL" + JSON.stringify(err) 109 | ); 110 | } 111 | ); 112 | } 113 | 114 | // adapted from http://stackoverflow.com/questions/15293694/blob-constructor-browser-compatibility 115 | _createBlob(data, datatype) { 116 | let blob; 117 | 118 | try { 119 | blob = new Blob([data], {type: datatype}); 120 | } 121 | catch (err) { 122 | window.BlobBuilder = window.BlobBuilder || 123 | window.WebKitBlobBuilder || 124 | window.MozBlobBuilder || 125 | window.MSBlobBuilder; 126 | 127 | if (err.name === "TypeError" && window.BlobBuilder) { 128 | var bb = new window.BlobBuilder(); 129 | bb.append(data); 130 | blob = bb.getBlob(datatype); 131 | } 132 | else if (err.name === "InvalidStateError") { 133 | // InvalidStateError (tested on FF13 WinXP) 134 | blob = new Blob([data], {type: datatype}); 135 | } 136 | else { 137 | // We're screwed, blob constructor unsupported entirely 138 | throw new LokiCordovaFSAdapterError( 139 | "Unable to create blob" + JSON.stringify(err) 140 | ); 141 | } 142 | } 143 | return blob; 144 | } 145 | } 146 | 147 | 148 | export default LokiCordovaFSAdapter; 149 | -------------------------------------------------------------------------------- /bin/loki-cordova-fs-adapter.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); 4 | 5 | var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; 6 | 7 | var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; 8 | 9 | var LokiCordovaFSAdapterError = (function (_Error) { 10 | function LokiCordovaFSAdapterError() { 11 | _classCallCheck(this, LokiCordovaFSAdapterError); 12 | 13 | if (_Error != null) { 14 | _Error.apply(this, arguments); 15 | } 16 | } 17 | 18 | _inherits(LokiCordovaFSAdapterError, _Error); 19 | 20 | return LokiCordovaFSAdapterError; 21 | })(Error); 22 | 23 | var TAG = "[LokiCordovaFSAdapter]"; 24 | 25 | var LokiCordovaFSAdapter = (function () { 26 | function LokiCordovaFSAdapter(options) { 27 | _classCallCheck(this, LokiCordovaFSAdapter); 28 | 29 | this.options = options; 30 | } 31 | 32 | _createClass(LokiCordovaFSAdapter, { 33 | saveDatabase: { 34 | value: function saveDatabase(dbname, dbstring, callback) { 35 | var _this = this; 36 | 37 | console.log(TAG, "saving database"); 38 | this._getFile(dbname, function (fileEntry) { 39 | fileEntry.createWriter(function (fileWriter) { 40 | fileWriter.onwriteend = function () { 41 | if (fileWriter.length === 0) { 42 | var blob = _this._createBlob(dbstring, "text/plain"); 43 | fileWriter.write(blob); 44 | callback(); 45 | } 46 | }; 47 | fileWriter.truncate(0); 48 | }, function (err) { 49 | console.error(TAG, "error writing file", err); 50 | throw new LokiCordovaFSAdapterError("Unable to write file" + JSON.stringify(err)); 51 | }); 52 | }, function (err) { 53 | console.error(TAG, "error getting file", err); 54 | throw new LokiCordovaFSAdapterError("Unable to get file" + JSON.stringify(err)); 55 | }); 56 | } 57 | }, 58 | loadDatabase: { 59 | value: function loadDatabase(dbname, callback) { 60 | console.log(TAG, "loading database"); 61 | this._getFile(dbname, function (fileEntry) { 62 | fileEntry.file(function (file) { 63 | var reader = new FileReader(); 64 | reader.onloadend = function (event) { 65 | var contents = event.target.result; 66 | if (contents.length === 0) { 67 | console.warn(TAG, "couldn't find database"); 68 | callback(null); 69 | } else { 70 | callback(contents); 71 | } 72 | }; 73 | reader.readAsText(file); 74 | }, function (err) { 75 | console.error(TAG, "error reading file", err); 76 | callback(new LokiCordovaFSAdapterError("Unable to read file" + err.message)); 77 | }); 78 | }, function (err) { 79 | console.error(TAG, "error getting file", err); 80 | callback(new LokiCordovaFSAdapterError("Unable to get file: " + err.message)); 81 | }); 82 | } 83 | }, 84 | deleteDatabase: { 85 | value: function deleteDatabase(dbname, callback) { 86 | var _this = this; 87 | console.log(TAG, "delete database"); 88 | window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) { 89 | var fileName = _this.options.prefix + "__" + dbname; 90 | // Very important to have { create: true } 91 | dir.getFile(fileName, { create: true }, function(fileEntry) { 92 | fileEntry.remove(function() { 93 | callback(); 94 | }, function (err) { 95 | console.error(TAG, "error delete file", err); 96 | throw new LokiCordovaFSAdapterError("Unable delete file" + JSON.stringify(err)); 97 | }); 98 | }, function (err) { 99 | console.error(TAG, "error delete database", err); 100 | throw new LokiCordovaFSAdapterError("Unable delete database" + JSON.stringify(err)); 101 | }); 102 | }, function (err) { 103 | throw new LokiCordovaFSAdapterError("Unable to resolve local file system URL" + JSON.stringify(err)); 104 | }); 105 | } 106 | }, 107 | _getFile: { 108 | value: function _getFile(name, handleSuccess, handleError) { 109 | var _this = this; 110 | 111 | window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) { 112 | var fileName = _this.options.prefix + "__" + name; 113 | dir.getFile(fileName, { create: true }, handleSuccess, handleError); 114 | }, function (err) { 115 | throw new LokiCordovaFSAdapterError("Unable to resolve local file system URL" + JSON.stringify(err)); 116 | }); 117 | } 118 | }, 119 | _createBlob: { 120 | 121 | // adapted from http://stackoverflow.com/questions/15293694/blob-constructor-browser-compatibility 122 | 123 | value: function _createBlob(data, datatype) { 124 | var blob = undefined; 125 | 126 | try { 127 | blob = new Blob([data], { type: datatype }); 128 | } catch (err) { 129 | window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; 130 | 131 | if (err.name === "TypeError" && window.BlobBuilder) { 132 | var bb = new window.BlobBuilder(); 133 | bb.append(data); 134 | blob = bb.getBlob(datatype); 135 | } else if (err.name === "InvalidStateError") { 136 | // InvalidStateError (tested on FF13 WinXP) 137 | blob = new Blob([data], { type: datatype }); 138 | } else { 139 | // We're screwed, blob constructor unsupported entirely 140 | throw new LokiCordovaFSAdapterError("Unable to create blob" + JSON.stringify(err)); 141 | } 142 | } 143 | return blob; 144 | } 145 | } 146 | }); 147 | 148 | return LokiCordovaFSAdapter; 149 | })(); 150 | 151 | module.exports = LokiCordovaFSAdapter; 152 | --------------------------------------------------------------------------------