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

store.get() default value #215

Closed
kawaiipantsu opened this issue Mar 10, 2022 · 3 comments
Closed

store.get() default value #215

kawaiipantsu opened this issue Mar 10, 2022 · 3 comments

Comments

@kawaiipantsu
Copy link

Hi,

if I create a new "store" with defaults set. Why does my get not return them ?
So what i'm basically is looking for is that the users actualy config.json file is out of sync with the defaults.

So lets say
Code updated to include new "something2"

  • test.item.something1: 'im something1'
  • test.item.something2: 'im something2'

The user is still running on a config.json from before the new update so he only have

  • test.item.something1: 'im something1'

Why does the etc.

  • store.get('test.item.something2')
    Not return the default value of "im something2" but instead its "undefined" ?

I dont understand the "defaultValue?" extra argument here.
If it's not pulling from the Store's defaults, then what use does it have ?

Or should i really make redundant code like

  • store.get('test.item.something2','im something2')

If it just looked at the default value you already set via defaults?

PS. Perhaps i'm missing something obviously :)
But i hope you understand my predicament.

@sindresorhus
Copy link
Owner

Are you using the defaults or schema option? If the latter, it might be related to: #102

@kawaiipantsu
Copy link
Author

kawaiipantsu commented Mar 16, 2022

I'm using defaults. So i have something like this

config-defaults.js

module.exports = {
    test: {
        item: {
            something1: 'Hi im something1',
            something2: 'Hi im something2'
        }
    }
};

And loading it like

const configDefaults = require('config-defaults.js');
const store = new Store({defaults: configDefaults});

So lets say the user in his physical config.json file only have the something1 test item, but i introduce the something2 test item in defaults. I would suspect that this would be true, but its not, its returning undefined.

console.log( store.get('test.item.something1') )
>> Hi im something1
console.log( store.get('test.item.something2') )
>> Hi im something2

But what i get is the following

console.log( store.get('test.item.something1') )
>> Hi im something1
console.log( store.get('test.item.something2') )
>> undefined

Obviously i can achive my end goal by setting the static value down the line in my code.
Like the following

console.log( store.get('test.item.something2','Hi im something2') )
>> Hi im something2

But that kind of makes the whole defaults redundant :)
Yes i know that it uses that "defaults" to write a new config.json if its not there. But heck, why not utilize that also to return defaults in the get() function when not found.

PS. I thought also that only the "scheme" part is for validating store.set() etc. Does store.get get it's default value from there when it does not exists in the users config file etc. ?

@kawaiipantsu
Copy link
Author

kawaiipantsu commented Mar 16, 2022

I just did some testing, and yes it's me that have not understood the relationship between defaults and scheme.
So you can consider this matter close, i have closed the issue 👍

I have to maintain 2 files, one for how the default config file is going to look and on to "validate" not only what it accepts but also whats default if its not found in the physical config file.

That is fine by me, i'm just glad it worked out :)

For anyone querulous in what you should do i now have this setup.

config-defaults.js

module.exports = {
    test: {
        item: {
            something1: 'Hi im something1',
            something2: 'Hi im something2'
        }
    }
};

And config-scheme.js

module.exports = { 
    test: {
        type: 'object',
        properties: {
            item: {
                type: 'object',
                properties: {
                    something1: {
                        type: 'string',
                        default: 'Hi im someting1'
                    },
                    something2: {
                        type: 'string',
                        default: 'Hi im something2'
                    }
                },
                default: {}
            }
        },
        default: {}
    }
};

And loads it all up like

const configDefaults = require('./assets/js/config-defaults.js');
const configScheme = require('./assets/js/config-scheme.js');
const store = new Store({defaults: configDefaults, schema: configScheme});

And now i get the desired result/output like the following (where the user don't have the latest changes in his physical config file.

console.log( store.get('test.item.something1') )
>> Hi im something1
console.log( store.get('test.item.something2') )
>> Hi im something2

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

2 participants