@wordrhyme/auto-crud-server 1.0.4 → 1.0.5
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 +319 -175
- package/dist/index.cjs +21 -10
- package/dist/index.d.cts +162 -90
- package/dist/index.d.ts +161 -89
- package/dist/index.js +19 -11
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -500,7 +500,7 @@ const filterItemSchema = zod.z.object({
|
|
|
500
500
|
operator: zod.z.enum(dataTableConfig.operators),
|
|
501
501
|
filterId: zod.z.string()
|
|
502
502
|
});
|
|
503
|
-
const
|
|
503
|
+
const baseListInputSchema = zod.z.object({
|
|
504
504
|
page: zod.z.number().min(1).default(1),
|
|
505
505
|
perPage: zod.z.number().min(1).max(100).default(10),
|
|
506
506
|
sort: zod.z.array(zod.z.object({
|
|
@@ -510,7 +510,9 @@ const listInputSchema = zod.z.object({
|
|
|
510
510
|
filters: zod.z.array(filterItemSchema).optional(),
|
|
511
511
|
joinOperator: zod.z.enum(["and", "or"]).default("and")
|
|
512
512
|
});
|
|
513
|
-
const
|
|
513
|
+
const baseGetInputSchema = zod.z.object({ id: zod.z.string() });
|
|
514
|
+
const defaultGetInputSchema = zod.z.union([zod.z.string(), baseGetInputSchema]);
|
|
515
|
+
const baseExportInputSchema = zod.z.object({
|
|
514
516
|
sort: zod.z.array(zod.z.object({
|
|
515
517
|
id: zod.z.string(),
|
|
516
518
|
desc: zod.z.boolean()
|
|
@@ -613,6 +615,9 @@ function createCrudRouter(config) {
|
|
|
613
615
|
});
|
|
614
616
|
const resolved = resolveConfig(config);
|
|
615
617
|
const softDelete = resolveSoftDelete(softDeleteOption);
|
|
618
|
+
const resolvedListInputSchema = config.listInputSchema ?? baseListInputSchema;
|
|
619
|
+
const resolvedGetInputSchema = config.getInputSchema ? zod.z.union([zod.z.string(), config.getInputSchema]) : defaultGetInputSchema;
|
|
620
|
+
const resolvedExportInputSchema = config.exportInputSchema ?? baseExportInputSchema;
|
|
616
621
|
const m = middleware;
|
|
617
622
|
const getIdColumn = () => table[idField];
|
|
618
623
|
const getSoftDeleteColumn = () => softDelete ? table[softDelete.column] : null;
|
|
@@ -654,7 +659,7 @@ function createCrudRouter(config) {
|
|
|
654
659
|
});
|
|
655
660
|
};
|
|
656
661
|
const procedures = {
|
|
657
|
-
list: resolved.procedureFactory("list").input(
|
|
662
|
+
list: resolved.procedureFactory("list").input(resolvedListInputSchema).output(listOutputSchema).query(async ({ ctx, input }) => {
|
|
658
663
|
await withGuard(ctx, "list");
|
|
659
664
|
const doList = async (listInput$1) => {
|
|
660
665
|
const offset = (listInput$1.page - 1) * listInput$1.perPage;
|
|
@@ -693,20 +698,23 @@ function createCrudRouter(config) {
|
|
|
693
698
|
});
|
|
694
699
|
return doList(listInput);
|
|
695
700
|
}),
|
|
696
|
-
get: resolved.procedureFactory("get").input(
|
|
701
|
+
get: resolved.procedureFactory("get").input(resolvedGetInputSchema).output(entityOutputSchema.nullable()).query(async ({ ctx, input }) => {
|
|
697
702
|
await withGuard(ctx, "get");
|
|
698
|
-
const doGet = async () => {
|
|
699
|
-
const
|
|
703
|
+
const doGet = async (getInput) => {
|
|
704
|
+
const id$1 = typeof getInput === "string" ? getInput : getInput.id;
|
|
705
|
+
const where = buildWhere(ctx, "get", (0, drizzle_orm.eq)(getIdColumn(), id$1));
|
|
700
706
|
const [item] = await ctx.db.select().from(table).where(where);
|
|
701
707
|
await withAuthorize(ctx, item, "get");
|
|
702
708
|
return item ?? null;
|
|
703
709
|
};
|
|
710
|
+
const id = typeof input === "string" ? input : input.id;
|
|
704
711
|
if (m.get) return m.get({
|
|
705
712
|
ctx,
|
|
706
|
-
id
|
|
707
|
-
|
|
713
|
+
id,
|
|
714
|
+
input,
|
|
715
|
+
next: async (modifiedInput) => doGet(modifiedInput ?? input)
|
|
708
716
|
});
|
|
709
|
-
return doGet();
|
|
717
|
+
return doGet(input);
|
|
710
718
|
}),
|
|
711
719
|
create: resolved.procedureFactory("create").input(schema).output(entityOutputSchema).mutation(async ({ ctx, input }) => {
|
|
712
720
|
await withGuard(ctx, "create");
|
|
@@ -852,7 +860,7 @@ function createCrudRouter(config) {
|
|
|
852
860
|
});
|
|
853
861
|
return doUpsert(input);
|
|
854
862
|
}),
|
|
855
|
-
export: resolved.procedureFactory("export").input(
|
|
863
|
+
export: resolved.procedureFactory("export").input(resolvedExportInputSchema).output(exportOutputSchema).query(async ({ ctx, input }) => {
|
|
856
864
|
await withGuard(ctx, "export");
|
|
857
865
|
const doExport = async (exportInput$1) => {
|
|
858
866
|
const effectiveLimit = Math.min(Math.max(exportInput$1.limit ?? maxExportSize, 1), maxExportSize);
|
|
@@ -1057,6 +1065,9 @@ const appRouter = router({});
|
|
|
1057
1065
|
//#endregion
|
|
1058
1066
|
exports.afterMiddleware = afterMiddleware;
|
|
1059
1067
|
exports.appRouter = appRouter;
|
|
1068
|
+
exports.baseExportInputSchema = baseExportInputSchema;
|
|
1069
|
+
exports.baseGetInputSchema = baseGetInputSchema;
|
|
1070
|
+
exports.baseListInputSchema = baseListInputSchema;
|
|
1060
1071
|
exports.beforeMiddleware = beforeMiddleware;
|
|
1061
1072
|
exports.composeMiddleware = composeMiddleware;
|
|
1062
1073
|
exports.createCrudRouter = createCrudRouter;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { PgTable } from "drizzle-orm/pg-core";
|
|
2
|
-
import * as
|
|
3
|
+
import * as _trpc_server2 from "@trpc/server";
|
|
3
4
|
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
4
5
|
import { SQL } from "drizzle-orm";
|
|
5
|
-
import { z } from "zod";
|
|
6
6
|
|
|
7
7
|
//#region src/trpc.d.ts
|
|
8
8
|
/**
|
|
@@ -12,22 +12,22 @@ import { z } from "zod";
|
|
|
12
12
|
interface Context {
|
|
13
13
|
db: PostgresJsDatabase<Record<string, never>>;
|
|
14
14
|
}
|
|
15
|
-
declare const router:
|
|
15
|
+
declare const router: _trpc_server2.TRPCRouterBuilder<{
|
|
16
16
|
ctx: Context;
|
|
17
17
|
meta: object;
|
|
18
|
-
errorShape:
|
|
18
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
19
19
|
transformer: true;
|
|
20
20
|
}>;
|
|
21
|
-
declare const publicProcedure:
|
|
21
|
+
declare const publicProcedure: _trpc_server2.TRPCProcedureBuilder<Context, object, object, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, false>;
|
|
22
22
|
//#endregion
|
|
23
23
|
//#region src/types/config.d.ts
|
|
24
|
-
type CrudOperation =
|
|
25
|
-
type WriteOperation =
|
|
24
|
+
type CrudOperation = 'list' | 'get' | 'create' | 'update' | 'delete' | 'deleteMany' | 'updateMany' | 'upsert' | 'export' | 'import' | 'createMany';
|
|
25
|
+
type WriteOperation = 'create' | 'update';
|
|
26
26
|
/**
|
|
27
27
|
* 表字段键类型提取
|
|
28
28
|
* 用于 omitFields 的强类型支持
|
|
29
29
|
*/
|
|
30
|
-
type TableColumnKeys<T extends PgTable> = keyof T[
|
|
30
|
+
type TableColumnKeys<T extends PgTable> = keyof T['_']['columns'];
|
|
31
31
|
type AnyProcedure = any;
|
|
32
32
|
interface SoftDeleteConfig {
|
|
33
33
|
/** 软删除标记列名 */
|
|
@@ -63,7 +63,7 @@ interface ListInput {
|
|
|
63
63
|
operator: string;
|
|
64
64
|
filterId: string;
|
|
65
65
|
}>;
|
|
66
|
-
joinOperator:
|
|
66
|
+
joinOperator: 'and' | 'or';
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* 列表查询结果类型
|
|
@@ -75,6 +75,14 @@ interface ListResult<TSelect> {
|
|
|
75
75
|
perPage: number;
|
|
76
76
|
pageCount: number;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* 单条查询输入类型
|
|
80
|
+
* - string: 兼容旧的 get(id) 调用
|
|
81
|
+
* - object: 支持通过 getInputSchema 扩展 include 等控制参数
|
|
82
|
+
*/
|
|
83
|
+
type GetInput = string | {
|
|
84
|
+
id: string;
|
|
85
|
+
};
|
|
78
86
|
/**
|
|
79
87
|
* 导出查询输入类型(复用 ListInput 的筛选/排序,无分页)
|
|
80
88
|
*/
|
|
@@ -90,7 +98,7 @@ interface ExportInput {
|
|
|
90
98
|
operator: string;
|
|
91
99
|
filterId: string;
|
|
92
100
|
}>;
|
|
93
|
-
joinOperator?:
|
|
101
|
+
joinOperator?: 'and' | 'or';
|
|
94
102
|
/** 导出数量限制,会被 clamp 到 [1, maxExportSize] */
|
|
95
103
|
limit?: number;
|
|
96
104
|
}
|
|
@@ -137,17 +145,18 @@ interface ImportInput {
|
|
|
137
145
|
* - "upsert": 存在则更新,不存在则新建
|
|
138
146
|
* - "error": 冲突行计入失败
|
|
139
147
|
*/
|
|
140
|
-
onConflict?:
|
|
148
|
+
onConflict?: 'skip' | 'upsert' | 'error';
|
|
141
149
|
}
|
|
142
|
-
interface ListMiddlewareParams<TContext, TSelect> {
|
|
150
|
+
interface ListMiddlewareParams<TContext, TSelect, TListInput extends ListInput = ListInput> {
|
|
143
151
|
ctx: TContext;
|
|
144
|
-
input:
|
|
145
|
-
next: (input?:
|
|
152
|
+
input: TListInput;
|
|
153
|
+
next: (input?: TListInput) => Promise<ListResult<TSelect>>;
|
|
146
154
|
}
|
|
147
|
-
interface GetMiddlewareParams<TContext, TSelect> {
|
|
155
|
+
interface GetMiddlewareParams<TContext, TSelect, TGetInput extends GetInput = GetInput> {
|
|
148
156
|
ctx: TContext;
|
|
149
157
|
id: string;
|
|
150
|
-
|
|
158
|
+
input: TGetInput;
|
|
159
|
+
next: (input?: TGetInput) => Promise<TSelect | null>;
|
|
151
160
|
}
|
|
152
161
|
interface CreateMiddlewareParams<TContext, TSelect, TInsert> {
|
|
153
162
|
ctx: TContext;
|
|
@@ -190,10 +199,10 @@ interface UpsertMiddlewareParams<TContext, TSelect, TInsert> {
|
|
|
190
199
|
isNew: boolean;
|
|
191
200
|
}>;
|
|
192
201
|
}
|
|
193
|
-
interface ExportMiddlewareParams<TContext, TSelect> {
|
|
202
|
+
interface ExportMiddlewareParams<TContext, TSelect, TExportInput extends ExportInput = ExportInput> {
|
|
194
203
|
ctx: TContext;
|
|
195
|
-
input:
|
|
196
|
-
next: (input?:
|
|
204
|
+
input: TExportInput;
|
|
205
|
+
next: (input?: TExportInput) => Promise<ExportResult<TSelect>>;
|
|
197
206
|
}
|
|
198
207
|
interface ImportMiddlewareParams<TContext> {
|
|
199
208
|
ctx: TContext;
|
|
@@ -234,15 +243,15 @@ interface CreateManyMiddlewareParams<TContext, TSelect, TInsert> {
|
|
|
234
243
|
* },
|
|
235
244
|
* }
|
|
236
245
|
*/
|
|
237
|
-
interface CrudMiddleware<TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown> {
|
|
246
|
+
interface CrudMiddleware<TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown, TListInput extends ListInput = ListInput, TGetInput extends GetInput = GetInput, TExportInput extends ExportInput = ExportInput> {
|
|
238
247
|
/**
|
|
239
248
|
* 列表查询包装器
|
|
240
249
|
*/
|
|
241
|
-
list?: (params: ListMiddlewareParams<TContext, TSelect>) => Promise<ListResult<TSelect>>;
|
|
250
|
+
list?: (params: ListMiddlewareParams<TContext, TSelect, TListInput>) => Promise<ListResult<TSelect>>;
|
|
242
251
|
/**
|
|
243
252
|
* 单条查询包装器
|
|
244
253
|
*/
|
|
245
|
-
get?: (params: GetMiddlewareParams<TContext, TSelect>) => Promise<TSelect | null>;
|
|
254
|
+
get?: (params: GetMiddlewareParams<TContext, TSelect, TGetInput>) => Promise<TSelect | null>;
|
|
246
255
|
/**
|
|
247
256
|
* 创建包装器
|
|
248
257
|
*/
|
|
@@ -281,7 +290,7 @@ interface CrudMiddleware<TContext = unknown, TSelect = unknown, TInsert = unknow
|
|
|
281
290
|
* 导出包装器
|
|
282
291
|
* 可用于审计日志、限流、异步队列调度
|
|
283
292
|
*/
|
|
284
|
-
export?: (params: ExportMiddlewareParams<TContext, TSelect>) => Promise<ExportResult<TSelect>>;
|
|
293
|
+
export?: (params: ExportMiddlewareParams<TContext, TSelect, TExportInput>) => Promise<ExportResult<TSelect>>;
|
|
285
294
|
/**
|
|
286
295
|
* 导入包装器
|
|
287
296
|
* 可用于审计日志、限流、异步队列调度
|
|
@@ -377,7 +386,7 @@ type ProcedureConfig = AnyProcedure | ProcedureMap | ProcedureFactory;
|
|
|
377
386
|
* authorize: (ctx, resource) => resource.ownerId === ctx.user.id,
|
|
378
387
|
* })
|
|
379
388
|
*/
|
|
380
|
-
interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown> {
|
|
389
|
+
interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown, TListInput extends ListInput = ListInput, TGetInput extends GetInput = GetInput, TExportInput extends ExportInput = ExportInput> {
|
|
381
390
|
/** Drizzle 表定义 */
|
|
382
391
|
table: TTable;
|
|
383
392
|
/**
|
|
@@ -408,6 +417,35 @@ interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown,
|
|
|
408
417
|
updateSchema?: z.ZodType<TUpdate>;
|
|
409
418
|
/** 查询返回 Schema */
|
|
410
419
|
selectSchema?: z.ZodType<TSelect>;
|
|
420
|
+
/**
|
|
421
|
+
* 列表查询输入 Schema(可选)
|
|
422
|
+
* - 默认使用内置基础列表输入:page、perPage、sort、filters、joinOperator
|
|
423
|
+
* - 业务侧可以基于 baseListInputSchema.extend(...) 增加自定义参数
|
|
424
|
+
* - 默认查询逻辑只读取基础字段,额外字段会保留给 middleware.list 使用
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* listInputSchema: baseListInputSchema.extend({
|
|
428
|
+
* include: z.object({ skus: z.boolean().optional() }).optional(),
|
|
429
|
+
* })
|
|
430
|
+
*/
|
|
431
|
+
listInputSchema?: z.ZodType<TListInput>;
|
|
432
|
+
/**
|
|
433
|
+
* 单条查询输入 Schema(可选)
|
|
434
|
+
* - 默认兼容 get(id) 和 get({ id })
|
|
435
|
+
* - 业务侧可以基于 baseGetInputSchema.extend(...) 增加 include 等控制参数
|
|
436
|
+
* - 配置后仍保留 get(id) 字符串调用兼容性
|
|
437
|
+
* - 默认查询逻辑只读取 id,额外字段会保留给 middleware.get 使用
|
|
438
|
+
*/
|
|
439
|
+
getInputSchema?: z.ZodType<Extract<TGetInput, {
|
|
440
|
+
id: string;
|
|
441
|
+
}>>;
|
|
442
|
+
/**
|
|
443
|
+
* 导出查询输入 Schema(可选)
|
|
444
|
+
* - 默认使用内置基础导出输入:sort、filters、joinOperator、limit
|
|
445
|
+
* - 业务侧可以基于 baseExportInputSchema.extend(...) 增加自定义参数
|
|
446
|
+
* - 默认查询逻辑只读取基础字段,额外字段会保留给 middleware.export 使用
|
|
447
|
+
*/
|
|
448
|
+
exportInputSchema?: z.ZodType<TExportInput>;
|
|
411
449
|
/**
|
|
412
450
|
* Procedure 配置(统一字段)
|
|
413
451
|
*
|
|
@@ -516,7 +554,7 @@ interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown,
|
|
|
516
554
|
* }),
|
|
517
555
|
* }
|
|
518
556
|
*/
|
|
519
|
-
middleware?: CrudMiddleware<TContext, TSelect, TInsert, TUpdate>;
|
|
557
|
+
middleware?: CrudMiddleware<TContext, TSelect, TInsert, TUpdate, TListInput, TGetInput, TExportInput>;
|
|
520
558
|
}
|
|
521
559
|
interface CrudProcedures {
|
|
522
560
|
list: AnyProcedure;
|
|
@@ -540,6 +578,94 @@ interface CrudProcedures {
|
|
|
540
578
|
type CrudRouterReturn = ReturnType<typeof router> & {
|
|
541
579
|
procedures: CrudProcedures;
|
|
542
580
|
};
|
|
581
|
+
declare const baseListInputSchema: z.ZodObject<{
|
|
582
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
583
|
+
perPage: z.ZodDefault<z.ZodNumber>;
|
|
584
|
+
sort: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
585
|
+
id: z.ZodString;
|
|
586
|
+
desc: z.ZodBoolean;
|
|
587
|
+
}, z.core.$strip>>>;
|
|
588
|
+
filters: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
589
|
+
id: z.ZodString;
|
|
590
|
+
value: z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>;
|
|
591
|
+
variant: z.ZodEnum<{
|
|
592
|
+
number: "number";
|
|
593
|
+
boolean: "boolean";
|
|
594
|
+
date: "date";
|
|
595
|
+
text: "text";
|
|
596
|
+
range: "range";
|
|
597
|
+
dateRange: "dateRange";
|
|
598
|
+
select: "select";
|
|
599
|
+
multiSelect: "multiSelect";
|
|
600
|
+
}>;
|
|
601
|
+
operator: z.ZodEnum<{
|
|
602
|
+
iLike: "iLike";
|
|
603
|
+
notILike: "notILike";
|
|
604
|
+
eq: "eq";
|
|
605
|
+
ne: "ne";
|
|
606
|
+
isEmpty: "isEmpty";
|
|
607
|
+
isNotEmpty: "isNotEmpty";
|
|
608
|
+
lt: "lt";
|
|
609
|
+
lte: "lte";
|
|
610
|
+
gt: "gt";
|
|
611
|
+
gte: "gte";
|
|
612
|
+
isBetween: "isBetween";
|
|
613
|
+
isRelativeToToday: "isRelativeToToday";
|
|
614
|
+
inArray: "inArray";
|
|
615
|
+
notInArray: "notInArray";
|
|
616
|
+
}>;
|
|
617
|
+
filterId: z.ZodString;
|
|
618
|
+
}, z.core.$strip>>>;
|
|
619
|
+
joinOperator: z.ZodDefault<z.ZodEnum<{
|
|
620
|
+
and: "and";
|
|
621
|
+
or: "or";
|
|
622
|
+
}>>;
|
|
623
|
+
}, z.core.$strip>;
|
|
624
|
+
declare const baseGetInputSchema: z.ZodObject<{
|
|
625
|
+
id: z.ZodString;
|
|
626
|
+
}, z.core.$strip>;
|
|
627
|
+
declare const baseExportInputSchema: z.ZodObject<{
|
|
628
|
+
sort: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
629
|
+
id: z.ZodString;
|
|
630
|
+
desc: z.ZodBoolean;
|
|
631
|
+
}, z.core.$strip>>>;
|
|
632
|
+
filters: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
633
|
+
id: z.ZodString;
|
|
634
|
+
value: z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>;
|
|
635
|
+
variant: z.ZodEnum<{
|
|
636
|
+
number: "number";
|
|
637
|
+
boolean: "boolean";
|
|
638
|
+
date: "date";
|
|
639
|
+
text: "text";
|
|
640
|
+
range: "range";
|
|
641
|
+
dateRange: "dateRange";
|
|
642
|
+
select: "select";
|
|
643
|
+
multiSelect: "multiSelect";
|
|
644
|
+
}>;
|
|
645
|
+
operator: z.ZodEnum<{
|
|
646
|
+
iLike: "iLike";
|
|
647
|
+
notILike: "notILike";
|
|
648
|
+
eq: "eq";
|
|
649
|
+
ne: "ne";
|
|
650
|
+
isEmpty: "isEmpty";
|
|
651
|
+
isNotEmpty: "isNotEmpty";
|
|
652
|
+
lt: "lt";
|
|
653
|
+
lte: "lte";
|
|
654
|
+
gt: "gt";
|
|
655
|
+
gte: "gte";
|
|
656
|
+
isBetween: "isBetween";
|
|
657
|
+
isRelativeToToday: "isRelativeToToday";
|
|
658
|
+
inArray: "inArray";
|
|
659
|
+
notInArray: "notInArray";
|
|
660
|
+
}>;
|
|
661
|
+
filterId: z.ZodString;
|
|
662
|
+
}, z.core.$strip>>>;
|
|
663
|
+
joinOperator: z.ZodDefault<z.ZodEnum<{
|
|
664
|
+
and: "and";
|
|
665
|
+
or: "or";
|
|
666
|
+
}>>;
|
|
667
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
668
|
+
}, z.core.$strip>;
|
|
543
669
|
/**
|
|
544
670
|
* 创建 CRUD Router
|
|
545
671
|
*
|
|
@@ -552,7 +678,7 @@ type CrudRouterReturn = ReturnType<typeof router> & {
|
|
|
552
678
|
* @typeParam TInsert - 创建输入类型
|
|
553
679
|
* @typeParam TUpdate - 更新输入类型
|
|
554
680
|
*/
|
|
555
|
-
declare function createCrudRouter<TTable extends PgTable = PgTable, TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown>(config: CrudRouterConfig<TTable, TContext, TSelect, TInsert, TUpdate>): CrudRouterReturn;
|
|
681
|
+
declare function createCrudRouter<TTable extends PgTable = PgTable, TContext = unknown, TSelect = unknown, TInsert = unknown, TUpdate = unknown, TListInput extends ListInput = ListInput, TGetInput extends GetInput = GetInput, TExportInput extends ExportInput = ExportInput>(config: CrudRouterConfig<TTable, TContext, TSelect, TInsert, TUpdate, TListInput, TGetInput, TExportInput>): CrudRouterReturn;
|
|
556
682
|
//#endregion
|
|
557
683
|
//#region src/lib/middleware-helpers.d.ts
|
|
558
684
|
/**
|
|
@@ -612,34 +738,6 @@ type AnyMiddlewareParams<TContext> = {
|
|
|
612
738
|
declare function composeMiddleware<TContext, TResult>(...middlewares: Array<(params: AnyMiddlewareParams<TContext>) => Promise<any>>): (params: AnyMiddlewareParams<TContext>) => Promise<TResult>;
|
|
613
739
|
//#endregion
|
|
614
740
|
//#region src/types/hook-types.d.ts
|
|
615
|
-
/**
|
|
616
|
-
* Hook 系统类型工具
|
|
617
|
-
*
|
|
618
|
-
* 提供 CrudHookEventMap 类型——从 Zod Schema / Drizzle Table 推导出
|
|
619
|
-
* auto-crud 相关的所有 Hook 事件类型。
|
|
620
|
-
*
|
|
621
|
-
* 下游插件通过 module augmentation 合并到全局 HookEventMap:
|
|
622
|
-
*
|
|
623
|
-
* @example
|
|
624
|
-
* ```typescript
|
|
625
|
-
* import type { CrudHookEventMap } from "@wordrhyme/auto-crud-server";
|
|
626
|
-
* import type { z } from "zod";
|
|
627
|
-
* import type { InferSelectModel } from "drizzle-orm";
|
|
628
|
-
*
|
|
629
|
-
* declare module "@wordrhyme/plugin" {
|
|
630
|
-
* interface HookEventMap extends CrudHookEventMap<
|
|
631
|
-
* "crm",
|
|
632
|
-
* "customers",
|
|
633
|
-
* z.infer<typeof createCustomerSchema>,
|
|
634
|
-
* z.infer<typeof updateCustomerSchema>,
|
|
635
|
-
* InferSelectModel<typeof customerTable>
|
|
636
|
-
* > {}
|
|
637
|
-
* }
|
|
638
|
-
*
|
|
639
|
-
* // 使用效果:
|
|
640
|
-
* // ctx.hooks.emit('crm.customers. ← IDE 自动补全所有 hook 名和 payload
|
|
641
|
-
* ```
|
|
642
|
-
*/
|
|
643
741
|
/**
|
|
644
742
|
* 从 CRUD Schema 类型自动推导 Hook 事件映射
|
|
645
743
|
*
|
|
@@ -652,8 +750,11 @@ declare function composeMiddleware<TContext, TResult>(...middlewares: Array<(par
|
|
|
652
750
|
* @typeParam TCreate - 创建输入类型(z.infer<typeof createSchema>)
|
|
653
751
|
* @typeParam TUpdate - 更新输入类型(z.infer<typeof updateSchema>)
|
|
654
752
|
* @typeParam TSelect - 查询返回类型(InferSelectModel<typeof table>)
|
|
753
|
+
* @typeParam TListInput - list procedure 输入类型
|
|
754
|
+
* @typeParam TGetInput - get procedure 输入类型
|
|
755
|
+
* @typeParam TExportInput - export procedure 输入类型
|
|
655
756
|
*/
|
|
656
|
-
type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCreate, TUpdate, TSelect> = { [K in `${PluginId}.${ResourceName}.beforeCreate`]: TCreate } & { [K in `${PluginId}.${ResourceName}.afterCreate`]: TSelect } & { [K in `${PluginId}.${ResourceName}.beforeUpdate`]: {
|
|
757
|
+
type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCreate, TUpdate, TSelect, TListInput extends ListInput = ListInput, TGetInput extends GetInput = string, TExportInput extends ExportInput = ExportInput> = { [K in `${PluginId}.${ResourceName}.beforeCreate`]: TCreate } & { [K in `${PluginId}.${ResourceName}.afterCreate`]: TSelect } & { [K in `${PluginId}.${ResourceName}.beforeUpdate`]: {
|
|
657
758
|
id: string;
|
|
658
759
|
data: TUpdate;
|
|
659
760
|
} } & { [K in `${PluginId}.${ResourceName}.afterUpdate`]: TSelect } & { [K in `${PluginId}.${ResourceName}.beforeDelete`]: {
|
|
@@ -664,47 +765,18 @@ type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCre
|
|
|
664
765
|
} } & { [K in `${PluginId}.${ResourceName}.delete`]: string } & { [K in `${PluginId}.${ResourceName}.deleteMany`]: string[] } & { [K in `${PluginId}.${ResourceName}.updateMany`]: {
|
|
665
766
|
ids: string[];
|
|
666
767
|
data: TUpdate;
|
|
667
|
-
} } & { [K in `${PluginId}.${ResourceName}.upsert`]: TCreate } & { [K in `${PluginId}.${ResourceName}.get`]:
|
|
668
|
-
page: number;
|
|
669
|
-
perPage: number;
|
|
670
|
-
sort?: Array<{
|
|
671
|
-
id: string;
|
|
672
|
-
desc: boolean;
|
|
673
|
-
}>;
|
|
674
|
-
filters?: Array<{
|
|
675
|
-
id: string;
|
|
676
|
-
value: string | string[];
|
|
677
|
-
variant: string;
|
|
678
|
-
operator: string;
|
|
679
|
-
filterId: string;
|
|
680
|
-
}>;
|
|
681
|
-
joinOperator: "and" | "or";
|
|
682
|
-
} } & { [K in `${PluginId}.${ResourceName}.export`]: {
|
|
683
|
-
sort?: Array<{
|
|
684
|
-
id: string;
|
|
685
|
-
desc: boolean;
|
|
686
|
-
}>;
|
|
687
|
-
filters?: Array<{
|
|
688
|
-
id: string;
|
|
689
|
-
value: string | string[];
|
|
690
|
-
variant: string;
|
|
691
|
-
operator: string;
|
|
692
|
-
filterId: string;
|
|
693
|
-
}>;
|
|
694
|
-
joinOperator?: "and" | "or";
|
|
695
|
-
limit?: number;
|
|
696
|
-
} } & { [K in `${PluginId}.${ResourceName}.import`]: {
|
|
768
|
+
} } & { [K in `${PluginId}.${ResourceName}.upsert`]: TCreate } & { [K in `${PluginId}.${ResourceName}.get`]: TGetInput } & { [K in `${PluginId}.${ResourceName}.list`]: TListInput } & { [K in `${PluginId}.${ResourceName}.export`]: TExportInput } & { [K in `${PluginId}.${ResourceName}.import`]: {
|
|
697
769
|
rows: unknown[];
|
|
698
|
-
onConflict?:
|
|
770
|
+
onConflict?: 'skip' | 'upsert' | 'error';
|
|
699
771
|
} } & { [K in `${PluginId}.${ResourceName}.createMany`]: TCreate[] };
|
|
700
772
|
//#endregion
|
|
701
773
|
//#region src/routers/index.d.ts
|
|
702
|
-
declare const appRouter:
|
|
774
|
+
declare const appRouter: _trpc_server2.TRPCBuiltRouter<{
|
|
703
775
|
ctx: Context;
|
|
704
776
|
meta: object;
|
|
705
|
-
errorShape:
|
|
777
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
706
778
|
transformer: true;
|
|
707
|
-
},
|
|
779
|
+
}, _trpc_server2.TRPCDecorateCreateRouterOptions<{}>>;
|
|
708
780
|
type AppRouter = typeof appRouter;
|
|
709
781
|
//#endregion
|
|
710
|
-
export { type AnyProcedure, type AppRouter, type CreateManyMiddlewareParams, type CreateMiddlewareParams, type CrudHookEventMap, type CrudMiddleware, type CrudOperation, type CrudProcedures, type CrudRouterConfig, type DeleteManyMiddlewareParams, type DeleteMiddlewareParams, type ExportInput, type ExportMiddlewareParams, type ExportResult, type GetMiddlewareParams, type ImportFailedRow, type ImportInput, type ImportMiddlewareParams, type ImportResult, type ListInput, type ListMiddlewareParams, type ListResult, type ProcedureConfig, type ProcedureFactory, type ProcedureMap, type SoftDeleteConfig, type SoftDeleteOption, type UpdateManyMiddlewareParams, type UpdateMiddlewareParams, type UpsertMiddlewareParams, type WriteOperation, afterMiddleware, appRouter, beforeMiddleware, composeMiddleware, createCrudRouter, publicProcedure, router };
|
|
782
|
+
export { type AnyProcedure, type AppRouter, type CreateManyMiddlewareParams, type CreateMiddlewareParams, type CrudHookEventMap, type CrudMiddleware, type CrudOperation, type CrudProcedures, type CrudRouterConfig, type DeleteManyMiddlewareParams, type DeleteMiddlewareParams, type ExportInput, type ExportMiddlewareParams, type ExportResult, type GetInput, type GetMiddlewareParams, type ImportFailedRow, type ImportInput, type ImportMiddlewareParams, type ImportResult, type ListInput, type ListMiddlewareParams, type ListResult, type ProcedureConfig, type ProcedureFactory, type ProcedureMap, type SoftDeleteConfig, type SoftDeleteOption, type UpdateManyMiddlewareParams, type UpdateMiddlewareParams, type UpsertMiddlewareParams, type WriteOperation, afterMiddleware, appRouter, baseExportInputSchema, baseGetInputSchema, baseListInputSchema, beforeMiddleware, composeMiddleware, createCrudRouter, publicProcedure, router };
|