@flumens/models 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Collection.d.ts +1 -1
- package/dist/Collection.js +108 -1
- package/dist/Drupal/User.d.ts +2 -2
- package/dist/Drupal/User.js +433 -1
- package/dist/Indicia/Media.js +369 -1
- package/dist/Indicia/Occurrence.d.ts +7 -7
- package/dist/Indicia/Occurrence.js +220 -1
- package/dist/Indicia/Sample.d.ts +2 -2
- package/dist/Indicia/Sample.js +687 -1
- package/dist/Indicia/SampleCollection.js +77 -1
- package/dist/Indicia/helpers.d.ts +1 -1
- package/dist/Indicia/helpers.js +120 -1
- package/dist/Model.d.ts +1 -1
- package/dist/Model.js +191 -1
- package/dist/Stores/LocalForageStore.js +228 -1
- package/dist/Stores/SQLiteDatabase.d.ts +1 -1
- package/dist/Stores/SQLiteDatabase.js +221 -1
- package/dist/Stores/SQLiteStore.d.ts +4 -6
- package/dist/Stores/SQLiteStore.js +310 -1
- package/dist/Stores/Store.js +11 -1
- package/dist/Stores/utils.js +60 -1
- package/dist/index.js +29 -1
- package/package.json +14 -19
|
@@ -1 +1,77 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
var mobx = require('mobx');
|
|
7
|
+
var Collection = require('../Collection.js');
|
|
8
|
+
|
|
9
|
+
var SampleCollection = /** @class */ (function (_super) {
|
|
10
|
+
tslib.__extends(SampleCollection, _super);
|
|
11
|
+
function SampleCollection(options) {
|
|
12
|
+
var _this = _super.call(this, options) || this;
|
|
13
|
+
_this.fetch = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
14
|
+
var modelsJSON, getModel, models;
|
|
15
|
+
var _this = this;
|
|
16
|
+
return tslib.__generator(this, function (_a) {
|
|
17
|
+
switch (_a.label) {
|
|
18
|
+
case 0:
|
|
19
|
+
if (!this.store || !this.Model) {
|
|
20
|
+
this.ready.resolve(false);
|
|
21
|
+
return [2 /*return*/];
|
|
22
|
+
}
|
|
23
|
+
return [4 /*yield*/, this.store.findAll()];
|
|
24
|
+
case 1:
|
|
25
|
+
modelsJSON = _a.sent();
|
|
26
|
+
getModel = function (json) {
|
|
27
|
+
return _this.Model.fromJSON(tslib.__assign(tslib.__assign({}, json), { attrs: json.data }));
|
|
28
|
+
};
|
|
29
|
+
models = modelsJSON.map(getModel);
|
|
30
|
+
this.push.apply(this, models);
|
|
31
|
+
this.ready.resolve(true);
|
|
32
|
+
return [2 /*return*/];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}); };
|
|
36
|
+
_this.removeAllSynced = function () {
|
|
37
|
+
var toWait = [];
|
|
38
|
+
_this.forEach(function (sample) {
|
|
39
|
+
if (sample.syncedAt) {
|
|
40
|
+
toWait.push(sample.destroy());
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return Promise.all(toWait);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Reverse order so that newest are at the top of list.
|
|
47
|
+
*/
|
|
48
|
+
_this.comparator = function (a) {
|
|
49
|
+
var date = new Date(a.createdAt);
|
|
50
|
+
return -date.getTime();
|
|
51
|
+
};
|
|
52
|
+
_this.reset = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
53
|
+
var destroyAllSamples;
|
|
54
|
+
return tslib.__generator(this, function (_a) {
|
|
55
|
+
switch (_a.label) {
|
|
56
|
+
case 0:
|
|
57
|
+
destroyAllSamples = this.map(function (sample) { return sample.destroy(); });
|
|
58
|
+
return [4 /*yield*/, Promise.all(destroyAllSamples)];
|
|
59
|
+
case 1:
|
|
60
|
+
_a.sent();
|
|
61
|
+
return [2 /*return*/];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}); };
|
|
65
|
+
var attachCollectionToModels = function (change) {
|
|
66
|
+
if (change.addedCount)
|
|
67
|
+
change.added.forEach(function (model) { return (model.collection = _this); }); // eslint-disable-line no-return-assign, no-param-reassign
|
|
68
|
+
if (change.removedCount)
|
|
69
|
+
change.removed.forEach(function (model) { return delete model.collection; }); // eslint-disable-line no-param-reassign
|
|
70
|
+
};
|
|
71
|
+
mobx.observe(_this, attachCollectionToModels);
|
|
72
|
+
return _this;
|
|
73
|
+
}
|
|
74
|
+
return SampleCollection;
|
|
75
|
+
}(Collection["default"]));
|
|
76
|
+
|
|
77
|
+
exports["default"] = SampleCollection;
|
|
@@ -18,7 +18,7 @@ export interface RemoteConfig {
|
|
|
18
18
|
[key: string]: string | number | null;
|
|
19
19
|
} | ((value: any, submission: any, model: any) => any);
|
|
20
20
|
}
|
|
21
|
-
export declare const boolToWarehouseValue: (val?: boolean |
|
|
21
|
+
export declare const boolToWarehouseValue: (val?: boolean | "t" | "f" | null) => "t" | "f";
|
|
22
22
|
export type Keys = (() => {
|
|
23
23
|
[key: string]: RemoteConfig;
|
|
24
24
|
}) | {
|
package/dist/Indicia/helpers.js
CHANGED
|
@@ -1 +1,120 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
|
|
7
|
+
var getErrorMessageFromObject = function (errors) {
|
|
8
|
+
return Object.entries(errors).reduce(function (string, err) { return "".concat(string).concat(err[0], " ").concat(err[1], "\n"); }, '');
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Converts DataURI object to a Blob.
|
|
12
|
+
*
|
|
13
|
+
* @param {type} dataURI
|
|
14
|
+
* @param {type} fileType
|
|
15
|
+
* @returns {undefined}
|
|
16
|
+
*/
|
|
17
|
+
function dataURItoBlob(dataURI, fileType) {
|
|
18
|
+
var binary = atob(dataURI.split(',')[1]);
|
|
19
|
+
var array = [];
|
|
20
|
+
for (var i = 0; i < binary.length; i++) {
|
|
21
|
+
array.push(binary.charCodeAt(i));
|
|
22
|
+
}
|
|
23
|
+
return new Blob([new Uint8Array(array)], {
|
|
24
|
+
type: fileType,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// Detecting data URLs
|
|
28
|
+
// https://gist.github.com/bgrins/6194623
|
|
29
|
+
// data URI - MDN https://developer.mozilla.org/en-US/docs/data_URIs
|
|
30
|
+
// The 'data' URL scheme: http://tools.ietf.org/html/rfc2397
|
|
31
|
+
// Valid URL Characters: http://tools.ietf.org/html/rfc2396#section2
|
|
32
|
+
function isDataURL(string) {
|
|
33
|
+
if (!string) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
var normalized = string.toString(); // numbers
|
|
37
|
+
/* eslint-disable no-useless-escape, max-len */
|
|
38
|
+
var regex = /^\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i;
|
|
39
|
+
return !!normalized.match(regex);
|
|
40
|
+
}
|
|
41
|
+
function getBlobFromURL(url, mediaType) {
|
|
42
|
+
if (isDataURL(url)) {
|
|
43
|
+
var blob = dataURItoBlob(url, mediaType);
|
|
44
|
+
return Promise.resolve(blob);
|
|
45
|
+
}
|
|
46
|
+
return new Promise(function (resolve) {
|
|
47
|
+
// load image
|
|
48
|
+
var xhr = new XMLHttpRequest();
|
|
49
|
+
xhr.open('GET', url, true);
|
|
50
|
+
xhr.responseType = 'blob';
|
|
51
|
+
xhr.onload = function () {
|
|
52
|
+
var blob = xhr.response;
|
|
53
|
+
resolve(blob);
|
|
54
|
+
};
|
|
55
|
+
// todo check error case
|
|
56
|
+
xhr.send();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var boolToWarehouseValue = function (val) {
|
|
60
|
+
return val === true || val === 't' ? 't' : 'f';
|
|
61
|
+
};
|
|
62
|
+
function validateRemoteModel() {
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
var survey = this.getSurvey();
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
var error = survey.verify && survey.verify(this.attrs, this);
|
|
67
|
+
var attributes = {};
|
|
68
|
+
if (error) {
|
|
69
|
+
var isYupError = (error === null || error === void 0 ? void 0 : error.name) === 'ValidationError' ||
|
|
70
|
+
(error === null || error === void 0 ? void 0 : error.type) === 'ValidationError';
|
|
71
|
+
var isZodError = error && 'issues' in error;
|
|
72
|
+
var isPlainError = !isYupError && !isZodError;
|
|
73
|
+
if (isPlainError) {
|
|
74
|
+
attributes.errors = [error.message];
|
|
75
|
+
}
|
|
76
|
+
else if (isZodError) {
|
|
77
|
+
attributes.errors = error.issues.map(function (_a) {
|
|
78
|
+
var message = _a.message;
|
|
79
|
+
return message;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
attributes = tslib.__assign({}, error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
var validateSubModel = function (agg, model) {
|
|
87
|
+
var invalids = model.validateRemote();
|
|
88
|
+
if (invalids) {
|
|
89
|
+
agg[model.cid] = invalids; // eslint-disable-line
|
|
90
|
+
agg[model.cid].model = model; // eslint-disable-line
|
|
91
|
+
}
|
|
92
|
+
return agg;
|
|
93
|
+
};
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
var samples = (this.samples || []).reduce(validateSubModel, {});
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
var occurrences = (this.occurrences || []).reduce(validateSubModel, {});
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
var media = (this.media || []).reduce(validateSubModel, {});
|
|
100
|
+
if (Object.keys(attributes).length ||
|
|
101
|
+
Object.keys(samples).length ||
|
|
102
|
+
Object.keys(occurrences).length ||
|
|
103
|
+
Object.keys(media).length) {
|
|
104
|
+
var models = tslib.__assign(tslib.__assign(tslib.__assign({}, samples), occurrences), media);
|
|
105
|
+
return {
|
|
106
|
+
attributes: attributes,
|
|
107
|
+
// @ts-ignore
|
|
108
|
+
model: this,
|
|
109
|
+
models: models,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
exports.boolToWarehouseValue = boolToWarehouseValue;
|
|
116
|
+
exports.dataURItoBlob = dataURItoBlob;
|
|
117
|
+
exports.getBlobFromURL = getBlobFromURL;
|
|
118
|
+
exports.getErrorMessageFromObject = getErrorMessageFromObject;
|
|
119
|
+
exports.isDataURL = isDataURL;
|
|
120
|
+
exports.validateRemoteModel = validateRemoteModel;
|
package/dist/Model.d.ts
CHANGED
package/dist/Model.js
CHANGED
|
@@ -1 +1,191 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
var mobx = require('mobx');
|
|
7
|
+
var mobxUtils = require('mobx-utils');
|
|
8
|
+
var utils = require('@flumens/utils');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Simple object clone.
|
|
12
|
+
*/
|
|
13
|
+
var clone = function (obj) { return JSON.parse(JSON.stringify(obj)); };
|
|
14
|
+
var defaults = {
|
|
15
|
+
deleted: false,
|
|
16
|
+
};
|
|
17
|
+
var Model = /** @class */ (function () {
|
|
18
|
+
function Model(_a) {
|
|
19
|
+
var _b = _a.id, id = _b === void 0 ? '' : _b, _c = _a.cid, cid = _c === void 0 ? '' : _c, createdAt = _a.createdAt, updatedAt = _a.updatedAt, syncedAt = _a.syncedAt, _d = _a.attrs, attrs = _d === void 0 ? {} : _d, store = _a.store;
|
|
20
|
+
var _this = this;
|
|
21
|
+
/**
|
|
22
|
+
* A deferred promise to flag if the model was initialised from the store.
|
|
23
|
+
*/
|
|
24
|
+
this.ready = new utils.Deferred();
|
|
25
|
+
this.debouncedValue = 3000;
|
|
26
|
+
// a mechanism to disable auto-sync
|
|
27
|
+
this._observerPaused = false;
|
|
28
|
+
this.id = id;
|
|
29
|
+
this.cid = cid || id || utils.UUIDv7();
|
|
30
|
+
var now = Date.now();
|
|
31
|
+
this.createdAt = createdAt || now;
|
|
32
|
+
this.updatedAt = updatedAt || now;
|
|
33
|
+
this.syncedAt = syncedAt;
|
|
34
|
+
this.attrs = clone(tslib.__assign(tslib.__assign({}, defaults), attrs));
|
|
35
|
+
mobx.makeObservable(this, {
|
|
36
|
+
createdAt: mobx.observable,
|
|
37
|
+
updatedAt: mobx.observable,
|
|
38
|
+
syncedAt: mobx.observable,
|
|
39
|
+
attrs: mobx.observable,
|
|
40
|
+
});
|
|
41
|
+
this.store = store;
|
|
42
|
+
var isDebouncing;
|
|
43
|
+
var debouncedSync = function () {
|
|
44
|
+
var args = [];
|
|
45
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
46
|
+
args[_i] = arguments[_i];
|
|
47
|
+
}
|
|
48
|
+
clearTimeout(isDebouncing);
|
|
49
|
+
isDebouncing = setTimeout(function () { return _this.sync.apply(_this, args); }, // eslint-disable-line prefer-spread
|
|
50
|
+
_this.debouncedValue);
|
|
51
|
+
};
|
|
52
|
+
var setUpdatedTimestamp = function () {
|
|
53
|
+
if (_this._observerPaused)
|
|
54
|
+
return;
|
|
55
|
+
_this.updatedAt = Date.now();
|
|
56
|
+
debouncedSync();
|
|
57
|
+
};
|
|
58
|
+
mobxUtils.deepObserve(this.attrs, setUpdatedTimestamp);
|
|
59
|
+
}
|
|
60
|
+
Model.prototype.setUpdatedAtTimestamp = function (newUpdatedAt) {
|
|
61
|
+
this.updatedAt = newUpdatedAt;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Initialize the model from store.
|
|
65
|
+
*/
|
|
66
|
+
Model.prototype.fetch = function () {
|
|
67
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
68
|
+
var document;
|
|
69
|
+
return tslib.__generator(this, function (_a) {
|
|
70
|
+
switch (_a.label) {
|
|
71
|
+
case 0:
|
|
72
|
+
if (!this.store) {
|
|
73
|
+
this.ready.resolve(false);
|
|
74
|
+
return [2 /*return*/];
|
|
75
|
+
}
|
|
76
|
+
return [4 /*yield*/, this.store.find(this.cid)];
|
|
77
|
+
case 1:
|
|
78
|
+
document = _a.sent();
|
|
79
|
+
if (!!document) return [3 /*break*/, 3];
|
|
80
|
+
return [4 /*yield*/, this.save()];
|
|
81
|
+
case 2:
|
|
82
|
+
_a.sent(); // persisting for the first time
|
|
83
|
+
this.ready.resolve(true);
|
|
84
|
+
return [2 /*return*/];
|
|
85
|
+
case 3:
|
|
86
|
+
if (document.id)
|
|
87
|
+
this.id = document.id; // checking presence for backwards compatibility
|
|
88
|
+
if (document.cid)
|
|
89
|
+
this.cid = document.cid; // checking presence for backwards compatibility
|
|
90
|
+
this._observerPaused = true;
|
|
91
|
+
mobx.set(this.attrs, document.data);
|
|
92
|
+
this._observerPaused = false;
|
|
93
|
+
this.createdAt = document.createdAt;
|
|
94
|
+
this.updatedAt = document.updatedAt;
|
|
95
|
+
this.syncedAt = document.syncedAt;
|
|
96
|
+
this.ready.resolve(true);
|
|
97
|
+
return [2 /*return*/];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Save the model to the offline store.
|
|
104
|
+
*/
|
|
105
|
+
Model.prototype.save = function () {
|
|
106
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
107
|
+
var json;
|
|
108
|
+
return tslib.__generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
if (!this.store) {
|
|
112
|
+
throw new Error('Trying to save locally without a store');
|
|
113
|
+
}
|
|
114
|
+
json = this.toJSON();
|
|
115
|
+
return [4 /*yield*/, this.store.save(tslib.__assign(tslib.__assign({}, json), { data: json.attrs }))];
|
|
116
|
+
case 1:
|
|
117
|
+
_a.sent();
|
|
118
|
+
return [2 /*return*/];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Destroy the model and remove from the offline store.
|
|
125
|
+
*/
|
|
126
|
+
Model.prototype.destroy = function () {
|
|
127
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
128
|
+
return tslib.__generator(this, function (_a) {
|
|
129
|
+
switch (_a.label) {
|
|
130
|
+
case 0:
|
|
131
|
+
if (!this.store) {
|
|
132
|
+
throw new Error('Trying to delete locally without a store');
|
|
133
|
+
}
|
|
134
|
+
return [4 /*yield*/, this.store.delete(this.cid)];
|
|
135
|
+
case 1:
|
|
136
|
+
_a.sent();
|
|
137
|
+
return [2 /*return*/];
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
143
|
+
Model.prototype.sync = function () {
|
|
144
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
145
|
+
return tslib.__generator(this, function (_a) {
|
|
146
|
+
switch (_a.label) {
|
|
147
|
+
case 0:
|
|
148
|
+
if (!this.store) return [3 /*break*/, 2];
|
|
149
|
+
return [4 /*yield*/, this.save()];
|
|
150
|
+
case 1:
|
|
151
|
+
_a.sent(); // if extended then store might be initialised in a subclass later
|
|
152
|
+
_a.label = 2;
|
|
153
|
+
case 2: return [2 /*return*/];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Returns a clean (no observables) JSON representation of the model.
|
|
160
|
+
*/
|
|
161
|
+
Model.prototype.toJSON = function () {
|
|
162
|
+
return {
|
|
163
|
+
id: this.id,
|
|
164
|
+
cid: this.cid,
|
|
165
|
+
createdAt: this.createdAt,
|
|
166
|
+
updatedAt: this.updatedAt,
|
|
167
|
+
syncedAt: this.syncedAt,
|
|
168
|
+
attrs: mobx.toJS(this.attrs),
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
Model.prototype.resetDefaults = function (defaultsToSet) {
|
|
172
|
+
var _this = this;
|
|
173
|
+
this.id = ''; // must be first in case a reaction is triggered from changing attrs that check this
|
|
174
|
+
var defaultsCopy = JSON.parse(JSON.stringify(tslib.__assign(tslib.__assign({}, defaults), defaultsToSet)));
|
|
175
|
+
mobx.set(this.attrs, defaultsCopy);
|
|
176
|
+
// clean up non-defaults
|
|
177
|
+
var defaultKeys = Object.keys(defaultsCopy);
|
|
178
|
+
var deleteProp = function (key) {
|
|
179
|
+
var isDefaultKey = defaultKeys.includes(key);
|
|
180
|
+
if (isDefaultKey)
|
|
181
|
+
return;
|
|
182
|
+
_this.attrs[key] = null; // might be a different type to current set value
|
|
183
|
+
delete _this.attrs[key];
|
|
184
|
+
};
|
|
185
|
+
Object.keys(this.attrs).forEach(deleteProp);
|
|
186
|
+
return this.save();
|
|
187
|
+
};
|
|
188
|
+
return Model;
|
|
189
|
+
}());
|
|
190
|
+
|
|
191
|
+
exports["default"] = Model;
|
|
@@ -1 +1,228 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
var LocalForage = require('localforage');
|
|
7
|
+
var CordovaSQLiteDriver = require('localforage-cordovasqlitedriver');
|
|
8
|
+
var utils = require('@flumens/utils');
|
|
9
|
+
var utils$1 = require('./utils.js');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var LocalForage__default = /*#__PURE__*/_interopDefaultLegacy(LocalForage);
|
|
14
|
+
var CordovaSQLiteDriver__default = /*#__PURE__*/_interopDefaultLegacy(CordovaSQLiteDriver);
|
|
15
|
+
|
|
16
|
+
var defaultDriverOrder = ['indexeddb', 'websql', 'localstorage'];
|
|
17
|
+
if (utils.isPlatform('hybrid')) {
|
|
18
|
+
if (!window.sqlitePlugin) {
|
|
19
|
+
throw new Error('cordova-sql-storage plugin is missing. Please install it.');
|
|
20
|
+
}
|
|
21
|
+
defaultDriverOrder = tslib.__spreadArray([CordovaSQLiteDriver__default["default"]], defaultDriverOrder, true);
|
|
22
|
+
}
|
|
23
|
+
// create local store
|
|
24
|
+
var defaultConfig = {
|
|
25
|
+
driverOrder: defaultDriverOrder,
|
|
26
|
+
};
|
|
27
|
+
var LocalForageStore = /** @class */ (function () {
|
|
28
|
+
function LocalForageStore(options) {
|
|
29
|
+
var _this = this;
|
|
30
|
+
var config = tslib.__assign(tslib.__assign({}, defaultConfig), options);
|
|
31
|
+
this.debugging = config.debugging;
|
|
32
|
+
// initialize db
|
|
33
|
+
this.localForage = null;
|
|
34
|
+
this.ready = new Promise(function (resolve, reject) {
|
|
35
|
+
// check custom drivers (eg. SQLite)
|
|
36
|
+
var customDriversPromise = new Promise(function (_resolve) {
|
|
37
|
+
if (config.driverOrder && typeof config.driverOrder[0] === 'object') {
|
|
38
|
+
LocalForage__default["default"].defineDriver(config.driverOrder[0]).then(_resolve);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
_resolve(undefined);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
if (!config.storeName) {
|
|
45
|
+
throw new Error('storeName prop is missing');
|
|
46
|
+
}
|
|
47
|
+
_this.storeName = config.storeName;
|
|
48
|
+
// config
|
|
49
|
+
customDriversPromise.then(function () {
|
|
50
|
+
var dbConfig = {
|
|
51
|
+
name: config.name || 'indicia',
|
|
52
|
+
storeName: config.storeName,
|
|
53
|
+
};
|
|
54
|
+
if (config.version) {
|
|
55
|
+
dbConfig.version = config.version;
|
|
56
|
+
}
|
|
57
|
+
var drivers = LocalForageStore._getDriverOrder(config.driverOrder);
|
|
58
|
+
var DB = config.LocalForage || LocalForage__default["default"];
|
|
59
|
+
// init
|
|
60
|
+
_this.localForage = DB.createInstance(dbConfig);
|
|
61
|
+
_this.localForage.setDriver(drivers).then(resolve).catch(reject);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
LocalForageStore._getDriverOrder = function (driverOrder) {
|
|
66
|
+
return driverOrder.map(function (driver) {
|
|
67
|
+
switch (driver) {
|
|
68
|
+
case 'indexeddb':
|
|
69
|
+
return LocalForage__default["default"].INDEXEDDB;
|
|
70
|
+
case 'websql':
|
|
71
|
+
return LocalForage__default["default"].WEBSQL;
|
|
72
|
+
case 'localstorage':
|
|
73
|
+
return LocalForage__default["default"].LOCALSTORAGE;
|
|
74
|
+
default:
|
|
75
|
+
// custom
|
|
76
|
+
if (typeof driver === 'object' && driver._driver) {
|
|
77
|
+
return driver._driver;
|
|
78
|
+
}
|
|
79
|
+
return console.error('No such db driver!');
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
LocalForageStore.prototype.save = function (val) {
|
|
84
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
85
|
+
var currentVal;
|
|
86
|
+
return tslib.__generator(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
89
|
+
case 1:
|
|
90
|
+
_a.sent();
|
|
91
|
+
if (!val.cid) {
|
|
92
|
+
throw new Error('Invalid key passed to store');
|
|
93
|
+
}
|
|
94
|
+
if (!this.debugging) return [3 /*break*/, 3];
|
|
95
|
+
return [4 /*yield*/, this.find(val.cid)];
|
|
96
|
+
case 2:
|
|
97
|
+
currentVal = _a.sent();
|
|
98
|
+
utils$1.printDiff(currentVal, val, val.cid, this.storeName);
|
|
99
|
+
_a.label = 3;
|
|
100
|
+
case 3: return [2 /*return*/, this.localForage.setItem(val.cid, val)];
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
LocalForageStore.prototype.find = function (key) {
|
|
106
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
107
|
+
return tslib.__generator(this, function (_a) {
|
|
108
|
+
switch (_a.label) {
|
|
109
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
110
|
+
case 1:
|
|
111
|
+
_a.sent();
|
|
112
|
+
if (!key) {
|
|
113
|
+
throw new Error('Invalid key passed to store');
|
|
114
|
+
}
|
|
115
|
+
return [2 /*return*/, this.localForage.getItem(key)];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
LocalForageStore.prototype.findAll = function () {
|
|
121
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
122
|
+
var models;
|
|
123
|
+
return tslib.__generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
126
|
+
case 1:
|
|
127
|
+
_a.sent();
|
|
128
|
+
models = [];
|
|
129
|
+
return [4 /*yield*/, this.localForage.iterate(function (value) {
|
|
130
|
+
models.push(value);
|
|
131
|
+
})];
|
|
132
|
+
case 2:
|
|
133
|
+
_a.sent();
|
|
134
|
+
return [2 /*return*/, models];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
LocalForageStore.prototype.delete = function (key) {
|
|
140
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
141
|
+
return tslib.__generator(this, function (_a) {
|
|
142
|
+
switch (_a.label) {
|
|
143
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
144
|
+
case 1:
|
|
145
|
+
_a.sent();
|
|
146
|
+
if (!key) {
|
|
147
|
+
throw new Error('Invalid key passed to store');
|
|
148
|
+
}
|
|
149
|
+
return [2 /*return*/, this.localForage.removeItem(key)];
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
LocalForageStore.prototype.deleteAll = function () {
|
|
155
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
156
|
+
var models;
|
|
157
|
+
var _this = this;
|
|
158
|
+
return tslib.__generator(this, function (_a) {
|
|
159
|
+
switch (_a.label) {
|
|
160
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
161
|
+
case 1:
|
|
162
|
+
_a.sent();
|
|
163
|
+
return [4 /*yield*/, this.findAll()];
|
|
164
|
+
case 2:
|
|
165
|
+
models = _a.sent();
|
|
166
|
+
return [4 /*yield*/, Promise.all(models.map(function (model) { return _this.delete(model); }))];
|
|
167
|
+
case 3:
|
|
168
|
+
_a.sent();
|
|
169
|
+
return [2 /*return*/];
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Exports all objects from the store.
|
|
176
|
+
*/
|
|
177
|
+
LocalForageStore.prototype.export = function () {
|
|
178
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
179
|
+
var addCIDs;
|
|
180
|
+
return tslib.__generator(this, function (_a) {
|
|
181
|
+
switch (_a.label) {
|
|
182
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
183
|
+
case 1:
|
|
184
|
+
_a.sent();
|
|
185
|
+
addCIDs = function (agg, val) {
|
|
186
|
+
var _a;
|
|
187
|
+
return (tslib.__assign(tslib.__assign({}, agg), (_a = {}, _a[val.cid] = val, _a)));
|
|
188
|
+
};
|
|
189
|
+
return [4 /*yield*/, this.findAll()];
|
|
190
|
+
case 2: return [2 /*return*/, (_a.sent()).reduce(addCIDs, {})];
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Imports objects to store.
|
|
197
|
+
*/
|
|
198
|
+
LocalForageStore.prototype.import = function (obj) {
|
|
199
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
200
|
+
var savingAll;
|
|
201
|
+
var _this = this;
|
|
202
|
+
return tslib.__generator(this, function (_a) {
|
|
203
|
+
switch (_a.label) {
|
|
204
|
+
case 0: return [4 /*yield*/, this.ready];
|
|
205
|
+
case 1:
|
|
206
|
+
_a.sent();
|
|
207
|
+
return [4 /*yield*/, this.deleteAll()];
|
|
208
|
+
case 2:
|
|
209
|
+
_a.sent();
|
|
210
|
+
if (typeof obj !== 'object') {
|
|
211
|
+
throw new Error('Invalid obj passed to store');
|
|
212
|
+
}
|
|
213
|
+
savingAll = Object.entries(obj).map(function (_a) {
|
|
214
|
+
var val = _a[1];
|
|
215
|
+
return _this.save(val);
|
|
216
|
+
});
|
|
217
|
+
return [4 /*yield*/, Promise.all(savingAll)];
|
|
218
|
+
case 3:
|
|
219
|
+
_a.sent();
|
|
220
|
+
return [2 /*return*/];
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
return LocalForageStore;
|
|
226
|
+
}());
|
|
227
|
+
|
|
228
|
+
exports["default"] = LocalForageStore;
|