Insert Query
Insert a new row with record in database table
Insert a Single Document
Create a file at index.js
:
index.js
import presql from "./connection";
async function InsertData() {
const data = await presql.create({
table: "users", // table name where record is to be inserted e.g.
data: {
firstName: "John", // read below first
lastName: "Doe", // read below first
phone: "9876543210", // read below first
email: "[email protected]", // read below first
password: "123456789", // read below first
}, //data is required parameter, only single object
});
console.log(data); // if showResults is true, it will print results
}
Note
Keep in mind
Pass table Column name in cammelCase Format as given,( firstName
-> first_name
)if table column is join by some prefix/suffix please config it in connection.js
by default tablename is joined by _
(underscore)
Insert Multiple Documents
index.js
import presql from "./connection";
async function InsertManyData() {
const data = await presql.createMany({
table: "users", // table name where record is to be inserted e.g.
data: [
{
firstName: "John",
lastName: "Doe",
},
{
firstName: "David",
lastName: "Olis",
},
{
firstName: "Rachna",
lastName: "Singh",
},
], // pass array of object containing values
});
console.log(data); // if showResults is true, it will print results
}