@wordrhyme/auto-crud-server 2.2.0 → 2.3.0

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 CHANGED
@@ -1338,3 +1338,25 @@ The database provider must implement governed `$counts(table, predicates)`, appl
1338
1338
  为 CRUD extension provider 实现 `buildConditions({ id, entityId, filters, joinOperator, search })`,返回 `{ filter?: SQL, search?: SQL }`。这属于可信进程内接口,客户端不能提供 SQL。使用 `entityId` 与标签关联表构造相关 `EXISTS` 查询,列表、总数和导出会在原有数据权限条件内组合这些谓词,无须先取一批实体 ID。
1339
1339
 
1340
1340
  扩展筛选统一通过既有 `buildConditions` 接口生成 SQL。标签字段不直接映射为基础表列,应由扩展 provider 提供条件和读取投影;赋值服务负责校验租户、字段及记录更新权限。
1341
+
1342
+ ## 按请求补充查询条件
1343
+
1344
+ `query({ ctx, input, operation })` 在守卫之后执行,`operation` 为 `list` 或 `export`,`input` 来自对应输入 schema 和 middleware。返回的 `where` 始终与普通筛选、搜索、scope 求交集,先于列表与导出总数、分页及导出限制执行。未配置时保持原行为。
1345
+
1346
+ ```ts
1347
+ createCrudRouter({
1348
+ table: products,
1349
+ listInputSchema,
1350
+ exportInputSchema,
1351
+ query: async ({ ctx, input }) => {
1352
+ if (!input.image) return {};
1353
+ const distance = await imageDistance(ctx, input.image);
1354
+ return {
1355
+ where: sql`${distance} <= ${0.65}`,
1356
+ ...(!input.sort?.length ? { orderBy: [asc(distance), asc(products.id)] } : {}),
1357
+ };
1358
+ },
1359
+ });
1360
+ ```
1361
+
1362
+ `orderBy` 提供时替换默认排序;权限和作用域不会被覆盖。SQL 表达式必须通过当前数据库 capability 的治理机制构造,不能把其他租户或无权读取的数据带入派生条件。本扩展只作用于列表及导出;批量 counts 仍使用其原有输入契约。
package/dist/index.cjs CHANGED
@@ -1489,11 +1489,17 @@ function createCrudRouter(config) {
1489
1489
  list: resolved.procedureFactory("list").input(resolvedListInputSchema).output(listOutputSchema).query(async ({ ctx, input }) => {
1490
1490
  await withGuard(ctx, "list");
1491
1491
  const doList = async (listInput$1) => {
1492
- const { effectiveInput, where } = await queryWhere(ctx, listInput$1);
1492
+ const extra = await config.query?.({
1493
+ ctx,
1494
+ input: listInput$1,
1495
+ operation: "list"
1496
+ });
1497
+ const { effectiveInput, where: baseWhere } = await queryWhere(ctx, listInput$1);
1498
+ const where = combineConditions(baseWhere, extra?.where);
1493
1499
  const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
1494
1500
  let query = ctx.db.select().from(table).$dynamic();
1495
1501
  if (where) query = query.where(where);
1496
- const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
1502
+ const orderByExpressions = extra?.orderBy ?? buildOrderByExpressions(effectiveInput.sort, true);
1497
1503
  if (orderByExpressions.length > 0) query = query.orderBy(...orderByExpressions);
1498
1504
  let countQuery = ctx.db.select({ count: drizzle_orm.sql`count(*)::int` }).from(table).$dynamic();
1499
1505
  if (where) countQuery = countQuery.where(where);
@@ -1778,6 +1784,11 @@ function createCrudRouter(config) {
1778
1784
  export: resolved.procedureFactory("export").input(resolvedExportInputSchema).output(exportOutputSchema).query(async ({ ctx, input }) => {
1779
1785
  await withGuard(ctx, "export");
1780
1786
  const doExport = async (exportInput$1) => {
1787
+ const extra = await config.query?.({
1788
+ ctx,
1789
+ input: exportInput$1,
1790
+ operation: "export"
1791
+ });
1781
1792
  const columns = await querySearch(ctx, exportInput$1.search);
1782
1793
  const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, {
1783
1794
  page: 1,
@@ -1803,10 +1814,10 @@ function createCrudRouter(config) {
1803
1814
  resolveDateRange: ctx.resolveDateRange
1804
1815
  }) : void 0;
1805
1816
  const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
1806
- const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
1817
+ const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition, extra?.where));
1807
1818
  let query = ctx.db.select().from(table).$dynamic();
1808
1819
  if (where) query = query.where(where);
1809
- const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, false);
1820
+ const orderByExpressions = extra?.orderBy ?? buildOrderByExpressions(effectiveInput.sort, false);
1810
1821
  if (orderByExpressions.length > 0) query = query.orderBy(...orderByExpressions);
1811
1822
  let countQuery = ctx.db.select({ count: drizzle_orm.sql`count(*)::int` }).from(table).$dynamic();
1812
1823
  if (where) countQuery = countQuery.where(where);
package/dist/index.d.cts CHANGED
@@ -675,6 +675,20 @@ interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown,
675
675
  * - 默认查询逻辑只读取基础字段,额外字段会保留给 middleware.export 使用
676
676
  */
677
677
  exportInputSchema?: z.ZodType<TExportInput>;
678
+ /** Additional request conditions, ANDed before count/pagination/export limits.
679
+ * orderBy, when provided, replaces the normal ordering. Scope and guards still apply.
680
+ */
681
+ query?: (params: {
682
+ ctx: TContext;
683
+ input: TListInput | TExportInput;
684
+ operation: 'list' | 'export';
685
+ }) => {
686
+ where?: SQL;
687
+ orderBy?: SQL[];
688
+ } | Promise<{
689
+ where?: SQL;
690
+ orderBy?: SQL[];
691
+ }>;
678
692
  /**
679
693
  * Procedure 配置(统一字段)
680
694
  *
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as _trpc_server0 from "@trpc/server";
1
+ import * as _trpc_server2 from "@trpc/server";
2
2
  import { AnyColumn, SQL, Table } from "drizzle-orm";
3
3
  import { z } from "zod";
4
4
  import superjson from "superjson";
@@ -41,13 +41,13 @@ interface Context {
41
41
  db: PostgresJsDatabase<Record<string, never>>;
42
42
  resolveDateRange?: DateRangeResolver;
43
43
  }
44
- declare const router: _trpc_server0.TRPCRouterBuilder<{
44
+ declare const router: _trpc_server2.TRPCRouterBuilder<{
45
45
  ctx: Context;
46
46
  meta: object;
47
- errorShape: _trpc_server0.TRPCDefaultErrorShape;
47
+ errorShape: _trpc_server2.TRPCDefaultErrorShape;
48
48
  transformer: true;
49
49
  }>;
50
- declare const publicProcedure: _trpc_server0.TRPCProcedureBuilder<Context, object, object, _trpc_server0.TRPCUnsetMarker, _trpc_server0.TRPCUnsetMarker, _trpc_server0.TRPCUnsetMarker, _trpc_server0.TRPCUnsetMarker, false>;
50
+ declare const publicProcedure: _trpc_server2.TRPCProcedureBuilder<Context, object, object, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, false>;
51
51
  //#endregion
52
52
  //#region src/types/config.d.ts
53
53
  type CrudOperation = 'list' | 'get' | 'create' | 'update' | 'delete' | 'deleteMany' | 'updateMany' | 'upsert' | 'export' | 'import' | 'createMany';
@@ -676,6 +676,20 @@ interface CrudRouterConfig<TTable extends PgTable = PgTable, TContext = unknown,
676
676
  * - 默认查询逻辑只读取基础字段,额外字段会保留给 middleware.export 使用
677
677
  */
678
678
  exportInputSchema?: z.ZodType<TExportInput>;
679
+ /** Additional request conditions, ANDed before count/pagination/export limits.
680
+ * orderBy, when provided, replaces the normal ordering. Scope and guards still apply.
681
+ */
682
+ query?: (params: {
683
+ ctx: TContext;
684
+ input: TListInput | TExportInput;
685
+ operation: 'list' | 'export';
686
+ }) => {
687
+ where?: SQL;
688
+ orderBy?: SQL[];
689
+ } | Promise<{
690
+ where?: SQL;
691
+ orderBy?: SQL[];
692
+ }>;
679
693
  /**
680
694
  * Procedure 配置(统一字段)
681
695
  *
@@ -1148,12 +1162,12 @@ type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCre
1148
1162
  } } & { [K in `${PluginId}.${ResourceName}.createMany`]: TCreate[] };
1149
1163
  //#endregion
1150
1164
  //#region src/routers/index.d.ts
1151
- declare const appRouter: _trpc_server0.TRPCBuiltRouter<{
1165
+ declare const appRouter: _trpc_server2.TRPCBuiltRouter<{
1152
1166
  ctx: Context;
1153
1167
  meta: object;
1154
- errorShape: _trpc_server0.TRPCDefaultErrorShape;
1168
+ errorShape: _trpc_server2.TRPCDefaultErrorShape;
1155
1169
  transformer: true;
1156
- }, _trpc_server0.TRPCDecorateCreateRouterOptions<{}>>;
1170
+ }, _trpc_server2.TRPCDecorateCreateRouterOptions<{}>>;
1157
1171
  type AppRouter = typeof appRouter;
1158
1172
  //#endregion
1159
1173
  export { type AnyProcedure, type AppRouter, type CountQuery, type CountsInput, type CountsResult, type CreateManyMiddlewareParams, type CreateMiddlewareParams, type CrudCapabilityMetadata, type CrudColumnCapability, type CrudColumnConfig, type CrudColumnExpression, type CrudColumnRef, type CrudExtensionFilter, type CrudExtensionMetadata, type CrudExtensionValueWrite, type CrudExtensionsConfig, type CrudExtensionsProvider, type CrudHookEventMap, type CrudMiddleware, type CrudOperation, type CrudProcedures, type CrudResourceMetadata, type CrudRouterConfig, type DateFilterRange, type DateRangeResolver, type DeleteManyMiddlewareParams, type DeleteMiddlewareParams, type ExportInput, type ExportMiddlewareParams, type ExportResult, type ExtendedColumnFilter, type FilterOperator, type FilterVariant, type GetInput, type GetMiddlewareParams, type ImportFailedRow, type ImportInput, type ImportIssue, 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, baseCountsInputSchema, baseExportInputSchema, baseGetInputSchema, baseListInputSchema, beforeMiddleware, composeMiddleware, createCrudRouter, publicProcedure, router, tagFilterValueSchema };
package/dist/index.js CHANGED
@@ -1458,11 +1458,17 @@ function createCrudRouter(config) {
1458
1458
  list: resolved.procedureFactory("list").input(resolvedListInputSchema).output(listOutputSchema).query(async ({ ctx, input }) => {
1459
1459
  await withGuard(ctx, "list");
1460
1460
  const doList = async (listInput$1) => {
1461
- const { effectiveInput, where } = await queryWhere(ctx, listInput$1);
1461
+ const extra = await config.query?.({
1462
+ ctx,
1463
+ input: listInput$1,
1464
+ operation: "list"
1465
+ });
1466
+ const { effectiveInput, where: baseWhere } = await queryWhere(ctx, listInput$1);
1467
+ const where = combineConditions(baseWhere, extra?.where);
1462
1468
  const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
1463
1469
  let query = ctx.db.select().from(table).$dynamic();
1464
1470
  if (where) query = query.where(where);
1465
- const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
1471
+ const orderByExpressions = extra?.orderBy ?? buildOrderByExpressions(effectiveInput.sort, true);
1466
1472
  if (orderByExpressions.length > 0) query = query.orderBy(...orderByExpressions);
1467
1473
  let countQuery = ctx.db.select({ count: sql`count(*)::int` }).from(table).$dynamic();
1468
1474
  if (where) countQuery = countQuery.where(where);
@@ -1747,6 +1753,11 @@ function createCrudRouter(config) {
1747
1753
  export: resolved.procedureFactory("export").input(resolvedExportInputSchema).output(exportOutputSchema).query(async ({ ctx, input }) => {
1748
1754
  await withGuard(ctx, "export");
1749
1755
  const doExport = async (exportInput$1) => {
1756
+ const extra = await config.query?.({
1757
+ ctx,
1758
+ input: exportInput$1,
1759
+ operation: "export"
1760
+ });
1750
1761
  const columns = await querySearch(ctx, exportInput$1.search);
1751
1762
  const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, {
1752
1763
  page: 1,
@@ -1772,10 +1783,10 @@ function createCrudRouter(config) {
1772
1783
  resolveDateRange: ctx.resolveDateRange
1773
1784
  }) : void 0;
1774
1785
  const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
1775
- const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
1786
+ const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition, extra?.where));
1776
1787
  let query = ctx.db.select().from(table).$dynamic();
1777
1788
  if (where) query = query.where(where);
1778
- const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, false);
1789
+ const orderByExpressions = extra?.orderBy ?? buildOrderByExpressions(effectiveInput.sort, false);
1779
1790
  if (orderByExpressions.length > 0) query = query.orderBy(...orderByExpressions);
1780
1791
  let countQuery = ctx.db.select({ count: sql`count(*)::int` }).from(table).$dynamic();
1781
1792
  if (where) countQuery = countQuery.where(where);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wordrhyme/auto-crud-server",
3
3
  "type": "module",
4
- "version": "2.2.0",
4
+ "version": "2.3.0",
5
5
  "description": "tRPC server utilities for auto-crud - automatic CRUD routers for Drizzle ORM",
6
6
  "author": "wordrhyme",
7
7
  "license": "MIT",
@@ -49,10 +49,10 @@
49
49
  "vitest": "^4.0.18",
50
50
  "zod": "^4.3.6",
51
51
  "@internal/eslint-config": "0.3.0",
52
+ "@internal/prettier-config": "0.0.1",
52
53
  "@internal/tsconfig": "0.1.0",
53
54
  "@internal/tsdown-config": "0.1.0",
54
- "@internal/vitest-config": "0.1.0",
55
- "@internal/prettier-config": "0.0.1"
55
+ "@internal/vitest-config": "0.1.0"
56
56
  },
57
57
  "prettier": "@internal/prettier-config",
58
58
  "scripts": {