electron-userland / electron-json-storage

:package: Easily write and read user settings in Electron apps
1.44k stars 80 forks source link

First time user, getting a new file per item added #95

Closed FaithInMotion closed 6 years ago

FaithInMotion commented 7 years ago

I am new to this storage tool. It seems that this tool wants to create a new file for each object I add to the "db".

Database setup:

/**
 * Database setup
 */
const db = require('electron-json-storage');

const userDataPath = (electron.app || electron.remote.app).getPath('userData');
db.setDataPath(userDataPath+"/database");

console.log(db.getDataPath());

Logged: /Users/Legend/Library/Application Support/budgeteer/database

Usage:

    var newID = 1;
    var newObject= {
        "id": newID,
        "category": "Giving",
        "lineItems": [
            {
                "name": "Tithe",
                "amount": 10,
                "date": "04/11/2017",
                "recurring": "weekly"
            }
        ]
    };

    var newID2 = 2;
    var newObject2 = {
        "id": newID2,
        "category": "Giving",
        "lineItems": [
            {
                "name": "Offerings",
                "amount": 10,
                "date": "04/15/2017",
                "recurring": "weekly"
            }
        ]
    };

    db.set(newID, newObject, function(){});
    db.set(newID2, newObject2, function(){});
    db.remove(newID, function(){});

    let listItems = db.getAll(function(error, data) {});

What I get (note that also the remove() apparently didn't happen - the file is still there and still has it's contents):

screen shot 2017-11-06 at 12 56 04 pm

I am not sure what I am doing wrong here.

FaithInMotion commented 7 years ago

EDIT: This comment is now wrong. The single file is just getting overwritten on every add. So this isn't really working for me at all.

I see one mistake, but I'm not going to close this as I still need the understanding. I can make the id the same in the set() calls and get everything in one file. Because I am setting my own id, this works for me - for now. But I'm not sure that this is still as intended - or even desirable? It means "key" is the name of the file and nothing else.

jviotti commented 7 years ago

Hi @FaithInMotion ,

I'm not sure I understand the confusion. The "key" is indeed a file name. Writing an object to a key means creating (or overriding) the file name that corresponds to the key, so I can set {"value":1} to key foo, and then set {"value":2}, which would override the previous value.

If you want to "extend" the object stored in a certain key, you can: