Skip to content

Open source personal finance application

fix(convex): paginate cascade deletes to prevent mutation limit failures

Ilya Komichev61643e8b3e3e
diff --git a/app/components/categories/useCategoriesStore.test.ts b/app/components/categories/useCategoriesStore.test.ts
index 8ed0d0eb..6aadb300 100644
--- a/app/components/categories/useCategoriesStore.test.ts
+++ b/app/components/categories/useCategoriesStore.test.ts
@@ -16,6 +16,7 @@ vi.mock('~/components/categories/utils', () => ({
}))
const mutationMock = vi.fn(() => Promise.resolve())
+const actionMock = vi.fn(() => Promise.resolve())
const onUpdateMock = vi.fn()
vi.stubGlobal('useConvexClientWithApi', () => ({
@@ -23,6 +24,7 @@ vi.stubGlobal('useConvexClientWithApi', () => ({
categories: { create: 'categories.create', list: 'categories.list', remove: 'categories.remove', update: 'categories.update', updateWithChildren: 'categories.updateWithChildren' },
},
client: {
+ action: actionMock,
mutation: mutationMock,
onUpdate: onUpdateMock,
},
@@ -76,6 +78,7 @@ describe('useCategoriesStore', () => {
await localforage.clear()
vi.clearAllMocks()
mutationMock.mockReturnValue(Promise.resolve())
+ actionMock.mockReturnValue(Promise.resolve())
})
afterEach(() => {
@@ -311,7 +314,7 @@ describe('useCategoriesStore', () => {
store.deleteCategory('c1')
- expect(mutationMock).toHaveBeenCalledWith('categories.remove', { id: 'c1' })
+ expect(actionMock).toHaveBeenCalledWith('categories.remove', { id: 'c1' })
})
it('cleans up offline queue on mutation success', async () => {
@@ -330,13 +333,13 @@ describe('useCategoriesStore', () => {
store.deleteCategory('transfer')
- expect(mutationMock).not.toHaveBeenCalled()
+ expect(actionMock).not.toHaveBeenCalled()
expect(offlineHelpers.pushOfflineOp).not.toHaveBeenCalled()
})
- it('shows toast on delete mutation failure', async () => {
+ it('shows toast on failure', async () => {
const { toastAddMock } = await import('~/test-utils/setup-store')
- mutationMock.mockReturnValue(Promise.reject(new Error('Server error')))
+ actionMock.mockReturnValue(Promise.reject(new Error('Server error')))
const store = useCategoriesStore()
store.items = { ...store.items, c1: makeCategory() }
diff --git a/app/components/categories/useCategoriesStore.ts b/app/components/categories/useCategoriesStore.ts
index 437b6418..bf10c7f9 100644
--- a/app/components/categories/useCategoriesStore.ts
+++ b/app/components/categories/useCategoriesStore.ts
@@ -342,7 +342,7 @@ export const useCategoriesStore = defineStore('categories', (): CategoriesStore
errorMessage: 'categories.errors.deleteFailed',
id,
items,
- mutation: client.mutation(api.categories.remove, { id: asConvexId<'categories'>(id) }),
+ mutation: client.action(api.categories.remove, { id: asConvexId<'categories'>(id) }),
})
}
diff --git a/app/components/wallets/useWalletsStore.test.ts b/app/components/wallets/useWalletsStore.test.ts
index 7d11a9cc..6365de15 100644
--- a/app/components/wallets/useWalletsStore.test.ts
+++ b/app/components/wallets/useWalletsStore.test.ts
@@ -15,6 +15,7 @@ vi.mock('~~/utils/simple', () => ({
}))
const mutationMock = vi.fn(() => Promise.resolve())
+const actionMock = vi.fn(() => Promise.resolve())
const onUpdateMock = vi.fn()
vi.stubGlobal('useConvexClientWithApi', () => ({
@@ -22,6 +23,7 @@ vi.stubGlobal('useConvexClientWithApi', () => ({
wallets: { create: 'wallets.create', list: 'wallets.list', remove: 'wallets.remove', update: 'wallets.update', updateOrder: 'wallets.updateOrder' },
},
client: {
+ action: actionMock,
mutation: mutationMock,
onUpdate: onUpdateMock,
},
@@ -93,6 +95,7 @@ describe('useWalletsStore', () => {
await localforage.clear()
vi.clearAllMocks()
mutationMock.mockReturnValue(Promise.resolve())
+ actionMock.mockReturnValue(Promise.resolve())
})
afterEach(() => {
@@ -224,7 +227,7 @@ describe('useWalletsStore', () => {
store.deleteWallet('w1')
- expect(mutationMock).toHaveBeenCalledWith('wallets.remove', { id: 'w1' })
+ expect(actionMock).toHaveBeenCalledWith('wallets.remove', { id: 'w1' })
})
it('cleans up offline queue on mutation success', async () => {
@@ -247,9 +250,9 @@ describe('useWalletsStore', () => {
expect(removeTrnsFromStoreMock).toHaveBeenCalledWith(['trn1', 'trn2'])
})
- it('shows toast on delete mutation failure', async () => {
+ it('shows toast on failure', async () => {
const { toastAddMock } = await import('~/test-utils/setup-store')
- mutationMock.mockReturnValue(Promise.reject(new Error('network')))
+ actionMock.mockReturnValue(Promise.reject(new Error('network')))
const store = useWalletsStore()
store.items = { w1: makeWallet() }
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index a5210dfa..d145d3af 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -208,7 +208,7 @@ export const useWalletsStore = defineStore('wallets', () => {
errorMessage: 'wallets.errors.deleteFailed',
id,
items,
- mutation: client.mutation(api.wallets.remove, { id: asConvexId<'wallets'>(id) }),
+ mutation: client.action(api.wallets.remove, { id: asConvexId<'wallets'>(id) }),
})
}
diff --git a/convex/categories.ts b/convex/categories.ts
index 7ff91e1e..79e533d8 100644
--- a/convex/categories.ts
+++ b/convex/categories.ts
@@ -3,7 +3,8 @@ import { v } from 'convex/values'
import type { Id } from './_generated/dataModel'
import type { MutationCtx } from './_generated/server'
-import { mutation, query } from './_generated/server'
+import { internal } from './_generated/api'
+import { action, internalMutation, internalQuery, mutation, query } from './_generated/server'
import { getAuthUser, getOwnEntity, requireAuthUser } from './shared'
import { removeTrnsFromHash } from './trnsHash'
@@ -169,37 +170,86 @@ export const update = mutation({
},
})
-export const remove = mutation({
- args: { id: v.id('categories') },
- handler: async (ctx, { id }) => {
- const user = await requireAuthUser(ctx)
- const category = await getOwnEntity(ctx, id, user._id)
-
+export const validateCategoryRemoval = internalQuery({
+ args: { id: v.id('categories'), userId: v.string() },
+ handler: async (ctx, { id, userId }) => {
+ const category = await ctx.db.get(id)
+ if (!category || category.userId !== userId)
+ throw new Error('Not found')
if (category.childIds?.length)
throw new Error('Cannot delete category with children')
+ },
+})
+
+export const removeTrnsByCategoryPage = internalMutation({
+ args: {
+ categoryId: v.id('categories'),
+ cursor: v.union(v.string(), v.null()),
+ userId: v.string(),
+ },
+ handler: async (ctx, { categoryId, cursor, userId }) => {
+ const page = await ctx.db
+ .query('trns')
+ .withIndex('by_user_category', q => q.eq('userId', userId).eq('categoryId', categoryId))
+ .paginate({ cursor, numItems: 500 })
- // Cascade delete all trns in this category (paginated)
const deletedIds: string[] = []
+ for (const trn of page.page) {
+ await ctx.db.delete(trn._id)
+ deletedIds.push(trn._id)
+ }
+
+ if (deletedIds.length)
+ await removeTrnsFromHash(ctx, userId, deletedIds)
+
+ return { continueCursor: page.continueCursor, isDone: page.isDone }
+ },
+})
+
+export const removeCategoryFinalize = internalMutation({
+ args: {
+ categoryId: v.id('categories'),
+ userId: v.string(),
+ },
+ handler: async (ctx, { categoryId, userId }) => {
+ const category = await ctx.db.get(categoryId)
+ if (!category || category.userId !== userId)
+ return
+ await removeFromParentChildIds(ctx, categoryId, category.parentId, userId)
+ await ctx.db.delete(categoryId)
+ },
+})
+
+export const remove = action({
+ args: { id: v.id('categories') },
+ handler: async (ctx, { id }) => {
+ const user = await ctx.runQuery(internal.userSettings.getCurrentUser)
+ if (!user)
+ throw new Error('Unauthorized')
+
+ // Validate before cascade delete (ownership + no children)
+ await ctx.runQuery(internal.categories.validateCategoryRemoval, {
+ id,
+ userId: user._id,
+ })
+
+ // Paginated trn deletion
let cursor: string | null = null
let isDone = false
while (!isDone) {
- const page = await ctx.db
- .query('trns')
- .withIndex('by_user_category', q => q.eq('userId', user._id).eq('categoryId', id))
- .paginate({ cursor, numItems: 500 })
- for (const trn of page.page) {
- await ctx.db.delete(trn._id)
- deletedIds.push(trn._id)
- }
- isDone = page.isDone
- cursor = page.continueCursor
+ const result = await ctx.runMutation(internal.categories.removeTrnsByCategoryPage, {
+ categoryId: id,
+ cursor,
+ userId: user._id,
+ })
+ isDone = result.isDone
+ cursor = result.continueCursor
}
- if (deletedIds.length)
- await removeTrnsFromHash(ctx, user._id, deletedIds)
-
- await removeFromParentChildIds(ctx, id, category.parentId, user._id)
- await ctx.db.delete(id)
+ await ctx.runMutation(internal.categories.removeCategoryFinalize, {
+ categoryId: id,
+ userId: user._id,
+ })
},
})
diff --git a/convex/wallets.ts b/convex/wallets.ts
index a1437046..3c1d9de3 100644
--- a/convex/wallets.ts
+++ b/convex/wallets.ts
@@ -1,6 +1,7 @@
import { v } from 'convex/values'
-import { mutation, query } from './_generated/server'
+import { internal } from './_generated/api'
+import { action, internalMutation, mutation, query } from './_generated/server'
import { getAuthUser, getOwnEntity, requireAuthUser, walletTypeValidator } from './shared'
import { removeTrnsFromHash } from './trnsHash'
@@ -84,48 +85,77 @@ export const updateOrder = mutation({
},
})
-export const remove = mutation({
- args: { id: v.id('wallets') },
- handler: async (ctx, { id }) => {
- const user = await requireAuthUser(ctx)
- const userId = user._id
- await getOwnEntity(ctx, id, userId)
+export const removeTrnsByWalletPage = internalMutation({
+ args: {
+ cursor: v.union(v.string(), v.null()),
+ indexName: v.union(
+ v.literal('by_user_wallet'),
+ v.literal('by_user_expenseWallet'),
+ v.literal('by_user_incomeWallet'),
+ ),
+ userId: v.string(),
+ walletId: v.id('wallets'),
+ },
+ handler: async (ctx, { cursor, indexName, userId, walletId }) => {
+ const page = await ctx.db
+ .query('trns')
+ .withIndex(indexName, q => q.eq('userId', userId).eq(
+ indexName === 'by_user_wallet' ? 'walletId' : indexName === 'by_user_expenseWallet' ? 'expenseWalletId' : 'incomeWalletId',
+ walletId,
+ ))
+ .paginate({ cursor, numItems: 500 })
const deletedIds: string[] = []
- const deletedSet = new Set<string>()
+ for (const trn of page.page) {
+ await ctx.db.delete(trn._id)
+ deletedIds.push(trn._id)
+ }
+
+ if (deletedIds.length)
+ await removeTrnsFromHash(ctx, userId, deletedIds)
- // Paginated helper to delete trns by index
- async function deleteTrnsByIndex(indexName: 'by_user_wallet' | 'by_user_expenseWallet' | 'by_user_incomeWallet', fieldValue: typeof id) {
+ return { continueCursor: page.continueCursor, isDone: page.isDone }
+ },
+})
+
+export const removeWalletFinalize = internalMutation({
+ args: {
+ userId: v.string(),
+ walletId: v.id('wallets'),
+ },
+ handler: async (ctx, { userId, walletId }) => {
+ const wallet = await ctx.db.get(walletId)
+ if (wallet && wallet.userId === userId)
+ await ctx.db.delete(walletId)
+ },
+})
+
+export const remove = action({
+ args: { id: v.id('wallets') },
+ handler: async (ctx, { id }) => {
+ const user = await ctx.runQuery(internal.userSettings.getCurrentUser)
+ if (!user)
+ throw new Error('Unauthorized')
+
+ const indexes = ['by_user_wallet', 'by_user_expenseWallet', 'by_user_incomeWallet'] as const
+ for (const indexName of indexes) {
let cursor: string | null = null
let isDone = false
while (!isDone) {
- const page = await ctx.db
- .query('trns')
- .withIndex(indexName, q => q.eq('userId', userId).eq(
- indexName === 'by_user_wallet' ? 'walletId' : indexName === 'by_user_expenseWallet' ? 'expenseWalletId' : 'incomeWalletId',
- fieldValue,
- ))
- .paginate({ cursor, numItems: 500 })
- for (const trn of page.page) {
- if (deletedSet.has(trn._id))
- continue
- await ctx.db.delete(trn._id)
- deletedIds.push(trn._id)
- deletedSet.add(trn._id)
- }
- isDone = page.isDone
- cursor = page.continueCursor
+ const result = await ctx.runMutation(internal.wallets.removeTrnsByWalletPage, {
+ cursor,
+ indexName,
+ userId: user._id,
+ walletId: id,
+ })
+ isDone = result.isDone
+ cursor = result.continueCursor
}
}
- // Delete regular trns and transfers referencing this wallet
- await deleteTrnsByIndex('by_user_wallet', id)
- await deleteTrnsByIndex('by_user_expenseWallet', id)
- await deleteTrnsByIndex('by_user_incomeWallet', id)
-
- await ctx.db.delete(id)
-
- if (deletedIds.length)
- await removeTrnsFromHash(ctx, userId, deletedIds)
+ await ctx.runMutation(internal.wallets.removeWalletFinalize, {
+ userId: user._id,
+ walletId: id,
+ })
},
})