Skip to content

Commit 73fc3c6

Browse files
authored
perf(drizzle): remove unnecessary db.select call in updateOne operation (#11847)
This will improve performance when updating a single document in postgres/drizzle, if the ID is known. Previously, this resulted in 2 sequential operations: - `db.select `to fetch the document by the ID - `upsertRow` to update the document (multiple db operations) This PR removes the unnecessary `db.select` call, as the document ID is already known
1 parent 7fb4b13 commit 73fc3c6

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

packages/drizzle/src/updateOne.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,51 @@ export const updateOne: UpdateOne = async function updateOne(
1919
joins: joinQuery,
2020
locale,
2121
req,
22+
returning,
2223
select,
2324
where: whereArg,
24-
returning,
2525
},
2626
) {
2727
const db = await getTransaction(this, req)
2828
const collection = this.payload.collections[collectionSlug].config
2929
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))
30-
const whereToUse = whereArg || { id: { equals: id } }
3130
let idToUpdate = id
3231

33-
const { joins, selectFields, where } = buildQuery({
34-
adapter: this,
35-
fields: collection.flattenedFields,
36-
locale,
37-
tableName,
38-
where: whereToUse,
39-
})
32+
if (!idToUpdate) {
33+
const { joins, selectFields, where } = buildQuery({
34+
adapter: this,
35+
fields: collection.flattenedFields,
36+
locale,
37+
tableName,
38+
where: whereArg,
39+
})
4040

41-
// selectDistinct will only return if there are joins
42-
const selectDistinctResult = await selectDistinct({
43-
adapter: this,
44-
chainedMethods: [{ args: [1], method: 'limit' }],
45-
db,
46-
joins,
47-
selectFields,
48-
tableName,
49-
where,
50-
})
41+
// selectDistinct will only return if there are joins
42+
const selectDistinctResult = await selectDistinct({
43+
adapter: this,
44+
chainedMethods: [{ args: [1], method: 'limit' }],
45+
db,
46+
joins,
47+
selectFields,
48+
tableName,
49+
where,
50+
})
5151

52-
if (selectDistinctResult?.[0]?.id) {
53-
idToUpdate = selectDistinctResult?.[0]?.id
54-
// If id wasn't passed but `where` without any joins, retrieve it with findFirst
55-
} else if (whereArg && !joins.length) {
56-
const table = this.tables[tableName]
52+
if (selectDistinctResult?.[0]?.id) {
53+
idToUpdate = selectDistinctResult?.[0]?.id
54+
// If id wasn't passed but `where` without any joins, retrieve it with findFirst
55+
} else if (whereArg && !joins.length) {
56+
const table = this.tables[tableName]
5757

58-
const docsToUpdate = await (db as LibSQLDatabase)
59-
.select({
60-
id: table.id,
61-
})
62-
.from(table)
63-
.where(where)
64-
.limit(1)
65-
idToUpdate = docsToUpdate?.[0]?.id
58+
const docsToUpdate = await (db as LibSQLDatabase)
59+
.select({
60+
id: table.id,
61+
})
62+
.from(table)
63+
.where(where)
64+
.limit(1)
65+
idToUpdate = docsToUpdate?.[0]?.id
66+
}
6667
}
6768

6869
const result = await upsertRow({
@@ -71,12 +72,12 @@ export const updateOne: UpdateOne = async function updateOne(
7172
data,
7273
db,
7374
fields: collection.flattenedFields,
75+
ignoreResult: returning === false,
7476
joinQuery,
7577
operation: 'update',
7678
req,
7779
select,
7880
tableName,
79-
ignoreResult: returning === false,
8081
})
8182

8283
if (returning === false) {

0 commit comments

Comments
 (0)