Skip to main content

INSERT

Single insertion

QueryBuilder

import { QueryBuilder } from 'mysql2-orm';

const { sql, params } = QueryBuilder.insert({
table: 'test',
values: {
column1: 'foo',
column2: 1,
},
});

MySQL2 ORM Class

import { MySQL } from 'mysql2-orm';

const pool = new MySQL({
// ...
});

await pool.insert({
table: 'test',
values: {
column1: 'foo',
column2: 1,
},
});

SQL Query

INSERT INTO `test` (`column1`, `column2`) VALUES (?, ?)
-- params: ['foo', 1]

Multiple insertions

QueryBuilder

import { QueryBuilder } from 'mysql2-orm';

const { sql, params } = QueryBuilder.insert({
table: 'test',
values: [
{
column1: 'foo',
column2: 1,
},
{
column1: 'bar',
column2: 2,
},
],
});

MySQL2 ORM Class

import { MySQL } from 'mysql2-orm';

const pool = new MySQL({
// ...
});

await pool.insert({
table: 'test',
values: [
{
column1: 'foo',
column2: 1,
},
{
column1: 'bar',
column2: 2,
},
],
});

SQL Query

INSERT INTO `test` (`column1`, `column2`) VALUES (?, ?), (?, ?)
-- params: ['foo', 1, 'bar', 2]