|
|
|
@ -827,7 +827,23 @@ export const writeIndexDB = (rows, table, database) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const readIndexDB = (key=null, table, database) => {
|
|
|
|
|
/**
|
|
|
|
|
* Reads data from an IndexedDB object store.
|
|
|
|
|
* It can read a single record by key, multiple records by an array of keys, or all records.
|
|
|
|
|
*
|
|
|
|
|
* @param {string|string[]|null} keys - The key(s) to read.
|
|
|
|
|
* - If `string`: Reads a single record and returns the data object directly.
|
|
|
|
|
* - If `string[]`: Reads multiple records and returns a Map of `rowkey` to `data` objects.
|
|
|
|
|
* - If `null` or `undefined` or `empty string/array`: Reads all records and returns a Map of `rowkey` to `data` objects.
|
|
|
|
|
* @param {string} table - The name of the IndexedDB object store (table).
|
|
|
|
|
* @param {string} database - The name of the IndexedDB database.
|
|
|
|
|
* @returns {Promise<any|Map<string, any>>} A promise that resolves with the data.
|
|
|
|
|
* - Single key: Resolves with the data object or `undefined` if not found.
|
|
|
|
|
* - Array of keys or All records: Resolves with a `Map` where keys are rowkeys and values are data objects.
|
|
|
|
|
* The Map will be empty if no records are found.
|
|
|
|
|
* - Rejects if there's an error opening the database or during the transaction.
|
|
|
|
|
*/
|
|
|
|
|
export const readIndexDB = (keys=null, table, database) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let openRequest = indexedDB.open(database)
|
|
|
|
|
openRequest.onupgradeneeded = function () {
|
|
|
|
@ -858,44 +874,80 @@ export const readIndexDB = (key=null, table, database) => {
|
|
|
|
|
let transaction = db.transaction(table, 'readonly')
|
|
|
|
|
let store = transaction.objectStore(table)
|
|
|
|
|
// read by key
|
|
|
|
|
const getRequest = isEmpty(key) ? store.all() : store.get(key);
|
|
|
|
|
getRequest.onsuccess = (event) => {
|
|
|
|
|
const result = event.target.result
|
|
|
|
|
if (result) {
|
|
|
|
|
console.log(`💾Found record with key ${key}:`, result)
|
|
|
|
|
resolve(result)
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`No record found with key ${key}.`)
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Handle array of keys
|
|
|
|
|
if (Array.isArray(keys) && keys.length > 0) {
|
|
|
|
|
const promises = keys.map(key => {
|
|
|
|
|
return new Promise((innerResolve) => {
|
|
|
|
|
const getRequest = store.get(key);
|
|
|
|
|
getRequest.onsuccess = (event) => {
|
|
|
|
|
const result = event.target.result;
|
|
|
|
|
if (result) {
|
|
|
|
|
// console.log(`💾Found record with key ${key}:`, result);
|
|
|
|
|
innerResolve([key, result]); // Resolve with [key, data] tuple
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`No record found with key ${key}.`);
|
|
|
|
|
innerResolve(void 0); // Resolve with undefined for non-existent keys
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
getRequest.onerror = (event) => {
|
|
|
|
|
console.error(`Error getting record with key ${key}:`, event.target.error);
|
|
|
|
|
innerResolve(undefined); // Resolve with undefined on error, or innerReject if you want to fail fast
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getRequest.onerror = (event) => {
|
|
|
|
|
console.error(`Error getting record with key ${key}:`, event.target.error)
|
|
|
|
|
Promise.all(promises)
|
|
|
|
|
.then(results => {
|
|
|
|
|
const resultMap = new Map();
|
|
|
|
|
results.forEach(item => {
|
|
|
|
|
if (item !== undefined) {
|
|
|
|
|
resultMap.set(item[0], item[1]); // item[0] is key, item[1] is data
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
resolve(resultMap);
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Error during batch read:', error);
|
|
|
|
|
reject(error); // Reject the main promise if Promise.all encounters an error
|
|
|
|
|
});
|
|
|
|
|
} else if (!isEmpty(keys)) { // Handle single key
|
|
|
|
|
const getRequest = store.get(keys);
|
|
|
|
|
getRequest.onsuccess = (event) => {
|
|
|
|
|
const result = event.target.result;
|
|
|
|
|
if (result) {
|
|
|
|
|
console.log(`💾Found record with key ${keys}:`, result);
|
|
|
|
|
resolve(result);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`No record found with key ${keys}.`);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
getRequest.onerror = (event) => {
|
|
|
|
|
console.error(`Error getting record with key ${keys}:`, event.target.error);
|
|
|
|
|
reject(event.target.error);
|
|
|
|
|
};
|
|
|
|
|
} else { // Handle read all
|
|
|
|
|
const getAllRequest = store.getAll();
|
|
|
|
|
getAllRequest.onsuccess = (event) => {
|
|
|
|
|
const allData = event.target.result;
|
|
|
|
|
const resultMap = new Map();
|
|
|
|
|
if (allData && allData.length > 0) {
|
|
|
|
|
allData.forEach(item => {
|
|
|
|
|
resultMap.set(item.key, item);
|
|
|
|
|
});
|
|
|
|
|
console.log(`💾Found all records:`, resultMap);
|
|
|
|
|
resolve(resultMap);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`No records found.`);
|
|
|
|
|
resolve(resultMap); // Resolve with an empty Map if no records
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
getAllRequest.onerror = (event) => {
|
|
|
|
|
console.error(`Error getting all records:`, event.target.error);
|
|
|
|
|
reject(event.target.error);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const request = store.openCursor(null, 'prev'); // 从后往前
|
|
|
|
|
// const results = [];
|
|
|
|
|
// let count = 0;
|
|
|
|
|
// request.onerror = function (e) {
|
|
|
|
|
// reject('Error getting records.')
|
|
|
|
|
// }
|
|
|
|
|
// request.onsuccess = function (e) {
|
|
|
|
|
// const cursor = e.target.result
|
|
|
|
|
// if (cursor) {
|
|
|
|
|
// if (count < limit) {
|
|
|
|
|
// results.unshift(cursor.value)
|
|
|
|
|
// count++
|
|
|
|
|
// cursor.continue()
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log(JSON.stringify(results))
|
|
|
|
|
// resolve(results)
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log(JSON.stringify(results))
|
|
|
|
|
// resolve(results)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|