data-store icon indicating copy to clipboard operation
data-store copied to clipboard

Does not seem to save new data, but reverts back to old data.

Open wavpro opened this issue 4 years ago • 6 comments

I am trying to store data with data store and then read with another file, but whenever I try to read it reverts the data to old data. I have tried both with the save() function and without, same issue all the time.

wavpro avatar Jan 08 '21 13:01 wavpro

Hi,

Same issue when I used with cucumber-js tests :(

julien235 avatar Mar 01 '21 15:03 julien235

Hi,

Same issue when I used with cucumber-js tests :( @julien235

Seems like it has to do with multiple declared stores. If you have multiple files you can try passing the store through functions.

wavpro avatar Mar 01 '21 15:03 wavpro

Do either of you have any code to demonstrate the issue you're having?

doowb avatar Mar 01 '21 16:03 doowb

Set up two files, declare store in both, use module.exports to run one file using the main file, write something, read using 2nd file consoles.log and then read it using the first file. I think this should replicate the issue.

Den mån 1 mars 2021 5:24 emBrian Woodward [email protected] skrev:

Do either of you have any code to demonstrate the issue you're having?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jonschlinkert/data-store/issues/39#issuecomment-788081107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHNIFRBL3GQKHMSTIDADPILTBO5UFANCNFSM4V2NNDJA .

wavpro avatar Mar 01 '21 16:03 wavpro

i believe it keeps a working version in memory, it definitely doesn't update the values based on changes to the file, until you restart the software and reload it. if you are including them both in the same application, i would establish it once in the main file and make the instance available globally so you can access it from multiple files. if you cant do that (maybe its across multiple apps), you probably want a database instead.

skeddles avatar May 11 '21 01:05 skeddles

I'm using a single file but creating multiple store instances to limit the scope of individual classes. Faced with the fact that deleted data was returned when trying to write to a file. I also use a wrapper for store.

This is a bad solution, but this helped:

const config = require('../config.js')

export class DB {

    private get db() {
        const db: any = require('data-store')({ path: process.cwd() + config.dbPath })

        if (this.prefix && !db.has(this.prefix)) {
            db.set(this.prefix, {})
        }

        return db
    }

    constructor(private prefix?: string) {}

    get(key?: string): any {
        if (this.prefix && key) {
            return this.db.get(`${this.prefix}.${key}`)
        }

        if (this.prefix && !key) {
            return this.db.get(this.prefix)
        }

        if (key) {
            return this.db.get(key)
        }

        return undefined
    }

    //other methods...
}

Knoot avatar Feb 14 '22 01:02 Knoot