Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexedDB Plugin by Andreas Abeck - Tiddler #3420

Open
AnthonyMuscio opened this issue Sep 5, 2018 · 6 comments
Open

IndexedDB Plugin by Andreas Abeck - Tiddler #3420

AnthonyMuscio opened this issue Sep 5, 2018 · 6 comments

Comments

@AnthonyMuscio
Copy link
Contributor

This tiddler on tiddlywiki.com seems to have a dead link when I used it now.

http://tw5-dev.cibm.de

@00SS
Copy link
Contributor

00SS commented Jan 13, 2019

@AnthonyMuscio
I believe this is the tiddler to delete:
IndexedDB Plugin by Andreas Abeck

@valpackett
Copy link

the wayback machine has it, the full plugin source is pretty short

here's a copy
/*\
title: $:/plugins/tiddlywiki/indexeddb/syncIndexedDb.js
type: application/javascript
module-type: syncadaptor

A sync adaptor module for synchronising IndexedDB

\*/
(function(){

    /*jslint node: true, browser: true */
    /*global $tw: false */
    'use strict';

    function SyncIndexedDb(options) {
        this.logger = new $tw.utils.Logger('SyncIndexedDb');
        this.indexedDB = window.indexedDB || window.mozIndexedDB;
    }

    SyncIndexedDb.prototype.getStatus = function (callback) {
        var self = this,
            request = this.indexedDB.open('tw5db', 1);

        request.onupgradeneeded = function (event) {
            self.db = this.result;
            if (self.db.objectStoreNames.contains('tw5')) {
                self.db.deleteObjectStore('tw5');
            }
            self.store = self.db.createObjectStore('tw5', {keyPath: 'title'});
        };

        request.onsuccess = function (event) {
            self.db = this.result;
            return callback(null, 1, '');
        };

        request.onerror = function (event) {
            self.db = this.result;
            return callback('DB Error');
        };
    };

    SyncIndexedDb.prototype.getSkinnyTiddlers = function (callback) {
        var tiddlers = [],
            objectStore = this.db.transaction('tw5').objectStore('tw5');

        objectStore.openCursor().onsuccess = function (event) {
            var cursor = event.target.result,
                tiddler;
            if (cursor) {
                tiddler = JSON.parse(cursor.value.data);
                tiddler.revision = cursor.value.revision;
                tiddlers.push(tiddler);
                cursor.continue();
            } else {
                return callback(null, tiddlers);
            }
        };

    };

    var saveTiddlerRevision = function (db, tiddler, callback, revision) {
        var trans = db.transaction(['tw5'], 'readwrite'),
            store = trans.objectStore('tw5'),
            request,
            data;

        data = {
            'title': tiddler.fields.title,
            'data': $tw.wiki.getTiddlerAsJson(tiddler.fields.title),
            'revision': revision
        };
        request = store.put(data);

        trans.oncomplete = function (e) {
            return callback(null, '', this.revision);
        };

        request.onerror = function (e) {
            return callback('Error Adding: ', e);
        };
    };


    SyncIndexedDb.prototype.getTiddlerInfo = function (tiddler) {
        return {};
    };


    /*
     Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
     */
    SyncIndexedDb.prototype.saveTiddler = function (tiddler, callback) {
        var transaction = this.db.transaction('tw5');
        var objectStore = transaction.objectStore('tw5');
        var request = objectStore.get(tiddler.fields.title);

        request.onerror = function (event) {
            return callback('Error loading ' + tiddler.fields.title);
        };

        transaction.oncomplete = function (event) {
            if (request.result !== undefined) {
                this.revision = request.result.revision;
                this.revision += 1;
            } else {
                this.revision = 1;
            }
            return saveTiddlerRevision(this.db, tiddler, callback, this.revision);
        };

    };

    /*
     Load a tiddler and invoke the callback with (err,tiddlerFields)

     We don't need to implement loading for the file system adaptor, because all the tiddler files will have been loaded during the boot process.
     */
    SyncIndexedDb.prototype.loadTiddler = function (title, callback) {
        var tiddler;
        var objectStore = this.db.transaction('tw5').objectStore('tw5');
        var request = objectStore.get(title);

        request.onerror = function (event) {
            return callback('Error loading ' + title);
        };
        request.onsuccess = function (event) {
            tiddler = JSON.parse(request.result.data);
            tiddler.revision = request.result.revision;
            return callback(null, tiddler);
        };
    };

    /*
     Delete a tiddler and invoke the callback with (err)
     */
    SyncIndexedDb.prototype.deleteTiddler = function (title, callback, options) {
        var trans = this.db.transaction(['tw5'], 'readwrite');
        var store = trans.objectStore('tw5');

        var request = store.delete(title);

        trans.oncomplete = function (e) {
            return callback(null);
        };

        request.onerror = function (e) {
            return callback('Error deleting: ', e);
        };

        return callback('');
    };

    // check if we are running in browser
    if (typeof(process) === 'undefined') {
        // when in browser check for indexedDb support
        if (window.indexedDB || window.mozIndexedDB) {
            exports.adaptorClass = SyncIndexedDb;
        }
    }

})();

@AnthonyMuscio
Copy link
Contributor Author

AnthonyMuscio commented Jul 17, 2022

@unrelentingtech thanks for retrieving this. I am making it into a plugin now, adding type Application/Javascript and need to add a module-type which I presume should be saver?

  • This has a lot of potential so I am keen to keep it alive
  • But I don't have it working yet, also it seems to be using some deprecated functions

@valpackett
Copy link

no, syncadaptor (look at the header comment; that is automatically parsed by tiddlywiki node.js)

I might be working on something bigger that involves indexeddb too…

@valpackett
Copy link

A whole year (and a week) later, introducing "something bigger": TiddlyPWA! 🚀

Blog Post | TW Talk Announcement | Codeberg Repo | Patreon

@AnthonyMuscio
Copy link
Contributor Author

This is an exciting approach, I am reviewing it now, it opened in Edge.

  • The padlock icon seems to suggest it has permission?
  • The ask for it button does not seem to respond
  • AN accidental refresh suggests it is working.

I will continue in talk tiddlywiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants