Skip to content

Commit

Permalink
add OnConflictUpdateTemplateSet
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexErrant committed Dec 1, 2024
1 parent 05b9c0a commit 153e7f4
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions app/src/sqlite/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ export const templateCollectionMethods = {
upsertTemplate: async function (template: Template) {
await tx(async (tx) => {
const { insertTemplate, remoteTemplates } = templateToDocType(template)
const conflictValues = {
...insertTemplate,
id: undefined,
created: undefined,
}
await tx
.insertInto('template')
.values(insertTemplate)
.onConflict((db) => db.doUpdateSet(conflictValues))
.onConflict((db) =>
db.doUpdateSet({
name: (x) => x.ref('excluded.name'),
css: (x) => x.ref('excluded.css'),
fields: (x) => x.ref('excluded.fields'),
templateType: (x) => x.ref('excluded.templateType'),
edited: (x) => x.ref('excluded.edited'),
ankiId: (x) => x.ref('excluded.ankiId'),
} satisfies OnConflictUpdateTemplateSet),
)
.execute()
const oldRts = await tx
.selectFrom('remoteTemplate')
Expand Down Expand Up @@ -503,3 +507,15 @@ type OnConflictUpdateRemoteTemplateSet = {
>,
) => unknown
}

// The point of this type is to cause an error if something is added to Template
// If that happens, you probably want to update the `doUpdateSet` call.
// If not, you an add an exception to the Exclude below.
type OnConflictUpdateTemplateSet = {
[K in keyof TemplateEntity as Exclude<K, 'id' | 'created'>]: (
x: ExpressionBuilder<
OnConflictDatabase<DB, 'template'>,
OnConflictTables<'template'>
>,
) => unknown
}

0 comments on commit 153e7f4

Please sign in to comment.