Skip to content

How to connect mysql database to sveltekit using sequelize ? #8

Answered by jeblister
jeblister asked this question in Questions
Discussion options

You must be logged in to vote

Here's an example of how to use Sequelize with SvelteKit hooks, using the Sveltekit documentation:

  1. Create a new file in your project called hooks.server.js.
  2. Import Sequelize and create a connection to your MySQL database.
  3. Define a hook function that queries the database using Sequelize.

Here's an example of what the code might look like:

import { Sequelize } from 'sequelize';

const sequelize = new Sequelize({
  dialect: 'mysql',
  host: 'localhost',
  username: 'root',
  password: 'password',
  database: 'mydatabase',
});

export async function handle({ event, resolve }) {
  const results = await sequelize.query('SELECT * FROM mytable');
  event.locals.myData = results[0];
  return res…

Replies: 1 comment

Comment options

jeblister
Mar 15, 2023
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by jeblister
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant