Temporary changes table
Merge new entries from a temporary changes table:
const result = await db
.mergeInto('wine as target')
.using(
'wine_stock_change as source',
'source.wine_name',
'target.name',
)
.whenNotMatchedAnd('source.stock_delta', '>', 0)
.thenInsertValues(({ ref }) => ({
name: ref('source.wine_name'),
stock: ref('source.stock_delta'),
}))
.whenMatchedAnd(
(eb) => eb('target.stock', '+', eb.ref('source.stock_delta')),
'>',
0,
)
.thenUpdateSet('stock', (eb) =>
eb('target.stock', '+', eb.ref('source.stock_delta')),
)
.whenMatched()
.thenDelete()
.executeTakeFirstOrThrow()More examples
The API documentation is packed with examples. The API docs are hosted here, but you can access the same documentation by hovering over functions/methods/classes in your IDE. The examples are always just one hover away!
For example, check out these sections: