backend-plus 2.1.5 → 2.1.6
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.
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
(function (factory) {
|
|
38
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
39
|
+
var v = factory(require, exports);
|
|
40
|
+
if (v !== undefined) module.exports = v;
|
|
41
|
+
}
|
|
42
|
+
else if (typeof define === "function" && define.amd) {
|
|
43
|
+
define(["require", "exports", "like-ar"], factory);
|
|
44
|
+
}
|
|
45
|
+
})(function (require, exports) {
|
|
46
|
+
//import { TableDefinition } from "backend-plus";
|
|
47
|
+
"use strict";
|
|
48
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
|
+
exports.LocalDbTransaction = exports.LocalDb = exports.detectedFeatures = void 0;
|
|
50
|
+
//x// <amd-dependency path="../../../node_modules/like-ar/like-ar.d.ts" name="likeAr"/>
|
|
51
|
+
//x// <reference path="../../node_modules/like-ar/like-ar.d.ts" />
|
|
52
|
+
/// <reference path="../node_modules/types.d.ts/modules/myOwn/in-myOwn.d.ts" />
|
|
53
|
+
/// <reference path="../../../lib/in-backend-plus.d.ts" />
|
|
54
|
+
/// <reference path="../../../lib/backend-plus.d.ts" />
|
|
55
|
+
/// <reference path="../../lib/in-backend-plus.d.ts" />
|
|
56
|
+
/// <reference path="../../lib/backend-plus.d.ts" />
|
|
57
|
+
/// <reference path="../lib/in-backend-plus.d.ts" />
|
|
58
|
+
/// <reference path="../lib/backend-plus.d.ts" />
|
|
59
|
+
/// <reference path="lib/in-backend-plus.d.ts" />
|
|
60
|
+
/// <reference path="lib/backend-plus.d.ts" />
|
|
61
|
+
/****
|
|
62
|
+
* Atención en la implementación de este módulo se tiene en cuenta que según
|
|
63
|
+
* https://caniuse.com/#feat=indexeddb ocurren dos cosas trágicas:
|
|
64
|
+
* 1) en IE y EDGE no hay claves de tipo array
|
|
65
|
+
* 2) en iOS de 8 a 9.3 hay un error que no permite que en dos objetos distintos haya una misma Pk
|
|
66
|
+
*
|
|
67
|
+
* más explicaciones en:
|
|
68
|
+
* 1) https://stackoverflow.com/questions/14283257/dataerror-when-creating-an-index-with-a-compound-key-in-ie-10
|
|
69
|
+
* 2) https://www.raymondcamden.com/2014/09/25/IndexedDB-on-iOS-8-Broken-Bad/
|
|
70
|
+
*
|
|
71
|
+
* hay un test en:
|
|
72
|
+
* a) https://codepen.io/cemerick/pen/Itymi
|
|
73
|
+
*
|
|
74
|
+
* workarround para contemplar IE, y iOS viejos:
|
|
75
|
+
* 1) se transforman las claves antes de usarlas en un string (JSON.stringify del array para el problema del IE)
|
|
76
|
+
* 2) se prefija el string con el nombre del objectStore (para que todas las claves sean distintas y no haya problemas con el iOS viejo)
|
|
77
|
+
*
|
|
78
|
+
* detección
|
|
79
|
+
* 1) por ahora solo probamos la detección del problema del array de claves
|
|
80
|
+
* 2) TODO: Falta programar la detección del problema de iOS viejo, quizás haya que ser explícito, hay que probarlo con varios iPad
|
|
81
|
+
*/
|
|
82
|
+
var likeAr = require("like-ar");
|
|
83
|
+
exports.detectedFeatures = {
|
|
84
|
+
needToUnwrapArrayKeys: null
|
|
85
|
+
};
|
|
86
|
+
var LocalDb = /** @class */ (function () {
|
|
87
|
+
function LocalDb(name) {
|
|
88
|
+
this.name = name;
|
|
89
|
+
var ldb = this;
|
|
90
|
+
var initialStores = {
|
|
91
|
+
$structures: 'name',
|
|
92
|
+
$internals: 'var',
|
|
93
|
+
$detect: ['detectKey'],
|
|
94
|
+
};
|
|
95
|
+
var initialVersionInfo = {
|
|
96
|
+
var: 'version',
|
|
97
|
+
num: 1,
|
|
98
|
+
timestamp: new Date().toJSON(),
|
|
99
|
+
stores: initialStores
|
|
100
|
+
};
|
|
101
|
+
var requestDB = indexedDB.open(this.name);
|
|
102
|
+
requestDB.onupgradeneeded = function () {
|
|
103
|
+
var db = requestDB.result;
|
|
104
|
+
if (!db.objectStoreNames.contains("$internals")) {
|
|
105
|
+
likeAr(initialStores).forEach(function (keyPath, tableName) {
|
|
106
|
+
var store = db.createObjectStore(tableName, { keyPath: keyPath });
|
|
107
|
+
if (tableName == '$internals') {
|
|
108
|
+
store.put(initialVersionInfo);
|
|
109
|
+
}
|
|
110
|
+
if (tableName == '$detect') {
|
|
111
|
+
ldb.wait4detectedFeatures = ldb.detectFeatures(store);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
ldb.wait4db = ldb.IDBX(requestDB);
|
|
117
|
+
}
|
|
118
|
+
LocalDb.deleteDatabase = function (name) {
|
|
119
|
+
var request = indexedDB.deleteDatabase(name);
|
|
120
|
+
return new Promise(function (resolve, reject) {
|
|
121
|
+
request.onsuccess = function () {
|
|
122
|
+
resolve();
|
|
123
|
+
};
|
|
124
|
+
request.onerror = function (event) {
|
|
125
|
+
reject();
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
LocalDb.prototype.IDBX = function (request) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
133
|
+
if ('onsuccess' in request) {
|
|
134
|
+
request.onsuccess = function () {
|
|
135
|
+
resolve(request.result);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
request.oncomplete = function () {
|
|
140
|
+
resolve();
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
request.onerror = function () {
|
|
144
|
+
alertPromise(request.error.message || request.error.name);
|
|
145
|
+
reject(request.error);
|
|
146
|
+
};
|
|
147
|
+
})];
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
LocalDb.prototype.registerStructure = function (tableDef) {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
153
|
+
var ldb, wait4dbCurrent, registerTask, result;
|
|
154
|
+
return __generator(this, function (_a) {
|
|
155
|
+
switch (_a.label) {
|
|
156
|
+
case 0:
|
|
157
|
+
ldb = this;
|
|
158
|
+
wait4dbCurrent = this.wait4db;
|
|
159
|
+
registerTask = this.registerStructureInside(tableDef, wait4dbCurrent);
|
|
160
|
+
this.wait4db = registerTask.then(function (result) { return result.wait4db; });
|
|
161
|
+
return [4 /*yield*/, registerTask];
|
|
162
|
+
case 1:
|
|
163
|
+
result = _a.sent();
|
|
164
|
+
return [2 /*return*/, result.result];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
LocalDb.prototype.detectFeatures = function (store) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
171
|
+
return __generator(this, function (_a) {
|
|
172
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
173
|
+
try {
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
var os = window.myOwn.config.useragent.os;
|
|
176
|
+
// @ts-ignore
|
|
177
|
+
var version = parseInt(window.myOwn.config.useragent.version.split('.')[0]);
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
throw Error("unknowed OS or version");
|
|
181
|
+
}
|
|
182
|
+
if (os === 'OS X' && version < 11) {
|
|
183
|
+
exports.detectedFeatures.needToUnwrapArrayKeys = true;
|
|
184
|
+
resolve(exports.detectedFeatures);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
var request = store.put({ detectKey: 'one' });
|
|
188
|
+
request.onsuccess = function () {
|
|
189
|
+
var key = request.result;
|
|
190
|
+
if (typeof key === "string") {
|
|
191
|
+
exports.detectedFeatures.needToUnwrapArrayKeys = true;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
exports.detectedFeatures.needToUnwrapArrayKeys = false;
|
|
195
|
+
}
|
|
196
|
+
resolve(exports.detectedFeatures);
|
|
197
|
+
};
|
|
198
|
+
request.onerror = function () {
|
|
199
|
+
exports.detectedFeatures.needToUnwrapArrayKeys = true;
|
|
200
|
+
resolve(exports.detectedFeatures);
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
})];
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
LocalDb.prototype.registerStructureInside = function (tableDef, wait4db) {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
209
|
+
var ldb, db, result, oldValue, infoStore, versionInfo, requestDB;
|
|
210
|
+
return __generator(this, function (_a) {
|
|
211
|
+
switch (_a.label) {
|
|
212
|
+
case 0:
|
|
213
|
+
ldb = this;
|
|
214
|
+
return [4 /*yield*/, wait4db];
|
|
215
|
+
case 1:
|
|
216
|
+
db = _a.sent();
|
|
217
|
+
result = { wait4db: wait4db, result: { changed: null } };
|
|
218
|
+
return [4 /*yield*/, this.IDBX(db.transaction('$structures', "readonly").objectStore('$structures').get(tableDef.name))];
|
|
219
|
+
case 2:
|
|
220
|
+
oldValue = _a.sent();
|
|
221
|
+
if (!oldValue) {
|
|
222
|
+
result.result.new = true;
|
|
223
|
+
}
|
|
224
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction('$structures', "readwrite").objectStore('$structures').put(tableDef))];
|
|
225
|
+
case 3:
|
|
226
|
+
_a.sent();
|
|
227
|
+
result.result.changed = JSON.stringify(tableDef) != JSON.stringify(oldValue);
|
|
228
|
+
if (!ldb.wait4detectedFeatures) {
|
|
229
|
+
ldb.wait4detectedFeatures = ldb.detectFeatures(db.transaction('$detect', "readwrite").objectStore('$detect'));
|
|
230
|
+
}
|
|
231
|
+
return [4 /*yield*/, ldb.wait4detectedFeatures];
|
|
232
|
+
case 4:
|
|
233
|
+
_a.sent();
|
|
234
|
+
infoStore = exports.detectedFeatures.needToUnwrapArrayKeys ? null : tableDef.primaryKey;
|
|
235
|
+
return [4 /*yield*/, this.IDBX(db.transaction('$internals', "readonly").objectStore('$internals').get('version'))];
|
|
236
|
+
case 5:
|
|
237
|
+
versionInfo = _a.sent();
|
|
238
|
+
if (!(JSON.stringify(versionInfo.stores[tableDef.name]) != JSON.stringify(infoStore))) return [3 /*break*/, 8];
|
|
239
|
+
db.close();
|
|
240
|
+
versionInfo.num++;
|
|
241
|
+
requestDB = indexedDB.open(this.name, versionInfo.num);
|
|
242
|
+
requestDB.onupgradeneeded = function () {
|
|
243
|
+
var db = requestDB.result;
|
|
244
|
+
if (versionInfo.stores[tableDef.name]) {
|
|
245
|
+
db.deleteObjectStore(tableDef.name);
|
|
246
|
+
}
|
|
247
|
+
if (infoStore != null) {
|
|
248
|
+
db.createObjectStore(tableDef.name, { keyPath: infoStore });
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
db.createObjectStore(tableDef.name);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
result.wait4db = ldb.IDBX(requestDB);
|
|
255
|
+
return [4 /*yield*/, result.wait4db];
|
|
256
|
+
case 6:
|
|
257
|
+
db = _a.sent();
|
|
258
|
+
versionInfo.stores[tableDef.name] = infoStore;
|
|
259
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction('$internals', "readwrite").objectStore('$internals').put(versionInfo))];
|
|
260
|
+
case 7:
|
|
261
|
+
_a.sent();
|
|
262
|
+
_a.label = 8;
|
|
263
|
+
case 8: return [2 /*return*/, result];
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
LocalDb.prototype.getStructure = function (tableName) {
|
|
269
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
270
|
+
var ldb, db, tableDef;
|
|
271
|
+
return __generator(this, function (_a) {
|
|
272
|
+
switch (_a.label) {
|
|
273
|
+
case 0:
|
|
274
|
+
ldb = this;
|
|
275
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
276
|
+
case 1:
|
|
277
|
+
db = _a.sent();
|
|
278
|
+
if (!ldb.wait4detectedFeatures) {
|
|
279
|
+
ldb.wait4detectedFeatures = ldb.detectFeatures(db.transaction('$detect', "readwrite").objectStore('$detect'));
|
|
280
|
+
}
|
|
281
|
+
return [4 /*yield*/, ldb.wait4detectedFeatures];
|
|
282
|
+
case 2:
|
|
283
|
+
_a.sent();
|
|
284
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction('$structures', "readonly").objectStore('$structures').get(tableName))];
|
|
285
|
+
case 3:
|
|
286
|
+
tableDef = _a.sent();
|
|
287
|
+
return [2 /*return*/, tableDef];
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
LocalDb.prototype.existsStructure = function (tableName) {
|
|
293
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
294
|
+
var tableDef;
|
|
295
|
+
return __generator(this, function (_a) {
|
|
296
|
+
switch (_a.label) {
|
|
297
|
+
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
298
|
+
case 1:
|
|
299
|
+
tableDef = _a.sent();
|
|
300
|
+
return [2 /*return*/, tableDef ? true : false];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
LocalDb.prototype.getOneIfExists = function (tableName, key) {
|
|
306
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
307
|
+
var ldb, db, internalKey, result;
|
|
308
|
+
return __generator(this, function (_a) {
|
|
309
|
+
switch (_a.label) {
|
|
310
|
+
case 0:
|
|
311
|
+
ldb = this;
|
|
312
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
313
|
+
case 1:
|
|
314
|
+
db = _a.sent();
|
|
315
|
+
if (exports.detectedFeatures.needToUnwrapArrayKeys) {
|
|
316
|
+
internalKey = tableName + JSON.stringify(key);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
internalKey = key;
|
|
320
|
+
}
|
|
321
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction(tableName, "readonly").objectStore(tableName).get(internalKey))];
|
|
322
|
+
case 2:
|
|
323
|
+
result = _a.sent();
|
|
324
|
+
return [2 /*return*/, result];
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
LocalDb.prototype.getOne = function (tableName, key) {
|
|
330
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
331
|
+
var result;
|
|
332
|
+
return __generator(this, function (_a) {
|
|
333
|
+
switch (_a.label) {
|
|
334
|
+
case 0: return [4 /*yield*/, this.getOneIfExists(tableName, key)];
|
|
335
|
+
case 1:
|
|
336
|
+
result = _a.sent();
|
|
337
|
+
if (!result) {
|
|
338
|
+
throw new Error("no result");
|
|
339
|
+
}
|
|
340
|
+
return [2 /*return*/, result];
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
LocalDb.prototype.getChild = function (tableName, parentKey) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
347
|
+
var ldb, db, rows, internalKey, cursor;
|
|
348
|
+
return __generator(this, function (_a) {
|
|
349
|
+
switch (_a.label) {
|
|
350
|
+
case 0:
|
|
351
|
+
ldb = this;
|
|
352
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
353
|
+
case 1:
|
|
354
|
+
db = _a.sent();
|
|
355
|
+
rows = [];
|
|
356
|
+
if (exports.detectedFeatures.needToUnwrapArrayKeys) {
|
|
357
|
+
internalKey = tableName + JSON.stringify(parentKey);
|
|
358
|
+
internalKey = internalKey.substr(0, internalKey.length - 1);
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
internalKey = parentKey ? parentKey : '';
|
|
362
|
+
}
|
|
363
|
+
cursor = db.transaction([tableName], 'readonly').objectStore(tableName).openCursor(parentKey == null ? null : IDBKeyRange.lowerBound(internalKey));
|
|
364
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
365
|
+
cursor.onsuccess = function (event) {
|
|
366
|
+
// @ts-ignore target no conoce result en la definición de TS. Verificar dentro de un tiempo si TS mejoró
|
|
367
|
+
var cursor = event.target.result;
|
|
368
|
+
if (cursor && ((parentKey == null) || (exports.detectedFeatures.needToUnwrapArrayKeys ?
|
|
369
|
+
internalKey == cursor.key.slice(0, internalKey.length) :
|
|
370
|
+
!parentKey.find(function (expectedValue, i) {
|
|
371
|
+
var storedValue = cursor.key[i];
|
|
372
|
+
return expectedValue != storedValue;
|
|
373
|
+
})))) {
|
|
374
|
+
rows.push(cursor.value);
|
|
375
|
+
cursor.continue();
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
resolve(rows);
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
cursor.onerror = function () {
|
|
382
|
+
reject(cursor.error);
|
|
383
|
+
};
|
|
384
|
+
})];
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
};
|
|
389
|
+
LocalDb.prototype.getAll = function (tableName) {
|
|
390
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
391
|
+
return __generator(this, function (_a) {
|
|
392
|
+
return [2 /*return*/, this.getChild(tableName, tableName[0] == '$' ? null : [])];
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
LocalDb.prototype.getAllStructures = function () {
|
|
397
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
398
|
+
return __generator(this, function (_a) {
|
|
399
|
+
return [2 /*return*/, this.getAll('$structures')];
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
};
|
|
403
|
+
LocalDb.prototype.isEmpty = function (tableName) {
|
|
404
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
405
|
+
var ldb, db, count;
|
|
406
|
+
return __generator(this, function (_a) {
|
|
407
|
+
switch (_a.label) {
|
|
408
|
+
case 0:
|
|
409
|
+
ldb = this;
|
|
410
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
411
|
+
case 1:
|
|
412
|
+
db = _a.sent();
|
|
413
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction(tableName, "readonly").objectStore(tableName).count())];
|
|
414
|
+
case 2:
|
|
415
|
+
count = _a.sent();
|
|
416
|
+
return [2 /*return*/, count == 0];
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
LocalDb.prototype.putOneAndGetIfNeeded = function (tableName, element, needed) {
|
|
422
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
423
|
+
var ldb, db, tableDef, createPromiseForFK, promisesArray;
|
|
424
|
+
return __generator(this, function (_a) {
|
|
425
|
+
switch (_a.label) {
|
|
426
|
+
case 0:
|
|
427
|
+
ldb = this;
|
|
428
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
429
|
+
case 1:
|
|
430
|
+
db = _a.sent();
|
|
431
|
+
return [4 /*yield*/, ldb.getStructure(tableName)];
|
|
432
|
+
case 2:
|
|
433
|
+
tableDef = _a.sent();
|
|
434
|
+
createPromiseForFK = function createPromiseForFK(fk) {
|
|
435
|
+
return Promise.resolve().then(function () {
|
|
436
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
437
|
+
var fkTableDef, pk, isFKCompleteInSource, fkRecord;
|
|
438
|
+
return __generator(this, function (_a) {
|
|
439
|
+
switch (_a.label) {
|
|
440
|
+
case 0: return [4 /*yield*/, ldb.getStructure(fk.references)];
|
|
441
|
+
case 1:
|
|
442
|
+
fkTableDef = _a.sent();
|
|
443
|
+
if (!fkTableDef) return [3 /*break*/, 3];
|
|
444
|
+
pk = [];
|
|
445
|
+
fk.fields.forEach(function (field) {
|
|
446
|
+
pk.push(element[field.source]);
|
|
447
|
+
});
|
|
448
|
+
isFKCompleteInSource = fk.fields.filter(function (field) {
|
|
449
|
+
return element[field.source] == null;
|
|
450
|
+
}).length == 0;
|
|
451
|
+
if (!isFKCompleteInSource) return [3 /*break*/, 3];
|
|
452
|
+
return [4 /*yield*/, ldb.getOneIfExists(fk.references, pk)];
|
|
453
|
+
case 2:
|
|
454
|
+
fkRecord = _a.sent();
|
|
455
|
+
if (fkRecord) {
|
|
456
|
+
fk.displayFields.forEach(function (field) {
|
|
457
|
+
element[fk.alias + '__' + field] = fkRecord[field];
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
_a.label = 3;
|
|
461
|
+
case 3: return [2 /*return*/];
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
};
|
|
467
|
+
promisesArray = [];
|
|
468
|
+
if (tableDef.foreignKeys) {
|
|
469
|
+
tableDef.foreignKeys.forEach(function (fk) {
|
|
470
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
471
|
+
return __generator(this, function (_a) {
|
|
472
|
+
promisesArray.push(createPromiseForFK(fk));
|
|
473
|
+
return [2 /*return*/];
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
if (tableDef.softForeignKeys) {
|
|
479
|
+
tableDef.softForeignKeys.forEach(function (fk) {
|
|
480
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
481
|
+
return __generator(this, function (_a) {
|
|
482
|
+
promisesArray.push(createPromiseForFK(fk));
|
|
483
|
+
return [2 /*return*/];
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return [2 /*return*/, Promise.all(promisesArray).then(function () {
|
|
489
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
490
|
+
var newKey, storeTask, storeTask, key;
|
|
491
|
+
return __generator(this, function (_a) {
|
|
492
|
+
switch (_a.label) {
|
|
493
|
+
case 0:
|
|
494
|
+
if (exports.detectedFeatures.needToUnwrapArrayKeys) {
|
|
495
|
+
newKey = tableName + JSON.stringify(tableDef.primaryKey.map(function (name) {
|
|
496
|
+
return element[name];
|
|
497
|
+
}));
|
|
498
|
+
storeTask = db.transaction(tableName, "readwrite").objectStore(tableName).put(element, newKey);
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
storeTask = db.transaction(tableName, "readwrite").objectStore(tableName).put(element);
|
|
502
|
+
}
|
|
503
|
+
if (!needed) return [3 /*break*/, 3];
|
|
504
|
+
return [4 /*yield*/, ldb.IDBX(storeTask)];
|
|
505
|
+
case 1:
|
|
506
|
+
key = _a.sent();
|
|
507
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction(tableName, "readonly").objectStore(tableName).get(key))];
|
|
508
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
509
|
+
case 3: return [2 /*return*/, null];
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
})];
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
};
|
|
518
|
+
LocalDb.prototype.putOne = function (tableName, element) {
|
|
519
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
520
|
+
return __generator(this, function (_a) {
|
|
521
|
+
return [2 /*return*/, this.putOneAndGetIfNeeded(tableName, element, true)];
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
LocalDb.prototype.putMany = function (tableName, elements) {
|
|
526
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
527
|
+
function putNext() {
|
|
528
|
+
if (i < elements.length) {
|
|
529
|
+
if (exports.detectedFeatures.needToUnwrapArrayKeys) {
|
|
530
|
+
var newKey = tableName + JSON.stringify(tableDef.primaryKey.map(function (name) {
|
|
531
|
+
return elements[i][name];
|
|
532
|
+
}));
|
|
533
|
+
objectStore.put(elements[i], newKey).onsuccess = putNext;
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
objectStore.put(elements[i]).onsuccess = putNext;
|
|
537
|
+
}
|
|
538
|
+
++i;
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
return Promise.resolve();
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
var ldb, db, tableDef, transaction, objectStore, i;
|
|
545
|
+
return __generator(this, function (_a) {
|
|
546
|
+
switch (_a.label) {
|
|
547
|
+
case 0:
|
|
548
|
+
ldb = this;
|
|
549
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
550
|
+
case 1:
|
|
551
|
+
db = _a.sent();
|
|
552
|
+
return [4 /*yield*/, ldb.getStructure(tableName)];
|
|
553
|
+
case 2:
|
|
554
|
+
tableDef = _a.sent();
|
|
555
|
+
transaction = db.transaction(tableName, "readwrite");
|
|
556
|
+
objectStore = transaction.objectStore(tableName);
|
|
557
|
+
i = 0;
|
|
558
|
+
putNext();
|
|
559
|
+
return [2 /*return*/];
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
LocalDb.prototype.close = function () {
|
|
565
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
566
|
+
var db;
|
|
567
|
+
return __generator(this, function (_a) {
|
|
568
|
+
switch (_a.label) {
|
|
569
|
+
case 0: return [4 /*yield*/, this.wait4db];
|
|
570
|
+
case 1:
|
|
571
|
+
db = _a.sent();
|
|
572
|
+
db.close();
|
|
573
|
+
this.wait4db = {
|
|
574
|
+
then: function () {
|
|
575
|
+
throw new Error("the database is closed");
|
|
576
|
+
},
|
|
577
|
+
catch: function () {
|
|
578
|
+
throw new Error("the database is closed");
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
return [2 /*return*/];
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
});
|
|
585
|
+
};
|
|
586
|
+
LocalDb.prototype.clear = function (tableName) {
|
|
587
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
588
|
+
var ldb, db;
|
|
589
|
+
return __generator(this, function (_a) {
|
|
590
|
+
switch (_a.label) {
|
|
591
|
+
case 0:
|
|
592
|
+
ldb = this;
|
|
593
|
+
return [4 /*yield*/, ldb.wait4db];
|
|
594
|
+
case 1:
|
|
595
|
+
db = _a.sent();
|
|
596
|
+
return [4 /*yield*/, ldb.IDBX(db.transaction(tableName, "readwrite").objectStore(tableName).clear())];
|
|
597
|
+
case 2:
|
|
598
|
+
_a.sent();
|
|
599
|
+
return [2 /*return*/];
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
};
|
|
604
|
+
return LocalDb;
|
|
605
|
+
}());
|
|
606
|
+
exports.LocalDb = LocalDb;
|
|
607
|
+
var LocalDbTransaction = /** @class */ (function () {
|
|
608
|
+
function LocalDbTransaction(localDbName) {
|
|
609
|
+
this.localDbName = localDbName;
|
|
610
|
+
this.oneByOneChain = Promise.resolve();
|
|
611
|
+
this.oneByOneChain = Promise.resolve();
|
|
612
|
+
}
|
|
613
|
+
LocalDbTransaction.prototype.inTransaction = function (callback) {
|
|
614
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
615
|
+
var name;
|
|
616
|
+
return __generator(this, function (_a) {
|
|
617
|
+
name = this.localDbName;
|
|
618
|
+
this.oneByOneChain = this.oneByOneChain.then(function () {
|
|
619
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
620
|
+
function closeLdb(previousError) {
|
|
621
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
622
|
+
var errClose_1;
|
|
623
|
+
return __generator(this, function (_a) {
|
|
624
|
+
switch (_a.label) {
|
|
625
|
+
case 0:
|
|
626
|
+
_a.trys.push([0, 2, , 3]);
|
|
627
|
+
return [4 /*yield*/, ldb.close()];
|
|
628
|
+
case 1:
|
|
629
|
+
_a.sent();
|
|
630
|
+
if (previousError) {
|
|
631
|
+
// @ts-ignore
|
|
632
|
+
previousError.ldbWasClosed = true;
|
|
633
|
+
}
|
|
634
|
+
return [3 /*break*/, 3];
|
|
635
|
+
case 2:
|
|
636
|
+
errClose_1 = _a.sent();
|
|
637
|
+
if (previousError) {
|
|
638
|
+
// @ts-ignore
|
|
639
|
+
previousError.ldbWasNotClosedBecause = errClose_1;
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
throw errClose_1;
|
|
643
|
+
}
|
|
644
|
+
return [3 /*break*/, 3];
|
|
645
|
+
case 3: return [2 /*return*/];
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
var ldb, result, err_1;
|
|
651
|
+
return __generator(this, function (_a) {
|
|
652
|
+
switch (_a.label) {
|
|
653
|
+
case 0:
|
|
654
|
+
ldb = new LocalDb(name);
|
|
655
|
+
_a.label = 1;
|
|
656
|
+
case 1:
|
|
657
|
+
_a.trys.push([1, 3, , 5]);
|
|
658
|
+
return [4 /*yield*/, callback(ldb)];
|
|
659
|
+
case 2:
|
|
660
|
+
result = _a.sent();
|
|
661
|
+
return [3 /*break*/, 5];
|
|
662
|
+
case 3:
|
|
663
|
+
err_1 = _a.sent();
|
|
664
|
+
return [4 /*yield*/, closeLdb(err_1)];
|
|
665
|
+
case 4:
|
|
666
|
+
_a.sent();
|
|
667
|
+
throw err_1;
|
|
668
|
+
case 5:
|
|
669
|
+
//await closeLdb();
|
|
670
|
+
return [2 /*return*/, result];
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
});
|
|
675
|
+
return [2 /*return*/, this.oneByOneChain];
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
};
|
|
679
|
+
LocalDbTransaction.prototype.getBindedInTransaction = function () {
|
|
680
|
+
var this4bind = this;
|
|
681
|
+
return function (ldb) {
|
|
682
|
+
return this4bind.inTransaction(ldb);
|
|
683
|
+
};
|
|
684
|
+
};
|
|
685
|
+
return LocalDbTransaction;
|
|
686
|
+
}());
|
|
687
|
+
exports.LocalDbTransaction = LocalDbTransaction;
|
|
688
|
+
});
|
|
689
|
+
//# sourceMappingURL=my-localdb.js.map
|