Yes, you can add a count column to your table and increment it each time you perform an upsert operation. In the onConflictDoUpdate function, you can take the current value of the count column and increment it by 1.
Here's an example of how you can do it:
import { defineTable, column, sql } from 'astro:db';
const UserProgrammingLanguage = defineTable({
columns: {
userId: column.number({ references: () => User.columns.id }),
programmingLanguageId: column.number({ references: () => ProgrammingLanguage.columns.id }),
count: column.number({ default: 0 }),
},
indexes: {
user_programming_language_idx: {
on: ['userId', 'programmingLanguageId'],
unique: true,
},
},
});
await UserProgrammingLanguage.upsert({
userId: 1,
programmingLanguageId: 2,
count: 1,
}, {
onConflictDoUpdate: {
set: {
count: sql`${UserProgrammingLanguage.columns.count} + 1`,
},
},
});
In this example, the UserProgrammingLanguage table has a count column that defaults to 0. When you perform an upsert operation, if a conflict occurs (i.e., a row with the same userId and programmingLanguageId already exists), the count column is incremented by 1.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: