mirror of
https://github.com/zoldar/jenot.git
synced 2026-01-03 06:22:55 +00:00
139 lines
3 KiB
JavaScript
139 lines
3 KiB
JavaScript
export class DBNoteStore extends EventTarget {
|
|
constructor(dbName, storeName) {
|
|
super();
|
|
this.dbName = dbName;
|
|
this.storeName = storeName;
|
|
this.db = null;
|
|
}
|
|
|
|
async all() {
|
|
const that = this;
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise((resolve, reject) => {
|
|
db
|
|
.transaction([that.storeName], "readonly")
|
|
.objectStore(that.storeName)
|
|
.getAll().onsuccess = (data) => resolve(data.target.result);
|
|
}),
|
|
);
|
|
}
|
|
|
|
async get(id) {
|
|
const that = this;
|
|
let result;
|
|
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise(
|
|
(resolve, reject) =>
|
|
(db
|
|
.transaction([that.storeName], "readonly")
|
|
.objectStore(that.storeName)
|
|
.get(id).onsuccess = (data) => resolve(data.target.result)),
|
|
),
|
|
);
|
|
}
|
|
|
|
async add(note) {
|
|
const that = this;
|
|
|
|
const entry = {
|
|
id: "id_" + Date.now(),
|
|
type: note.type,
|
|
content: note.content,
|
|
created: new Date(),
|
|
};
|
|
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise(
|
|
(resolve, reject) =>
|
|
(db
|
|
.transaction([that.storeName], "readwrite")
|
|
.objectStore(that.storeName)
|
|
.add(entry).onsuccess = () => resolve(null)),
|
|
),
|
|
);
|
|
}
|
|
|
|
async reset() {
|
|
const that = this;
|
|
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise(
|
|
(resolve, reject) =>
|
|
(db
|
|
.transaction([that.storeName], "readwrite")
|
|
.objectStore(that.storeName)
|
|
.clear().onsuccess = () => resolve(null)),
|
|
),
|
|
);
|
|
}
|
|
|
|
async remove({ id }) {
|
|
const that = this;
|
|
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise(
|
|
(resolve, reject) =>
|
|
(db
|
|
.transaction([that.storeName], "readwrite")
|
|
.objectStore(that.storeName)
|
|
.delete(id).onsuccess = () => resolve(null)),
|
|
),
|
|
);
|
|
}
|
|
|
|
async update(note) {
|
|
const that = this;
|
|
|
|
return this.#connect().then(
|
|
(db) =>
|
|
new Promise(
|
|
(resolve, reject) =>
|
|
(db
|
|
.transaction([that.storeName], "readwrite")
|
|
.objectStore(that.storeName)
|
|
.put(note).onsuccess = () => resolve(null)),
|
|
),
|
|
);
|
|
}
|
|
|
|
saveStorage() {
|
|
this.dispatchEvent(new CustomEvent("save"));
|
|
}
|
|
|
|
async #connect() {
|
|
if (!this.db) {
|
|
this.db = await this.#dbConnect();
|
|
}
|
|
|
|
return this.db;
|
|
}
|
|
|
|
#dbConnect() {
|
|
const that = this;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const request = indexedDB.open(this.dbName, 1);
|
|
|
|
request.onsuccess = (e) => {
|
|
resolve(e.target.result);
|
|
};
|
|
|
|
request.onerror = (e) => {
|
|
console.error(`indexedDB error: ${e.target.errorCode}`);
|
|
};
|
|
|
|
request.onupgradeneeded = (e) => {
|
|
const db = e.target.result;
|
|
db.createObjectStore(that.storeName, {
|
|
keyPath: "id",
|
|
});
|
|
};
|
|
});
|
|
}
|
|
}
|