Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we have append method ? #131

Open
igabriel-olint opened this issue Jun 3, 2019 · 8 comments
Open

Can we have append method ? #131

igabriel-olint opened this issue Jun 3, 2019 · 8 comments

Comments

@igabriel-olint
Copy link

Eg. storage.append(keyname,data,callback)

@Woeler
Copy link

Woeler commented Jun 24, 2019

Seconding this

@Hrithvik-Alex
Copy link

I would appreciate this as well

@two-one-five
Copy link

two-one-five commented Jan 3, 2020

I concur. It really bothers me that I have to have 20 JSON files for 20 keys, instead of being able to traverse one file with 20 keys within it.

@Tobias-Keller
Copy link

I concur. It really bothers me that I have to have 20 JSON files for 20 keys, instead of being able to traverse one file with 20 keys within it.

where is the problem? append your keys to a existing file:

let newKeys = ['foo', 'bob'];
storage.get('myJsonFile', function (error, settings) {
    if (error) throw error
    settings.keys.push(...newKeys);
    storage.set('myJsonFile', settings, function (error) {
        if (error) throw error
    })
})

@Serjeel-Ranjan-911
Copy link

if you want to append to a file having data in array of objects form [ {} ,{} ,{} ]. then for now we can do is

    storage.get('yourfile',(error, data) =>{
        if (error) throw error;
        data.push(newData);
        storage.set('yourfile', data,(err) => {if (err) throw err});
    });

if the file is fresh. write an emtpy array [] to it to avoid any errors.

@jviotti
Copy link
Member

jviotti commented Jun 23, 2021

@Serjeel-Ranjan-911 Sounds interesting and simple enough. Perhaps you would be interested in sending a PR yourself, or at least an initial draft?

@Serjeel-Ranjan-911
Copy link

@Serjeel-Ranjan-911 Sounds interesting and simple enough. Perhaps you would be interested in sending a PR yourself, or at least an initial draft?

@jviotti this is not an efficient method. It's a temporary fix for people making it work. This would be bad for very large files.

@jviotti
Copy link
Member

jviotti commented Aug 19, 2021

@Serjeel-Ranjan-911 I agree, but sadly you can't do anything more with JSON with the parsers available in Node.js. Most JSON parsers require the entire document to be parsed before any modification, and then written back again. If your use case involves appending a lot of data to a list, I'd recommend using something other than JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants