"onConflictDoUpdate" fails with error in Array.flatMap

I am trying to run an upsert function, and update the values if it already exists. Now I might be completely in the wrong with the code. I have made a base "repository" class that has the upsert function simplified as follows:
protected async upsert(
data: typeof this.schema.$inferInsert | typeof this.schema.$inferInsert[]
){
return await this.repository.insert(this.schema).values(data).onConflictDoUpdate({
target: this.schema.id,
set: data
});
}
protected async upsert(
data: typeof this.schema.$inferInsert | typeof this.schema.$inferInsert[]
){
return await this.repository.insert(this.schema).values(data).onConflictDoUpdate({
target: this.schema.id,
set: data
});
}
now I have tried the upsert in a loop for the array is the data happens to be an array, did not do anything, getting the same error.
Cannot read properties of undefined (reading 'name')
at file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:921
at Array.flatMap (<anonymous>)
at UDe.buildUpdateSet (file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:848)
at QueryPromise.onConflictDoUpdate (file:///var/task/shared/modules/syncer/handlers/sync.mjs:723:85175)
at $De.upsert (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:3003)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async $De.insertScan (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:6785)
Cannot read properties of undefined (reading 'name')
at file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:921
at Array.flatMap (<anonymous>)
at UDe.buildUpdateSet (file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:848)
at QueryPromise.onConflictDoUpdate (file:///var/task/shared/modules/syncer/handlers/sync.mjs:723:85175)
at $De.upsert (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:3003)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async $De.insertScan (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:6785)
2 Replies
Lostra
Lostra6mo ago
Update: A simple update function fails with the same This piece of code to update works (I know there's lots of duplicates)
protected async updateExisting(
data: typeof this.schema.$inferInsert | (typeof this.schema.$inferInsert)[],
): Promise<T[] | undefined> {
if (Array.isArray(data)) {
const returnArray: (typeof this.schema.$inferSelect)[] = [];
for (const dataEntry of data) {
const result = await this.repository
.update(this.schema)
.set(dataEntry)
.where(eq(this.schema.id, dataEntry.id))
.returning();
returnArray.push(result);
}

return returnArray as T[] | undefined;
}
// ... same for a single entry
}
protected async updateExisting(
data: typeof this.schema.$inferInsert | (typeof this.schema.$inferInsert)[],
): Promise<T[] | undefined> {
if (Array.isArray(data)) {
const returnArray: (typeof this.schema.$inferSelect)[] = [];
for (const dataEntry of data) {
const result = await this.repository
.update(this.schema)
.set(dataEntry)
.where(eq(this.schema.id, dataEntry.id))
.returning();
returnArray.push(result);
}

return returnArray as T[] | undefined;
}
// ... same for a single entry
}
The idea here is that it's taking a looooooong time to do a select for the ID, check if it's undefined or not then insert/update based on that.
Icemourne
Icemourne6mo ago
you can do it with some raw sql
set: {
columnName: sql`excluded.columnName`,
},
set: {
columnName: sql`excluded.columnName`,
},
this is for postgres probably would be similar/same on others had same problem
Want results from more Discord servers?
Add your server