|
|
|
@ -919,7 +919,7 @@ export const deleteIndexDBbyKey = (key, table, database) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function cleanOldData(database, storeName, dateKey = 'timestamp') {
|
|
|
|
|
function cleanOldData(database, storeNames=[], dateKey = 'timestamp') {
|
|
|
|
|
return function (daysToKeep = 7) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let deletedCount = 0
|
|
|
|
@ -927,85 +927,92 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
|
|
|
|
|
|
|
|
|
|
let openRequest = indexedDB.open(database, INDEXED_DB_VERSION)
|
|
|
|
|
openRequest.onupgradeneeded = function () {
|
|
|
|
|
var db = openRequest.result
|
|
|
|
|
// var db = openRequest.result
|
|
|
|
|
// 数据库是否存在
|
|
|
|
|
if (!db.objectStoreNames.contains(storeName)) {
|
|
|
|
|
var store = db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true })
|
|
|
|
|
store.createIndex('timestamp', 'timestamp', { unique: false })
|
|
|
|
|
} else {
|
|
|
|
|
const logStore = openRequest.transaction.objectStore(storeName)
|
|
|
|
|
if (!logStore.indexNames.contains('timestamp')) {
|
|
|
|
|
logStore.createIndex('timestamp', 'timestamp', { unique: false })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// if (!db.objectStoreNames.contains(storeName)) {
|
|
|
|
|
// var store = db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true })
|
|
|
|
|
// store.createIndex('timestamp', 'timestamp', { unique: false })
|
|
|
|
|
// } else {
|
|
|
|
|
// const logStore = openRequest.transaction.objectStore(storeName)
|
|
|
|
|
// if (!logStore.indexNames.contains('timestamp')) {
|
|
|
|
|
// logStore.createIndex('timestamp', 'timestamp', { unique: false })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
openRequest.onsuccess = function (e) {
|
|
|
|
|
let db = e.target.result
|
|
|
|
|
// 数据库是否存在
|
|
|
|
|
if (!db.objectStoreNames.contains(storeName)) {
|
|
|
|
|
resolve('Database does not exist.')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// if (!db.objectStoreNames.contains(storeName)) {
|
|
|
|
|
// resolve('Database does not exist.')
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Calculate the cutoff timestamp for "X days ago"
|
|
|
|
|
const cutoffTimestamp = Date.now() - daysToKeep * 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
|
|
// Identify old data using the date index and primary key ID
|
|
|
|
|
const readTransaction = db.transaction([storeName], 'readwrite')
|
|
|
|
|
const objectStore = readTransaction.objectStore(storeName)
|
|
|
|
|
const objectStoreNames = isEmpty(storeNames) ? db.objectStoreNames : storeNames
|
|
|
|
|
|
|
|
|
|
if (!objectStore.indexNames.contains(`${dateKey}`)) {
|
|
|
|
|
// Clear the store
|
|
|
|
|
let clearRequest = objectStore.clear()
|
|
|
|
|
console.log(`Cleanup complete. clear ${storeName} records.`)
|
|
|
|
|
resolve();
|
|
|
|
|
clearRequest.onerror = function (e) {}
|
|
|
|
|
clearRequest.onsuccess = function (e) {}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Get records older than 'daysToKeep' using the index
|
|
|
|
|
const dateIndex = objectStore.index(`${dateKey}`)
|
|
|
|
|
const dateRange = IDBKeyRange.upperBound(cutoffTimestamp, false) // Get keys < cutoffTimestamp (strictly older)
|
|
|
|
|
if (!isEmpty(objectStoreNames)) {
|
|
|
|
|
const objectStores = Array.from(objectStoreNames).map((storeName) => db.transaction([storeName], 'readwrite').objectStore(storeName))
|
|
|
|
|
|
|
|
|
|
const dateCursorRequest = dateIndex.openCursor(dateRange)
|
|
|
|
|
|
|
|
|
|
dateCursorRequest.onsuccess = (event) => {
|
|
|
|
|
const cursor = event.target.result
|
|
|
|
|
if (cursor) {
|
|
|
|
|
recordsToDelete.add(cursor.primaryKey) // Add the primary key of the record to the set
|
|
|
|
|
cursor.continue()
|
|
|
|
|
} else {
|
|
|
|
|
// Delete identified data in a new transaction
|
|
|
|
|
const deleteTransaction = db.transaction([storeName], 'readwrite')
|
|
|
|
|
const deleteObjectStore = deleteTransaction.objectStore(storeName)
|
|
|
|
|
for (const objectStore of objectStores) {
|
|
|
|
|
// Identify old data using the date index and primary key ID
|
|
|
|
|
|
|
|
|
|
deleteTransaction.oncomplete = () => {
|
|
|
|
|
console.log(`Cleanup complete. Deleted ${deletedCount} records.`)
|
|
|
|
|
resolve(deletedCount)
|
|
|
|
|
if (!objectStore.indexNames.contains(`${dateKey}`)) {
|
|
|
|
|
// Clear the store
|
|
|
|
|
let clearRequest = objectStore.clear()
|
|
|
|
|
console.log(`Cleanup complete. clear ${objectStore.name} records.`)
|
|
|
|
|
resolve()
|
|
|
|
|
clearRequest.onerror = function (e) {}
|
|
|
|
|
clearRequest.onsuccess = function (e) {}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Get records older than 'daysToKeep' using the index
|
|
|
|
|
const dateIndex = objectStore.index(`${dateKey}`)
|
|
|
|
|
const dateRange = IDBKeyRange.upperBound(cutoffTimestamp, false) // Get keys < cutoffTimestamp (strictly older)
|
|
|
|
|
|
|
|
|
|
const dateCursorRequest = dateIndex.openCursor(dateRange)
|
|
|
|
|
|
|
|
|
|
dateCursorRequest.onsuccess = (event) => {
|
|
|
|
|
const cursor = event.target.result
|
|
|
|
|
if (cursor) {
|
|
|
|
|
recordsToDelete.add(cursor.primaryKey) // Add the primary key of the record to the set
|
|
|
|
|
cursor.continue()
|
|
|
|
|
} else {
|
|
|
|
|
const storeName = objectStore.name;
|
|
|
|
|
// Delete identified data in a new transaction
|
|
|
|
|
const deleteTransaction = db.transaction([storeName], 'readwrite')
|
|
|
|
|
const deleteObjectStore = deleteTransaction.objectStore(storeName)
|
|
|
|
|
|
|
|
|
|
deleteTransaction.oncomplete = () => {
|
|
|
|
|
console.log(`Cleanup complete. Deleted ${deletedCount} records in ${database}.${storeName}.`)
|
|
|
|
|
resolve(deletedCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteTransaction.onerror = (event) => {
|
|
|
|
|
console.error('Deletion transaction error:', event.target.error)
|
|
|
|
|
reject(event.target.error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert Set to Array for forEach
|
|
|
|
|
Array.from(recordsToDelete).forEach((key) => {
|
|
|
|
|
const deleteRequest = deleteObjectStore.delete(key)
|
|
|
|
|
deleteRequest.onsuccess = () => {
|
|
|
|
|
deletedCount++
|
|
|
|
|
}
|
|
|
|
|
deleteRequest.onerror = (event) => {
|
|
|
|
|
console.warn(`Failed to delete record with key ${key}:`, event.target.error)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteTransaction.onerror = (event) => {
|
|
|
|
|
console.error('Deletion transaction error:', event.target.error)
|
|
|
|
|
dateCursorRequest.onerror = (event) => {
|
|
|
|
|
console.error('Error opening date cursor for deletion:', event.target.error)
|
|
|
|
|
reject(event.target.error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert Set to Array for forEach
|
|
|
|
|
Array.from(recordsToDelete).forEach((key) => {
|
|
|
|
|
const deleteRequest = deleteObjectStore.delete(key)
|
|
|
|
|
deleteRequest.onsuccess = () => {
|
|
|
|
|
deletedCount++
|
|
|
|
|
}
|
|
|
|
|
deleteRequest.onerror = (event) => {
|
|
|
|
|
console.warn(`Failed to delete record with key ${key}:`, event.target.error)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dateCursorRequest.onerror = (event) => {
|
|
|
|
|
console.error('Error opening date cursor for deletion:', event.target.error)
|
|
|
|
|
reject(event.target.error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
openRequest.onerror = function (e) {
|
|
|
|
|
reject('Error opening database:'+database, e)
|
|
|
|
@ -1014,4 +1021,5 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const clean7DaysWebsocketLog = cleanOldData('LogWebsocketData', 'LogStore');
|
|
|
|
|
export const clean7DaysWebsocketLog = cleanOldData('LogWebsocketData', ['LogStore']);
|
|
|
|
|
export const clean7DaysMailboxLog = cleanOldData('mailbox');
|
|
|
|
|