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

Cannot specify database when using an existing connection pool #146

Open
RedSpid3r opened this issue Oct 19, 2023 · 1 comment
Open

Cannot specify database when using an existing connection pool #146

RedSpid3r opened this issue Oct 19, 2023 · 1 comment

Comments

@RedSpid3r
Copy link

RedSpid3r commented Oct 19, 2023

I am trying to using my existing connection pool with express-mysql-session, however, in doing so i am unable to specify the database name.

My app connected to multiple databases which is why I am not setting database name in mysql2 createPool options.

	const sessionStore = new MySQLStore({ database: "db_name" }, pool);
	app.use(
		session({
			key: `APP_${process.env.NODE_ENV}`,
			secret: process.env.SESSION_SECRET,
			store: sessionStore,
			resave: false,
			saveUninitialized: false,
			clearExpired: true,
			checkExpirationInterval: 900000,
			cookie: {
				secure: process.env.NODE_ENV === "production" ? true : false,
				maxAge: 2629800000,
			},
		})
	);

this results in:

  code: 'ER_NO_DB_ERROR',
  errno: 1046,
  sql: "SELECT `data` AS data, `expires` as expires FROM `sessions` WHERE `session_id` = '[REDACTED]'",
  sqlState: '3D000',
  sqlMessage: 'No database selected'

Is there a way to make this work or do i need to keep using 2 separate connection pools?

@chill117
Copy link
Owner

chill117 commented Oct 21, 2023

When you pass an existing pool or connection object to the MySQLStore constructor, the module will not create a new pool internally - it will use the one that you provided. So the database-related options (e.g. database) will not be used. Create a new pool object with the other database name and then pass that to the constructor.

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