{"_id":"@alexop/express-mysql-session","name":"@alexop/express-mysql-session","dist-tags":{"latest":"2.2.0"},"versions":{"2.2.0":{"name":"@alexop/express-mysql-session","description":"A MySQL session store for express.js","version":"2.2.0","main":"index.js","scripts":{"test":"npm run test:unit && npm run test:integration","test:integration":"mocha test/integration/ --recursive --reporter spec --ui bdd --exit","test:unit":"mocha test/unit/ --recursive --reporter spec --ui bdd --exit"},"repository":{"type":"git","url":"git://github.com/alexop1000/express-mysql-session.git"},"author":{"name":"Charles Hill","email":"chill@degreesofzero.com"},"license":"MIT","funding":{"type":"individual","url":"https://degreesofzero.com/donate.html?project=express-mysql-session"},"dependencies":{"debug":"4.3.4","express-session":"1.17.2","mysql2":"3.1.0","underscore":"1.13.3"},"devDependencies":{"async":"3.2.3","chai":"4.3.6","express":"4.18.0","mocha":"9.2.2","promise-mysql":"5.1.0"},"bugs":{"url":"https://github.com/alexop1000/express-mysql-session/issues"},"homepage":"https://github.com/alexop1000/express-mysql-session#readme","directories":{"test":"test"},"gitHead":"26bc6d7b06d5e8866f719a69081afd0c273496b2","_id":"@alexop/express-mysql-session@2.2.0","_nodeVersion":"18.6.0","_npmVersion":"7.20.6","dist":{"integrity":"sha512-sOZzRB+xv2yKqnZNs4sc3rBoCCxkno4+l49OH67f3Nct+HWRhpNUCPvFkV+8vaRak1g/+yf3TT5xqKF0CvASdg==","shasum":"ca164a823c5abc02877fc267de54720a7032fd67","tarball":"https://registry.npmjs.org/@alexop/express-mysql-session/-/express-mysql-session-2.2.0.tgz","fileCount":7,"unpackedSize":30112,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICWBr1souFEfT7n3Gx0ka3lCSWfbUQYm4VKZnjEiq4UCAiAThrf7PReAE9BAVfayYbzZetoT9WFr6NB+LYt2vEYvzw=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj199RACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq+MRAAhOgpr9Th5si/vTws1WHDNyPQmTexUM3t8woBYMsoITZFiQY/\r\n7xAboWSR2BloFf9HjMTuMu7EoIAjVJDW96zbZ3QsCbQ69IIuIIQwYukTvHFb\r\nHi2iJnd/vQ1JCY2cYg1i7x2uP5KnQ1lSyQFANVWwVFb/jU66HKAFPTRsdD9V\r\nTaLM0+LUbVwiyOCzQHngggsCIesqnRhjhEadAnjkCCvnB58cR93HW9b4M3nM\r\nO6JmmIKMfRIxkYsucywvVP5gQ8Q4zb5K/np/O0T0qkmEPEOUodV1ixUn3oyd\r\nl1gi6FGJmHSsyOFBezykdr+EJHyipVz8RfChY8rh4e9a9r3GaGER7kmLUxDc\r\nMdX+ABTTWpab+3c1ukVVzTai+mOt4971mtVTupjKt+4rE9eStXi0Y6/DmdSQ\r\ndP/c6N7SEZjNlXOxv1QlcWIS19PGlquFC/JQFWL2euwK8BBpnVuLyIi3R92h\r\noxqmlyZLEtGZ6cJIN1GUDgmCOB6D32GfrVXqGU6mmUD4XSBi/Ovwm5gROaYh\r\nEOQSREfxARLBC3j2VJCU1Oy+cUrK9Fy8Eo9oHhwJJIf2AQMmFc88uZqmRY73\r\nWqlonJhd17OGVMVNJX0zwAZ8arGmkufL79E70Z4cKSIzh4HiwcmrlqOVxSNs\r\nll0RydGYyc3ZZUmWNgmJMetUFxrTidNiWdc=\r\n=TeRC\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alexop","email":"alexop1000@gmail.com"},"maintainers":[{"name":"alexop","email":"alexop1000@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/express-mysql-session_2.2.0_1675091793719_0.7251955242709716"},"_hasShrinkwrap":false}},"time":{"created":"2023-01-30T15:16:33.540Z","2.2.0":"2023-01-30T15:16:33.897Z","modified":"2023-01-30T15:16:34.068Z"},"maintainers":[{"name":"alexop","email":"alexop1000@gmail.com"}],"description":"A MySQL session store for express.js","homepage":"https://github.com/alexop1000/express-mysql-session#readme","repository":{"type":"git","url":"git://github.com/alexop1000/express-mysql-session.git"},"author":{"name":"Charles Hill","email":"chill@degreesofzero.com"},"bugs":{"url":"https://github.com/alexop1000/express-mysql-session/issues"},"license":"MIT","readme":"# express-mysql-session\r\n\r\nA MySQL session store for [express.js](http://expressjs.com/).\r\n\r\n![Build Status](https://github.com/chill117/express-mysql-session/actions/workflows/ci.yml/badge.svg)\r\n\r\n* [Installation](#installation)\r\n* [Important Notes](#important-notes)\r\n\t* [Older Versions](#older-versions)\r\n\t* [Session Table Collation](#session-table-collation)\r\n* [Usage](#usage)\r\n\t* [With an existing MySQL connection or pool](#with-an-existing-mysql-connection-or-pool)\r\n\t* [Closing the session store](#closing-the-session-store)\r\n\t* [Options](#options)\r\n\t\t* [Custom database table schema](#custom-database-table-schema)\r\n\t* [With mysql2](#with-mysql2)\r\n\t* [Debugging](#debugging)\r\n* [Contributing](#contributing)\r\n\t* [Configure Local Environment](#configure-local-environment)\r\n\t* [Tests](#tests)\r\n* [Changelog](#changelog)\r\n* [License](#license)\r\n* [Funding](#funding)\r\n\r\n\r\n## Installation\r\n\r\nAdd to your application via `npm`:\r\n```bash\r\nnpm install express-mysql-session --save\r\n```\r\nThis will install `express-mysql-session` and add it to your application's `package.json` file.\r\n\r\n\r\n## Important Notes\r\n\r\nPotential gotchas and other important information goes here.\r\n\r\n### Older Versions\r\n\r\nFor users who are still using express-mysql-session `0.x`. Changes have been made to the constructor, which are backwards compatible, but you could run into troubles if using an older version of this module with the latest documentation. You can find the documentation for the older version [here](https://github.com/chill117/express-mysql-session/tree/9fbcf51416a00a7a525c1e6e431033125a2945b0).\r\n\r\n### Session Table Collation\r\n\r\nThis module creates a database table to save session data. This data is stored in a MySQL text field with the [utf8mb4](https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html) collation - added in [MySQL 5.5.3](https://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-3.html). The reason for this is to fully support the utf8 character set. If you absolutely must use an older version of MySQL, create your sessions table before initializing the `MySQLStore`.\r\n\r\n\r\n## Usage\r\n\r\nUse with your express session middleware, like this:\r\n```js\r\nvar express = require('express');\r\nvar app = module.exports = express();\r\nvar session = require('express-session');\r\nvar MySQLStore = require('express-mysql-session')(session);\r\n\r\nvar options = {\r\n\thost: 'localhost',\r\n\tport: 3306,\r\n\tuser: 'session_test',\r\n\tpassword: 'password',\r\n\tdatabase: 'session_test'\r\n};\r\n\r\nvar sessionStore = new MySQLStore(options);\r\n\r\napp.use(session({\r\n\tkey: 'session_cookie_name',\r\n\tsecret: 'session_cookie_secret',\r\n\tstore: sessionStore,\r\n\tresave: false,\r\n\tsaveUninitialized: false\r\n}));\r\n```\r\n\r\nThe session store will internally create a `mysql` [connection pool](https://github.com/mysqljs/mysql#pooling-connections) which handles the (re)connection to the database. By default, the pool consists of 1 connection, but you can override this using the `connectionLimit` option. There are additional [pool options](https://github.com/mysqljs/mysql#pool-options) you can provide, which will be passed to the constructor of the `mysql` connection pool.\r\n\r\nThe sessions database table should be automatically created, when using default options. If for whatever reason the table is not created, you can find the schema [here](https://github.com/chill117/express-mysql-session/blob/master/schema.sql).\r\n\r\n### With an existing MySQL connection or pool\r\n\r\nTo pass in an existing MySQL database connection or pool, you would do something like this:\r\n```js\r\nvar mysql = require('mysql');\r\nvar session = require('express-session');\r\nvar MySQLStore = require('express-mysql-session')(session);\r\n\r\nvar options = {\r\n    host: 'localhost',\r\n    port: 3306,\r\n    user: 'db_user',\r\n    password: 'password',\r\n    database: 'db_name'\r\n};\r\n\r\nvar connection = mysql.createConnection(options); // or mysql.createPool(options);\r\nvar sessionStore = new MySQLStore({}/* session store options */, connection);\r\n```\r\n\r\n### Closing the session store\r\n\r\nTo cleanly close the session store:\r\n```js\r\nsessionStore.close();\r\n```\r\n\r\n\r\n### Options\r\n\r\nHere is a list of all available options:\r\n```js\r\nvar options = {\r\n\t// Host name for database connection:\r\n\thost: 'localhost',\r\n\t// Port number for database connection:\r\n\tport: 3306,\r\n\t// Database user:\r\n\tuser: 'session_test',\r\n\t// Password for the above database user:\r\n\tpassword: 'password',\r\n\t// Database name:\r\n\tdatabase: 'session_test',\r\n\t// Whether or not to automatically check for and clear expired sessions:\r\n\tclearExpired: true,\r\n\t// How frequently expired sessions will be cleared; milliseconds:\r\n\tcheckExpirationInterval: 900000,\r\n\t// The maximum age of a valid session; milliseconds:\r\n\texpiration: 86400000,\r\n\t// Whether or not to create the sessions database table, if one does not already exist:\r\n\tcreateDatabaseTable: true,\r\n\t// Number of connections when creating a connection pool:\r\n\tconnectionLimit: 1,\r\n\t// Whether or not to end the database connection when the store is closed.\r\n\t// The default value of this option depends on whether or not a connection was passed to the constructor.\r\n\t// If a connection object is passed to the constructor, the default value for this option is false.\r\n\tendConnectionOnClose: true,\r\n\t// Make touch() a noop to cut down on db writes.\r\n\tdisableTouch: false,\r\n\tcharset: 'utf8mb4_bin',\r\n\tschema: {\r\n\t\ttableName: 'sessions',\r\n\t\tcolumnNames: {\r\n\t\t\tsession_id: 'session_id',\r\n\t\t\texpires: 'expires',\r\n\t\t\tdata: 'data'\r\n\t\t}\r\n\t}\r\n};\r\n```\r\n\r\n\r\n#### Custom database table schema\r\n\r\nIt is possible to use a custom schema for your sessions database table. This can be useful if you want to have extra columns (e.g. \"user_id\"), indexes, foreign keys, etc. You could also change the type of the \"data\" column to a smaller or larger text type (e.g. \"TINYTEXT\", \"LONGTEXT\", \"BLOB\") or native \"JSON\" type.\r\n\r\nSet the `createDatabaseTable` option to `FALSE` so that the session store does not automatically create a sessions table.\r\n\r\nUse the `schema` option to provide the custom table and column names to the session store.\r\n```js\r\nvar session = require('express-session');\r\nvar MySQLStore = require('express-mysql-session')(session);\r\n\r\nvar options = {\r\n\thost: 'localhost',\r\n\tport: 3306,\r\n\tuser: 'session_test',\r\n\tpassword: 'password',\r\n\tdatabase: 'session_test',\r\n\tcreateDatabaseTable: false,\r\n\tschema: {\r\n\t\ttableName: 'custom_sessions_table_name',\r\n\t\tcolumnNames: {\r\n\t\t\tsession_id: 'custom_session_id',\r\n\t\t\texpires: 'custom_expires_column_name',\r\n\t\t\tdata: 'custom_data_column_name'\r\n\t\t}\r\n\t}\r\n};\r\n\r\nvar sessionStore = new MySQLStore(options);\r\n```\r\n\r\n### With mysql2\r\n\r\nThis module is compatible with the [mysql2](https://github.com/sidorares/node-mysql2) module. You will need to create and pass an instance of the mysql2 connection object as follows:\r\n```js\r\nvar session = require('express-session');\r\nvar mysql2 = require('mysql2/promise');\r\nvar MySQLStore = require('express-mysql-session')(session);\r\n\r\nvar options = {\r\n\thost: 'localhost',\r\n\tport: 3306,\r\n\tuser: 'session_test',\r\n\tpassword: 'password',\r\n\tdatabase: 'session_test'\r\n};\r\n\r\nvar connection = mysql2.createPool(options);\r\nvar sessionStore = new MySQLStore({}, connection);\r\n```\r\n\r\n\r\n### Debugging\r\n\r\n`express-mysql-session` uses the [debug module](https://github.com/visionmedia/debug) to output debug messages to the console. To output all debug messages, run your node app with the `DEBUG` environment variable:\r\n```\r\nDEBUG=express-mysql-session* node your-app.js\r\n```\r\nThis will output log messages as well as error messages from `express-mysql-session`.\r\n\r\nIf you also might need MySQL-related debug and error messages, see [debugging node-mysql](https://github.com/mysqljs/mysql#debugging-and-reporting-problems).\r\n\r\n\r\n## Contributing\r\n\r\nThere are a number of ways you can contribute:\r\n\r\n* **Improve or correct the documentation** - All the documentation is in this readme file. If you see a mistake, or think something should be clarified or expanded upon, please [submit a pull request](https://github.com/chill117/express-mysql-session/pulls/new)\r\n* **Report a bug** - Please review [existing issues](https://github.com/chill117/express-mysql-session/issues) before submitting a new one; to avoid duplicates. If you can't find an issue that relates to the bug you've found, please [create a new one](https://github.com/chill117/express-mysql-session/issues).\r\n* **Request a feature** - Again, please review the [existing issues](https://github.com/chill117/express-mysql-session/issues) before posting a feature request. If you can't find an existing one that covers your feature idea, please [create a new one](https://github.com/chill117/express-mysql-session/issues).\r\n* **Fix a bug** - Have a look at the [existing issues](https://github.com/chill117/express-mysql-session/issues) for the project. If there's a bug in there that you'd like to tackle, please feel free to do so. I would ask that when fixing a bug, that you first create a failing test that proves the bug. Then to fix the bug, make the test pass. This should hopefully ensure that the bug never creeps into the project again. After you've done all that, you can [submit a pull request](https://github.com/chill117/express-mysql-session/pulls/new) with your changes.\r\n\r\nBefore you contribute code, please read through at least some of the source code for the project. I would appreciate it if any pull requests for source code changes follow the coding style of the rest of the project.\r\n\r\nNow if you're still interested, you'll need to get your local environment configured.\r\n\r\n\r\n### Configure Local Environment\r\n\r\n#### Step 1: Get the Code\r\n\r\nFirst, you'll need to pull down the code from GitHub:\r\n```\r\ngit clone https://github.com/chill117/express-mysql-session.git\r\n```\r\n\r\n#### Step 2: Install Dependencies\r\n\r\nSecond, you'll need to install the project dependencies as well as the dev dependencies. To do this, simply run the following from the directory you created in step 1:\r\n```bash\r\nnpm install\r\n```\r\n\r\n#### Step 3: Set Up the Test Database\r\n\r\nNow, you'll need to set up a local test database:\r\n```js\r\n{\r\n\thost: 'localhost',\r\n\tport: 3306,\r\n\tuser: 'session_test',\r\n\tpassword: 'password',\r\n\tdatabase: 'session_test'\r\n};\r\n```\r\n*The test database settings are located in [test/config.js](https://github.com/chill117/express-mysql-session/blob/master/test/config.js)*\r\n\r\nAlternatively, you can provide custom database configurations via environment variables:\r\n```\r\nDB_HOST=\"localhost\"\r\nDB_PORT=\"3306\"\r\nDB_USER=\"session_test\"\r\nDB_PASS=\"password\"\r\nDB_NAME=\"session_test\"\r\n```\r\n\r\n\r\n### Tests\r\n\r\nThis project includes an automated regression test suite. To run the tests:\r\n```bash\r\nnpm test\r\n```\r\n\r\n\r\n## Changelog\r\n\r\nSee [changelog.md](https://github.com/chill117/express-mysql-session/blob/master/changelog.md)\r\n\r\n\r\n## License\r\n\r\nThis software is [MIT licensed](https://tldrlegal.com/license/mit-license):\r\n> A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source.  There are many variations of this license in use.\r\n\r\n\r\n## Funding\r\n\r\nThis project is free and open-source. If you would like to show your appreciation by helping to fund the project's continued development and maintenance, you can find available options [here](https://degreesofzero.com/donate.html?project=express-mysql-session).\r\n","readmeFilename":"readme.md"}