feat: 西班牙语日记学习工具与知识库
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
const DB_NAME = 'SpanishDiary';
|
||||
const DB_VERSION = 1;
|
||||
const STORE_NAME = 'entries';
|
||||
|
||||
let db = null;
|
||||
|
||||
function openDb() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (db) { resolve(db); return; }
|
||||
const req = indexedDB.open(DB_NAME, DB_VERSION);
|
||||
req.onupgradeneeded = (e) => {
|
||||
const d = e.target.result;
|
||||
if (!d.objectStoreNames.contains(STORE_NAME)) {
|
||||
const store = d.createObjectStore(STORE_NAME, { keyPath: 'id' });
|
||||
store.createIndex('date', 'date', { unique: false });
|
||||
}
|
||||
};
|
||||
req.onsuccess = (e) => { db = e.target.result; resolve(db); };
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
}
|
||||
|
||||
function getAllEntries() {
|
||||
return openDb().then(db => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const index = store.index('date');
|
||||
const req = index.openCursor(null, 'prev');
|
||||
const results = [];
|
||||
req.onsuccess = (e) => {
|
||||
const cursor = e.target.result;
|
||||
if (cursor) { results.push(cursor.value); cursor.continue(); }
|
||||
else resolve(results);
|
||||
};
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getEntry(id) {
|
||||
return openDb().then(db => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const req = store.get(id);
|
||||
req.onsuccess = () => resolve(req.result || null);
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function addEntry(entry) {
|
||||
return openDb().then(db => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const req = store.add(entry);
|
||||
req.onsuccess = () => resolve(entry);
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function updateEntry(entry) {
|
||||
return openDb().then(db => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const req = store.put(entry);
|
||||
req.onsuccess = () => resolve(entry);
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function deleteEntry(id) {
|
||||
return openDb().then(db => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const req = store.delete(id);
|
||||
req.onsuccess = () => resolve();
|
||||
req.onerror = (e) => reject(e.target.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user