Skip to content

Commit 20dad8c

Browse files
committed
doc
1 parent 72fe460 commit 20dad8c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ export const authClient = createClient<DataModel, typeof schema>({
6060
schema,
6161
triggers: {
6262
user: {
63+
beforeCreate: async (_ctx, data) => {
64+
// Ensure every user has a username, filling in a simple fallback
65+
const username =
66+
data.username?.trim() ||
67+
data.email?.split('@')[0] ||
68+
`user-${Date.now()}`;
69+
70+
return {
71+
...data,
72+
username,
73+
};
74+
},
6375
onCreate: async (ctx, user) => {
6476
// Direct access to your database
6577
// Example: Create personal organization
@@ -74,6 +86,14 @@ export const authClient = createClient<DataModel, typeof schema>({
7486
personalOrganizationId: orgId,
7587
});
7688
},
89+
beforeDelete: async (ctx, user) => {
90+
// Example: clean up custom tables before removing the user
91+
if (user.personalOrganizationId) {
92+
await ctx.db.delete(user.personalOrganizationId);
93+
}
94+
95+
return user;
96+
},
7797
},
7898
session: {
7999
onCreate: async (ctx, session) => {
@@ -130,7 +150,14 @@ export const getAuth = <Ctx extends QueryCtx | MutationCtx>(ctx: Ctx) => {
130150
};
131151

132152
// 6. Export trigger handlers for Convex
133-
export const { onCreate, onDelete, onUpdate } = authClient.triggersApi();
153+
export const {
154+
beforeCreate,
155+
beforeDelete,
156+
beforeUpdate,
157+
onCreate,
158+
onDelete,
159+
onUpdate,
160+
} = authClient.triggersApi();
134161

135162
// 7. Export API functions for internal use
136163
export const {
@@ -144,6 +171,8 @@ export const {
144171
} = createApi(schema, auth.options);
145172
```
146173

174+
The trigger API exposes both `before*` and `on*` hooks. The `before` variants run inside the same Convex transaction just ahead of the database write, letting you normalize input, enforce invariants, or perform cleanup and return any transformed payload that should be persisted.
175+
147176
```ts
148177
// convex/http.ts
149178
import { httpRouter } from 'convex/server';

0 commit comments

Comments
 (0)