@ztimson/utils 0.26.20 → 0.26.21
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 +45 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +45 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
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
|
-
|
|
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)
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
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
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
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,6 +1114,7 @@ ${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();
|
|
1117
|
+
await newDb.connection;
|
|
1093
1118
|
Object.assign(this, newDb);
|
|
1094
1119
|
await this.connection;
|
|
1095
1120
|
}
|
|
@@ -1103,6 +1128,7 @@ ${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();
|
|
1131
|
+
await newDb.connection;
|
|
1106
1132
|
Object.assign(this, newDb);
|
|
1107
1133
|
await this.connection;
|
|
1108
1134
|
});
|
|
@@ -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(
|
|
1161
|
+
const request = fn2(tx.objectStore(table));
|
|
1137
1162
|
request.onsuccess = () => resolve(request.result);
|
|
1138
1163
|
request.onerror = () => reject(request.error);
|
|
1139
1164
|
});
|