makrdev
makrdev
CCConvex Community
Created by makrdev on 12/24/2024 in #general
makrdev's Thread
There was an error in one of the cascade triggers, and it prevented all of them deleting other resources. Is that what you mean by 'transactional'?
8 replies
CCConvex Community
Created by makrdev on 12/24/2024 in #general
makrdev's Thread
@lee Hey Lee, what do you think about the solution I shared above?
8 replies
CCConvex Community
Created by makrdev on 12/24/2024 in #general
makrdev's Thread
Solution:
// Cascade delete all files when a project is deleted
triggers.register("projects", async (ctx, change) => {
if (change.operation === "delete") {
// Get files with pagination
const { continueCursor, page } = await ctx.db
.query("files")
.withIndex("by_project_id", (q) => q.eq("projectId", change.id))
.paginate({ numItems: 1000, cursor: null });

// Delete files
await asyncMap(page, (file) => ctx.db.delete(file._id));

// Continue deleting files if there are more
if (continueCursor) {
await ctx.scheduler.runAfter(0, internal.helpers.batch.batchDeleteFiles, {
projectId: change.id,
cursor: continueCursor,
numItems: 1000,
});
}
}
});

export const batchDeleteFiles = internalMutation({
args: {
cursor: v.string(),
projectId: v.id("projects"),
numItems: v.number(),
},
handler: async (ctx, { projectId, cursor, numItems }) => {
// Get the files
const { page, continueCursor } = await ctx.db
.query("files")
.withIndex("by_project_id", (q) => q.eq("projectId", projectId))
.paginate({
numItems,
cursor,
});

// Delete files
await asyncMap(page, (file) => ctx.db.delete(file._id));

if (continueCursor) {
await ctx.scheduler.runAfter(0, internal.helpers.batch.batchDeleteFiles, {
projectId,
cursor: continueCursor,
numItems,
});
}
},
});
// Cascade delete all files when a project is deleted
triggers.register("projects", async (ctx, change) => {
if (change.operation === "delete") {
// Get files with pagination
const { continueCursor, page } = await ctx.db
.query("files")
.withIndex("by_project_id", (q) => q.eq("projectId", change.id))
.paginate({ numItems: 1000, cursor: null });

// Delete files
await asyncMap(page, (file) => ctx.db.delete(file._id));

// Continue deleting files if there are more
if (continueCursor) {
await ctx.scheduler.runAfter(0, internal.helpers.batch.batchDeleteFiles, {
projectId: change.id,
cursor: continueCursor,
numItems: 1000,
});
}
}
});

export const batchDeleteFiles = internalMutation({
args: {
cursor: v.string(),
projectId: v.id("projects"),
numItems: v.number(),
},
handler: async (ctx, { projectId, cursor, numItems }) => {
// Get the files
const { page, continueCursor } = await ctx.db
.query("files")
.withIndex("by_project_id", (q) => q.eq("projectId", projectId))
.paginate({
numItems,
cursor,
});

// Delete files
await asyncMap(page, (file) => ctx.db.delete(file._id));

if (continueCursor) {
await ctx.scheduler.runAfter(0, internal.helpers.batch.batchDeleteFiles, {
projectId,
cursor: continueCursor,
numItems,
});
}
},
});
8 replies
CCConvex Community
Created by erquhart on 11/6/2024 in #show-and-tell
Next.js SaaS Starter - Convex V1
Thanks for the video! I have a similar setup but couldn't deploy to Vercel so far. How to deploy Convex when it's living in packages/backend ? What did you put into build command?
30 replies
CCConvex Community
Created by makrdev on 10/9/2024 in #support-community
What’s the best way to connect Convex DB to external services?
Local Development This is another blurry area. Convex provides a dev environment for each developer on the team, and it automatically pushes local changes to this environment. That’s a great experience! But I had to watch videos and explore other resources to fully understand it. I’m still unclear on how we push changes to the production environment, how teams can collaborate on a project, and what workflows we should follow. Integrations I was happy to see that I can use Hono with Convex. Integrations like this create a sense of familiarity and help avoid the feeling of being locked into a closed environment. Also, creating content around keywords like Hono, Cloudflare Workers, Zod, etc., could attract more users through SEO and community engagement.
15 replies
CCConvex Community
Created by makrdev on 10/9/2024 in #support-community
What’s the best way to connect Convex DB to external services?
Hey Tom, I don’t think this is just a runtime clarification. I’ve been going through the documentation a lot lately (as well as other resources like Stack and the Youtube channel), and a few areas still seem a bit unclear to me: Server-to-Server Communication Convex works great in browser (client) environments, and the documentation is mainly focused on that. However, many applications involve microservices and require server-to-server communication. Since we’re using Convex as our single source of truth (for database and types, etc.), we should be able to communicate between servers in a type-safe way. When I came across the http-client, I thought, “Great, I can use this in an edge runtime.” But when I saw that it needed to be imported from convex/browser, I got a bit confused and started wondering if this might be an anti-pattern. What I’m suggesting is that the documentation and package could use more clarity and abstraction around this use case. Schema Migrations There’s not a lot of information about schema migrations in the documentation. From what I gathered from a blog post, Convex seems to handle this in a specific way. Schema migrations are a pain point for most developers, and it’s something we need to understand clearly before starting.
15 replies
CCConvex Community
Created by makrdev on 10/9/2024 in #support-community
What’s the best way to connect Convex DB to external services?
Thank you guys 🤜
15 replies
CCConvex Community
Created by makrdev on 10/9/2024 in #support-community
What’s the best way to connect Convex DB to external services?
It’s JavaScript similar to Convex runtime. I’m looking for something like an admin client that I can run on the server(edge) with type safety.
15 replies