oscklm
oscklm•16mo ago

Data in db.patch() gets overwritten, even though its not defined?

Hey, when using db.patch() and passing in the args i wanna patch from, one of my fields in this case thumbnailId and thumbnailUrl gets removed? Even though they arent defined, so i would expect that they get ignored? I might be overlooking something very obvious, but have been tryna fix this for a bit now without luck.
export const updateDetails = mutation({
args: {
videoId: v.id('videos'),
title: v.optional(v.string()),
description: v.optional(v.string()),
tags: videoSchema.tags,
category: videoSchema.category,
},
handler: async (ctx, args) => {
return await ctx.db.patch(args.videoId, {
snippet: {
title: args.title,
description: args.description,
},
tags: args.tags,
category: args.category,
})
},
})
export const updateDetails = mutation({
args: {
videoId: v.id('videos'),
title: v.optional(v.string()),
description: v.optional(v.string()),
tags: videoSchema.tags,
category: videoSchema.category,
},
handler: async (ctx, args) => {
return await ctx.db.patch(args.videoId, {
snippet: {
title: args.title,
description: args.description,
},
tags: args.tags,
category: args.category,
})
},
})
3 Replies
ian
ian•15mo ago
Are the fields in the snippet? A year ago we did a deep patch, but that had pretty unexpected consequences, so the behavior is now (and has been since 1.0 at least) that it's a shallow patch. It only replaces the top-level fields. You can safely (and with the same performance) do:
const video = await ctx.db.get(args.videoId);
video.snippet.title = args.title;
video.snippet.description = args.description
video.tags = args.tags;
video.category = args.category;
await ctx.db.replace(video._id, video);
const video = await ctx.db.get(args.videoId);
video.snippet.title = args.title;
video.snippet.description = args.description
video.tags = args.tags;
video.category = args.category;
await ctx.db.replace(video._id, video);
oscklm
oscklmOP•15mo ago
Thanks for the heads up! I was un aware what shallow patch meant exactly, but makes sense now. I'm assuming this is also a valid approach performance wise then?:
export const updateDetails = mutation({
args: {
videoId: v.id('videos'),
title: v.optional(v.string()),
description: v.optional(v.string()),
tags: videoSchema.tags,
category: videoSchema.category,
},
handler: async (ctx, args) => {
const video = await ctx.db.get(args.videoId)
return await ctx.db.patch(args.videoId, {
snippet: {
...video?.snippet,
title: args.title,
description: args.description,
},
tags: args.tags,
category: args.category,
})
},
})
export const updateDetails = mutation({
args: {
videoId: v.id('videos'),
title: v.optional(v.string()),
description: v.optional(v.string()),
tags: videoSchema.tags,
category: videoSchema.category,
},
handler: async (ctx, args) => {
const video = await ctx.db.get(args.videoId)
return await ctx.db.patch(args.videoId, {
snippet: {
...video?.snippet,
title: args.title,
description: args.description,
},
tags: args.tags,
category: args.category,
})
},
})
ian
ian•15mo ago
Yes, that works too. 🎂

Did you find this page helpful?