dexie / Dexie.js

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

Using Dexie server-side to make it compatible with angular universal. #678

Open mlb6 opened 6 years ago

mlb6 commented 6 years ago

Hello,

I'm looking to use Dexie for the server-side rendering of my Angular 5+ app. As there is no IndexedDB on the server-side I'd like to replace it with a mongoDB database. Could you share some details on the way I could implement this ?

Thanks.

dfahlander commented 6 years ago

This is one of my greatest interests right now. My vision is to adapt Dexie server-side to SQL or Mongo queries directly towards SQL databases like Postgres, MySQL, SQLServer or MongoDB. But there are some challenges along the way.

What should be possible already today, is to use IndexedDbShim on node, and use Dexie on top of that. The IndexedDBShim team have been focusing on node support a lot last year, and the first one-liner to read on their page is "Use a single, indexable, offline storage API across all desktop and mobile browsers and Node.js.".

That said, IndexedDBShim does not use MongoDB but SQLite instead, and the use case is still for me on a "should work" basis still. I have no integration tests yet in Dexie tests suite.

mlb6 commented 6 years ago

@dfahlander Thanks for your feedback. I'm gonna try to use IndexedDbShim with Dexie in the meantime!

mlb6 commented 6 years ago

I've tried with indexedDbShim and it works. Here is what I've added to my server.js file :

const setGlobalVars = require('indexeddbshim');
global.window = global;
setGlobalVars(null, {
    checkOrigin: false
});

I still have an other issue which is related to the way universal works. Universal renders the HTML string when the first VM tick returns as stable. As indexedDb queries are async operations, the HTML string is most of the time rendered too early. There is an angular Feature Request to control the render timing. A workaround would be to make Dexie async operations appear as a macrotask to Zone as it is suggested by this comment, but I have not tried yet.

dfahlander commented 6 years ago

Very interesting that you got it to work. Thanks for sharing!

Maybe possible to wrap the db facing code something like this: return Zone.current.run(()=> db.theTable.where('x').equals(y).toArray()). Not tried it and not enough familiar with zonejs to know for sure.

whishkid commented 3 years ago

This is one of my greatest interests right now. My vision is to adapt Dexie server-side to SQL or Mongo queries directly towards SQL databases like Postgres, MySQL, SQLServer or MongoDB. But there are some challenges along the way.

What should be possible already today, is to use IndexedDbShim on node, and use Dexie on top of that. The IndexedDBShim team have been focusing on node support a lot last year, and the first one-liner to read on their page is "Use a single, indexable, offline storage API across all desktop and mobile browsers and Node.js.".

That said, IndexedDBShim does not use MongoDB but SQLite instead, and the use case is still for me on a "should work" basis still. I have no integration tests yet in Dexie tests suite.

@dfahlander Any news/progress on server side dexie (/cloud)