X
Xata4mo ago
AD Amorim

how to make am update request without returning Xata metadata?

The code below calls: const proj = await xata.db.project.update(id, data); I would like to remove the metadata that comes from it in order to avoid parsing errors from zod at: project.parse(proj) Is that possible? I know we can do this when getting data but not sure how the update transaction does that.
export const updateProject = async (
id: ProjectRecord["id"],
data: Partial<Project>
) => {
// If data validation fails, return errors early. Otherwise, continue.
const validatedFields = project.omit({ id: true, clockify_id: true }).safeParse(data);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
message: "Missing Fields. Failed to Update Project.",
};
}

try {
const proj = await xata.db.project.update(id, data);

return project.parse(proj);
} catch (error) {
console.error("Failed to Update Project.", error);
// If a database error occurs, return a more specific error.
return {
errors: error,
message: "Database Error: Failed to Update Project.",
};
}
};
export const updateProject = async (
id: ProjectRecord["id"],
data: Partial<Project>
) => {
// If data validation fails, return errors early. Otherwise, continue.
const validatedFields = project.omit({ id: true, clockify_id: true }).safeParse(data);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
message: "Missing Fields. Failed to Update Project.",
};
}

try {
const proj = await xata.db.project.update(id, data);

return project.parse(proj);
} catch (error) {
console.error("Failed to Update Project.", error);
// If a database error occurs, return a more specific error.
return {
errors: error,
message: "Database Error: Failed to Update Project.",
};
}
};
4 Replies
AD Amorim
AD Amorim4mo ago
Never mind, I have installed the last version of the SDK and it now works:
const proj = await xata.db.project.update(id, data, ["*", "client_id.name"]);
const proj = await xata.db.project.update(id, data, ["*", "client_id.name"]);
kostas
kostas4mo ago
Right, an array with column names to be returned can be passed as a parameter to the update call - just like you showed. I don't think there is a way to exclude the xata metadata though as that is returned by default even when specifying columns to return. Glad that things worked though. Were you able to handle the response including the xata metadata?
AD Amorim
AD Amorim4mo ago
yep the issue ended up not being related to the Xata metadata. It was because client_id was returning only the object with its id inside... and zod was validating id + name.... So, when I got the name back it sorted the issue. Also, returning
project.parse(proj)
project.parse(proj)
is enough to remove xata metadata in the frontend code.
kostas
kostas4mo ago
Very nice, thank you for sharing
Want results from more Discord servers?
Add your server