Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ Dependency: https://github.com/apache/cordova-plugin-file
Based on the code from https://github.com/annoyingmouse/lokiFileSystemAdapter


```js
var LokiCordovaFSAdapter = require("./cordova-file-system-adapter");
### Usage
#### import file
`bin/loki-cordova-fs-adapter.js`

var adapter = new LokiCordovaFSAdapter({"prefix": "loki"});
#### options
- `prefix`
specify the persistence filename prefix

- `dataDirectory`
specify the persistence file directory, default `cordova.file.dataDirectory`

```
var adapter = new lokiCordovaFsAdapter({
"prefix": "loki",
"dataDirectory": cordova.file.documentsDirectory
});

var db = new loki("dbname", {adapter: adapter});
```
Expand Down
156 changes: 106 additions & 50 deletions bin/loki-cordova-fs-adapter.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,102 @@
"use strict";
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["module", "exports"], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports);
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports);
global.lokiCordovaFsAdapter = mod.exports;
}
})(this, function (module, exports) {
"use strict";

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; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});

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; };
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}

var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();

var LokiCordovaFSAdapterError = (function (_Error) {
function LokiCordovaFSAdapterError() {
_classCallCheck(this, LokiCordovaFSAdapterError);
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}

if (_Error != null) {
_Error.apply(this, arguments);
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}

return call && (typeof call === "object" || typeof call === "function") ? call : self;
}

_inherits(LokiCordovaFSAdapterError, _Error);
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}

return LokiCordovaFSAdapterError;
})(Error);
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

var TAG = "[LokiCordovaFSAdapter]";
var LokiCordovaFSAdapterError = function (_Error) {
_inherits(LokiCordovaFSAdapterError, _Error);

var LokiCordovaFSAdapter = (function () {
function LokiCordovaFSAdapter(options) {
_classCallCheck(this, LokiCordovaFSAdapter);
function LokiCordovaFSAdapterError() {
_classCallCheck(this, LokiCordovaFSAdapterError);

this.options = options;
}
return _possibleConstructorReturn(this, (LokiCordovaFSAdapterError.__proto__ || Object.getPrototypeOf(LokiCordovaFSAdapterError)).apply(this, arguments));
}

return LokiCordovaFSAdapterError;
}(Error);

var TAG = "[LokiCordovaFSAdapter]";

var LokiCordovaFSAdapter = function () {
function LokiCordovaFSAdapter(options) {
_classCallCheck(this, LokiCordovaFSAdapter);

this.options = options;
}

_createClass(LokiCordovaFSAdapter, {
saveDatabase: {
_createClass(LokiCordovaFSAdapter, [{
key: "saveDatabase",
value: function saveDatabase(dbname, dbstring, callback) {
var _this = this;
var _this2 = this;

console.log(TAG, "saving database");
this._getFile(dbname, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function () {
if (fileWriter.length === 0) {
var blob = _this._createBlob(dbstring, "text/plain");
var blob = _this2._createBlob(dbstring, "text/plain");
fileWriter.write(blob);
callback();
}
Expand All @@ -54,8 +111,8 @@ var LokiCordovaFSAdapter = (function () {
throw new LokiCordovaFSAdapterError("Unable to get file" + JSON.stringify(err));
});
}
},
loadDatabase: {
}, {
key: "loadDatabase",
value: function loadDatabase(dbname, callback) {
console.log(TAG, "loading database");
this._getFile(dbname, function (fileEntry) {
Expand All @@ -80,17 +137,17 @@ var LokiCordovaFSAdapter = (function () {
callback(new LokiCordovaFSAdapterError("Unable to get file: " + err.message));
});
}
},
deleteDatabase: {
}, {
key: "deleteDatabase",
value: function deleteDatabase(dbname, callback) {
var _this = this;
console.log(TAG, "delete database");
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
var fileName = _this.options.prefix + "__" + dbname;
// Very important to have { create: true }
dir.getFile(fileName, { create: true }, function(fileEntry) {
fileEntry.remove(function() {
callback();
var _this3 = this;

var _dataDirectory = this.options.dataDirectory || cordova.file.dataDirectory;
window.resolveLocalFileSystemURL(_dataDirectory, function (dir) {
var fileName = _this3.options.prefix + "__" + dbname;
dir.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.remove(function () {
callback();
}, function (err) {
console.error(TAG, "error delete file", err);
throw new LokiCordovaFSAdapterError("Unable delete file" + JSON.stringify(err));
Expand All @@ -103,25 +160,23 @@ var LokiCordovaFSAdapter = (function () {
throw new LokiCordovaFSAdapterError("Unable to resolve local file system URL" + JSON.stringify(err));
});
}
},
_getFile: {
}, {
key: "_getFile",
value: function _getFile(name, handleSuccess, handleError) {
var _this = this;
var _this4 = this;

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
var fileName = _this.options.prefix + "__" + name;
var _dataDirectory = this.options.dataDirectory || cordova.file.dataDirectory;
window.resolveLocalFileSystemURL(_dataDirectory, function (dir) {
var fileName = _this4.options.prefix + "__" + name;
dir.getFile(fileName, { create: true }, handleSuccess, handleError);
}, function (err) {
throw new LokiCordovaFSAdapterError("Unable to resolve local file system URL" + JSON.stringify(err));
});
}
},
_createBlob: {

// adapted from http://stackoverflow.com/questions/15293694/blob-constructor-browser-compatibility

}, {
key: "_createBlob",
value: function _createBlob(data, datatype) {
var blob = undefined;
var blob = void 0;

try {
blob = new Blob([data], { type: datatype });
Expand All @@ -142,10 +197,11 @@ var LokiCordovaFSAdapter = (function () {
}
return blob;
}
}
});
}]);

return LokiCordovaFSAdapter;
})();
return LokiCordovaFSAdapter;
}();

module.exports = LokiCordovaFSAdapter;
exports.default = LokiCordovaFSAdapter;
module.exports = exports["default"];
});
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

eslint src/*.es6 && echo "ESLint done"
babel src/*.es6 --out-file bin/loki-cordova-fs-adapter.js && echo "Babel done"
gulp js && echo "Babel done"
15 changes: 15 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const gulp = require('gulp')
const $ = require('gulp-load-plugins')();


// Transpile all JS to ES5.
gulp.task('js', function() {
return gulp.src(['src/loki-cordova-fs-adapter.es6'])
.pipe($.babel({
presets: ['es2015'],
plugins: [
"add-module-exports", "transform-es2015-modules-umd"
]
}))
.pipe(gulp.dest('bin/'));
});
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@
"bugs": {
"url": "https://github.com/cosmith/loki-cordova-fs-adapter/issues"
},
"homepage": "https://github.com/cosmith/loki-cordova-fs-adapter#readme"
"homepage": "https://github.com/cosmith/loki-cordova-fs-adapter#readme",
"devDependencies": {
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-load-plugins": "^1.5.0"
}
}
10 changes: 6 additions & 4 deletions src/loki-cordova-fs-adapter.es6
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ class LokiCordovaFSAdapter {
}
);
}

deleteDatabase(dbname, callback) {
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
let _dataDirectory = this.options.dataDirectory || cordova.file.dataDirectory;
window.resolveLocalFileSystemURL(_dataDirectory,
(dir) => {
let fileName = this.options.prefix + "__" + dbname;
dir.getFile(fileName, {create: true},
dir.getFile(fileName, {create: true},
(fileEntry) => {
fileEntry.remove(
() => {
Expand Down Expand Up @@ -98,7 +99,8 @@ class LokiCordovaFSAdapter {
}

_getFile(name, handleSuccess, handleError) {
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
let _dataDirectory = this.options.dataDirectory || cordova.file.dataDirectory;
window.resolveLocalFileSystemURL(_dataDirectory,
(dir) => {
let fileName = this.options.prefix + "__" + name;
dir.getFile(fileName, {create: true}, handleSuccess, handleError);
Expand Down