Ewok
Ewok•2y ago

Programmatic batch inserts

Akin to npx convex import, is there anyway (or any plans) to have the capability to do batch inserts that can be triggered by a client call (i.e. db.batchInsert(tableName, [...items])? The result would be a single item for each row in a table
3 Replies
ballingt
ballingt•2y ago
This might be an unimportant detail but it might be relevant: db.insert(tablename, item) can't be called on the client; it must run on a Convex server. So you can write a helper much like this yourself,
function batchInsert(db, table, items) {
return await Promise.all(items.map(item => {
return db.insert(table, item);
}));
}
function batchInsert(db, table, items) {
return await Promise.all(items.map(item => {
return db.insert(table, item);
}));
}
Since Convex functions run on the server, this all happens in a single round-trip from client to server to client. @Ewok does that make sense, am I getting at what you're asking?
Ewok
EwokOP•2y ago
Ah yes, thanks for clarifying my question @ballingt 🙂 I guess my real question is will each db.insert(table, item) call in the loop be counted as a function call against my quota or will the entire batch be counted as one?
ballingt
ballingt•2y ago
Ah gotcha, this is just one function call.

Did you find this page helpful?