import mysql, {
ConnectionOptions,
ResultSetHeader,
RowDataPacket,
} from 'mysql2/promise';
interface User extends RowDataPacket {
id: number;
name: string;
}
(async () => {
const access: ConnectionOptions = {
host: '',
user: '',
password: '',
database: '',
multipleStatements: true,
};
const conn = await mysql.createConnection(access);
await conn.query<ResultSetHeader>('DROP TABLE IF EXISTS `users`;');
await conn.query<ResultSetHeader>(
'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));'
);
const [inserted] = await conn.execute<ResultSetHeader>(
'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);',
['Josh', 'John', 'Marie', 'Gween']
);
console.log('Inserted:', inserted.affectedRows);
const [rows] = await conn.query<User[][]>(
[
'SELECT * FROM `users` ORDER BY `name` ASC LIMIT 2;',
'SELECT * FROM `users` ORDER BY `name` ASC LIMIT 2 OFFSET 2;',
].join(' ')
);
rows.forEach((users) => {
users.forEach((user) => {
console.log('-----------');
console.log('id: ', user.id);
console.log('name:', user.name);
});
});
await conn.end();
})();