First of all, congratularions for the great library!
I'm doing a application using GitHub Electron and, Dexie is undefined in the application, if a add new Dexie(db, option) it does not identify Dexie.
Debugging, inside Dexie definition protected function, if I set window.Dexie = Dexie then it works fine.
The project I'm using it is in gitHub also if you want to test
Cheers
I just had the same issue. The root cause is that when using electron, you are implicitly using commonJS. Dexie is built to use commonJS natively. To use Dexie, you need to explicitly load Dexie:
var Dexie = require("dexie");
If your application is used both in Electron as well as a traditional browser, you can detect commonJS and load Dexie via commonJS if it is available.
if (typeof global !== 'undefined' && typeof module !== 'undefined' && module.exports) {
// CommonJS
window.Dexie = require("dexie");
}
It's not meaningful to set window.Dexie = require('dexie'). Electron has built-in CommonJS support also in web pages so just do var Dexie = require('dexie') in the modules where you need it.
In Electron, the only thing to keep in mind, is that you must run Dexie targeting code from a browser window, web worker or service worker. Not from the main process (because main process is just a DOM-less node process without indexedDB support).
Else than that, you are free to include Dexie using your preferred module system - plain script include works fine, but I would suggest to use the native commonjs support that comes with Electrion and use require('dexie').
Here's an example page that uses Dexie from within a BrowserWindow page:
<html>
<head>
<script>
// Import Dexie
const Dexie = require('dexie');
//
// Declare Database
//
let db = new Dexie("FriendDatabase");
db.version(1).stores({ friends: "++id,name,age" });
//
// Have Fun
//
db.transaction('rw', db.friends, function*() {
// Make sure we have something in DB:
if ((yield db.friends.where('name').equals('Josephine').count()) === 0) {
let id = yield db.friends.add({name: "Josephine", age: 21});
alert (`Addded friend with id ${id}`);
}
// Query:
let youngFriends = yield db.friends.where("age").below(25).toArray();
// Show result:
alert ("My young friends: " + JSON.stringify(youngFriends));
}).catch(e => {
alert(e);
});
</script>
</head>
</html>
The full sample is available to clone and download at: https://github.com/dfahlander/Dexie.js/tree/master/samples/electron
Most helpful comment
It's not meaningful to set window.Dexie = require('dexie'). Electron has built-in CommonJS support also in web pages so just do
var Dexie = require('dexie')in the modules where you need it.In Electron, the only thing to keep in mind, is that you must run Dexie targeting code from a browser window, web worker or service worker. Not from the main process (because main process is just a DOM-less node process without indexedDB support).
Else than that, you are free to include Dexie using your preferred module system - plain script include works fine, but I would suggest to use the native commonjs support that comes with Electrion and use require('dexie').
Here's an example page that uses Dexie from within a
BrowserWindowpage:The full sample is available to clone and download at: https://github.com/dfahlander/Dexie.js/tree/master/samples/electron