From 52ccc121c942182f8471e50845452f379b3c7feb Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 10 Nov 2022 19:33:36 +0300 Subject: [PATCH] experimenting with journal indexedDB store... Signed-off-by: Alex A. Naanou --- pwiki/store/base.js | 112 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/pwiki/store/base.js b/pwiki/store/base.js index a445e0b..d7881a3 100755 --- a/pwiki/store/base.js +++ b/pwiki/store/base.js @@ -19,6 +19,118 @@ var index = require('../index') //--------------------------------------------------------------------- +var JournalDB = +module.JournalDB = +object.Constructor('JournalDB', { + id: undefined, + __version__: 1, + + __promisify: async function(req){ + return new Promise(function(resolve, reject){ + req.onsuccess = function(){ + resolve(req.result) } + req.onerror = function(evt){ + reject(evt.error) } }) }, + + __db: undefined, + get db(){ + if(this.__db){ + return this.__db } + + var that = this + var id = this.id + + if(id == null){ + throw new Error('JournalDB(..): need an id.') } + if(typeof(indexedDB) == 'undefined'){ + throw new Error('JournalDB(..): indexedDB not supported.') } + + return new Promise(function(resolve, reject){ + var req = indexedDB.open(id, this.__version__) + // setup/upgrade... + req.onupgradeneeded = function(evt){ + var old = evt.oldVersion + if(old > this.__version__){ + throw new Error(`JournalDB(..): stored version (${old}) newer than expected: ${this.__version__}`) } + // setup/update... + var db = that.__db = req.result + switch(old){ + // setup... + case 0: + console.log('CREATE!!') + var journal = db.createObjectStore('journal', {keyPath: 'date'}) + journal.createIndex('path', 'path') + // update... + case 1: + // update code... + } } + req.onversionchange = function(evt){ + // XXX close connections and reload... + // ...this means that two+ versions of code opened the + // same db and we need to reload the older versions... + // XXX + } + req.onerror = function(evt){ + reject(evt.error) } + req.onsuccess = function(evt){ + that.__db = req.result } }) }, + + // XXX + get length(){ + return this.__promisify( + (await this.db) + .transaction('journal', 'readonly') + .objectStore('journal') + .count()) }, + + slice: async function(from, to){ + return this.__promisify( + (await this.db) + .transaction('journal', 'readonly') + .objectStore('journal') + .getAll(IDBKeyRange.lowerBound(from ?? 0, true))) }, + + path: async function(path){ + return this.__promisify( + (await this.db) + .transaction('journal', 'readonly') + .objectStore('journal') + .index('path') + .getAll(...arguments)) }, + + get paths(){ + }, + + // remove entries up to date... + trim: function(date){ + // XXX + }, + add: async function(path, action, ...data){ + return this.__promisify( + (await this.db) + .transaction('journal', 'readwrite') + .objectStore('journal') + .add({ + // high-resolution date... + date: + performance.timing.navigationStart + + performance.now(), + path, + action, + data, + })) }, + + clear: function(){ + indexedDB.deleteDatabase(this.id) + delete this.db }, + + __init__: function(id){ + var that = this + this.id = id ?? this.id + this.db }, +}) + + //--------------------------------------------------------------------- // Store...