gundb / gun-level

LevelDB storage plugin for gunDB
75 stars 15 forks source link

I am trying to get gun-level to work with fruitdown in the browser #23

Closed JonathanDDuncan closed 7 years ago

JonathanDDuncan commented 7 years ago

I came across your example https://gist.github.com/PsychoLlama/5894445f48254f6f3f00

and added modified my create-elm-app to run parts of your example as I couldn't get it to run directly.

in index.js I have


var Gun = require('gun-level');

// so you can experiment in the browser
window.gun = new Gun({
    level: {
        // sets the "down" implementation
        down: require('fruitdown')
    }
});

const bob = gun.get('bob').put({ name: 'Bob' })
const dave = gun.get('dave').put({ name: 'Dave' })

// Write a fun circular reference. 
bob.path('friend').put(dave)
dave.path('friend').put(bob)

// Print the data! 
bob.path('friend.name').val()
bob.path('friend.friend.name').val()

and I added

new webpack.IgnorePlugin(/(gun\/lib\/wsp|fs)/)

to my pluggins

I am getting the following error in the console.

[HMR] Waiting for update signal from WDS...
index.js:118 Uncaught TypeError: level.get is not a function
    at Adapter.read (index.js:118)
    at Object.get (index.js:37)
    at wire (gun.js:732)
    at load (gun.js:709)
    at Object.on [as fn] (gun.js:738)
    at Gun.eval [as get] (gun.js:743)
    at eval (webpack:///./src/index.js?:39)
    at Object.<anonymous> (bundle.js:1336)
    at __webpack_require__ (bundle.js:556)
    at fn (bundle.js:87)
gun.js:1180 Warning! You have no peers to connect to!

I'm not sure what the problem could be.

PsychoLlama commented 7 years ago

Hey @JonathanDDuncan! Thanks for reporting this issue!

Between v3 and v4 gun-level changed its api a bit, which might be the cause of the errors you're seeing here. The gist linked above was written for v3, thus all the hacky webpack ignore fs/wsp/lib grossness. Hopefully a working v4 implementation will be less code. I'm gonna try to reproduce this issue on my machine, but in the meantime this may help you move foward (assumes you're using gun-level v4.0.1 & gun v0.3.x):

+ const leveldb = levelUp('my-database-name', {
+   db: require('fruitdown')
+ })
window.gun = new Gun({
-   level: {
-     // sets the "down" implementation
-       down: require('fruitdown')
-   }
+     level: leveldb
});

I'll hack at it on my end keep you in the loop.

PsychoLlama commented 7 years ago

So after playing around with it for a while, I got pieces of it working with indexedDB in the browser. I did actually need the json-loader in webpack for levelup. I'm not sure yet why it was complaining.

Here's what the index.js file looks like:

const Gun = require('gun/gun');

// Imported for side effects.
require('gun-level');

const levelUp = require('levelup');
const fruitdown = require('fruitdown');

const level = levelUp('my-database-name', {
    db: fruitdown,
});

// So you can experiment in the browser.
window.gun = new Gun({ level: level });

The revised webpack file:

module.exports = {
    entry: './index.js',
    output: { filename: 'bundle.js' },

    module: {
        loaders: [{
            test: /\.json$/,
            loader: 'json',
        }],
    },
};

The dependencies I needed to get it "working":

npm install --save-dev webpack json-loader
npm install --save gun gun-level levelup fruitdown

There's two issues I've noticed though.

I need to do more digging to see if the issue is with how it's configured, the gun-level plugin, or with gun itself.

Probably not gonna happen tonight, but I'll do my best to get it resolved soon. Once again, thanks for reporting this!

For convenience I've attached a zip of my working files. gun-level-fruitdown.zip

JonathanDDuncan commented 7 years ago

Hi @PsychoLlama thank for taking a look at it. I tried out the zip file you attached and it works!! Still have to get it working with my app setup. But that will be for another day.

I am seeing the same issues with the data being saved to local storage too. Also the fakekey in the indexedDB without the real data.

Thanks Jonathan

PsychoLlama commented 7 years ago

Woohoo, glad it helped! I'm probably gonna take another look at it tonight and figure out why not all the data is saved.

JonathanDDuncan commented 7 years ago

Some of the reasons I wanted to save to IndexedDb was so that I could have more space for saving. But if the data isn't actually saved to IndexedDb just the keys, and localstorage is still being used. Then I expect if I do a big enough of a project I would still run out of space in localstorage even though I could still save tons more to IndexedDb. ;-(

I haven't actually run out of space yet with localstorage but want to be on the safe side.

Did you have a chance to look at the code?

Jonathan

PsychoLlama commented 7 years ago

Hey @JonathanDDuncan, sorry for the delay! I've dug into the code and added all sorts of log statements. I am now enlightened. Turns out all the data is actually making it to IndexedDB! It's a full replica of what's in localStorage. Sadly, after digging around in the gun source code, it doesn't look like you can disable localStorage.

For now, I'm using localStorage.clear() before loading the bundle, though naturally that might not suite your needs. I'll pester @amark about an option to disable it.

Here's the updated example code: gun-level-idb.zip