Notes2/static/js/node_store.mjs

205 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-12-01 10:21:29 +01:00
import { API } from 'api'
2024-11-30 17:10:46 +01:00
export class NodeStore {
2024-12-02 15:26:35 +01:00
constructor() {//{{{
2024-11-30 17:10:46 +01:00
if (!('indexedDB' in window)) {
throw 'Missing IndexedDB'
}
this.db = null
2024-12-02 15:26:35 +01:00
}//}}}
async initializeDB() {//{{{
2024-11-30 17:10:46 +01:00
return new Promise((resolve, reject) => {
2024-12-03 06:53:31 +01:00
const req = indexedDB.open('notes', 3)
2024-11-30 17:10:46 +01:00
// Schema upgrades for IndexedDB.
// These can start from different points depending on updates to Notes2 since a device was online.
req.onupgradeneeded = (event) => {
2024-12-03 06:53:31 +01:00
let treeNodes
let nodes
let appState
2024-12-02 15:26:35 +01:00
const db = event.target.result
const trx = event.target.transaction
2024-11-30 17:10:46 +01:00
for (let i = event.oldVersion + 1; i <= event.newVersion; i++) {
console.log(`Upgrade to schema ${i}`)
// The schema transformations.
switch (i) {
case 1:
2024-12-03 06:53:31 +01:00
treeNodes = db.createObjectStore('treeNodes', { keyPath: 'UUID' })
treeNodes.createIndex('nameIndex', 'Name', { unique: false })
nodes = db.createObjectStore('nodes', { keyPath: 'UUID' })
nodes.createIndex('nameIndex', 'Name', { unique: false })
2024-11-30 17:10:46 +01:00
break
2024-12-03 06:53:31 +01:00
2024-11-30 17:10:46 +01:00
case 2:
2024-12-03 06:53:31 +01:00
trx.objectStore('treeNodes').createIndex('parentIndex', 'ParentUUID', { unique: false })
2024-11-30 17:10:46 +01:00
break
2024-12-03 06:53:31 +01:00
case 3:
appState = db.createObjectStore('appState', { keyPath: 'key' })
2024-11-30 17:10:46 +01:00
}
}
}
req.onsuccess = (event) => {
this.db = event.target.result
resolve()
}
req.onerror = (event) => {
reject(event.target.error)
}
})
2024-12-02 15:26:35 +01:00
}//}}}
2024-11-30 17:10:46 +01:00
2024-12-03 06:53:31 +01:00
async getAppState(key) {//{{{
return new Promise((resolve, reject) => {
const trx = this.db.transaction('appState', 'readonly')
const appState = trx.objectStore('appState')
const getRequest = appState.get(key)
getRequest.onsuccess = (event) => {
if (event.target.result !== undefined) {
resolve(event.target.result)
} else {
resolve(null)
}
}
getRequest.onerror = (event) => reject(event.target.error)
})
}//}}}
async setAppState(key, value) {//{{{
return new Promise((resolve, reject) => {
try {
const t = this.db.transaction('appState', 'readwrite')
const appState = t.objectStore('appState')
t.onerror = (event) => {
console.log('transaction error', event.target.error)
reject(event.target.error)
}
t.oncomplete = () => {
resolve()
}
const record = { key, value }
const addReq = appState.put(record)
addReq.onerror = (event) => {
console.log('Error!', event.target.error, key, value)
}
} catch (e) {
reject(e)
}
})
}//}}}
async updateTreeRecords(records) {//{{{
return new Promise((resolve, reject) => {
2024-12-03 13:56:38 +01:00
const t = this.db.transaction('treeNodes', 'readwrite')
const nodeStore = t.objectStore('treeNodes')
t.onerror = (event) => {
console.log('transaction error', event.target.error)
reject(event.target.error)
}
t.oncomplete = () => {
resolve()
}
// records is an object, not an array.
for (const i in records) {
const record = records[i]
let addReq
let op
if (record.Deleted) {
op = 'deleting'
addReq = nodeStore.delete(record.UUID)
} else {
op = 'upserting'
addReq = nodeStore.put(record)
2024-12-03 06:53:31 +01:00
}
2024-12-03 13:56:38 +01:00
addReq.onsuccess = () => {
console.log(`${op} ${record.UUID} (${record.Name})`)
2024-12-03 06:53:31 +01:00
}
2024-12-03 13:56:38 +01:00
addReq.onerror = (event) => {
console.log(`error ${op} ${record.UUID}`, event.target.error)
reject(event.target.error)
2024-12-03 06:53:31 +01:00
}
}
2024-12-03 13:56:38 +01:00
2024-12-03 06:53:31 +01:00
})
}//}}}
2024-12-02 15:26:35 +01:00
async add(records) {//{{{
2024-11-30 17:10:46 +01:00
return new Promise((resolve, reject) => {
try {
2024-12-02 15:26:35 +01:00
const t = this.db.transaction('nodes', 'readwrite')
const nodeStore = t.objectStore('nodes')
2024-11-30 17:10:46 +01:00
t.onerror = (event) => {
console.log('transaction error', event.target.error)
reject(event.target.error)
}
t.oncomplete = () => {
resolve()
}
2024-12-03 06:53:31 +01:00
// records is an object, not an array.
for (const recordIdx in records) {
const record = records[recordIdx]
2024-12-02 15:26:35 +01:00
const addReq = nodeStore.put(record)
addReq.onsuccess = () => {
2024-11-30 17:10:46 +01:00
console.log('OK!', record.ID, record.Name)
}
addReq.onerror = (event) => {
console.log('Error!', event.target.error, record.ID)
}
2024-12-02 15:26:35 +01:00
}
2024-11-30 17:10:46 +01:00
} catch (e) {
console.log(e)
}
})
2024-12-02 15:26:35 +01:00
}//}}}
async get(id) {//{{{
2024-12-01 10:21:29 +01:00
return new Promise((resolve, reject) => {
// Node is always returned from IndexedDB if existing there.
// Otherwise an attempt to get it from backend is executed.
const trx = this.db.transaction('nodes', 'readonly')
const nodeStore = trx.objectStore('nodes')
const getRequest = nodeStore.get(id)
getRequest.onsuccess = (event) => {
// Node found in IndexedDB and returned.
if (event.target.result !== undefined) {
resolve(event.target.result)
return
}
2024-11-30 17:10:46 +01:00
2024-12-01 10:21:29 +01:00
// Node not found and a request to the backend is made.
API.query("POST", `/node/retrieve/${id}`, {})
.then(res => {
const trx = this.db.transaction('nodes', 'readwrite')
const nodeStore = trx.objectStore('nodes')
const putRequest = nodeStore.put(res.Node)
putRequest.onsuccess = () => resolve(res.Node)
putRequest.onerror = (event) => {
reject(event.target.error)
}
})
.catch(e => reject(e))
}
})
2024-12-02 15:26:35 +01:00
}//}}}
async getTreeNodes() {//{{{
2024-12-01 10:21:29 +01:00
return new Promise((resolve, reject) => {
2024-12-02 15:26:35 +01:00
const trx = this.db.transaction('nodes', 'readonly')
const nodeStore = trx.objectStore('nodes')
const req = nodeStore.getAll()
2024-12-01 10:21:29 +01:00
req.onsuccess = (event) => resolve(event.target.result)
req.onerror = (event) => reject(event.target.error)
2024-11-30 17:10:46 +01:00
})
2024-12-02 15:26:35 +01:00
}//}}}
2024-11-30 17:10:46 +01:00
}
2024-12-02 15:26:35 +01:00
// vim: foldmethod=marker