1

I'm now trying for a few hours to understand why this happens, in my electron app i would like to use the nedb-promises package("nedb-promises": "^6.2.1",). Installation and configuration works so far but on every app start (dev & prod) the db file got replaced by a new / empty one. Should'nt the package not handle that?

I've took the code from this example: https://shivekkhurana.medium.com/persist-data-in-electron-apps-using-nedb-5fa35500149a

// db.js
const {app} = require('electron');
const Datastore = require('nedb-promises');

const dbFactory = (fileName) => Datastore.create({
    filename: `${process.env.NODE_ENV === 'development' ? '.' : app.getPath('userData')}/data/${fileName}`,
    timestampData: true,
    autoload: true
});

const db = {
    customers: dbFactory('customers.db'),
    tasks: dbFactory('tasks.db')
};

module.exports = db;
import db from './db'
....

// load task should not be important because file is already replaced when arriving here
ipcMain.handle('Elements:Get', async (event, args) => {
    // 'Select * from Customers
    let data = await db.customers.find({});
    console.log(data);
    return data;
})

...

// Set an item
ipcMain.handle('Element:Save', async (event, data) => {
    console.log(data)
    const result = db.customers.insertOne(data.item).then((newDoc) => {
        console.log(newDoc)
        return newDoc
    }).catch((err) => {
        console.log("Error while Adding")
        console.log(err)
    });
    console.log(result);
    return result;
})

Note: After "adding" newDoc contains the new element and when checking the file manually in the filesystem it is added. When i now close the app and open again the file got replaced.

I've checked the docs up and down - i have no clue what i'm doing wrong - thanks for your help.

Marco
  • 473
  • 5
  • 21

0 Answers0