zodvex 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -40
- package/dist/index.d.mts +224 -69
- package/dist/index.d.ts +224 -69
- package/dist/index.js +126 -191
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +112 -184
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/builders.ts +310 -0
- package/src/custom.ts +4 -84
- package/src/index.ts +3 -0
- package/src/mapping/core.ts +16 -0
- package/src/tables.ts +34 -32
- package/src/utils.ts +9 -6
- package/src/wrappers.ts +60 -52
package/README.md
CHANGED
|
@@ -226,12 +226,14 @@ Handlers should return domain values shaped by your `returns` schema. zodvex val
|
|
|
226
226
|
|
|
227
227
|
```ts
|
|
228
228
|
import { z } from "zod";
|
|
229
|
-
import { zQuery,
|
|
229
|
+
import { zQuery, zodTable, returnsAs } from "zodvex";
|
|
230
230
|
import { query } from "./_generated/server";
|
|
231
231
|
|
|
232
|
-
// Table with doc helpers
|
|
233
|
-
const
|
|
234
|
-
|
|
232
|
+
// Table with doc helpers
|
|
233
|
+
export const Users = zodTable("users", {
|
|
234
|
+
name: z.string(),
|
|
235
|
+
createdAt: z.date()
|
|
236
|
+
});
|
|
235
237
|
|
|
236
238
|
// 1) Return full docs, strongly typed
|
|
237
239
|
export const listUsers = zQuery(
|
|
@@ -326,9 +328,10 @@ const postShape = {
|
|
|
326
328
|
export const Posts = zodTable("posts", postShape);
|
|
327
329
|
|
|
328
330
|
// Access properties
|
|
329
|
-
Posts.table;
|
|
330
|
-
Posts.shape;
|
|
331
|
-
Posts.zDoc;
|
|
331
|
+
Posts.table; // → Table definition for defineSchema
|
|
332
|
+
Posts.shape; // → Original plain object shape
|
|
333
|
+
Posts.zDoc; // → ZodObject with _id and _creationTime system fields
|
|
334
|
+
Posts.docArray; // → z.array(zDoc) for return type validation
|
|
332
335
|
|
|
333
336
|
// Use in schema.ts
|
|
334
337
|
export default defineSchema({
|
|
@@ -336,6 +339,14 @@ export default defineSchema({
|
|
|
336
339
|
.index("by_author", ["authorId"])
|
|
337
340
|
.index("by_published", ["published"]),
|
|
338
341
|
});
|
|
342
|
+
|
|
343
|
+
// Use docArray for return types
|
|
344
|
+
export const listPosts = zQuery(
|
|
345
|
+
query,
|
|
346
|
+
{},
|
|
347
|
+
async (ctx) => ctx.db.query("posts").collect(),
|
|
348
|
+
{ returns: Posts.docArray }
|
|
349
|
+
);
|
|
339
350
|
```
|
|
340
351
|
|
|
341
352
|
#### Working with Large Schemas
|
|
@@ -370,52 +381,87 @@ const zClerkFields = z.object(pickShape(users, ["email", "firstName", "lastName"
|
|
|
370
381
|
// Use zClerkFields in function wrappers or validators
|
|
371
382
|
```
|
|
372
383
|
|
|
373
|
-
|
|
384
|
+
## 🔧 Advanced Usage
|
|
385
|
+
|
|
386
|
+
### Builder Pattern for Reusable Function Creators
|
|
374
387
|
|
|
375
|
-
|
|
388
|
+
For projects with many functions, create reusable builders instead of repeating the base builder.
|
|
389
|
+
Builders use Convex's native `{ args, handler, returns }` object syntax:
|
|
376
390
|
|
|
377
391
|
```ts
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
392
|
+
import { query, mutation } from "./_generated/server";
|
|
393
|
+
import { zQueryBuilder, zMutationBuilder } from "zodvex";
|
|
394
|
+
|
|
395
|
+
// Create reusable builders
|
|
396
|
+
export const zq = zQueryBuilder(query);
|
|
397
|
+
export const zm = zMutationBuilder(mutation);
|
|
398
|
+
|
|
399
|
+
// Use them with Convex-style object syntax
|
|
400
|
+
export const getUser = zq({
|
|
401
|
+
args: { id: z.string() },
|
|
402
|
+
handler: async (ctx, { id }) => {
|
|
403
|
+
return ctx.db.get(id);
|
|
404
|
+
}
|
|
382
405
|
});
|
|
383
406
|
|
|
384
|
-
export const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
Posts.docArray; // → z.array(docSchema) for return types
|
|
407
|
+
export const updateUser = zm({
|
|
408
|
+
args: { id: z.string(), name: z.string() },
|
|
409
|
+
handler: async (ctx, { id, name }) => {
|
|
410
|
+
return ctx.db.patch(id, { name });
|
|
411
|
+
}
|
|
412
|
+
});
|
|
391
413
|
```
|
|
392
414
|
|
|
393
|
-
|
|
415
|
+
### Custom Context with Middleware
|
|
394
416
|
|
|
395
|
-
|
|
417
|
+
Use custom builders to inject auth, permissions, or other context into your functions.
|
|
418
|
+
zodvex provides `customCtx` (re-exported from convex-helpers) for convenience:
|
|
396
419
|
|
|
397
420
|
```ts
|
|
398
|
-
import {
|
|
399
|
-
import {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
421
|
+
import { query, mutation } from "./_generated/server";
|
|
422
|
+
import { zCustomQueryBuilder, zCustomMutationBuilder, customCtx } from "zodvex";
|
|
423
|
+
|
|
424
|
+
// Create authenticated query builder
|
|
425
|
+
export const authQuery = zCustomQueryBuilder(
|
|
426
|
+
query,
|
|
427
|
+
customCtx(async (ctx) => {
|
|
428
|
+
const user = await getUserOrThrow(ctx);
|
|
429
|
+
return { user };
|
|
430
|
+
})
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
// Create authenticated mutation builder
|
|
434
|
+
export const authMutation = zCustomMutationBuilder(
|
|
435
|
+
mutation,
|
|
436
|
+
customCtx(async (ctx) => {
|
|
437
|
+
const user = await getUserOrThrow(ctx);
|
|
438
|
+
return { user };
|
|
439
|
+
})
|
|
416
440
|
);
|
|
441
|
+
|
|
442
|
+
// Use them with automatic type-safe context
|
|
443
|
+
export const getProfile = authQuery({
|
|
444
|
+
args: {},
|
|
445
|
+
handler: async (ctx) => {
|
|
446
|
+
// ctx.user is automatically available and typed!
|
|
447
|
+
return ctx.db.query("users").filter(q => q.eq(q.field("_id"), ctx.user._id)).first();
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
export const updateProfile = authMutation({
|
|
452
|
+
args: { name: z.string() },
|
|
453
|
+
handler: async (ctx, { name }) => {
|
|
454
|
+
// ctx.user is automatically available and typed!
|
|
455
|
+
return ctx.db.patch(ctx.user._id, { name });
|
|
456
|
+
}
|
|
457
|
+
});
|
|
417
458
|
```
|
|
418
459
|
|
|
460
|
+
**Available custom builders:**
|
|
461
|
+
- `zCustomQueryBuilder` - for queries with custom context
|
|
462
|
+
- `zCustomMutationBuilder` - for mutations with custom context
|
|
463
|
+
- `zCustomActionBuilder` - for actions with custom context
|
|
464
|
+
|
|
419
465
|
### Working with Dates
|
|
420
466
|
|
|
421
467
|
```ts
|
package/dist/index.d.mts
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Customization } from 'convex-helpers/server/customFunctions';
|
|
2
|
+
export { customCtx } from 'convex-helpers/server/customFunctions';
|
|
3
|
+
import * as convex_server from 'convex/server';
|
|
4
|
+
import { RegisteredQuery, RegisteredMutation, RegisteredAction, DefaultFunctionArgs, FunctionVisibility, ArgsArrayToObject, GenericDataModel, QueryBuilder, GenericQueryCtx } from 'convex/server';
|
|
2
5
|
import * as convex_values from 'convex/values';
|
|
3
6
|
import { VAny, VOptional, VUnion, VId, GenericId, VString, VFloat64, VInt64, VBoolean, VNull, VArray, VObject, VLiteral, VRecord, PropertyValidators } from 'convex/values';
|
|
4
|
-
import
|
|
5
|
-
import { RegisteredQuery, RegisteredMutation, RegisteredAction, DefaultFunctionArgs, FunctionVisibility, ArgsArrayToObject, GenericDataModel, QueryBuilder, GenericQueryCtx, MutationBuilder, GenericMutationCtx, ActionBuilder, GenericActionCtx } from 'convex/server';
|
|
6
|
-
import { Customization } from 'convex-helpers/server/customFunctions';
|
|
7
|
+
import { z } from 'zod';
|
|
7
8
|
import * as convex_helpers from 'convex-helpers';
|
|
8
9
|
|
|
9
|
-
type ConvexCodec<T> = {
|
|
10
|
-
validator: any;
|
|
11
|
-
encode: (value: T) => any;
|
|
12
|
-
decode: (value: any) => T;
|
|
13
|
-
pick: <K extends keyof T>(keys: K[]) => ConvexCodec<Pick<T, K>>;
|
|
14
|
-
};
|
|
15
|
-
declare function convexCodec<T>(schema: z.ZodType<T>): ConvexCodec<T>;
|
|
16
|
-
declare function toConvexJS(schema?: any, value?: any): any;
|
|
17
|
-
declare function fromConvexJS(value: any, schema: any): any;
|
|
18
|
-
|
|
19
10
|
type IsZid<T> = T extends {
|
|
20
11
|
_tableName: infer _TableName extends string;
|
|
21
12
|
} ? true : false;
|
|
@@ -147,14 +138,189 @@ type CustomBuilder<FuncType extends 'query' | 'mutation' | 'action', CustomArgsV
|
|
|
147
138
|
(ctx: Overwrite<InputCtx, CustomCtx>, ...args: ArgsForHandlerType<ArgsOutput<ArgsValidator>, CustomMadeArgs>): ReturnValue;
|
|
148
139
|
}): Registration<FuncType, Visibility, ArgsArrayToObject<CustomArgsValidator extends Record<string, never> ? ArgsInput<ArgsValidator> : ArgsInput<ArgsValidator> extends [infer A] ? [Expand<A & convex_values.ObjectType<CustomArgsValidator>>] : [convex_values.ObjectType<CustomArgsValidator>]>, ReturnsZodValidator extends void ? ReturnValue : ReturnValueOutput<ReturnsZodValidator>>;
|
|
149
140
|
};
|
|
141
|
+
declare function customFnBuilder<Ctx extends Record<string, any>, Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, ExtraArgs extends Record<string, any> = Record<string, any>>(builder: Builder, customization: Customization<Ctx, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): (fn: any) => any;
|
|
150
142
|
declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel, ExtraArgs extends Record<string, any> = Record<string, any>>(query: QueryBuilder<DataModel, Visibility>, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericQueryCtx<DataModel>, Visibility, ExtraArgs>;
|
|
151
143
|
declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, ExtraArgs extends Record<string, any> = Record<string, any>>(query: QueryBuilder<any, Visibility>, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, any, Visibility, ExtraArgs>;
|
|
152
|
-
declare function zStrictQuery<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(query: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
153
|
-
declare function zStrictMutation<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(mutation: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'mutation', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
154
|
-
declare function zStrictAction<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(action: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'action', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
155
144
|
declare function zCustomMutation<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Builder extends (fn: any) => any, Visibility extends FunctionVisibility = 'public', ExtraArgs extends Record<string, any> = Record<string, any>>(mutation: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'mutation', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
156
145
|
declare function zCustomAction<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Builder extends (fn: any) => any, Visibility extends FunctionVisibility = 'public', ExtraArgs extends Record<string, any> = Record<string, any>>(action: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'action', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
157
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Creates a reusable query builder from a Convex query builder.
|
|
149
|
+
* Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { query } from './_generated/server'
|
|
154
|
+
* import { zQueryBuilder } from 'zodvex'
|
|
155
|
+
*
|
|
156
|
+
* // Create a reusable builder
|
|
157
|
+
* export const zq = zQueryBuilder(query)
|
|
158
|
+
*
|
|
159
|
+
* // Use it with Convex-style object syntax
|
|
160
|
+
* export const getUser = zq({
|
|
161
|
+
* args: { id: z.string() },
|
|
162
|
+
* handler: async (ctx, { id }) => {
|
|
163
|
+
* return ctx.db.get(id)
|
|
164
|
+
* }
|
|
165
|
+
* })
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
declare function zQueryBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
|
|
169
|
+
args?: A;
|
|
170
|
+
handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
|
|
171
|
+
returns?: R;
|
|
172
|
+
}) => RegisteredQuery<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
|
|
173
|
+
/**
|
|
174
|
+
* Creates a reusable mutation builder from a Convex mutation builder.
|
|
175
|
+
* Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { mutation } from './_generated/server'
|
|
180
|
+
* import { zMutationBuilder } from 'zodvex'
|
|
181
|
+
*
|
|
182
|
+
* // Create a reusable builder
|
|
183
|
+
* export const zm = zMutationBuilder(mutation)
|
|
184
|
+
*
|
|
185
|
+
* // Use it with Convex-style object syntax
|
|
186
|
+
* export const updateUser = zm({
|
|
187
|
+
* args: { id: z.string(), name: z.string() },
|
|
188
|
+
* handler: async (ctx, { id, name }) => {
|
|
189
|
+
* return ctx.db.patch(id, { name })
|
|
190
|
+
* }
|
|
191
|
+
* })
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
declare function zMutationBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
|
|
195
|
+
args?: A;
|
|
196
|
+
handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
|
|
197
|
+
returns?: R;
|
|
198
|
+
}) => RegisteredMutation<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
|
|
199
|
+
/**
|
|
200
|
+
* Creates a reusable action builder from a Convex action builder.
|
|
201
|
+
* Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* import { action } from './_generated/server'
|
|
206
|
+
* import { zActionBuilder } from 'zodvex'
|
|
207
|
+
*
|
|
208
|
+
* // Create a reusable builder
|
|
209
|
+
* export const za = zActionBuilder(action)
|
|
210
|
+
*
|
|
211
|
+
* // Use it with Convex-style object syntax
|
|
212
|
+
* export const sendEmail = za({
|
|
213
|
+
* args: { to: z.string().email(), subject: z.string() },
|
|
214
|
+
* handler: async (ctx, { to, subject }) => {
|
|
215
|
+
* // Send email
|
|
216
|
+
* }
|
|
217
|
+
* })
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function zActionBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
|
|
221
|
+
args?: A;
|
|
222
|
+
handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
|
|
223
|
+
returns?: R;
|
|
224
|
+
}) => RegisteredAction<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
|
|
225
|
+
/**
|
|
226
|
+
* Creates a custom query builder with context injection from a Convex query builder.
|
|
227
|
+
* Allows you to add custom context (like auth, permissions, etc.) to your queries.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { query } from './_generated/server'
|
|
232
|
+
* import { zCustomQueryBuilder, customCtx } from 'zodvex'
|
|
233
|
+
*
|
|
234
|
+
* // Create a builder with auth context
|
|
235
|
+
* export const authQuery = zCustomQueryBuilder(
|
|
236
|
+
* query,
|
|
237
|
+
* customCtx(async (ctx) => {
|
|
238
|
+
* const user = await getUserOrThrow(ctx)
|
|
239
|
+
* return { user }
|
|
240
|
+
* })
|
|
241
|
+
* )
|
|
242
|
+
*
|
|
243
|
+
* // Use it with automatic user injection
|
|
244
|
+
* export const getMyProfile = authQuery({
|
|
245
|
+
* args: {},
|
|
246
|
+
* handler: async (ctx) => {
|
|
247
|
+
* // ctx.user is automatically available
|
|
248
|
+
* return ctx.db.get(ctx.user._id)
|
|
249
|
+
* }
|
|
250
|
+
* })
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
declare function zCustomQueryBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(query: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
254
|
+
/**
|
|
255
|
+
* Creates a custom mutation builder with context injection from a Convex mutation builder.
|
|
256
|
+
* Allows you to add custom context (like auth, permissions, etc.) to your mutations.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* import { mutation } from './_generated/server'
|
|
261
|
+
* import { zCustomMutationBuilder, customCtx } from 'zodvex'
|
|
262
|
+
*
|
|
263
|
+
* // Create a builder with auth context
|
|
264
|
+
* export const authMutation = zCustomMutationBuilder(
|
|
265
|
+
* mutation,
|
|
266
|
+
* customCtx(async (ctx) => {
|
|
267
|
+
* const user = await getUserOrThrow(ctx)
|
|
268
|
+
* return { user }
|
|
269
|
+
* })
|
|
270
|
+
* )
|
|
271
|
+
*
|
|
272
|
+
* // Use it with automatic user injection
|
|
273
|
+
* export const updateProfile = authMutation({
|
|
274
|
+
* args: { name: z.string() },
|
|
275
|
+
* handler: async (ctx, { name }) => {
|
|
276
|
+
* // ctx.user is automatically available
|
|
277
|
+
* await ctx.db.patch(ctx.user._id, { name })
|
|
278
|
+
* }
|
|
279
|
+
* })
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
declare function zCustomMutationBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(mutation: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'mutation', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
283
|
+
/**
|
|
284
|
+
* Creates a custom action builder with context injection from a Convex action builder.
|
|
285
|
+
* Allows you to add custom context (like auth, permissions, etc.) to your actions.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* import { action } from './_generated/server'
|
|
290
|
+
* import { zCustomActionBuilder, customCtx } from 'zodvex'
|
|
291
|
+
*
|
|
292
|
+
* // Create a builder with auth context
|
|
293
|
+
* export const authAction = zCustomActionBuilder(
|
|
294
|
+
* action,
|
|
295
|
+
* customCtx(async (ctx) => {
|
|
296
|
+
* const identity = await ctx.auth.getUserIdentity()
|
|
297
|
+
* if (!identity) throw new Error('Unauthorized')
|
|
298
|
+
* return { userId: identity.subject }
|
|
299
|
+
* })
|
|
300
|
+
* )
|
|
301
|
+
*
|
|
302
|
+
* // Use it with automatic auth injection
|
|
303
|
+
* export const sendEmail = authAction({
|
|
304
|
+
* args: { to: z.string().email() },
|
|
305
|
+
* handler: async (ctx, { to }) => {
|
|
306
|
+
* // ctx.userId is automatically available
|
|
307
|
+
* await sendEmailService(to, ctx.userId)
|
|
308
|
+
* }
|
|
309
|
+
* })
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
declare function zCustomActionBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(action: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'action', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
|
|
313
|
+
|
|
314
|
+
type ConvexCodec<T> = {
|
|
315
|
+
validator: any;
|
|
316
|
+
encode: (value: T) => any;
|
|
317
|
+
decode: (value: any) => T;
|
|
318
|
+
pick: <K extends keyof T>(keys: K[]) => ConvexCodec<Pick<T, K>>;
|
|
319
|
+
};
|
|
320
|
+
declare function convexCodec<T>(schema: z.ZodType<T>): ConvexCodec<T>;
|
|
321
|
+
declare function toConvexJS(schema?: any, value?: any): any;
|
|
322
|
+
declare function fromConvexJS(value: any, schema: any): any;
|
|
323
|
+
|
|
158
324
|
type BaseCodec = {
|
|
159
325
|
check: (schema: any) => boolean;
|
|
160
326
|
toValidator: (schema: any) => any;
|
|
@@ -186,7 +352,29 @@ declare function zodDocOrNull<TableName extends string, Shape extends z.ZodRawSh
|
|
|
186
352
|
* This architectural decision is important for projects that rely heavily on type safety and
|
|
187
353
|
* developer experience, as it avoids the type erasure that can occur when using ZodObject directly.
|
|
188
354
|
*
|
|
189
|
-
* Returns
|
|
355
|
+
* Returns the Table definition along with Zod schemas for documents and arrays.
|
|
356
|
+
*
|
|
357
|
+
* @param name - The table name
|
|
358
|
+
* @param shape - A raw object mapping field names to Zod validators
|
|
359
|
+
* @returns A Table with attached shape, zDoc schema, and docArray helper
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
362
|
+
* ```ts
|
|
363
|
+
* const Users = zodTable('users', {
|
|
364
|
+
* name: z.string(),
|
|
365
|
+
* email: z.string().email(),
|
|
366
|
+
* age: z.number().optional()
|
|
367
|
+
* })
|
|
368
|
+
*
|
|
369
|
+
* // Use in schema
|
|
370
|
+
* export default defineSchema({ users: Users.table })
|
|
371
|
+
*
|
|
372
|
+
* // Use for return types
|
|
373
|
+
* export const getUsers = zQuery(query, {},
|
|
374
|
+
* async (ctx) => ctx.db.query('users').collect(),
|
|
375
|
+
* { returns: Users.docArray }
|
|
376
|
+
* )
|
|
377
|
+
* ```
|
|
190
378
|
*/
|
|
191
379
|
declare function zodTable<TableName extends string, Shape extends Record<string, z.ZodTypeAny>>(name: TableName, shape: Shape): {
|
|
192
380
|
name: TableName;
|
|
@@ -238,55 +426,14 @@ declare function zodTable<TableName extends string, Shape extends Record<string,
|
|
|
238
426
|
_id: ReturnType<typeof zid<TableName>>;
|
|
239
427
|
_creationTime: z.ZodNumber;
|
|
240
428
|
}>;
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
docSchema: z.ZodObject<{
|
|
245
|
-
_id: z.ZodType<GenericId<TableName>, unknown, z.core.$ZodTypeInternals<GenericId<TableName>, unknown>> & {
|
|
246
|
-
_tableName: TableName;
|
|
247
|
-
};
|
|
248
|
-
_creationTime: z.ZodNumber;
|
|
249
|
-
}, z.core.$strip>;
|
|
250
|
-
docArray: z.ZodArray<z.ZodObject<{
|
|
429
|
+
docArray: z.ZodArray<z.ZodObject<Readonly<{
|
|
430
|
+
[k: string]: z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
431
|
+
}> & {
|
|
251
432
|
_id: z.ZodType<GenericId<TableName>, unknown, z.core.$ZodTypeInternals<GenericId<TableName>, unknown>> & {
|
|
252
433
|
_tableName: TableName;
|
|
253
434
|
};
|
|
254
435
|
_creationTime: z.ZodNumber;
|
|
255
436
|
}, z.core.$strip>>;
|
|
256
|
-
name: TableName;
|
|
257
|
-
table: convex_server.TableDefinition<convex_values.VObject<{
|
|
258
|
-
[x: string]: any;
|
|
259
|
-
[x: number]: any;
|
|
260
|
-
[x: symbol]: any;
|
|
261
|
-
}, ConvexValidatorFromZodFieldsAuto<any>, "required", string>, {}, {}, {}>;
|
|
262
|
-
doc: convex_values.VObject<{
|
|
263
|
-
[x: string]: any;
|
|
264
|
-
[x: number]: any;
|
|
265
|
-
[x: symbol]: any;
|
|
266
|
-
}, {
|
|
267
|
-
[x: string]: convex_values.VFloat64<number, "required"> | convex_values.VNull<null, "required"> | convex_values.VLiteral<z.core.util.Literal, "required"> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VUnion<unknown, any[], "required", any> | convex_values.VUnion<unknown, any[], "required", any> | convex_values.VString<any, "required"> | convex_values.VFloat64<any, "required"> | convex_values.VInt64<any, "required"> | convex_values.VBoolean<any, "required"> | convex_values.VArray<any, convex_values.VAny<"required", "required", string>, "required"> | convex_values.VObject<any, {
|
|
268
|
-
readonly [x: string]: convex_values.VAny<"required", "required", string>;
|
|
269
|
-
}, "required", string> | convex_values.VUnion<z.core.util.EnumValue, any[], "required", any> | convex_values.VRecord<Record<string, unknown>, convex_values.VString<string, "required">, convex_values.VAny<"required", "required", string>, "required", string> | convex_values.VLiteral<z.core.util.Literal, "optional"> | convex_values.VAny<"required" | undefined, "optional", string> | convex_values.VUnion<unknown, any[], "optional", any> | convex_values.VString<any, "optional"> | convex_values.VFloat64<any, "optional"> | convex_values.VFloat64<number, "optional"> | convex_values.VInt64<any, "optional"> | convex_values.VBoolean<any, "optional"> | convex_values.VNull<null, "optional"> | convex_values.VArray<any, convex_values.VAny<"required", "required", string>, "optional"> | convex_values.VObject<any, {
|
|
270
|
-
readonly [x: string]: convex_values.VAny<"required", "required", string>;
|
|
271
|
-
}, "optional", string> | convex_values.VUnion<z.core.util.EnumValue, any[], "optional", any> | convex_values.VRecord<Record<string, unknown>, convex_values.VString<string, "required">, convex_values.VAny<"required", "required", string>, "optional", string>;
|
|
272
|
-
_id: convex_values.VId<GenericId<TableName>, "required">;
|
|
273
|
-
_creationTime: convex_values.VFloat64<number, "required">;
|
|
274
|
-
}, "required", string>;
|
|
275
|
-
withoutSystemFields: ConvexValidatorFromZodFieldsAuto<any>;
|
|
276
|
-
withSystemFields: {
|
|
277
|
-
[x: string]: convex_values.VFloat64<number, "required"> | convex_values.VNull<null, "required"> | convex_values.VLiteral<z.core.util.Literal, "required"> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VAny<"required", "required", string> | convex_values.VUnion<unknown, any[], "required", any> | convex_values.VUnion<unknown, any[], "required", any> | convex_values.VString<any, "required"> | convex_values.VFloat64<any, "required"> | convex_values.VInt64<any, "required"> | convex_values.VBoolean<any, "required"> | convex_values.VArray<any, convex_values.VAny<"required", "required", string>, "required"> | convex_values.VObject<any, {
|
|
278
|
-
readonly [x: string]: convex_values.VAny<"required", "required", string>;
|
|
279
|
-
}, "required", string> | convex_values.VUnion<z.core.util.EnumValue, any[], "required", any> | convex_values.VRecord<Record<string, unknown>, convex_values.VString<string, "required">, convex_values.VAny<"required", "required", string>, "required", string> | convex_values.VLiteral<z.core.util.Literal, "optional"> | convex_values.VAny<"required" | undefined, "optional", string> | convex_values.VUnion<unknown, any[], "optional", any> | convex_values.VString<any, "optional"> | convex_values.VFloat64<any, "optional"> | convex_values.VFloat64<number, "optional"> | convex_values.VInt64<any, "optional"> | convex_values.VBoolean<any, "optional"> | convex_values.VNull<null, "optional"> | convex_values.VArray<any, convex_values.VAny<"required", "required", string>, "optional"> | convex_values.VObject<any, {
|
|
280
|
-
readonly [x: string]: convex_values.VAny<"required", "required", string>;
|
|
281
|
-
}, "optional", string> | convex_values.VUnion<z.core.util.EnumValue, any[], "optional", any> | convex_values.VRecord<Record<string, unknown>, convex_values.VString<string, "required">, convex_values.VAny<"required", "required", string>, "optional", string>;
|
|
282
|
-
_id: convex_values.VId<GenericId<TableName>, "required">;
|
|
283
|
-
_creationTime: convex_values.VFloat64<number, "required">;
|
|
284
|
-
};
|
|
285
|
-
systemFields: {
|
|
286
|
-
_id: convex_values.VId<GenericId<TableName>, "required">;
|
|
287
|
-
_creationTime: convex_values.VFloat64<number, "required">;
|
|
288
|
-
};
|
|
289
|
-
_id: convex_values.VId<GenericId<TableName>, "required">;
|
|
290
437
|
};
|
|
291
438
|
|
|
292
439
|
declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
@@ -314,27 +461,35 @@ declare function zPaginated<T extends z.ZodTypeAny>(item: T): z.ZodObject<{
|
|
|
314
461
|
*/
|
|
315
462
|
declare function mapDateFieldToNumber(field: z.ZodTypeAny): z.ZodTypeAny;
|
|
316
463
|
type Mask = readonly string[] | Record<string, boolean | 1 | true>;
|
|
464
|
+
/**
|
|
465
|
+
* Returns a plain shape object containing only the selected fields.
|
|
466
|
+
* Accepts either a ZodObject or a raw shape object.
|
|
467
|
+
*/
|
|
317
468
|
declare function pickShape(schemaOrShape: z.ZodObject<any> | Record<string, any>, mask: Mask): Record<string, any>;
|
|
318
469
|
declare function safePick(schema: z.ZodObject<any>, mask: Mask): z.ZodObject<any>;
|
|
470
|
+
/**
|
|
471
|
+
* Convenience: omit a set of keys by building the complement.
|
|
472
|
+
* Avoids using Zod's .omit() which can cause type depth issues.
|
|
473
|
+
*/
|
|
319
474
|
declare function safeOmit(schema: z.ZodObject<any>, mask: Mask): z.ZodObject<any>;
|
|
320
475
|
|
|
321
|
-
declare function zQuery<
|
|
476
|
+
declare function zQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(query: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
322
477
|
returns?: R;
|
|
323
478
|
}): RegisteredQuery<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
324
|
-
declare function zInternalQuery<
|
|
479
|
+
declare function zInternalQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(internalQuery: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
325
480
|
returns?: R;
|
|
326
481
|
}): RegisteredQuery<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
327
|
-
declare function zMutation<
|
|
482
|
+
declare function zMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(mutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
328
483
|
returns?: R;
|
|
329
484
|
}): RegisteredMutation<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
330
|
-
declare function zInternalMutation<
|
|
485
|
+
declare function zInternalMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(internalMutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
331
486
|
returns?: R;
|
|
332
487
|
}): RegisteredMutation<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
333
|
-
declare function zAction<
|
|
488
|
+
declare function zAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(action: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
334
489
|
returns?: R;
|
|
335
490
|
}): RegisteredAction<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
336
|
-
declare function zInternalAction<
|
|
491
|
+
declare function zInternalAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(internalAction: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>, options?: {
|
|
337
492
|
returns?: R;
|
|
338
493
|
}): RegisteredAction<Visibility, ZodToConvexArgs<A>, Promise<InferReturns<R>>>;
|
|
339
494
|
|
|
340
|
-
export { type ConvexCodec, type ConvexValidatorFromZod, type ConvexValidatorFromZodFieldsAuto, type CustomBuilder, type ExtractCtx, type ExtractVisibility, type InferArgs, type InferHandlerReturns, type InferReturns, type PreserveReturnType, type Zid, type ZodToConvexArgs, type ZodValidator, convexCodec, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, handleZodValidationError, isDateSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, zAction, zCustomAction, zCustomMutation, zCustomQuery, zInternalAction, zInternalMutation, zInternalQuery, zMutation, zPaginated, zQuery,
|
|
495
|
+
export { type ConvexCodec, type ConvexValidatorFromZod, type ConvexValidatorFromZodFieldsAuto, type CustomBuilder, type ExtractCtx, type ExtractVisibility, type InferArgs, type InferHandlerReturns, type InferReturns, type PreserveReturnType, type Zid, type ZodToConvexArgs, type ZodValidator, convexCodec, customFnBuilder, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, handleZodValidationError, isDateSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, zAction, zActionBuilder, zCustomAction, zCustomActionBuilder, zCustomMutation, zCustomMutationBuilder, zCustomQuery, zCustomQueryBuilder, zInternalAction, zInternalMutation, zInternalQuery, zMutation, zMutationBuilder, zPaginated, zQuery, zQueryBuilder, zid, zodDoc, zodDocOrNull, zodTable, zodToConvex, zodToConvexFields };
|