chill117 / express-mysql-session

A MySQL session store for the express framework in node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for JSON column type for the data column

ThreeDfish opened this issue · comments

It would be nice to have option JSON column type support, this would be greatly useful for those of us who use MySQL 5.7 or later.

It should be an optional configuration option. I could fork and add it and put in a pull request... it wouldn't take much to add that option...

Hello, @ThreeDfish

Can you provide more detail on this? Which column would use the JSON type and what benefits might it provide in this case?

This would be for the data column. The data in the data column is JSON anyway, and it would be useful to be able to store it in its native format, then the data would not need to be stringified or parsed when storing and retrieving. Also, this would allow applications to easily search the data for specific keys without having the load the data or parse it.

I'd love to take a shot at this somewhere next week if @chill117 doesn't feel like it.
My guess would be to just add a boolean config option that switches between JSON-mode and plaintext-mode, and then changing the queries/used schema file based on that. Would that be enough or am I missing something here?

@HoldYourWaffle Please feel free to open a PR for this - I kindly ask that you please follow the code-style of the rest of the project and to include tests. Thank you!

commented

I added a PR to just use LONGTEXT instead of TEXT. using JSON type would require mysql 5.7 over above. That may prove problematic for legacy code?

#96

As @ThreeDfish said in his original comment, this should be a configurable option (off by default?) so users can get the best deal possible for their version of MySQL

I really need this capability -- I am going to fork and add it so I can start using it -- I will then do a PR.

Please take a look at my modifications here ( https://github.com/ThreeDfish/express-mysql-session ) and let me know if it looks good for a PR

Hi, @ThreeDfish . Thanks for taking the time to move forward with this one.

A few thoughts:

  • Tests:
    • Any PR for this feature should include tests. A sufficient test could be checking that the data column of the sessions table is created with the correct type depending upon the value of the new json option.
  • Backwards compatible:
    • Maintaining the existing behavior (not json column data type) for users of the module that use default options.
  • Making an almost duplicate schema file feels a bit off. But I am not sure of a better alternative. Your thoughts?

@chill117 As far as the alternate schema file goes, it seems to me that the simplest solution is usually the best solution...

As far as the tests go, I am not opposed to adding a test, however, I have not done tests before and that part of the code is something that would probably take me a while to get up to speed on.

Where do you see the additional test being added ( I have a vague idea, but I'd rather ask somebody that knows :-)

As far as the tests go, I am not opposed to adding a test, however, I have not done tests before and that part of the code is something that would probably take me a while to get up to speed on.

Where do you see the additional test being added ( I have a vague idea, but I'd rather ask somebody that knows :-)

You can have a look here. Those are the unit tests specifically related to the constructor options.

And if you need a bit of help to understand what's going on in the tests: the mocha docs might help.

I've picked up where @ThreeDfish left off, and submitted #120 . Thanks everyone for your input here.

It appears that no new options nor significant changes were required to support the JSON type. The commit 885b9d3 includes new tests and clarifications in the readme to help address the case when using the JSON type for the data column - see Custom database table schema for details.

In the end, the solution is simply to create your session table with the JSON type for the data column. The module will continue to work as expected.