To make it so that the insert query is executed when “db.insert(table).values(rows)” was called

As shown below, I want to insert a large number of records into the table in several parts, but when “db.insert(table).values(rows)” is executed, the insert query is not issued, and the query is issued after the entire loop is completed.
import * as relations from "~/infrastructure/drizzle/relations";
import * as schema from "~/infrastructure/drizzle/schema";

const client = postgres(process.env.DATABASE_URL);
const db = drizzlePg(client, {
logger: {
logQuery: (query: string, parameters: unknown[]) => {
console.log("Executed query:", query);
console.log("With parameters:", parameters);
},
},
schema: { ...schema, ...relations },
});

async function insertDummyUsers(count: number) {
for(let i = 0; i<count; i+=100) {
const userRecords = []
for(let j = 0; j<100; j++){
// push dummy user info to users
}
await db.table(users).insert(userRecords)
}
}

insertDummyUsers(100000)
import * as relations from "~/infrastructure/drizzle/relations";
import * as schema from "~/infrastructure/drizzle/schema";

const client = postgres(process.env.DATABASE_URL);
const db = drizzlePg(client, {
logger: {
logQuery: (query: string, parameters: unknown[]) => {
console.log("Executed query:", query);
console.log("With parameters:", parameters);
},
},
schema: { ...schema, ...relations },
});

async function insertDummyUsers(count: number) {
for(let i = 0; i<count; i+=100) {
const userRecords = []
for(let j = 0; j<100; j++){
// push dummy user info to users
}
await db.table(users).insert(userRecords)
}
}

insertDummyUsers(100000)
If I do this, I will run out of heap and get an error like the one shown in the attached file, so I want to make it so that the query is executed each time, without storing it until the end. How can I do this?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?