Skip to content

persisted Json objects always access filePath of most recently created object #15

Open
@tebemis

Description

@tebemis

Rporter: @tmoher

here’s the behavior:

source code:

var alpha = nutella.persist.getJsonObjectStore('alpha');
console.log(alpha.filePath());

var beta = nutella.persist.getJsonObjectStore('beta');
console.log(alpha.filePath());
console.log(beta.filePath());

var gamma = nutella.persist.getJsonObjectStore('gamma');
console.log(alpha.filePath());
console.log(beta.filePath());
console.log(gamma.filePath());

console output:

data/TEST-TEST-0/alpha.json
data/TEST-TEST-0/beta.json
data/TEST-TEST-0/beta.json
data/TEST-TEST-0/gamma.json
data/TEST-TEST-0/gamma.json
data/TEST-TEST-0/gamma.json

• same problem with load. it will always load from the most recent pathName.

• same problem in getJsonObjectCollection

my amateur analysis:

somehow alpha and beta are losing their pathNames.
i’m a js newbie w.r.t. OOP, but it seems like the filePath
is shared among all instances of persisted objects.

if i make this change in json_file_persisted_object.js:

    Object.prototype.save = function(path) {
        fs.writeFileSync(path, JSON.stringify(this));
    };

and add the path explicitly in the save call, e.g.:

alpha.save(‘data/…/alpha.json’)

then everything works.

so, i have a workaround for now.

if you can’t fine a quick answer, could you replace
the save function in the nutella lib with:

    Object.prototype.save = function(path) {
        if (arguments.length == 1) fs.writeFileSync(path, JSON.stringify(this));
        else fs.writeFileSync(this.filePath(), JSON.stringify(this));
        
    };

so that my code will be compatible with any fixes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions