OscarO
Convex Community5mo ago
11 replies
Oscar

Redundant typing

Hey noob question here

I have a table definition here :
//schema.ts
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
import { authTables } from "@convex-dev/auth/server";

export default defineSchema({
  ...authTables,
  users: defineTable({
    name: v.string(),
    email: v.string(),
    phone: v.string(),
    company: v.optional(v.string()),
    createdAt: v.number(),
  }).index("by_email", ["email"]),
  orders: defineTable({
    orderId: v.id("orders"),
    clientEmail: v.string(),
    price: v.number() ?? v.string(),
    createdAt: v.number(),
  }).index("by_user", ["orderId"]),
});


as you can see, I define the type of price here. In another file :
//orderFunctions.ts
import { mutation, query } from "./_generated/server";
import { v } from "convex/values";

export const addOrder = mutation({
  args: {
    clientEmail: v.string(),
    price: v.optional(v.string() ?? v.number()),
  },
  handler: async (ctx, args) => {
    const userId = await ctx.db.insert("orders", {
      clientEmail: args.clientEmail,
      price: args.price ?? "no price given",
      createdAt: Date.now() ?? "no date given",
    });
    return userId;
  },
});

export const getOrders = query({
  args: {},
  handler: async (ctx) => {
    return await ctx.db.query("orders").collect();
  },
});


As you can see I am also defining types of args here, and setting values accordingly in "insert" part.


My question is : since I'm already defining types when doing the tables, can I use these types in the args ? so that I don't have to copy/paste types between table and args ?
Was this page helpful?