live-cache 0.2.3 → 0.2.4
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 +184 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +184 -53
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +184 -53
- package/dist/index.umd.js.map +1 -1
- package/dist/react/useController.d.ts +0 -2
- package/dist/storage-manager/IndexDbStorageManager.d.ts +10 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1085,35 +1085,79 @@ class Transactions {
|
|
|
1085
1085
|
*/
|
|
1086
1086
|
class IndexDbStorageManager extends StorageManager {
|
|
1087
1087
|
constructor(options = {}) {
|
|
1088
|
-
var _a, _b, _c, _d;
|
|
1088
|
+
var _a, _b, _c, _d, _e;
|
|
1089
1089
|
super((_a = options.prefix) !== null && _a !== void 0 ? _a : "live-cache:");
|
|
1090
1090
|
this.dbPromise = null;
|
|
1091
|
-
this.
|
|
1092
|
-
this.storeName = (
|
|
1093
|
-
this.prefix = (
|
|
1091
|
+
this.dbPromises = new Map();
|
|
1092
|
+
this.storeName = (_b = options.storeName) !== null && _b !== void 0 ? _b : "collections";
|
|
1093
|
+
this.prefix = (_c = options.prefix) !== null && _c !== void 0 ? _c : "live-cache:";
|
|
1094
|
+
this.useSameDatabase = (_d = options.useSameDatabase) !== null && _d !== void 0 ? _d : false;
|
|
1095
|
+
this.dbName = (_e = options.dbName) !== null && _e !== void 0 ? _e : "live-cache";
|
|
1094
1096
|
}
|
|
1095
1097
|
key(name) {
|
|
1096
1098
|
return `${this.prefix}${name}`;
|
|
1097
1099
|
}
|
|
1098
|
-
|
|
1099
|
-
if (this.
|
|
1100
|
+
getDbName(name) {
|
|
1101
|
+
if (this.useSameDatabase) {
|
|
1102
|
+
return this.dbName;
|
|
1103
|
+
}
|
|
1104
|
+
else {
|
|
1105
|
+
// Create a separate database for each collection
|
|
1106
|
+
const collectionName = name !== null && name !== void 0 ? name : this.storeName;
|
|
1107
|
+
return `${this.dbName}-${collectionName}`;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
openDb(name) {
|
|
1111
|
+
if (this.useSameDatabase) {
|
|
1112
|
+
if (this.dbPromise)
|
|
1113
|
+
return this.dbPromise;
|
|
1114
|
+
this.dbPromise = new Promise((resolve, reject) => {
|
|
1115
|
+
if (typeof indexedDB === "undefined") {
|
|
1116
|
+
reject(new Error("indexedDB is not available in this environment"));
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
const dbName = this.getDbName();
|
|
1120
|
+
const request = indexedDB.open(dbName, 1);
|
|
1121
|
+
request.onupgradeneeded = () => {
|
|
1122
|
+
const db = request.result;
|
|
1123
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
1124
|
+
db.createObjectStore(this.storeName);
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1127
|
+
request.onsuccess = () => resolve(request.result);
|
|
1128
|
+
request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error("Failed to open IndexedDB")); };
|
|
1129
|
+
});
|
|
1100
1130
|
return this.dbPromise;
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
// Use separate database per collection
|
|
1134
|
+
if (!name) {
|
|
1135
|
+
throw new Error("Collection name is required when useSameDatabase is false");
|
|
1105
1136
|
}
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1137
|
+
const existing = this.dbPromises.get(name);
|
|
1138
|
+
if (existing)
|
|
1139
|
+
return existing;
|
|
1140
|
+
const promise = new Promise((resolve, reject) => {
|
|
1141
|
+
if (typeof indexedDB === "undefined") {
|
|
1142
|
+
reject(new Error("indexedDB is not available in this environment"));
|
|
1143
|
+
return;
|
|
1111
1144
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1145
|
+
const dbName = this.getDbName(name);
|
|
1146
|
+
const request = indexedDB.open(dbName, 1);
|
|
1147
|
+
request.onupgradeneeded = () => {
|
|
1148
|
+
const db = request.result;
|
|
1149
|
+
// When using separate databases, use a single object store named "documents"
|
|
1150
|
+
const storeName = "documents";
|
|
1151
|
+
if (!db.objectStoreNames.contains(storeName)) {
|
|
1152
|
+
db.createObjectStore(storeName);
|
|
1153
|
+
}
|
|
1154
|
+
};
|
|
1155
|
+
request.onsuccess = () => resolve(request.result);
|
|
1156
|
+
request.onerror = () => { var _a; return reject((_a = request.error) !== null && _a !== void 0 ? _a : new Error("Failed to open IndexedDB")); };
|
|
1157
|
+
});
|
|
1158
|
+
this.dbPromises.set(name, promise);
|
|
1159
|
+
return promise;
|
|
1160
|
+
}
|
|
1117
1161
|
}
|
|
1118
1162
|
idbGet(key) {
|
|
1119
1163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1132,6 +1176,22 @@ class IndexDbStorageManager extends StorageManager {
|
|
|
1132
1176
|
});
|
|
1133
1177
|
});
|
|
1134
1178
|
}
|
|
1179
|
+
idbGetAll(name) {
|
|
1180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1181
|
+
const db = yield this.openDb(name);
|
|
1182
|
+
const storeName = this.useSameDatabase ? this.storeName : "documents";
|
|
1183
|
+
return yield new Promise((resolve, reject) => {
|
|
1184
|
+
const tx = db.transaction(storeName, "readonly");
|
|
1185
|
+
const store = tx.objectStore(storeName);
|
|
1186
|
+
const req = store.getAll();
|
|
1187
|
+
req.onsuccess = () => {
|
|
1188
|
+
const values = req.result;
|
|
1189
|
+
resolve(Array.isArray(values) ? values : []);
|
|
1190
|
+
};
|
|
1191
|
+
req.onerror = () => { var _a; return reject((_a = req.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB getAll failed")); };
|
|
1192
|
+
});
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1135
1195
|
idbSet(key, value) {
|
|
1136
1196
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1137
1197
|
const db = yield this.openDb();
|
|
@@ -1145,6 +1205,27 @@ class IndexDbStorageManager extends StorageManager {
|
|
|
1145
1205
|
});
|
|
1146
1206
|
});
|
|
1147
1207
|
}
|
|
1208
|
+
idbSetAll(name, models) {
|
|
1209
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1210
|
+
const db = yield this.openDb(name);
|
|
1211
|
+
const storeName = this.useSameDatabase ? this.storeName : "documents";
|
|
1212
|
+
yield new Promise((resolve, reject) => {
|
|
1213
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
1214
|
+
const store = tx.objectStore(storeName);
|
|
1215
|
+
// Clear existing documents first
|
|
1216
|
+
store.clear();
|
|
1217
|
+
// Store each document individually using _id as key
|
|
1218
|
+
for (const model of models) {
|
|
1219
|
+
if (model && model._id) {
|
|
1220
|
+
store.put(model, model._id);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
tx.oncomplete = () => resolve();
|
|
1224
|
+
tx.onerror = () => { var _a; return reject((_a = tx.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB setAll failed")); };
|
|
1225
|
+
tx.onabort = () => { var _a; return reject((_a = tx.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB setAll aborted")); };
|
|
1226
|
+
});
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1148
1229
|
idbDelete(key) {
|
|
1149
1230
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1150
1231
|
const db = yield this.openDb();
|
|
@@ -1158,50 +1239,105 @@ class IndexDbStorageManager extends StorageManager {
|
|
|
1158
1239
|
});
|
|
1159
1240
|
});
|
|
1160
1241
|
}
|
|
1242
|
+
idbDeleteAll(name) {
|
|
1243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1244
|
+
const db = yield this.openDb(name);
|
|
1245
|
+
const storeName = this.useSameDatabase ? this.storeName : "documents";
|
|
1246
|
+
yield new Promise((resolve, reject) => {
|
|
1247
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
1248
|
+
const store = tx.objectStore(storeName);
|
|
1249
|
+
store.clear();
|
|
1250
|
+
tx.oncomplete = () => resolve();
|
|
1251
|
+
tx.onerror = () => { var _a; return reject((_a = tx.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB deleteAll failed")); };
|
|
1252
|
+
tx.onabort = () => { var _a; return reject((_a = tx.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB deleteAll aborted")); };
|
|
1253
|
+
});
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1161
1256
|
get(name) {
|
|
1162
1257
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1258
|
+
if (this.useSameDatabase) {
|
|
1259
|
+
const k = this.key(name);
|
|
1260
|
+
try {
|
|
1261
|
+
const value = yield this.idbGet(k);
|
|
1262
|
+
return (value !== null && value !== void 0 ? value : []);
|
|
1263
|
+
}
|
|
1264
|
+
catch (_a) {
|
|
1265
|
+
return [];
|
|
1266
|
+
}
|
|
1167
1267
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1268
|
+
else {
|
|
1269
|
+
// When using separate databases, get all documents from the collection's database
|
|
1270
|
+
try {
|
|
1271
|
+
const values = yield this.idbGetAll(name);
|
|
1272
|
+
return values;
|
|
1273
|
+
}
|
|
1274
|
+
catch (_b) {
|
|
1275
|
+
return [];
|
|
1276
|
+
}
|
|
1170
1277
|
}
|
|
1171
1278
|
});
|
|
1172
1279
|
}
|
|
1173
1280
|
set(name, models) {
|
|
1174
1281
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1175
|
-
const k = this.key(name);
|
|
1176
1282
|
const value = Array.isArray(models) ? models : [];
|
|
1177
|
-
|
|
1178
|
-
|
|
1283
|
+
if (this.useSameDatabase) {
|
|
1284
|
+
const k = this.key(name);
|
|
1285
|
+
try {
|
|
1286
|
+
yield this.idbSet(k, value);
|
|
1287
|
+
}
|
|
1288
|
+
catch (_a) {
|
|
1289
|
+
// ignore write errors
|
|
1290
|
+
}
|
|
1179
1291
|
}
|
|
1180
|
-
|
|
1181
|
-
//
|
|
1292
|
+
else {
|
|
1293
|
+
// When using separate databases, store each document individually by _id
|
|
1294
|
+
try {
|
|
1295
|
+
yield this.idbSetAll(name, value);
|
|
1296
|
+
}
|
|
1297
|
+
catch (_b) {
|
|
1298
|
+
// ignore write errors
|
|
1299
|
+
}
|
|
1182
1300
|
}
|
|
1183
1301
|
});
|
|
1184
1302
|
}
|
|
1185
1303
|
delete(name) {
|
|
1186
1304
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1305
|
+
if (this.useSameDatabase) {
|
|
1306
|
+
const k = this.key(name);
|
|
1307
|
+
try {
|
|
1308
|
+
yield this.idbDelete(k);
|
|
1309
|
+
}
|
|
1310
|
+
catch (_a) {
|
|
1311
|
+
// ignore delete errors
|
|
1312
|
+
}
|
|
1190
1313
|
}
|
|
1191
|
-
|
|
1192
|
-
//
|
|
1314
|
+
else {
|
|
1315
|
+
// When using separate databases, clear all documents from the collection's database
|
|
1316
|
+
try {
|
|
1317
|
+
yield this.idbDeleteAll(name);
|
|
1318
|
+
}
|
|
1319
|
+
catch (_b) {
|
|
1320
|
+
// ignore delete errors
|
|
1321
|
+
}
|
|
1193
1322
|
}
|
|
1194
1323
|
});
|
|
1195
1324
|
}
|
|
1196
1325
|
getParams() {
|
|
1197
1326
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1327
|
+
if (this.useSameDatabase) {
|
|
1328
|
+
const db = yield this.openDb();
|
|
1329
|
+
return new Promise((resolve, reject) => {
|
|
1330
|
+
const req = db.transaction(this.storeName, "readonly").objectStore(this.storeName).getAllKeys();
|
|
1331
|
+
const keys = req.result.map(x => x.toString().replace(this.prefix, ""));
|
|
1332
|
+
req.onsuccess = () => resolve(keys);
|
|
1333
|
+
req.onerror = () => { var _a; return reject((_a = req.error) !== null && _a !== void 0 ? _a : new Error("IndexedDB get params failed")); };
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
else {
|
|
1337
|
+
// When using separate databases, return the list of database names (collections)
|
|
1338
|
+
// This is a simplified implementation - in practice, you might want to track collections differently
|
|
1339
|
+
return Array.from(this.dbPromises.keys());
|
|
1340
|
+
}
|
|
1205
1341
|
});
|
|
1206
1342
|
}
|
|
1207
1343
|
}
|
|
@@ -1322,16 +1458,14 @@ function useRegister(controller, store = getDefaultObjectStore()) {
|
|
|
1322
1458
|
* ```
|
|
1323
1459
|
*/
|
|
1324
1460
|
function useController(name, where, options) {
|
|
1325
|
-
var _a, _b
|
|
1326
|
-
(_a = options === null || options === void 0 ? void 0 : options.initialise) !== null && _a !== void 0 ? _a : true;
|
|
1461
|
+
var _a, _b;
|
|
1327
1462
|
const optionalStore = options === null || options === void 0 ? void 0 : options.store;
|
|
1328
|
-
const
|
|
1329
|
-
const withInvalidation = (_c = options === null || options === void 0 ? void 0 : options.withInvalidation) !== null && _c !== void 0 ? _c : true;
|
|
1463
|
+
const withInvalidation = (_a = options === null || options === void 0 ? void 0 : options.withInvalidation) !== null && _a !== void 0 ? _a : true;
|
|
1330
1464
|
const [data, setData] = useState([]);
|
|
1331
1465
|
const [loading, setLoading] = useState(false);
|
|
1332
1466
|
const [error, setError] = useState(null);
|
|
1333
1467
|
const defaultStore = useContext(context);
|
|
1334
|
-
const store = (
|
|
1468
|
+
const store = (_b = optionalStore !== null && optionalStore !== void 0 ? optionalStore : defaultStore) !== null && _b !== void 0 ? _b : null;
|
|
1335
1469
|
if (!store) {
|
|
1336
1470
|
throw Error("Store is not defined");
|
|
1337
1471
|
}
|
|
@@ -1350,15 +1484,12 @@ function useController(name, where, options) {
|
|
|
1350
1484
|
controller.invalidator.registerInvalidation();
|
|
1351
1485
|
}
|
|
1352
1486
|
void store.initialiseOnce(name, where);
|
|
1353
|
-
// controller.initialise(where);
|
|
1354
1487
|
return () => {
|
|
1355
|
-
|
|
1356
|
-
controller.abort();
|
|
1357
|
-
}
|
|
1488
|
+
controller.abort();
|
|
1358
1489
|
cleanup();
|
|
1359
1490
|
controller.invalidator.unregisterInvalidation();
|
|
1360
1491
|
};
|
|
1361
|
-
}, [
|
|
1492
|
+
}, [where, withInvalidation]);
|
|
1362
1493
|
return { controller, data, loading, error };
|
|
1363
1494
|
}
|
|
1364
1495
|
|