-
Notifications
You must be signed in to change notification settings - Fork 236
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
Add defaults file for auto store #475
Add defaults file for auto store #475
Conversation
If provided, the app will try to merge data from a defaults file with data from the session. Defaults will only apply if there isn't a corresponding value already in the session, so data can be overridden within the prototype if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this @mikeshawdev!
What do you think about having the kit create the autoDataDefaults file from a template if it doesn't already exist? We did this with the .env
so that there's no risk of us overwriting it, but users upgrading to the kit still get the file.
app/defaults.js
Outdated
* Provide default values for the user session. These are automatically added | ||
* via the `autoStoreData` middleware. These values will only be added to the | ||
* session, if a value doesn't already exist. | ||
* @type {Object} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this comment include an example for how to use to set some data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this comment
lib/utils.js
Outdated
@@ -258,6 +258,19 @@ exports.autoStoreData = function (req, res, next) { | |||
req.session.data = {} | |||
} | |||
|
|||
try { | |||
let defaultsFilePath = path.join(__dirname, '/../app/defaults.js') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this expected to set other defaults or just defaults for the autoDataStore? If the latter, I wonder if the name could be more specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just for the session data defaults, I've renamed the file to be more explicit
Moved `app/defaults.js` to `app/data/session-defaults.js`, which is a more obvious name and structure
If the session data defaults file doesn't exist on start, we create it from a template file
@edwardhorsford I think that's a great idea, my latest commit adds this behaviour to the kit |
lib/utils.js
Outdated
storeData(defaults, req.session) | ||
} | ||
} catch (e) { | ||
console.error('Could not load defaults file') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be more specific about what the defaults file is? Default user data file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could also specify the location it's looking for.
Removed this file as it would be generated by the app
Make comments on how to work with the file clearer
Thanks for all these changes @mikeshawdev. I get an error when running it - I think related to the non existent Other than that, I've tested and it seems to work well. I added a few defaults to the file and they appear as normal. GETing new values then overwrites them. One final thing to do would be to squash relevant commits. |
This all runs on every request, wonder if that's the best approach |
lib/utils.js
Outdated
@@ -258,6 +258,19 @@ exports.autoStoreData = function (req, res, next) { | |||
req.session.data = {} | |||
} | |||
|
|||
try { | |||
let defaultsFilePath = path.join(__dirname, '/../app/data/session-defaults.js') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requiring the file can come out of the middleware, it only needs to happen once right?
Previously, the creation of the session defaults file failed if the `app/data` directory didn't exist. This commit adds a check for directory existence, and creates it if not already there
Hope you don't mind, had a bash at a small refactor of the utils.js code:
I don't think you need to send the defaults to |
@edwardhorsford cheers for looking it over! Once everyone is happy, I'll squash the commits @joelanman That change looks good to me! You're right, the file code only needs to happen once! I'll add your code to the PR now |
Previously, the session defaults file was required as part of the middleware. However, running this on every request is inefficient. This commit moves the require outside the middleware, so it's only required once when the app starts.
@edwardhorsford @joelanman changes added! |
Rebased and merged here: #501 Thanks @mikeshawdev ! |
Add the ability to provide default values to be added to session data. Key/value pairs that already exist in the session will not be overwritten.
Fixes #474