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