@wordrhyme/auto-crud-server 2.1.0 → 2.1.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 +5 -0
- package/dist/index.cjs +27 -5
- package/dist/index.d.cts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +27 -5
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1145,6 +1145,11 @@ create index tasks_tenant_created_at_id_idx
|
|
|
1145
1145
|
应为真实文本列配置 `pg_trgm` GIN 索引;JSON/custom expression 需要与生成 SQL 一致的
|
|
1146
1146
|
表达式索引。
|
|
1147
1147
|
|
|
1148
|
+
可信扩展 provider 的 `getMetadata()` 可以通过 `fields[fieldId].search: false`
|
|
1149
|
+
关闭某个基础字段的全局搜索。该覆盖同时收窄能力元数据、列表、数量统计及导出查询;
|
|
1150
|
+
未声明覆盖的字段沿用 router 的搜索白名单。`search: true` 不会把白名单以外的基础字段
|
|
1151
|
+
加入搜索,router 的 `search: false` 仍关闭整个搜索能力。客户端不能通过请求参数扩大范围。
|
|
1152
|
+
|
|
1148
1153
|
扩展字段筛选与搜索使用可信服务端 provider 的 `buildConditions` 契约。Host 接收
|
|
1149
1154
|
CRUD target、原表 ID 列、扩展筛选条件和搜索词,返回组织隔离的 SQL `filter` / `search`
|
|
1150
1155
|
条件(例如关联 `EXISTS`),不返回全量 ID 数组。基础筛选与扩展筛选按 `joinOperator`
|
package/dist/index.cjs
CHANGED
|
@@ -777,10 +777,22 @@ function mergeCrudCapabilities(base, extension, disabled) {
|
|
|
777
777
|
sort: mergeFieldCapability(base.sort, extension?.sort, disabled.sort)
|
|
778
778
|
};
|
|
779
779
|
}
|
|
780
|
+
function allowsSearch(fields, id) {
|
|
781
|
+
const field = fields?.[id];
|
|
782
|
+
return !(field && typeof field === "object" && "search" in field && field.search === false);
|
|
783
|
+
}
|
|
780
784
|
function withCrudCapabilities(metadata, capabilities, disabled) {
|
|
785
|
+
const merged = mergeCrudCapabilities(capabilities, metadata?.capabilities, disabled);
|
|
786
|
+
const fields = merged.search.fields.filter((id) => allowsSearch(metadata?.fields, id));
|
|
781
787
|
return {
|
|
782
788
|
...metadata ?? {},
|
|
783
|
-
capabilities:
|
|
789
|
+
capabilities: {
|
|
790
|
+
...merged,
|
|
791
|
+
search: {
|
|
792
|
+
enabled: merged.search.enabled && (merged.search.fields.length === 0 || fields.length > 0 || metadata?.capabilities?.search?.enabled === true && metadata.capabilities.search.fields.length === 0),
|
|
793
|
+
fields
|
|
794
|
+
}
|
|
795
|
+
}
|
|
784
796
|
};
|
|
785
797
|
}
|
|
786
798
|
function getTableColumn(table, columnId) {
|
|
@@ -1328,8 +1340,17 @@ function createCrudRouter(config) {
|
|
|
1328
1340
|
message: `Forbidden: Cannot ${operation} this resource`
|
|
1329
1341
|
});
|
|
1330
1342
|
};
|
|
1343
|
+
const querySearch = async (ctx, search) => {
|
|
1344
|
+
if (!search?.trim() || !Array.isArray(searchColumns)) return searchColumns;
|
|
1345
|
+
const target = readCrudTarget(config);
|
|
1346
|
+
const provider = resolveCrudExtensions(ctx, config);
|
|
1347
|
+
if (!target || !provider?.getMetadata) return searchColumns;
|
|
1348
|
+
const metadata = await provider.getMetadata({ id: target.id });
|
|
1349
|
+
return searchColumns.filter((column) => allowsSearch(metadata.fields, getColumnRefId(column)));
|
|
1350
|
+
};
|
|
1331
1351
|
const queryWhere = async (ctx, input) => {
|
|
1332
|
-
const
|
|
1352
|
+
const columns = await querySearch(ctx, input.search);
|
|
1353
|
+
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, input);
|
|
1333
1354
|
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1334
1355
|
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1335
1356
|
table,
|
|
@@ -1346,7 +1367,7 @@ function createCrudRouter(config) {
|
|
|
1346
1367
|
},
|
|
1347
1368
|
resolveDateRange: ctx.resolveDateRange
|
|
1348
1369
|
}) : void 0;
|
|
1349
|
-
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput,
|
|
1370
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
|
|
1350
1371
|
return {
|
|
1351
1372
|
effectiveInput,
|
|
1352
1373
|
where: buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition))
|
|
@@ -1677,7 +1698,8 @@ function createCrudRouter(config) {
|
|
|
1677
1698
|
export: resolved.procedureFactory("export").input(resolvedExportInputSchema).output(exportOutputSchema).query(async ({ ctx, input }) => {
|
|
1678
1699
|
await withGuard(ctx, "export");
|
|
1679
1700
|
const doExport = async (exportInput$1) => {
|
|
1680
|
-
const
|
|
1701
|
+
const columns = await querySearch(ctx, exportInput$1.search);
|
|
1702
|
+
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, {
|
|
1681
1703
|
page: 1,
|
|
1682
1704
|
perPage: Math.min(Math.max(exportInput$1.limit ?? maxExportSize, 1), maxExportSize),
|
|
1683
1705
|
joinOperator: exportInput$1.joinOperator ?? "and",
|
|
@@ -1700,7 +1722,7 @@ function createCrudRouter(config) {
|
|
|
1700
1722
|
},
|
|
1701
1723
|
resolveDateRange: ctx.resolveDateRange
|
|
1702
1724
|
}) : void 0;
|
|
1703
|
-
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput,
|
|
1725
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
|
|
1704
1726
|
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1705
1727
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1706
1728
|
if (where) query = query.where(where);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnyColumn, SQL, Table } from "drizzle-orm";
|
|
2
2
|
import { PgTable } from "drizzle-orm/pg-core";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import * as
|
|
4
|
+
import * as _trpc_server2 from "@trpc/server";
|
|
5
5
|
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
6
6
|
|
|
7
7
|
//#region src/lib/filter-columns.d.ts
|
|
@@ -40,13 +40,13 @@ interface Context {
|
|
|
40
40
|
db: PostgresJsDatabase<Record<string, never>>;
|
|
41
41
|
resolveDateRange?: DateRangeResolver;
|
|
42
42
|
}
|
|
43
|
-
declare const router:
|
|
43
|
+
declare const router: _trpc_server2.TRPCRouterBuilder<{
|
|
44
44
|
ctx: Context;
|
|
45
45
|
meta: object;
|
|
46
|
-
errorShape:
|
|
46
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
47
47
|
transformer: true;
|
|
48
48
|
}>;
|
|
49
|
-
declare const publicProcedure:
|
|
49
|
+
declare const publicProcedure: _trpc_server2.TRPCProcedureBuilder<Context, object, object, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, _trpc_server2.TRPCUnsetMarker, false>;
|
|
50
50
|
//#endregion
|
|
51
51
|
//#region src/types/config.d.ts
|
|
52
52
|
type CrudOperation = 'list' | 'get' | 'create' | 'update' | 'delete' | 'deleteMany' | 'updateMany' | 'upsert' | 'export' | 'import' | 'createMany';
|
|
@@ -1079,12 +1079,12 @@ type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCre
|
|
|
1079
1079
|
} } & { [K in `${PluginId}.${ResourceName}.createMany`]: TCreate[] };
|
|
1080
1080
|
//#endregion
|
|
1081
1081
|
//#region src/routers/index.d.ts
|
|
1082
|
-
declare const appRouter:
|
|
1082
|
+
declare const appRouter: _trpc_server2.TRPCBuiltRouter<{
|
|
1083
1083
|
ctx: Context;
|
|
1084
1084
|
meta: object;
|
|
1085
|
-
errorShape:
|
|
1085
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
1086
1086
|
transformer: true;
|
|
1087
|
-
},
|
|
1087
|
+
}, _trpc_server2.TRPCDecorateCreateRouterOptions<{}>>;
|
|
1088
1088
|
type AppRouter = typeof appRouter;
|
|
1089
1089
|
//#endregion
|
|
1090
1090
|
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 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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
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:
|
|
44
|
+
declare const router: _trpc_server2.TRPCRouterBuilder<{
|
|
45
45
|
ctx: Context;
|
|
46
46
|
meta: object;
|
|
47
|
-
errorShape:
|
|
47
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
48
48
|
transformer: true;
|
|
49
49
|
}>;
|
|
50
|
-
declare const publicProcedure:
|
|
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';
|
|
@@ -1080,12 +1080,12 @@ type CrudHookEventMap<PluginId extends string, ResourceName extends string, TCre
|
|
|
1080
1080
|
} } & { [K in `${PluginId}.${ResourceName}.createMany`]: TCreate[] };
|
|
1081
1081
|
//#endregion
|
|
1082
1082
|
//#region src/routers/index.d.ts
|
|
1083
|
-
declare const appRouter:
|
|
1083
|
+
declare const appRouter: _trpc_server2.TRPCBuiltRouter<{
|
|
1084
1084
|
ctx: Context;
|
|
1085
1085
|
meta: object;
|
|
1086
|
-
errorShape:
|
|
1086
|
+
errorShape: _trpc_server2.TRPCDefaultErrorShape;
|
|
1087
1087
|
transformer: true;
|
|
1088
|
-
},
|
|
1088
|
+
}, _trpc_server2.TRPCDecorateCreateRouterOptions<{}>>;
|
|
1089
1089
|
type AppRouter = typeof appRouter;
|
|
1090
1090
|
//#endregion
|
|
1091
1091
|
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 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 };
|
package/dist/index.js
CHANGED
|
@@ -746,10 +746,22 @@ function mergeCrudCapabilities(base, extension, disabled) {
|
|
|
746
746
|
sort: mergeFieldCapability(base.sort, extension?.sort, disabled.sort)
|
|
747
747
|
};
|
|
748
748
|
}
|
|
749
|
+
function allowsSearch(fields, id) {
|
|
750
|
+
const field = fields?.[id];
|
|
751
|
+
return !(field && typeof field === "object" && "search" in field && field.search === false);
|
|
752
|
+
}
|
|
749
753
|
function withCrudCapabilities(metadata, capabilities, disabled) {
|
|
754
|
+
const merged = mergeCrudCapabilities(capabilities, metadata?.capabilities, disabled);
|
|
755
|
+
const fields = merged.search.fields.filter((id) => allowsSearch(metadata?.fields, id));
|
|
750
756
|
return {
|
|
751
757
|
...metadata ?? {},
|
|
752
|
-
capabilities:
|
|
758
|
+
capabilities: {
|
|
759
|
+
...merged,
|
|
760
|
+
search: {
|
|
761
|
+
enabled: merged.search.enabled && (merged.search.fields.length === 0 || fields.length > 0 || metadata?.capabilities?.search?.enabled === true && metadata.capabilities.search.fields.length === 0),
|
|
762
|
+
fields
|
|
763
|
+
}
|
|
764
|
+
}
|
|
753
765
|
};
|
|
754
766
|
}
|
|
755
767
|
function getTableColumn(table, columnId) {
|
|
@@ -1297,8 +1309,17 @@ function createCrudRouter(config) {
|
|
|
1297
1309
|
message: `Forbidden: Cannot ${operation} this resource`
|
|
1298
1310
|
});
|
|
1299
1311
|
};
|
|
1312
|
+
const querySearch = async (ctx, search) => {
|
|
1313
|
+
if (!search?.trim() || !Array.isArray(searchColumns)) return searchColumns;
|
|
1314
|
+
const target = readCrudTarget(config);
|
|
1315
|
+
const provider = resolveCrudExtensions(ctx, config);
|
|
1316
|
+
if (!target || !provider?.getMetadata) return searchColumns;
|
|
1317
|
+
const metadata = await provider.getMetadata({ id: target.id });
|
|
1318
|
+
return searchColumns.filter((column) => allowsSearch(metadata.fields, getColumnRefId(column)));
|
|
1319
|
+
};
|
|
1300
1320
|
const queryWhere = async (ctx, input) => {
|
|
1301
|
-
const
|
|
1321
|
+
const columns = await querySearch(ctx, input.search);
|
|
1322
|
+
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, input);
|
|
1302
1323
|
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1303
1324
|
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1304
1325
|
table,
|
|
@@ -1315,7 +1336,7 @@ function createCrudRouter(config) {
|
|
|
1315
1336
|
},
|
|
1316
1337
|
resolveDateRange: ctx.resolveDateRange
|
|
1317
1338
|
}) : void 0;
|
|
1318
|
-
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput,
|
|
1339
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
|
|
1319
1340
|
return {
|
|
1320
1341
|
effectiveInput,
|
|
1321
1342
|
where: buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition))
|
|
@@ -1646,7 +1667,8 @@ function createCrudRouter(config) {
|
|
|
1646
1667
|
export: resolved.procedureFactory("export").input(resolvedExportInputSchema).output(exportOutputSchema).query(async ({ ctx, input }) => {
|
|
1647
1668
|
await withGuard(ctx, "export");
|
|
1648
1669
|
const doExport = async (exportInput$1) => {
|
|
1649
|
-
const
|
|
1670
|
+
const columns = await querySearch(ctx, exportInput$1.search);
|
|
1671
|
+
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, columns, {
|
|
1650
1672
|
page: 1,
|
|
1651
1673
|
perPage: Math.min(Math.max(exportInput$1.limit ?? maxExportSize, 1), maxExportSize),
|
|
1652
1674
|
joinOperator: exportInput$1.joinOperator ?? "and",
|
|
@@ -1669,7 +1691,7 @@ function createCrudRouter(config) {
|
|
|
1669
1691
|
},
|
|
1670
1692
|
resolveDateRange: ctx.resolveDateRange
|
|
1671
1693
|
}) : void 0;
|
|
1672
|
-
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput,
|
|
1694
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, columns);
|
|
1673
1695
|
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1674
1696
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1675
1697
|
if (where) query = query.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.1.
|
|
4
|
+
"version": "2.1.1",
|
|
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/tsdown-config": "0.1.0",
|
|
53
52
|
"@internal/tsconfig": "0.1.0",
|
|
54
|
-
"@internal/
|
|
55
|
-
"@internal/
|
|
53
|
+
"@internal/prettier-config": "0.0.1",
|
|
54
|
+
"@internal/tsdown-config": "0.1.0",
|
|
55
|
+
"@internal/vitest-config": "0.1.0"
|
|
56
56
|
},
|
|
57
57
|
"prettier": "@internal/prettier-config",
|
|
58
58
|
"scripts": {
|