@bbn/bbn 2.0.237 → 2.0.238
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/$.js +1 -1
- package/dist/bbn.js +3 -3
- package/dist/bbn.js.map +1 -1
- package/dist/bbn.sw.js +3 -3
- package/dist/bbn.sw.js.map +1 -1
- package/dist/com.js +13 -21
- package/dist/{vars.d.ts → data.d.ts} +1 -0
- package/dist/{vars.js → data.js} +1 -0
- package/dist/db/classes/Object.d.ts +3 -0
- package/dist/db/classes/Object.js +458 -0
- package/dist/db/classes/ObjectProxy.d.ts +18 -0
- package/dist/db/classes/ObjectProxy.js +77 -0
- package/dist/db/classes/Proxy.d.ts +17 -0
- package/dist/db/classes/Proxy.js +66 -0
- package/dist/db/functions/fieldsFromFilter.d.ts +1 -0
- package/dist/db/functions/fieldsFromFilter.js +33 -0
- package/dist/db/functions/getPrimaryKey.d.ts +2 -0
- package/dist/db/functions/getPrimaryKey.js +8 -0
- package/dist/db/functions/requestToPromise.d.ts +1 -0
- package/dist/db/functions/requestToPromise.js +6 -0
- package/dist/db/functions/transactionDone.d.ts +1 -0
- package/dist/db/functions/transactionDone.js +7 -0
- package/dist/db/functions/transformResult.d.ts +1 -0
- package/dist/db/functions/transformResult.js +16 -0
- package/dist/db/types.d.ts +54 -0
- package/dist/db/types.js +1 -0
- package/dist/db.d.ts +1 -65
- package/dist/db.js +1 -557
- package/dist/dt/classes/{date.d.ts → Date.d.ts} +1 -1
- package/dist/dt/classes/{date.js → Date.js} +8 -3
- package/dist/dt/classes/{dateTime.d.ts → DateTime.d.ts} +1 -1
- package/dist/dt/classes/{dateTime.js → DateTime.js} +9 -3
- package/dist/dt/classes/{dt.d.ts → Dt.d.ts} +1 -1
- package/dist/dt/classes/{dt.js → Dt.js} +36 -48
- package/dist/dt/classes/Duration.js +193 -0
- package/dist/dt/classes/{monthDay.d.ts → MonthDay.d.ts} +1 -1
- package/dist/dt/classes/{monthDay.js → MonthDay.js} +2 -2
- package/dist/dt/classes/{time.d.ts → Time.d.ts} +1 -1
- package/dist/dt/classes/{time.js → Time.js} +2 -2
- package/dist/dt/classes/{yearMonth.d.ts → YearMonth.d.ts} +1 -1
- package/dist/dt/classes/{yearMonth.js → YearMonth.js} +2 -2
- package/dist/dt/classes/{zoned.d.ts → Zoned.d.ts} +1 -1
- package/dist/dt/classes/{zoned.js → Zoned.js} +9 -3
- package/dist/dt/functions/buildLocaleFromIntl.js +1 -1
- package/dist/dt/functions/guessFormat.js +1 -1
- package/dist/dt/functions/intl-weekinfo-polyfill.js +11 -4
- package/dist/dt/functions/parse.js +20 -22
- package/dist/dt/functions/setupIntl.js +33 -44
- package/dist/dt.d.ts +1 -1
- package/dist/dt.js +9 -7
- package/dist/env.d.ts +1 -1
- package/dist/env.js +6 -7
- package/dist/fn/ajax/abort.js +1 -1
- package/dist/fn/ajax/ajax.d.ts +1 -1
- package/dist/fn/ajax/ajax.js +2 -2
- package/dist/fn/ajax/download.js +1 -1
- package/dist/fn/ajax/getFileContent.js +2 -3
- package/dist/fn/ajax/link.js +2 -2
- package/dist/fn/ajax/stream.js +2 -2
- package/dist/fn/browser/executeSlowly.js +11 -22
- package/dist/fn/browser/log.js +2 -3
- package/dist/fn/browser/yieldToBrowser.js +7 -19
- package/dist/fn/html/makeReactive.js +2 -2
- package/dist/fn/init.d.ts +1 -1
- package/dist/fn/init.js +4 -5
- package/dist/fn/object/_filter.js +1 -1
- package/dist/fn/phone/fphone.js +1 -2
- package/dist/fn/string/cast.js +1 -2
- package/dist/fn/string/shorten.js +1 -1
- package/dist/fn/string/treatForHash.js +1 -1
- package/dist/fn/style/addColors.d.ts +1 -1
- package/dist/fn/style/addColors.js +4 -4
- package/dist/fn/type/checkType.js +1 -2
- package/dist/fn/type/isCp.js +1 -2
- package/dist/fn/type/isHostname.js +1 -1
- package/dist/fn/type/isIP.js +1 -1
- package/dist/fn/type/isSame.js +2 -2
- package/dist/fn/type/isURL.js +1 -1
- package/dist/index-no-dep.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/dist/dt/classes/duration.js +0 -199
- /package/dist/dt/classes/{duration.d.ts → Duration.d.ts} +0 -0
package/dist/db.js
CHANGED
|
@@ -1,558 +1,2 @@
|
|
|
1
|
-
|
|
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
|
-
import _ from './_.js';
|
|
11
|
-
import iterate from './fn/loop/iterate.js';
|
|
12
|
-
import log from './fn/browser/log.js';
|
|
13
|
-
import isObject from './fn/type/isObject.js';
|
|
14
|
-
import isArray from './fn/type/isArray.js';
|
|
15
|
-
import extend from './fn/object/extend.js';
|
|
16
|
-
const idb = globalThis.indexedDB ||
|
|
17
|
-
globalThis.webkitIndexedDB ||
|
|
18
|
-
globalThis.mozIndexedDB ||
|
|
19
|
-
globalThis.OIndexedDB ||
|
|
20
|
-
globalThis.msIndexedDB;
|
|
21
|
-
const transformResult = (obj, fields) => {
|
|
22
|
-
if (!obj) {
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
if (fields === null || fields === void 0 ? void 0 : fields.length) {
|
|
26
|
-
const res = {};
|
|
27
|
-
iterate(obj, (v, n) => {
|
|
28
|
-
if (fields.includes(n)) {
|
|
29
|
-
res[n] = v;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
return res;
|
|
33
|
-
}
|
|
34
|
-
return obj;
|
|
35
|
-
};
|
|
36
|
-
const fieldsFromFilter = (filter, fields = []) => {
|
|
37
|
-
var _a;
|
|
38
|
-
if ((_a = filter === null || filter === void 0 ? void 0 : filter.conditions) === null || _a === void 0 ? void 0 : _a.length) {
|
|
39
|
-
filter.conditions.forEach((cond) => {
|
|
40
|
-
if (cond.field && !fields.includes(cond.field)) {
|
|
41
|
-
fields.push(cond.field);
|
|
42
|
-
}
|
|
43
|
-
else if (cond.conditions) {
|
|
44
|
-
fieldsFromFilter(cond, fields);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
else if (isObject(filter)) {
|
|
49
|
-
iterate(filter, (_v, n) => {
|
|
50
|
-
if (!fields.includes(n)) {
|
|
51
|
-
fields.push(n);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
else if (isArray(filter)) {
|
|
56
|
-
filter.forEach((cond) => {
|
|
57
|
-
if (cond.field && !fields.includes(cond.field)) {
|
|
58
|
-
fields.push(cond.field);
|
|
59
|
-
}
|
|
60
|
-
else if (cond.conditions) {
|
|
61
|
-
fieldsFromFilter(cond, fields);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
return fields;
|
|
66
|
-
};
|
|
67
|
-
const getPrimaryKey = (structure) => {
|
|
68
|
-
var _a, _b;
|
|
69
|
-
if (!((_b = (_a = structure.keys) === null || _a === void 0 ? void 0 : _a.PRIMARY) === null || _b === void 0 ? void 0 : _b.columns)) {
|
|
70
|
-
throw new Error(_('No primary key defined in the structure'));
|
|
71
|
-
}
|
|
72
|
-
const cols = structure.keys.PRIMARY.columns;
|
|
73
|
-
return cols.length > 1 ? cols : cols[0];
|
|
74
|
-
};
|
|
75
|
-
const requestToPromise = (req) => new Promise((resolve, reject) => {
|
|
76
|
-
req.onsuccess = () => resolve(req.result);
|
|
77
|
-
req.onerror = () => reject(req.error);
|
|
78
|
-
});
|
|
79
|
-
const transactionDone = (tx) => new Promise((resolve, reject) => {
|
|
80
|
-
tx.oncomplete = () => resolve();
|
|
81
|
-
tx.onabort = () => reject(tx.error);
|
|
82
|
-
tx.onerror = () => reject(tx.error);
|
|
83
|
-
});
|
|
84
|
-
class DbObject {
|
|
85
|
-
constructor(dbName) {
|
|
86
|
-
this.lastErr = null;
|
|
87
|
-
this.dbName = dbName;
|
|
88
|
-
}
|
|
89
|
-
hasMissingStores(database, conn) {
|
|
90
|
-
const structures = db._structures[database] || {};
|
|
91
|
-
return Object.keys(structures).some(storeName => !conn.objectStoreNames.contains(storeName));
|
|
92
|
-
}
|
|
93
|
-
getConnection() {
|
|
94
|
-
return __awaiter(this, arguments, void 0, function* (database = '') {
|
|
95
|
-
let conn = db._connections[database || this.dbName];
|
|
96
|
-
if (!conn) {
|
|
97
|
-
debugger;
|
|
98
|
-
yield db.open(database || this.dbName);
|
|
99
|
-
if (!db._connections[database || this.dbName]) {
|
|
100
|
-
throw new Error(_('The database %s is not open', this.dbName));
|
|
101
|
-
}
|
|
102
|
-
conn = db._connections[database || this.dbName];
|
|
103
|
-
}
|
|
104
|
-
return conn;
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
get structure() {
|
|
108
|
-
const structure = db._structures[this.dbName];
|
|
109
|
-
if (!structure) {
|
|
110
|
-
throw new Error(_('No structure defined for database %s', this.dbName));
|
|
111
|
-
}
|
|
112
|
-
return structure;
|
|
113
|
-
}
|
|
114
|
-
getStore(table, mode) {
|
|
115
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
const connection = yield this.getConnection();
|
|
117
|
-
if (!this.structure[table]) {
|
|
118
|
-
throw new Error(_('Table %s is not defined in database structure %s', table, this.dbName));
|
|
119
|
-
}
|
|
120
|
-
if (!connection.objectStoreNames.contains(table)) {
|
|
121
|
-
throw new Error(_('Table %s does not exist in database %s. Schema upgrade required.', table, this.dbName));
|
|
122
|
-
}
|
|
123
|
-
const tx = connection.transaction([table], mode);
|
|
124
|
-
tx.onabort = () => {
|
|
125
|
-
this.lastErr = tx.error;
|
|
126
|
-
log("Abort in store transaction: " + table, tx.error);
|
|
127
|
-
};
|
|
128
|
-
tx.onerror = () => {
|
|
129
|
-
this.lastErr = tx.error;
|
|
130
|
-
log("Error in store transaction: " + table, tx.error);
|
|
131
|
-
};
|
|
132
|
-
try {
|
|
133
|
-
const os = tx.objectStore(table);
|
|
134
|
-
return [tx, os];
|
|
135
|
-
}
|
|
136
|
-
catch (e) {
|
|
137
|
-
this.lastErr = e;
|
|
138
|
-
log("Error getting object store: " + table, e);
|
|
139
|
-
throw new Error(_('Error getting table %s from database %s', table, this.dbName));
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
lastError() {
|
|
144
|
-
return this.lastErr;
|
|
145
|
-
}
|
|
146
|
-
close() {
|
|
147
|
-
const conn = db._connections[this.dbName];
|
|
148
|
-
if (conn) {
|
|
149
|
-
conn.close();
|
|
150
|
-
delete db._connections[this.dbName];
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
insert(table, data) {
|
|
154
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
-
const rows = Array.isArray(data) ? data : [data];
|
|
156
|
-
if (!rows.length) {
|
|
157
|
-
return 0;
|
|
158
|
-
}
|
|
159
|
-
const [tx, store] = yield this.getStore(table, 'readwrite');
|
|
160
|
-
let inserted = 0;
|
|
161
|
-
for (const row of rows) {
|
|
162
|
-
const req = store.put(row);
|
|
163
|
-
req.onsuccess = () => {
|
|
164
|
-
inserted++;
|
|
165
|
-
};
|
|
166
|
-
req.onerror = () => {
|
|
167
|
-
this.lastErr = req.error;
|
|
168
|
-
log(req.error);
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
yield transactionDone(tx);
|
|
172
|
-
return inserted;
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
update(table_1, data_1, where_1) {
|
|
176
|
-
return __awaiter(this, arguments, void 0, function* (table, data, where, replace = false) {
|
|
177
|
-
const rows = yield this.selectAll(table, [], where);
|
|
178
|
-
if (!rows.length) {
|
|
179
|
-
return 0;
|
|
180
|
-
}
|
|
181
|
-
const structure = this.structure[table];
|
|
182
|
-
const primary = getPrimaryKey(structure);
|
|
183
|
-
if (Array.isArray(primary)) {
|
|
184
|
-
throw new Error(_('Composite primary keys are not supported by this update implementation'));
|
|
185
|
-
}
|
|
186
|
-
const [tx, store] = yield this.getStore(table, 'readwrite');
|
|
187
|
-
let updated = 0;
|
|
188
|
-
for (const row of rows) {
|
|
189
|
-
const nextRow = extend({}, replace ? { [primary]: row[primary] } : row, data);
|
|
190
|
-
if (!(primary in nextRow)) {
|
|
191
|
-
throw new Error(_('No primary key in the data'));
|
|
192
|
-
}
|
|
193
|
-
const req = store.put(nextRow);
|
|
194
|
-
req.onsuccess = () => {
|
|
195
|
-
updated++;
|
|
196
|
-
};
|
|
197
|
-
req.onerror = () => {
|
|
198
|
-
this.lastErr = req.error;
|
|
199
|
-
log(req.error);
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
yield transactionDone(tx);
|
|
203
|
-
return updated;
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
delete(table, where) {
|
|
207
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
const structure = this.structure[table];
|
|
209
|
-
const primary = getPrimaryKey(structure);
|
|
210
|
-
if (Array.isArray(primary)) {
|
|
211
|
-
throw new Error(_('Composite primary keys are not supported by this delete implementation'));
|
|
212
|
-
}
|
|
213
|
-
if (!(primary in where)) {
|
|
214
|
-
throw new Error(_('No primary key in the filter'));
|
|
215
|
-
}
|
|
216
|
-
const [tx, store] = yield this.getStore(table, 'readwrite');
|
|
217
|
-
store.delete(where[primary]);
|
|
218
|
-
yield transactionDone(tx);
|
|
219
|
-
return 1;
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
selectOne(table_1, field_1) {
|
|
223
|
-
return __awaiter(this, arguments, void 0, function* (table, field, where = null, order = null, start = 0, limit = 1) {
|
|
224
|
-
var _a;
|
|
225
|
-
const rows = yield this.selectAll(table, [field], where, order, start, limit);
|
|
226
|
-
return (_a = rows === null || rows === void 0 ? void 0 : rows[0]) === null || _a === void 0 ? void 0 : _a[field];
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
select(table_1) {
|
|
230
|
-
return __awaiter(this, arguments, void 0, function* (table, fields = [], where = null, order = null, start = 0) {
|
|
231
|
-
const rows = yield this.selectAll(table, fields, where, order, start, 1);
|
|
232
|
-
return rows.length ? rows[0] : null;
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
selectAll(table_1) {
|
|
236
|
-
return __awaiter(this, arguments, void 0, function* (table, fields = [], where = null, order = null, start = 0, limit = null) {
|
|
237
|
-
void order;
|
|
238
|
-
const [tx, store] = yield this.getStore(table, 'readonly');
|
|
239
|
-
const structure = this.structure[table];
|
|
240
|
-
const primary = getPrimaryKey(structure);
|
|
241
|
-
const results = [];
|
|
242
|
-
const searchField = isObject(where)
|
|
243
|
-
? Object.keys(where)[0]
|
|
244
|
-
: (!where || isArray(where) ? null : primary);
|
|
245
|
-
if (!Array.isArray(primary) && searchField === primary) {
|
|
246
|
-
if (Array.isArray(where === null || where === void 0 ? void 0 : where[primary])) {
|
|
247
|
-
const ids = where[primary];
|
|
248
|
-
const max = Math.min(ids.length - start, limit !== null && limit !== void 0 ? limit : ids.length);
|
|
249
|
-
const slice = ids.slice(start, start + max);
|
|
250
|
-
for (const id of slice) {
|
|
251
|
-
const row = yield requestToPromise(store.get(id));
|
|
252
|
-
const transformed = transformResult(row, fields);
|
|
253
|
-
if (transformed) {
|
|
254
|
-
results.push(transformed);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
yield transactionDone(tx);
|
|
258
|
-
return results;
|
|
259
|
-
}
|
|
260
|
-
const key = isObject(where)
|
|
261
|
-
? where[primary]
|
|
262
|
-
: where;
|
|
263
|
-
const row = yield requestToPromise(store.get(key));
|
|
264
|
-
const transformed = transformResult(row, fields);
|
|
265
|
-
if (transformed) {
|
|
266
|
-
results.push(transformed);
|
|
267
|
-
}
|
|
268
|
-
yield transactionDone(tx);
|
|
269
|
-
return results;
|
|
270
|
-
}
|
|
271
|
-
yield new Promise((resolve, reject) => {
|
|
272
|
-
const req = store.openCursor();
|
|
273
|
-
let i = 0;
|
|
274
|
-
req.onsuccess = (e) => {
|
|
275
|
-
var _a, _b;
|
|
276
|
-
const cursor = e.target.result;
|
|
277
|
-
if (!cursor) {
|
|
278
|
-
resolve();
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
const matches = !where || !((_b = (_a = globalThis.bbn) === null || _a === void 0 ? void 0 : _a.fn) === null || _b === void 0 ? void 0 : _b.search)
|
|
282
|
-
? true
|
|
283
|
-
: 0 === globalThis.bbn.fn.search([cursor.value], where);
|
|
284
|
-
if (matches) {
|
|
285
|
-
if (i >= start) {
|
|
286
|
-
const transformed = transformResult(cursor.value, fields);
|
|
287
|
-
if (transformed) {
|
|
288
|
-
results.push(transformed);
|
|
289
|
-
}
|
|
290
|
-
if (limit !== null && results.length >= limit) {
|
|
291
|
-
resolve();
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
i++;
|
|
296
|
-
}
|
|
297
|
-
cursor.continue();
|
|
298
|
-
};
|
|
299
|
-
req.onerror = () => {
|
|
300
|
-
this.lastErr = req.error;
|
|
301
|
-
log(req.error);
|
|
302
|
-
reject(req.error);
|
|
303
|
-
};
|
|
304
|
-
});
|
|
305
|
-
yield transactionDone(tx);
|
|
306
|
-
return results;
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
getColumnValues(table_1, field_1) {
|
|
310
|
-
return __awaiter(this, arguments, void 0, function* (table, field, where = null, order = null, start = 0, limit = null) {
|
|
311
|
-
const rows = yield this.selectAll(table, fieldsFromFilter(where, [field]), where, order, start, limit);
|
|
312
|
-
return rows
|
|
313
|
-
.map(row => row[field])
|
|
314
|
-
.filter(v => v !== undefined);
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
copyTable(target_1, table_1) {
|
|
318
|
-
return __awaiter(this, arguments, void 0, function* (target, table, fields = [], where = null, order = null, start = 0, limit = null) {
|
|
319
|
-
if (!this.structure[table]) {
|
|
320
|
-
throw new Error(_('Source table %s does not exist in structure', table));
|
|
321
|
-
}
|
|
322
|
-
let connection = yield this.getConnection();
|
|
323
|
-
if (!connection.objectStoreNames.contains(target)) {
|
|
324
|
-
connection.close();
|
|
325
|
-
yield db.add(this.dbName, target, this.structure[table]);
|
|
326
|
-
connection = yield this.getConnection();
|
|
327
|
-
}
|
|
328
|
-
if (!connection.objectStoreNames.contains(target)) {
|
|
329
|
-
throw new Error(_('The target table %s does not exist', target));
|
|
330
|
-
}
|
|
331
|
-
const rows = yield this.selectAll(table, fields, where, order, start, limit);
|
|
332
|
-
if (!rows.length) {
|
|
333
|
-
return 0;
|
|
334
|
-
}
|
|
335
|
-
const res = yield this.insert(target, rows);
|
|
336
|
-
connection.close();
|
|
337
|
-
return res;
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
deleteTable(table) {
|
|
341
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
342
|
-
yield db.remove(this.dbName, table);
|
|
343
|
-
return true;
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
const db = {
|
|
348
|
-
_structures: {},
|
|
349
|
-
_connections: {},
|
|
350
|
-
_objects: {},
|
|
351
|
-
_stores: {},
|
|
352
|
-
ok: idb !== undefined,
|
|
353
|
-
close(database) {
|
|
354
|
-
const conn = this._connections[database];
|
|
355
|
-
if (conn) {
|
|
356
|
-
conn.close();
|
|
357
|
-
delete this._connections[database];
|
|
358
|
-
}
|
|
359
|
-
if (this._objects[database]) {
|
|
360
|
-
delete this._objects[database];
|
|
361
|
-
}
|
|
362
|
-
},
|
|
363
|
-
updateStructure(storeName, structure, database) {
|
|
364
|
-
const primary = getPrimaryKey(structure);
|
|
365
|
-
if (!database.objectStoreNames.contains(storeName)) {
|
|
366
|
-
const store = database.createObjectStore(storeName, {
|
|
367
|
-
keyPath: primary
|
|
368
|
-
});
|
|
369
|
-
iterate(structure.keys, (a, n) => {
|
|
370
|
-
if (n !== 'PRIMARY') {
|
|
371
|
-
store.createIndex(n, a.columns.length > 1 ? a.columns : a.columns[0], { unique: !!a.unique });
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
},
|
|
376
|
-
importStructure(database, structure) {
|
|
377
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
378
|
-
var _a, _b, _c;
|
|
379
|
-
if (!this._structures[database]) {
|
|
380
|
-
this._structures[database] = {};
|
|
381
|
-
}
|
|
382
|
-
if (this._connections[database]) {
|
|
383
|
-
throw new Error(_('Database %s is already open. Close it before adding a new store.', database));
|
|
384
|
-
}
|
|
385
|
-
yield this.open(database);
|
|
386
|
-
let conn = this._connections[database];
|
|
387
|
-
let upgrade = false;
|
|
388
|
-
for (const storeName in structure) {
|
|
389
|
-
if (!((_b = (_a = structure[storeName]) === null || _a === void 0 ? void 0 : _a.keys) === null || _b === void 0 ? void 0 : _b.PRIMARY) || !((_c = structure[storeName]) === null || _c === void 0 ? void 0 : _c.fields)) {
|
|
390
|
-
log("Error on importStructure with store " + storeName, structure[storeName]);
|
|
391
|
-
throw new Error(_('The database structure for %s is not valid (missing keys, fields, or primary key)', storeName));
|
|
392
|
-
}
|
|
393
|
-
this._structures[database][storeName] = structure[storeName];
|
|
394
|
-
// DB exists already: open temporarily and check if store exists
|
|
395
|
-
const hasStore = conn.objectStoreNames.contains(storeName);
|
|
396
|
-
if (!hasStore) {
|
|
397
|
-
upgrade = true;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
if (upgrade) {
|
|
401
|
-
yield this.reopenWithUpgrade(database);
|
|
402
|
-
}
|
|
403
|
-
this.close(database);
|
|
404
|
-
});
|
|
405
|
-
},
|
|
406
|
-
getExistingVersion(name) {
|
|
407
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
408
|
-
const live = this._connections[name];
|
|
409
|
-
if (live) {
|
|
410
|
-
return live.version;
|
|
411
|
-
}
|
|
412
|
-
return new Promise((resolve, reject) => {
|
|
413
|
-
if (!idb) {
|
|
414
|
-
reject(new Error(_('IndexedDB is not available')));
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
const req = idb.open(name);
|
|
418
|
-
req.onsuccess = () => {
|
|
419
|
-
const database = req.result;
|
|
420
|
-
const version = database.version;
|
|
421
|
-
database.close();
|
|
422
|
-
resolve(version);
|
|
423
|
-
};
|
|
424
|
-
req.onupgradeneeded = () => {
|
|
425
|
-
// Database did not exist before; this open created it temporarily.
|
|
426
|
-
// Version is therefore effectively 1.
|
|
427
|
-
const database = req.result;
|
|
428
|
-
const version = database.version;
|
|
429
|
-
database.close();
|
|
430
|
-
resolve(version);
|
|
431
|
-
};
|
|
432
|
-
req.onerror = () => reject(req.error);
|
|
433
|
-
});
|
|
434
|
-
});
|
|
435
|
-
},
|
|
436
|
-
reopenWithUpgrade(name) {
|
|
437
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
438
|
-
if (!this._structures[name]) {
|
|
439
|
-
throw new Error(_('Impossible to find a structure for the database %s', name));
|
|
440
|
-
}
|
|
441
|
-
const existingVersion = yield this.getExistingVersion(name);
|
|
442
|
-
const nextVersion = existingVersion + 1;
|
|
443
|
-
log(_('Going from version %s to version %s for database %s', existingVersion, nextVersion, name));
|
|
444
|
-
if (this._connections[name]) {
|
|
445
|
-
this.close(name);
|
|
446
|
-
}
|
|
447
|
-
return yield this.open(name, nextVersion);
|
|
448
|
-
});
|
|
449
|
-
},
|
|
450
|
-
open(database, version) {
|
|
451
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
452
|
-
if (!idb) {
|
|
453
|
-
throw new Error(_('IndexedDB is not available'));
|
|
454
|
-
}
|
|
455
|
-
if (!this._structures[database]) {
|
|
456
|
-
throw new Error(_('Impossible to find a structure for the database %s', database));
|
|
457
|
-
}
|
|
458
|
-
if (this._objects[database] && this._connections[database]) {
|
|
459
|
-
return this._objects[database];
|
|
460
|
-
}
|
|
461
|
-
yield new Promise((resolve, reject) => {
|
|
462
|
-
const req = version ? idb.open(database, version) : idb.open(database);
|
|
463
|
-
req.onupgradeneeded = () => {
|
|
464
|
-
const db = req.result;
|
|
465
|
-
this._connections[database] = req.result;
|
|
466
|
-
const dbStructure = this._structures[database] || {};
|
|
467
|
-
iterate(dbStructure, (structure, storeName) => {
|
|
468
|
-
if (!db.objectStoreNames.contains(storeName)) {
|
|
469
|
-
this.updateStructure(storeName, structure, db);
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
};
|
|
473
|
-
req.onsuccess = () => {
|
|
474
|
-
this._connections[database] = req.result;
|
|
475
|
-
resolve(req.result);
|
|
476
|
-
};
|
|
477
|
-
req.onerror = () => reject(req.error);
|
|
478
|
-
req.onblocked = (event) => {
|
|
479
|
-
reject(_('open: Upgrade blocked for database %s. Please close other connections to proceed.', database));
|
|
480
|
-
};
|
|
481
|
-
});
|
|
482
|
-
this._objects[database] = new DbObject(database);
|
|
483
|
-
return this._objects[database];
|
|
484
|
-
});
|
|
485
|
-
},
|
|
486
|
-
add(database, name, structure) {
|
|
487
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
488
|
-
var _a;
|
|
489
|
-
if (!((_a = structure === null || structure === void 0 ? void 0 : structure.keys) === null || _a === void 0 ? void 0 : _a.PRIMARY) || !(structure === null || structure === void 0 ? void 0 : structure.fields)) {
|
|
490
|
-
log("Error on add", structure);
|
|
491
|
-
throw new Error(_('The database structure for %s is not valid (missing keys, fields, or primary key)', name));
|
|
492
|
-
}
|
|
493
|
-
if (!this._structures[database]) {
|
|
494
|
-
this._structures[database] = {};
|
|
495
|
-
}
|
|
496
|
-
this._structures[database][name] = structure;
|
|
497
|
-
if (this._connections[database]) {
|
|
498
|
-
throw new Error(_('Database %s is already open. Close it before adding a new store.', database));
|
|
499
|
-
}
|
|
500
|
-
yield this.open(database);
|
|
501
|
-
let conn = this._connections[database];
|
|
502
|
-
// DB exists already: open temporarily and check if store exists
|
|
503
|
-
const hasStore = conn.objectStoreNames.contains(name);
|
|
504
|
-
if (!hasStore) {
|
|
505
|
-
const tmp = yield this.reopenWithUpgrade(database);
|
|
506
|
-
if (!this._connections[database]) {
|
|
507
|
-
throw new Error(_('The database %s is not open after upgrade', database));
|
|
508
|
-
}
|
|
509
|
-
conn = this._connections[database];
|
|
510
|
-
}
|
|
511
|
-
this.close(database);
|
|
512
|
-
return true;
|
|
513
|
-
});
|
|
514
|
-
},
|
|
515
|
-
remove(database, name) {
|
|
516
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
517
|
-
const currentStructure = this._structures[database];
|
|
518
|
-
if (!(currentStructure === null || currentStructure === void 0 ? void 0 : currentStructure[name])) {
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
const old = currentStructure[name];
|
|
522
|
-
delete currentStructure[name];
|
|
523
|
-
try {
|
|
524
|
-
const conn = this._connections[database];
|
|
525
|
-
if (!conn) {
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
const nextVersion = conn.version + 1;
|
|
529
|
-
this.close(database);
|
|
530
|
-
yield new Promise((resolve, reject) => {
|
|
531
|
-
if (!idb) {
|
|
532
|
-
reject(new Error(_('IndexedDB is not available')));
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
const req = idb.open(database, nextVersion);
|
|
536
|
-
req.onupgradeneeded = () => {
|
|
537
|
-
const dbInstance = req.result;
|
|
538
|
-
if (dbInstance.objectStoreNames.contains(name)) {
|
|
539
|
-
dbInstance.deleteObjectStore(name);
|
|
540
|
-
dbInstance.close();
|
|
541
|
-
}
|
|
542
|
-
};
|
|
543
|
-
req.onsuccess = () => {
|
|
544
|
-
req.result.close();
|
|
545
|
-
resolve();
|
|
546
|
-
};
|
|
547
|
-
req.onerror = () => reject(req.error);
|
|
548
|
-
req.onblocked = () => reject(new Error(_('Remove blocked for database %s', database)));
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
catch (e) {
|
|
552
|
-
currentStructure[name] = old;
|
|
553
|
-
throw e;
|
|
554
|
-
}
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
};
|
|
1
|
+
import db from './db/classes/Object.js';
|
|
558
2
|
export default db;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import bbnDt from './
|
|
2
|
+
import bbnDt from './Dt.js';
|
|
3
3
|
import getRow from '../../fn/object/getRow.js';
|
|
4
4
|
export default class bbnDtDate extends bbnDt {
|
|
5
|
+
kind = 'date';
|
|
5
6
|
constructor(y, m, d) {
|
|
6
7
|
let value;
|
|
7
8
|
if (!y) {
|
|
@@ -26,7 +27,6 @@ export default class bbnDtDate extends bbnDt {
|
|
|
26
27
|
value = new Temporal.PlainDate(y, m, d || 1);
|
|
27
28
|
}
|
|
28
29
|
super(value);
|
|
29
|
-
this.kind = 'date';
|
|
30
30
|
}
|
|
31
31
|
compareSameKind(other) {
|
|
32
32
|
const cmp = Temporal.PlainDate.compare(this.value, other.value);
|
|
@@ -48,7 +48,12 @@ export default class bbnDtDate extends bbnDt {
|
|
|
48
48
|
}
|
|
49
49
|
fdate(long = false, weekday = false) {
|
|
50
50
|
const date = new Date(this.year(), this.month() - 1, this.day());
|
|
51
|
-
const opt =
|
|
51
|
+
const opt = {
|
|
52
|
+
year: 'numeric',
|
|
53
|
+
month: long ? 'long' : 'numeric',
|
|
54
|
+
day: 'numeric',
|
|
55
|
+
...(weekday ? { weekday: (long ? 'long' : 'short') } : {}),
|
|
56
|
+
};
|
|
52
57
|
const d = new Intl.DateTimeFormat([bbn.env.lang, ...navigator.languages], opt);
|
|
53
58
|
return d.format(date);
|
|
54
59
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import bbnDt from './
|
|
2
|
+
import bbnDt from './Dt.js';
|
|
3
3
|
export default class bbnDtDateTime extends bbnDt<Temporal.PlainDateTime> {
|
|
4
4
|
readonly kind: bbnDtKind;
|
|
5
5
|
constructor(y?: any, m?: number, d?: number, h?: number, i?: number, s?: number, ms?: number);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import bbnDt from './
|
|
2
|
+
import bbnDt from './Dt.js';
|
|
3
3
|
import getRow from '../../fn/object/getRow.js';
|
|
4
4
|
export default class bbnDtDateTime extends bbnDt {
|
|
5
|
+
kind = 'dateTime';
|
|
5
6
|
constructor(y, m, d, h, i, s, ms) {
|
|
6
7
|
let value;
|
|
7
8
|
if (!y) {
|
|
@@ -25,7 +26,6 @@ export default class bbnDtDateTime extends bbnDt {
|
|
|
25
26
|
value = new Temporal.PlainDateTime(y, m, d || 1, h || 0, i || 0, s || 0, ms || 0);
|
|
26
27
|
}
|
|
27
28
|
super(value);
|
|
28
|
-
this.kind = 'dateTime';
|
|
29
29
|
}
|
|
30
30
|
format(format) {
|
|
31
31
|
// long
|
|
@@ -43,7 +43,13 @@ export default class bbnDtDateTime extends bbnDt {
|
|
|
43
43
|
return '';
|
|
44
44
|
}
|
|
45
45
|
const date = new Date(this.year(), this.month() - 1, this.day(), this.hour(), this.minute(), this.second());
|
|
46
|
-
const opt =
|
|
46
|
+
const opt = {
|
|
47
|
+
year: 'numeric',
|
|
48
|
+
month: long ? 'long' : '2-digit',
|
|
49
|
+
day: '2-digit',
|
|
50
|
+
...(weekday ? { weekday: (long ? 'long' : 'short') } : {}),
|
|
51
|
+
...(withTime ? { hour: (long ? '2-digit' : 'numeric'), minute: '2-digit' } : {})
|
|
52
|
+
};
|
|
47
53
|
const d = new Intl.DateTimeFormat([bbn.env.lang, ...navigator.languages], opt);
|
|
48
54
|
return d.format(date);
|
|
49
55
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Temporal } from 'temporal-polyfill';
|
|
2
2
|
import { bbnDtTemporal } from '../vars/types.js';
|
|
3
|
-
import bbnDtDuration from './
|
|
3
|
+
import bbnDtDuration from './Duration.js';
|
|
4
4
|
export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
|
|
5
5
|
#private;
|
|
6
6
|
abstract readonly kind: bbnDtKind;
|