dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.49k stars 642 forks source link

Unable to use dexie-observable with Next.Js #1101

Open geofmureithi-zz opened 4 years ago

geofmureithi-zz commented 4 years ago

Here are the steps to recreate the bug.

npx create-next-app
cd my-app
yarn add dexie dexie-observable dexie-syncable

package.json

{
    "dexie": "^3.0.2",
    "dexie-observable": "^1.0.0-beta.8",
    "dexie-syncable": "^1.0.0-beta.8",
   .... next etc
}

I then created a file src/db.js with the following contents:

import Dexie from 'dexie';
import 'dexie-observable'
import 'dexie-syncable'

const db = new Dexie('ReactSampleDB')
db.version(1).stores({ todos: '++id' })

export default db;

Then in index.js,

import db from '../src/db'
....
console.log(db.verno)

Running yarn dev returns the following error:

ReferenceError: self is not defined
    at .../my-app/node_modules/dexie-observable/dist/dexie-observable.js:544:14
    at .../my-app/node_modules/dexie-observable/dist/dexie-observable.js:24:82
    at Object.<anonymous> (.../my-app/node_modules/dexie-observable/dist/dexie-observable.js:27:2)
    at Module._compile (internal/modules/cjs/loader.js:688:30)

Removing the line does seem to fix the issue.

carsonfarmer commented 4 years ago

Did you find a workaround for this? I'm running into the same problem running in Nodejs with indexeddbshim.

dfahlander commented 4 years ago

The addons are not designed to run on node. I know we made a job to make Dexie itself run on node with indexeddbshim even though not perfectly. However, in nextjs I don't see the use case to use the addons server-side. This particular error is probably easily fixed, but the question is how the intent is to use those addons on node. There might be other errors to haunt down than these before these addons runs on node. I'm not so experienced with next.js but I suppose there is a way to skip importing the addons server-side.

geofmureithi-zz commented 4 years ago

@carsonfarmer dynamic imports work. Another trick could be using https://github.com/ds300/patch-package though I haven't tried it

Edit: my answer might not be applicable specifically to node.

carsonfarmer commented 4 years ago

Thanks @geofmureithi. For now, I'm able to work around this by writing in Typescript and compiling to CommonJS (which I'm doing anyway), but that seems fragile, so the dynamic imports might be the best route. Also fair enough @dfahlander, though there are plenty of legitimate use-cases for having something like dexie-syncable running in node to sync updates across "peers". In my case, it is about having (mostly) isomorphic code that runs similarly in node and the browser. So far, things appear to be working quite well, other than a few assumptions such as the one touched on in this issue 🤞 .

dfahlander commented 4 years ago

@carsonfarmer I see about the value of isomorphism. The question is whether the server-part would be a client to another sync server, or if the server part itself should represent the sync server (using something similar to this or sync-server).

carsonfarmer commented 4 years ago

In my case, possibly both. I am looking at a "hybrid" network of clients and servers, some of which operate in a browser, and some of which operate in node, but only one (depending on the network setup) is going to be the sync server at a time.

Anyway, didn't want to derail the issue here. The above discussion seems to have mostly addressed things, or provided sufficient justification for why things are the way they are :)