How to persist data in an Electron app?

前端 未结 9 1138
刺人心
刺人心 2021-01-30 07:00

I\'ve been scouring the Electron documentation to try and figure out how to persist data in an Electron app. For example, in iOS or OS X, you could use NSUserDefaults to store u

9条回答
  •  悲哀的现实
    2021-01-30 07:33

    NeDB is the only suggested or featured tool as an embedded persistent database for Electron by Electron, currently. - http://electron.atom.io/community/

    It's also could be useful to store user settings if settings are complex.

    Why NeDB could be a better solution on this case?

    Embedded persistent or in memory database for Node.js, nw.js, Electron and browsers, 100% JavaScript, no binary dependency. API is a subset of MongoDB's and it's plenty fast. - NeDB

    Creating or loading a database:

    var Datastore = require('nedb')
      , db = new Datastore({ filename: 'path/to/datafile', autoload: true });
    // You can issue commands right away
    

    Inserting a document:

    var doc = { hello: 'world'
                   , n: 5
                   , today: new Date()
                   , nedbIsAwesome: true
                   , notthere: null
                   , notToBeSaved: undefined  // Will not be saved
                   , fruits: [ 'apple', 'orange', 'pear' ]
                   , infos: { name: 'nedb' }
                   };
    
    db.insert(doc, function (err, newDoc) {   // Callback is optional
      // newDoc is the newly inserted document, including its _id
      // newDoc has no key called notToBeSaved since its value was undefined
    });
    

    Finding documents:

    // Finding all inhabited planets in the solar system
    db.find({ system: 'solar', inhabited: true }, function (err, docs) {
      // docs is an array containing document Earth only
    });
    

    The list goes on...

    Update - September 2019

    As of 2019, this is no longer the valid answer. See the answers of @jviotti and @Tharanga below.

提交回复
热议问题