mwarger
mwarger
CCConvex Community
Created by mwarger on 8/20/2024 in #support-community
Convex CRUD "framework" example - anyone doing this?
For reference, here's what I'm playing with:
// Utility function to create a new record with optional Zod validation
export function createEntityMutation<T extends TableNames>({
tableName,
schema,
authCallback = checkEmployeeAuth,
validationSchema,
}: {
tableName: T;
schema: any;
authCallback?: AuthCallback;
validationSchema?: ZodSchema<any>;
}) {
// Optional Zod schema for validation
return authMutation({
args: schema,
handler: async (ctx, args) => {
await authCallback(ctx, args.facilityId);

// Perform Zod validation if a schema is provided
if (validationSchema) {
validationSchema.parse(args);
}

return await ctx.db.insert(tableName, {
...args,
created_by: ctx.user._id,
created_at: new Date().toISOString(),
updated_by: ctx.user._id,
updated_at: new Date().toISOString(),
});
},
});
}

export const createSport = createEntityMutation({
tableName: SPORTS_TABLE_NAME,
schema: {
name: v.string(),
facilityId: v.id('facilities'),
description: v.optional(v.string()),
},
});
// Utility function to create a new record with optional Zod validation
export function createEntityMutation<T extends TableNames>({
tableName,
schema,
authCallback = checkEmployeeAuth,
validationSchema,
}: {
tableName: T;
schema: any;
authCallback?: AuthCallback;
validationSchema?: ZodSchema<any>;
}) {
// Optional Zod schema for validation
return authMutation({
args: schema,
handler: async (ctx, args) => {
await authCallback(ctx, args.facilityId);

// Perform Zod validation if a schema is provided
if (validationSchema) {
validationSchema.parse(args);
}

return await ctx.db.insert(tableName, {
...args,
created_by: ctx.user._id,
created_at: new Date().toISOString(),
updated_by: ctx.user._id,
updated_at: new Date().toISOString(),
});
},
});
}

export const createSport = createEntityMutation({
tableName: SPORTS_TABLE_NAME,
schema: {
name: v.string(),
facilityId: v.id('facilities'),
description: v.optional(v.string()),
},
});
You can see I don't have this fully abstracted, and I effectively have a multi-tenant setup using the facilityId - and yeah, this same type of thing could be done using the crud helper or similar utility.
11 replies
CCConvex Community
Created by mwarger on 8/20/2024 in #support-community
Convex CRUD "framework" example - anyone doing this?
I have not thought this through very deeply, except for using very basic examples for simple entities in my project - I'm sure I would hit some sort of complexity wall at some point. However, it seems like the type-safe schema (and possible reference traversal with Id<> types) would make it possible for basic use-cases (either during run-time or some type of codegen)
11 replies
CCConvex Community
Created by mwarger on 8/20/2024 in #support-community
Convex CRUD "framework" example - anyone doing this?
I have seen that, yeah. I mentioned the Table utility above, but I really should have said the crud helper. I was playing around with making different backend utilities, and the ones I used to replace the "table" ones have some built-in basic auth checks and whatnot.
I'm more curious if anyone (or convex) has considered the above, which is taking that style of convention (whether it's the built-in crud helper from convex or not) and building basic CRUD UI around it?
11 replies
CCConvex Community
Created by mwarger on 3/22/2024 in #support-community
How to filter based on a field while also using search index?
Oh, this makes total sense. Thanks! 🎉
6 replies
CCConvex Community
Created by mwarger on 3/10/2024 in #support-community
How to view current usage?
I was looking for this inside the project, my bad.
6 replies
CCConvex Community
Created by mwarger on 3/10/2024 in #support-community
How to view current usage?
Thank you!
6 replies
CCConvex Community
Created by thedevstockgirl on 2/12/2024 in #support-community
Ability to pass additional data to auth context.
I'm looking into this and it seems like I might have to also hijack another field. With something like Hasura, you can put x-hasura-role, for example, in the claims object and put whatever you want there. I spent a while wondering why my "role" field I added to my JWT claim wasn't coming over, which is something I store in my metadata. I would definitely like to see the ability to customize this somehow.
20 replies