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

Add defaults file for auto store #475

Closed
wants to merge 14 commits into from
Closed

Add defaults file for auto store #475

wants to merge 14 commits into from

Conversation

mikeshawdev
Copy link
Contributor

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

Mike Shaw added 4 commits January 8, 2018 16:36
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.
Copy link
Contributor

@edwardhorsford edwardhorsford left a 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}
Copy link
Contributor

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?

Copy link
Contributor Author

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')
Copy link
Contributor

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.

Copy link
Contributor Author

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

Mike Shaw added 2 commits January 8, 2018 20:09
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
@mikeshawdev
Copy link
Contributor Author

mikeshawdev commented Jan 8, 2018

@edwardhorsford I think that's a great idea, my latest commit adds this behaviour to the kit

@mikeshawdev mikeshawdev changed the title Feature/add defaults file for auto store Add defaults file for auto store Jan 9, 2018
lib/utils.js Outdated
storeData(defaults, req.session)
}
} catch (e) {
console.error('Could not load defaults file')
Copy link
Contributor

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?

Copy link
Contributor

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.

@edwardhorsford
Copy link
Contributor

Thanks for all these changes @mikeshawdev.

I get an error when running it - I think related to the non existent app/data folder. Can you take a look? I guess you could either create the folder or put the file in app/.

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.

@joelanman
Copy link
Contributor

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')
Copy link
Contributor

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
@joelanman
Copy link
Contributor

Hope you don't mind, had a bash at a small refactor of the utils.js code:

// Get session default data from file
let sessionDefaults = {}

const defaultsFilePath = path.join(__dirname, '/../app/data/session-defaults.js')

try {
  sessionDefaults = require(defaultsFilePath)
} catch (e) {
  console.error('Could not load the session defaults from app/data/session-defaults.js. Might be a syntax error?')
}

// Middleware - store any data sent in session, and pass it to all views
exports.autoStoreData = function (req, res, next) {
  if (!req.session.data) {
    req.session.data = {}
  }

  req.session.data = Object.assign({}, sessionDefaults, req.session.data)

  storeData(req.body, req.session)
  storeData(req.query, req.session)

I don't think you need to send the defaults to storeData? That function does things like checking for _unchecked values which we wouldn't get in the defaults

@mikeshawdev
Copy link
Contributor Author

mikeshawdev commented Jan 24, 2018

@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.
@mikeshawdev
Copy link
Contributor Author

@edwardhorsford @joelanman changes added!

@joelanman
Copy link
Contributor

Rebased and merged here: #501

Thanks @mikeshawdev !

@joelanman joelanman closed this Aug 15, 2018
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

Successfully merging this pull request may close these issues.

3 participants