@bbn/bbn 2.0.235 → 2.0.237

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/db.js CHANGED
@@ -65,6 +65,10 @@ const fieldsFromFilter = (filter, fields = []) => {
65
65
  return fields;
66
66
  };
67
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
+ }
68
72
  const cols = structure.keys.PRIMARY.columns;
69
73
  return cols.length > 1 ? cols : cols[0];
70
74
  };
@@ -90,6 +94,7 @@ class DbObject {
90
94
  return __awaiter(this, arguments, void 0, function* (database = '') {
91
95
  let conn = db._connections[database || this.dbName];
92
96
  if (!conn) {
97
+ debugger;
93
98
  yield db.open(database || this.dbName);
94
99
  if (!db._connections[database || this.dbName]) {
95
100
  throw new Error(_('The database %s is not open', this.dbName));
@@ -118,13 +123,21 @@ class DbObject {
118
123
  const tx = connection.transaction([table], mode);
119
124
  tx.onabort = () => {
120
125
  this.lastErr = tx.error;
121
- log(tx.error);
126
+ log("Abort in store transaction: " + table, tx.error);
122
127
  };
123
128
  tx.onerror = () => {
124
129
  this.lastErr = tx.error;
125
- log(tx.error);
130
+ log("Error in store transaction: " + table, tx.error);
126
131
  };
127
- return [tx, tx.objectStore(table)];
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
+ }
128
141
  });
129
142
  }
130
143
  lastError() {
@@ -362,15 +375,31 @@ const db = {
362
375
  },
363
376
  importStructure(database, structure) {
364
377
  return __awaiter(this, void 0, void 0, function* () {
378
+ var _a, _b, _c;
365
379
  if (!this._structures[database]) {
366
380
  this._structures[database] = {};
367
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;
368
388
  for (const storeName in structure) {
369
- if (!this._structures[database].hasOwnProperty(storeName)) {
370
- this._structures[database][storeName] = structure[storeName];
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));
371
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);
372
402
  }
373
- yield this.reopenWithUpgrade(database);
374
403
  this.close(database);
375
404
  });
376
405
  },
@@ -433,6 +462,7 @@ const db = {
433
462
  const req = version ? idb.open(database, version) : idb.open(database);
434
463
  req.onupgradeneeded = () => {
435
464
  const db = req.result;
465
+ this._connections[database] = req.result;
436
466
  const dbStructure = this._structures[database] || {};
437
467
  iterate(dbStructure, (structure, storeName) => {
438
468
  if (!db.objectStoreNames.contains(storeName)) {
@@ -457,6 +487,7 @@ const db = {
457
487
  return __awaiter(this, void 0, void 0, function* () {
458
488
  var _a;
459
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);
460
491
  throw new Error(_('The database structure for %s is not valid (missing keys, fields, or primary key)', name));
461
492
  }
462
493
  if (!this._structures[database]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.235",
3
+ "version": "2.0.237",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",