Gorka Cesium
Gorka Cesium2y ago

How to use args instead of Doc type in a mutation?

Currently I'm doing this with a type params so I can type the employee arg.
import { WithoutSystemFields } from 'convex/server';
import { Doc } from './_generated/dataModel';
import { mutation } from './_generated/server';

type params = {
employee: WithoutSystemFields<Doc<'employees'>>;
};
export default mutation(async ({ db }, { employee }: params) => {
try {
db.insert('employees', employee);
} catch (e) {
console.log(e);
}
});
import { WithoutSystemFields } from 'convex/server';
import { Doc } from './_generated/dataModel';
import { mutation } from './_generated/server';

type params = {
employee: WithoutSystemFields<Doc<'employees'>>;
};
export default mutation(async ({ db }, { employee }: params) => {
try {
db.insert('employees', employee);
} catch (e) {
console.log(e);
}
});
How can I do this with the new argument validation https://docs.convex.dev/functions/args-validation ?
Argument Validation | Convex Developer Hub
Argument validators ensure that queries,
2 Replies
sshader
sshader2y ago
I think you could try something like
export const messages = v.object({
author: v.string(),
body: v.string(),
});

export default defineSchema({
messages: defineTable(messages),
});
export const messages = v.object({
author: v.string(),
body: v.string(),
});

export default defineSchema({
messages: defineTable(messages),
});
in your schema and then
import { messages } from "./schema";
import { mutation } from "./_generated/server";

export default mutation({
args: {
message: messages,
},
import { messages } from "./schema";
import { mutation } from "./_generated/server";

export default mutation({
args: {
message: messages,
},
in the argument validator? Both the schema + your arg validator would be sharing the same definition, so all the types should stay in sync?
Gorka Cesium
Gorka CesiumOP2y ago
oh ok, i see. I'll try it, thanks. would be good to add it to the docs

Did you find this page helpful?