Local, file-based database for an Electron application
Asked Answered
W

1

12

We are working on an application that will be offered both as a web-based and as a cross-platform desktop solution by means of Electron.

Due to customer requirements, the desktop client cannot make use of "the cloud" to store data; all data should be stored in the local machine or, even better, the user should have the option to keep the database/data file on an external HDD so that another user on the same local network can use the same data file.

We've been looking at NeDB, PouchDB, etc, but all these use either Web SQL or IndexedDB on the browser itself to store the data. NeDB can theoretically use the file system but that seems only possible for Node Webkit apps.

Another option is of course MongoDB, but it requires setting up a site on a web server. Seeing as how our users will set that up in on their own machines, that will work for one user only but would make it very hard for them to share the data (note: assume users with little technical know-how).

  • Is there a way to force NeDB to persist data in a file instead of the in-browser database?
  • Alternatively, does any one know of a file-based, compact database that plays well with electron/node?

We'd preferably like to use a NoSQL database, but options of file-based SQL databases will be considered as well.

Wolfhound answered 6/6, 2016 at 10:24 Comment(1)
Can you dive more into the requirements surrounding the "web-based" part of this? ccnokes is correct with his answer when dealing with Electron, but, from your question, it appears you need something that will work in a standard Browser as well. Is that right?Mending
L
7

I have some experience with NeDB in an Electron app and I can say it will definitely work on the filesystem.

How are you initializing NeDB (or whatever your database choice is)? Also, are you initializing it in the main or renderer process? If you can share that, I think we could trace the issue to a configuration issue.

This is how you start NeDB with a persistent data-store that saves to disk.

var Datastore = require('nedb')
  , db = new Datastore({ filename: 'path/to/datafile', autoload: true });

I think MongoDB is going to be overkill for an Electron app (it's meant to be really a high performance, distributed database running in the cloud).

Another option you could consider is LevelDB (a key/value store that can persist to the filesystem) which is popular in the node community. (EDIT 4/17/17 IndexedDB uses LevelDB underneath the hood, so if you go that route, may as well just use that)

One aspect I would definitely evaluate carefully is: How difficult is this database going to be to package and distribute? How do I integrate it into my build system? Level and NeDB can be included simply via npm install and any native code compiling is handled seamlessly with node-gyp, which is as simple as it gets. However, bundling Mongo, for example, will require some work to get a working build for each different platform.

Lagunas answered 7/6, 2016 at 2:50 Comment(4)
I think you might be onto something here. I do initialise the database with a filename and autoload... The thing is that we use the electron main proces to initialize an Aurelia app, and I initialize the db from the Aurelia app. I'm new to Electron, how do I initialize NeDB from the main or rederer process? And how can that db then be made available to the rest of the app?Wolfhound
@SergiPapaseit initializing it from the main or renderer with a filesystem backing will be the same I think. As far as making it available to the rest of the app....just export it from a module. If started it from the main and you need to access it from a renderer you can use Electrons's IPC or remote API.Lagunas
@ccnokes, are the recommendations still the same for 2021? Is there any benefit to a locally stored file of the DB over Indexeddb in the browser? Are there any other suggestions you'd have given recent progress? (NeDB has not have many recent commits on its repo)Pollock
@Pollock I don't know anymore -- sorry, I've been out of electron development for a bit. I'd say the overall guidelines are the same: go as simple as possible, and a high performance database for a client app just doesn't make sense.Lagunas

© 2022 - 2024 — McMap. All rights reserved.