Return on insert.onConflictDoNothing()

Is there a way to return data if insert.onConflictDoNothing() has conflicts? For example
const insertedProducts = await db
.insert(Product)
.values(productData)
.returning({
id: Product.id,
product_num: Product.product_num,
storeId: Product.storeId,
})
.onConflictDoNothing();

const updatedProducts = await doSomething(
insertedProducts,
);

const insertedPrices = await db
.insert(Price)
.values(updatedProducts) // i want to use insertedProducts here
.returning({
id: Price.id,
productId: Price.productId,
storeId: Price.storeId,
})
.onConflictDoNothing();
const insertedProducts = await db
.insert(Product)
.values(productData)
.returning({
id: Product.id,
product_num: Product.product_num,
storeId: Product.storeId,
})
.onConflictDoNothing();

const updatedProducts = await doSomething(
insertedProducts,
);

const insertedPrices = await db
.insert(Price)
.values(updatedProducts) // i want to use insertedProducts here
.returning({
id: Price.id,
productId: Price.productId,
storeId: Price.storeId,
})
.onConflictDoNothing();
I want to use insertedProducts again, and when insertedProducts returns something, it all works fine. However, when insertedProducts doesnt return something due to a conflict, it doesnt return anything and my code fails with
D:\..\sg-backend\node_modules\src\sqlite-core\query-builders\insert.ts:53
throw new Error('values() must be called with at least one value');
^
Error: values() must be called with at least one value
at SQLiteInsertBuilder.values (D:\..\sg-backend\node_modules\src\sqlite-core\query-builders\insert.ts:53:10)
at D:\..\sg-backend\app\common\db\writeToDB.ts:113:6
at Generator.next (<anonymous>)
at fulfilled (D:\..\sg-backend\app\common\db\writeToDB.ts:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
D:\..\sg-backend\node_modules\src\sqlite-core\query-builders\insert.ts:53
throw new Error('values() must be called with at least one value');
^
Error: values() must be called with at least one value
at SQLiteInsertBuilder.values (D:\..\sg-backend\node_modules\src\sqlite-core\query-builders\insert.ts:53:10)
at D:\..\sg-backend\app\common\db\writeToDB.ts:113:6
at Generator.next (<anonymous>)
at fulfilled (D:\..\sg-backend\app\common\db\writeToDB.ts:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
The only other solution i can think of is to read existing values from db if insertedProducts.length === 0
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server