MySQL does not have a built-in schema definition language like Mongoose, so you'll have to create tables manually using SQL queries.
note must require mysql2 at top and install it npm i mysql2 : const mysql = require('mysql2/promise');
Step:01 Create a pool connection
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'your_database_name'
});
Step:02 Function to create user table
async function createUserTable() {
const connection = await
pool.getConnection();
try {
// Create user table
await connection.query(`
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
)
`);
console.log('User table created successfully');
} catch (error) {
console.error('Error creating user table:', error);
} finally {
connection.release();
}
}
Step:03 Call the function to create the user table
createUserTable();
mongoose: elegant mongodb object modeling for node.js
– Knotty