@ztimson/utils 0.26.20 → 0.26.22

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/index.cjs CHANGED
@@ -1037,20 +1037,36 @@ ${opts.message || this.desc}`;
1037
1037
  this.database = database;
1038
1038
  this.version = version;
1039
1039
  this.connection = new Promise((resolve, reject) => {
1040
- const req = indexedDB.open(this.database, this.version);
1040
+ let req;
1041
+ try {
1042
+ req = indexedDB.open(this.database, this.version);
1043
+ } catch (err) {
1044
+ return reject(err);
1045
+ }
1041
1046
  this.tables = !tables ? [] : tables.map((t) => {
1042
1047
  t = typeof t == "object" ? t : { name: t };
1043
1048
  return { ...t, name: t.name.toString() };
1044
1049
  });
1045
1050
  req.onerror = () => reject(req.error);
1046
1051
  req.onsuccess = () => {
1047
- const db = req.result;
1052
+ let db;
1053
+ try {
1054
+ db = req.result;
1055
+ } catch (err) {
1056
+ return reject(err);
1057
+ }
1048
1058
  const existing = Array.from(db.objectStoreNames);
1049
- if (!tables) this.tables = existing.map((t) => {
1050
- const tx = db.transaction(t, "readonly");
1051
- const store = tx.objectStore(t);
1052
- return { name: t, key: store.keyPath };
1053
- });
1059
+ if (!tables) {
1060
+ this.tables = existing.map((t) => {
1061
+ try {
1062
+ const tx = db.transaction(t, "readonly");
1063
+ const store = tx.objectStore(t);
1064
+ return { name: t, key: store.keyPath };
1065
+ } catch {
1066
+ return { name: t };
1067
+ }
1068
+ });
1069
+ }
1054
1070
  const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
1055
1071
  if (tables && desired.symmetricDifference(new ASet(existing)).length) {
1056
1072
  db.close();
@@ -1064,18 +1080,26 @@ ${opts.message || this.desc}`;
1064
1080
  };
1065
1081
  req.onupgradeneeded = () => {
1066
1082
  this.upgrading = true;
1067
- const db = req.result;
1068
- const existingTables = new ASet(Array.from(db.objectStoreNames));
1069
- if (tables) {
1070
- const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
1071
- existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
1072
- desired.difference(existingTables).forEach((name) => {
1073
- const t = this.tables.find(findByProp("name", name));
1074
- db.createObjectStore(name, {
1075
- keyPath: t == null ? void 0 : t.key,
1076
- autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
1083
+ let db;
1084
+ try {
1085
+ db = req.result;
1086
+ } catch {
1087
+ return;
1088
+ }
1089
+ try {
1090
+ const existingTables = new ASet(Array.from(db.objectStoreNames));
1091
+ if (tables) {
1092
+ const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
1093
+ existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
1094
+ desired.difference(existingTables).forEach((name) => {
1095
+ const t = this.tables.find(findByProp("name", name));
1096
+ db.createObjectStore(name, {
1097
+ keyPath: t == null ? void 0 : t.key,
1098
+ autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
1099
+ });
1077
1100
  });
1078
- });
1101
+ }
1102
+ } catch {
1079
1103
  }
1080
1104
  };
1081
1105
  });
@@ -1090,8 +1114,9 @@ ${opts.message || this.desc}`;
1090
1114
  if (!this.includes(table.name)) {
1091
1115
  const newDb = new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1);
1092
1116
  conn.close();
1093
- Object.assign(this, newDb);
1117
+ this.connection = newDb.connection;
1094
1118
  await this.connection;
1119
+ Object.assign(this, newDb);
1095
1120
  }
1096
1121
  return this.table(table.name);
1097
1122
  });
@@ -1103,8 +1128,9 @@ ${opts.message || this.desc}`;
1103
1128
  const conn = await this.connection;
1104
1129
  const newDb = new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1);
1105
1130
  conn.close();
1106
- Object.assign(this, newDb);
1131
+ this.connection = newDb.connection;
1107
1132
  await this.connection;
1133
+ Object.assign(this, newDb);
1108
1134
  });
1109
1135
  }
1110
1136
  includes(name) {
@@ -1131,9 +1157,8 @@ ${opts.message || this.desc}`;
1131
1157
  await this.database.waitForUpgrade();
1132
1158
  const db = await this.database.connection;
1133
1159
  const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
1134
- const store = tx.objectStore(table);
1135
1160
  return new Promise((resolve, reject) => {
1136
- const request = fn2(store);
1161
+ const request = fn2(tx.objectStore(table));
1137
1162
  request.onsuccess = () => resolve(request.result);
1138
1163
  request.onerror = () => reject(request.error);
1139
1164
  });