cascade delets
This commit is contained in:
parent
79d13fdfac
commit
e2cba48f0d
2 changed files with 44 additions and 0 deletions
|
|
@ -29,6 +29,18 @@ export default ts.config(
|
|||
argsIgnorePattern: '^_',
|
||||
},
|
||||
],
|
||||
'no-restricted-imports': [
|
||||
'error',
|
||||
{
|
||||
patterns: [
|
||||
{
|
||||
group: ['*/_generated/server'],
|
||||
importNames: ['mutation', 'internalMutation'],
|
||||
message: 'Use functions.ts for mutation',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
|||
32
src/lib/backend/convex/functions.ts
Normal file
32
src/lib/backend/convex/functions.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import {
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
internalMutation as rawInternalMutation,
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
mutation as rawMutation,
|
||||
} from './_generated/server';
|
||||
|
||||
import { customCtx, customMutation } from 'convex-helpers/server/customFunctions';
|
||||
import { Triggers } from 'convex-helpers/server/triggers';
|
||||
import { DataModel } from './_generated/dataModel';
|
||||
|
||||
const triggers = new Triggers<DataModel>();
|
||||
|
||||
// Cascade delete messages when a conversation is deleted
|
||||
triggers.register('conversations', async (ctx, change) => {
|
||||
if (change.operation === 'delete') {
|
||||
const query = ctx.db
|
||||
.query('messages')
|
||||
.withIndex('by_conversation', (q) => q.eq('conversation_id', change.id));
|
||||
|
||||
for await (const message of query) {
|
||||
await ctx.db.delete(message._id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Cascade delete rules when a user is deleted
|
||||
|
||||
// create wrappers that replace the built-in `mutation` and `internalMutation`
|
||||
// the wrappers override `ctx` so that `ctx.db.insert`, `ctx.db.patch`, etc. run registered trigger functions
|
||||
export const mutation = customMutation(rawMutation, customCtx(triggers.wrapDB));
|
||||
export const internalMutation = customMutation(rawInternalMutation, customCtx(triggers.wrapDB));
|
||||
Loading…
Add table
Reference in a new issue