@wordrhyme/auto-crud-server 1.5.1 → 2.0.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 +12 -0
- package/dist/index.cjs +63 -53
- package/dist/index.d.cts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +63 -53
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1145,6 +1145,18 @@ create index tasks_tenant_created_at_id_idx
|
|
|
1145
1145
|
应为真实文本列配置 `pg_trgm` GIN 索引;JSON/custom expression 需要与生成 SQL 一致的
|
|
1146
1146
|
表达式索引。
|
|
1147
1147
|
|
|
1148
|
+
扩展字段筛选与搜索使用可信服务端 provider 的 `buildConditions` 契约。Host 接收
|
|
1149
|
+
CRUD target、原表 ID 列、扩展筛选条件和搜索词,返回组织隔离的 SQL `filter` / `search`
|
|
1150
|
+
条件(例如关联 `EXISTS`),不返回全量 ID 数组。基础筛选与扩展筛选按 `joinOperator`
|
|
1151
|
+
组合;基础搜索与扩展搜索按 OR 组合,两组再按 AND 组合。最终仍经过原有 scope、
|
|
1152
|
+
软删除和 Host 的权限治理,列表、精确总数与导出使用同一条件。
|
|
1153
|
+
|
|
1154
|
+
这个契约只适用于同进程可信 Host,不接受客户端 SQL,也不授予插件 Core 表访问权限。
|
|
1155
|
+
扩展条件不设匹配 ID 数量上限;数据库负载仍需通过索引和查询执行策略管理。
|
|
1156
|
+
Host 与此版本必须配套升级:仅提供旧 `matchEntityIds` / `searchEntityIds` 的 provider
|
|
1157
|
+
执行扩展查询时会收到 `PRECONDITION_FAILED`,不会静默降级到可能截断的 ID 查询。
|
|
1158
|
+
纯基础表查询、分页大小和导出数量限制保持原有行为。
|
|
1159
|
+
|
|
1148
1160
|
扩展字段 provider 可以实现批量写接口,避免 `createMany`、`updateMany` 和 `import`
|
|
1149
1161
|
逐行访问数据库。未实现时仍兼容旧的 `saveExtraValues`:
|
|
1150
1162
|
|
package/dist/index.cjs
CHANGED
|
@@ -812,14 +812,11 @@ function resolveColumnTarget({ table, columnId, allowedColumns }) {
|
|
|
812
812
|
}
|
|
813
813
|
return getTableColumn(table, columnId);
|
|
814
814
|
}
|
|
815
|
-
const
|
|
816
|
-
const
|
|
815
|
+
const CRUD_EXTENSION_FILTER = Symbol("auto-crud-extension-filter");
|
|
816
|
+
const CRUD_EXTENSION_SEARCH = Symbol("auto-crud-extension-search");
|
|
817
817
|
function isObjectRecord(value) {
|
|
818
818
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
819
819
|
}
|
|
820
|
-
function isCrudExtensionIdFilter(filter, idField) {
|
|
821
|
-
return filter.id === idField && filter.filterId === CRUD_EXTENSION_FILTER_ID;
|
|
822
|
-
}
|
|
823
820
|
function readCrudTarget(config) {
|
|
824
821
|
const id = config.id;
|
|
825
822
|
if (!id) return null;
|
|
@@ -997,10 +994,12 @@ function createCrudBatchTransactionInput(inputs) {
|
|
|
997
994
|
extraValues: inputs.some((input) => input.extraValues) ? {} : null
|
|
998
995
|
};
|
|
999
996
|
}
|
|
1000
|
-
function
|
|
997
|
+
function readProjectionValue(value) {
|
|
1001
998
|
if (!isObjectRecord(value)) return value;
|
|
1002
|
-
if ("
|
|
999
|
+
if (typeof value.type === "string" && value.type !== "ref" && "value" in value) return value.value;
|
|
1000
|
+
if ("refId" in value && value.refId !== void 0) return value.refId;
|
|
1003
1001
|
if ("value" in value) return value.value;
|
|
1002
|
+
if ("display" in value && value.display !== null && value.display !== void 0) return value.display;
|
|
1004
1003
|
return value;
|
|
1005
1004
|
}
|
|
1006
1005
|
async function enrichCrudRows(ctx, config, idField, rows) {
|
|
@@ -1014,14 +1013,13 @@ async function enrichCrudRows(ctx, config, idField, rows) {
|
|
|
1014
1013
|
id: target.id,
|
|
1015
1014
|
entityIds
|
|
1016
1015
|
});
|
|
1017
|
-
const metadata = await provider.getMetadata?.({ id: target.id });
|
|
1018
1016
|
return rows.map((row) => {
|
|
1019
1017
|
const rowId = row[idField];
|
|
1020
1018
|
const projected = rowId !== null && rowId !== void 0 ? projections[String(rowId)] : void 0;
|
|
1021
1019
|
if (!projected || Object.keys(projected).length === 0) return row;
|
|
1022
1020
|
return {
|
|
1023
1021
|
...row,
|
|
1024
|
-
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field,
|
|
1022
|
+
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field, readProjectionValue(value)]))
|
|
1025
1023
|
};
|
|
1026
1024
|
});
|
|
1027
1025
|
}
|
|
@@ -1057,26 +1055,46 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1057
1055
|
filters: baseFilters
|
|
1058
1056
|
};
|
|
1059
1057
|
const provider = resolveCrudExtensions(ctx, config);
|
|
1060
|
-
const
|
|
1061
|
-
if (
|
|
1062
|
-
if (
|
|
1058
|
+
const extensionSearch = search && !searchDisabled;
|
|
1059
|
+
if (!provider?.buildConditions) {
|
|
1060
|
+
if (extensionFilters.length > 0 || extensionSearch && provider?.searchEntityIds) throw new __trpc_server.TRPCError({
|
|
1063
1061
|
code: "PRECONDITION_FAILED",
|
|
1064
|
-
message: "CRUD extension
|
|
1062
|
+
message: "CRUD extension queries require a provider with buildConditions; upgrade the Host/provider"
|
|
1065
1063
|
});
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1064
|
+
if (extensionSearch && !buildCrudSearchCondition({
|
|
1065
|
+
table,
|
|
1066
|
+
search,
|
|
1067
|
+
searchColumns
|
|
1068
|
+
})) throw new __trpc_server.TRPCError({
|
|
1069
|
+
code: "PRECONDITION_FAILED",
|
|
1070
|
+
message: "CRUD search is not available"
|
|
1071
|
+
});
|
|
1072
|
+
return {
|
|
1073
|
+
...input,
|
|
1074
|
+
filters: baseFilters
|
|
1075
|
+
};
|
|
1072
1076
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1077
|
+
if (extensionFilters.length === 0 && !extensionSearch) return {
|
|
1078
|
+
...input,
|
|
1079
|
+
filters: baseFilters
|
|
1080
|
+
};
|
|
1081
|
+
const entityId = getTableColumn(table, idField);
|
|
1082
|
+
if (!entityId) throw new __trpc_server.TRPCError({
|
|
1083
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
1084
|
+
message: "CRUD identity column is unavailable"
|
|
1085
|
+
});
|
|
1086
|
+
const conditions = await provider.buildConditions({
|
|
1075
1087
|
id: target.id,
|
|
1076
|
-
|
|
1077
|
-
|
|
1088
|
+
entityId,
|
|
1089
|
+
filters: extensionFilters,
|
|
1090
|
+
...input.joinOperator ? { joinOperator: input.joinOperator } : {},
|
|
1091
|
+
...extensionSearch ? { search } : {}
|
|
1078
1092
|
});
|
|
1079
|
-
|
|
1093
|
+
if (extensionFilters.length > 0 && !conditions.filter) throw new __trpc_server.TRPCError({
|
|
1094
|
+
code: "PRECONDITION_FAILED",
|
|
1095
|
+
message: "CRUD extension filter condition is unavailable"
|
|
1096
|
+
});
|
|
1097
|
+
if (extensionSearch && !conditions.search && !buildCrudSearchCondition({
|
|
1080
1098
|
table,
|
|
1081
1099
|
search,
|
|
1082
1100
|
searchColumns
|
|
@@ -1084,37 +1102,25 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1084
1102
|
code: "PRECONDITION_FAILED",
|
|
1085
1103
|
message: "CRUD search is not available"
|
|
1086
1104
|
});
|
|
1087
|
-
const matchedSets = (await Promise.all(matchRequests)).map((matchedIds$1) => new Set(matchedIds$1));
|
|
1088
|
-
const effectiveInput = extensionSearchIds === void 0 ? input : {
|
|
1089
|
-
...input,
|
|
1090
|
-
[CRUD_EXTENSION_SEARCH_IDS]: extensionSearchIds
|
|
1091
|
-
};
|
|
1092
|
-
if (matchedSets.length === 0) return {
|
|
1093
|
-
...effectiveInput,
|
|
1094
|
-
filters: baseFilters
|
|
1095
|
-
};
|
|
1096
|
-
let matchedIds = matchedSets[0] ? [...matchedSets[0]] : [];
|
|
1097
|
-
for (const set of matchedSets.slice(1)) matchedIds = matchedIds.filter((id) => set.has(id));
|
|
1098
1105
|
return {
|
|
1099
|
-
...
|
|
1100
|
-
filters:
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
variant: "multiSelect",
|
|
1104
|
-
operator: "inArray",
|
|
1105
|
-
filterId: CRUD_EXTENSION_FILTER_ID
|
|
1106
|
-
}]
|
|
1106
|
+
...input,
|
|
1107
|
+
filters: baseFilters,
|
|
1108
|
+
...extensionFilters.length > 0 && conditions.filter ? { [CRUD_EXTENSION_FILTER]: conditions.filter } : {},
|
|
1109
|
+
...extensionSearch && conditions.search ? { [CRUD_EXTENSION_SEARCH]: conditions.search } : {}
|
|
1107
1110
|
};
|
|
1108
1111
|
}
|
|
1109
|
-
function
|
|
1112
|
+
function combineExtensionFilter(base, input) {
|
|
1113
|
+
const extension = input[CRUD_EXTENSION_FILTER];
|
|
1114
|
+
return input.joinOperator === "or" ? (0, drizzle_orm.or)(base, extension) : (0, drizzle_orm.and)(base, extension);
|
|
1115
|
+
}
|
|
1116
|
+
function buildCrudSearchWithExtensions(table, input, searchColumns) {
|
|
1110
1117
|
const baseCondition = buildCrudSearchCondition({
|
|
1111
1118
|
table,
|
|
1112
1119
|
search: input.search,
|
|
1113
1120
|
searchColumns
|
|
1114
1121
|
});
|
|
1115
|
-
const
|
|
1116
|
-
if (
|
|
1117
|
-
const extensionCondition = (0, drizzle_orm.inArray)(getTableColumn(table, idField), extensionIds);
|
|
1122
|
+
const extensionCondition = input[CRUD_EXTENSION_SEARCH];
|
|
1123
|
+
if (extensionCondition === void 0) return baseCondition;
|
|
1118
1124
|
return baseCondition ? (0, drizzle_orm.or)(baseCondition, extensionCondition) : extensionCondition;
|
|
1119
1125
|
}
|
|
1120
1126
|
function buildCrudSearchCondition({ table, search, searchColumns }) {
|
|
@@ -1317,8 +1323,8 @@ function createCrudRouter(config) {
|
|
|
1317
1323
|
const doList = async (listInput$1) => {
|
|
1318
1324
|
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, searchColumns, listInput$1);
|
|
1319
1325
|
const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
|
|
1320
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1321
|
-
const
|
|
1326
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1327
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1322
1328
|
table,
|
|
1323
1329
|
filters: validatedFilters,
|
|
1324
1330
|
joinOperator: effectiveInput.joinOperator,
|
|
@@ -1332,7 +1338,9 @@ function createCrudRouter(config) {
|
|
|
1332
1338
|
});
|
|
1333
1339
|
},
|
|
1334
1340
|
resolveDateRange: ctx.resolveDateRange
|
|
1335
|
-
}) : void 0
|
|
1341
|
+
}) : void 0;
|
|
1342
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1343
|
+
const where = buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1336
1344
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1337
1345
|
if (where) query = query.where(where);
|
|
1338
1346
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
|
|
@@ -1602,8 +1610,8 @@ function createCrudRouter(config) {
|
|
|
1602
1610
|
...exportInput$1
|
|
1603
1611
|
});
|
|
1604
1612
|
const effectiveLimit = Math.min(Math.max(effectiveInput.limit ?? maxExportSize, 1), maxExportSize);
|
|
1605
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1606
|
-
const
|
|
1613
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1614
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1607
1615
|
table,
|
|
1608
1616
|
filters: validatedFilters,
|
|
1609
1617
|
joinOperator: effectiveInput.joinOperator ?? "and",
|
|
@@ -1617,7 +1625,9 @@ function createCrudRouter(config) {
|
|
|
1617
1625
|
});
|
|
1618
1626
|
},
|
|
1619
1627
|
resolveDateRange: ctx.resolveDateRange
|
|
1620
|
-
}) : void 0
|
|
1628
|
+
}) : void 0;
|
|
1629
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1630
|
+
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1621
1631
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1622
1632
|
if (where) query = query.where(where);
|
|
1623
1633
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, false);
|
package/dist/index.d.cts
CHANGED
|
@@ -186,8 +186,6 @@ interface ExportInput {
|
|
|
186
186
|
}
|
|
187
187
|
interface CrudExtensionMetadata {
|
|
188
188
|
schema?: unknown;
|
|
189
|
-
/** Field config may opt into preserveReferenceValue: true to return reference
|
|
190
|
-
* IDs for editing and data-source label resolution. Default: display values. */
|
|
191
189
|
fields?: Record<string, unknown>;
|
|
192
190
|
errors?: string[];
|
|
193
191
|
/** 扩展提供的查询能力,会与 router 基础能力合并。 */
|
|
@@ -227,17 +225,44 @@ interface CrudExtensionsProvider {
|
|
|
227
225
|
rows: CrudExtensionValueWrite[];
|
|
228
226
|
tx?: unknown;
|
|
229
227
|
}) => Promise<void>;
|
|
228
|
+
/**
|
|
229
|
+
* Field projections may be raw values or envelopes containing refId, value,
|
|
230
|
+
* and display. Host envelopes with a non-ref type return value directly.
|
|
231
|
+
* Otherwise list/get/export return a defined refId (including null), then
|
|
232
|
+
* value when present. Providers should put editable values in value, including
|
|
233
|
+
* ID arrays for multi-reference fields. display is a legacy fallback only when
|
|
234
|
+
* no raw value exists; consumers resolve labels through their data sources.
|
|
235
|
+
*/
|
|
230
236
|
readProjection?: (input: {
|
|
231
237
|
id: string;
|
|
232
238
|
entityIds: string[];
|
|
233
239
|
fields?: string[];
|
|
234
240
|
}) => Promise<Record<string, Record<string, unknown>>>;
|
|
241
|
+
/**
|
|
242
|
+
* Trusted server-side provider only: compose tenant-scoped SQL predicates.
|
|
243
|
+
* No matching ID arrays are materialized. Conditions join the base query
|
|
244
|
+
* before permissions, count, pagination and export limits are applied.
|
|
245
|
+
* Non-empty filters require a filter condition; a search with no matches
|
|
246
|
+
* should return SQL false. Never accept SQL supplied by an HTTP client.
|
|
247
|
+
*/
|
|
248
|
+
buildConditions?: (input: {
|
|
249
|
+
id: string;
|
|
250
|
+
entityId: AnyColumn;
|
|
251
|
+
filters: CrudExtensionFilter[];
|
|
252
|
+
joinOperator?: 'and' | 'or';
|
|
253
|
+
search?: string;
|
|
254
|
+
}) => Promise<{
|
|
255
|
+
filter?: SQL;
|
|
256
|
+
search?: SQL;
|
|
257
|
+
}>;
|
|
258
|
+
/** Legacy API; list/export require buildConditions for extension matching. */
|
|
235
259
|
matchEntityIds?: (input: {
|
|
236
260
|
id: string;
|
|
237
261
|
filters: CrudExtensionFilter[];
|
|
238
262
|
joinOperator?: 'and' | 'or';
|
|
239
263
|
limit?: number;
|
|
240
264
|
}) => Promise<string[]>;
|
|
265
|
+
/** Legacy ID lookup; not used by the list/export query path. */
|
|
241
266
|
searchEntityIds?: (input: {
|
|
242
267
|
id: string;
|
|
243
268
|
search: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -187,8 +187,6 @@ interface ExportInput {
|
|
|
187
187
|
}
|
|
188
188
|
interface CrudExtensionMetadata {
|
|
189
189
|
schema?: unknown;
|
|
190
|
-
/** Field config may opt into preserveReferenceValue: true to return reference
|
|
191
|
-
* IDs for editing and data-source label resolution. Default: display values. */
|
|
192
190
|
fields?: Record<string, unknown>;
|
|
193
191
|
errors?: string[];
|
|
194
192
|
/** 扩展提供的查询能力,会与 router 基础能力合并。 */
|
|
@@ -228,17 +226,44 @@ interface CrudExtensionsProvider {
|
|
|
228
226
|
rows: CrudExtensionValueWrite[];
|
|
229
227
|
tx?: unknown;
|
|
230
228
|
}) => Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Field projections may be raw values or envelopes containing refId, value,
|
|
231
|
+
* and display. Host envelopes with a non-ref type return value directly.
|
|
232
|
+
* Otherwise list/get/export return a defined refId (including null), then
|
|
233
|
+
* value when present. Providers should put editable values in value, including
|
|
234
|
+
* ID arrays for multi-reference fields. display is a legacy fallback only when
|
|
235
|
+
* no raw value exists; consumers resolve labels through their data sources.
|
|
236
|
+
*/
|
|
231
237
|
readProjection?: (input: {
|
|
232
238
|
id: string;
|
|
233
239
|
entityIds: string[];
|
|
234
240
|
fields?: string[];
|
|
235
241
|
}) => Promise<Record<string, Record<string, unknown>>>;
|
|
242
|
+
/**
|
|
243
|
+
* Trusted server-side provider only: compose tenant-scoped SQL predicates.
|
|
244
|
+
* No matching ID arrays are materialized. Conditions join the base query
|
|
245
|
+
* before permissions, count, pagination and export limits are applied.
|
|
246
|
+
* Non-empty filters require a filter condition; a search with no matches
|
|
247
|
+
* should return SQL false. Never accept SQL supplied by an HTTP client.
|
|
248
|
+
*/
|
|
249
|
+
buildConditions?: (input: {
|
|
250
|
+
id: string;
|
|
251
|
+
entityId: AnyColumn;
|
|
252
|
+
filters: CrudExtensionFilter[];
|
|
253
|
+
joinOperator?: 'and' | 'or';
|
|
254
|
+
search?: string;
|
|
255
|
+
}) => Promise<{
|
|
256
|
+
filter?: SQL;
|
|
257
|
+
search?: SQL;
|
|
258
|
+
}>;
|
|
259
|
+
/** Legacy API; list/export require buildConditions for extension matching. */
|
|
236
260
|
matchEntityIds?: (input: {
|
|
237
261
|
id: string;
|
|
238
262
|
filters: CrudExtensionFilter[];
|
|
239
263
|
joinOperator?: 'and' | 'or';
|
|
240
264
|
limit?: number;
|
|
241
265
|
}) => Promise<string[]>;
|
|
266
|
+
/** Legacy ID lookup; not used by the list/export query path. */
|
|
242
267
|
searchEntityIds?: (input: {
|
|
243
268
|
id: string;
|
|
244
269
|
search: string;
|
package/dist/index.js
CHANGED
|
@@ -781,14 +781,11 @@ function resolveColumnTarget({ table, columnId, allowedColumns }) {
|
|
|
781
781
|
}
|
|
782
782
|
return getTableColumn(table, columnId);
|
|
783
783
|
}
|
|
784
|
-
const
|
|
785
|
-
const
|
|
784
|
+
const CRUD_EXTENSION_FILTER = Symbol("auto-crud-extension-filter");
|
|
785
|
+
const CRUD_EXTENSION_SEARCH = Symbol("auto-crud-extension-search");
|
|
786
786
|
function isObjectRecord(value) {
|
|
787
787
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
788
788
|
}
|
|
789
|
-
function isCrudExtensionIdFilter(filter, idField) {
|
|
790
|
-
return filter.id === idField && filter.filterId === CRUD_EXTENSION_FILTER_ID;
|
|
791
|
-
}
|
|
792
789
|
function readCrudTarget(config) {
|
|
793
790
|
const id = config.id;
|
|
794
791
|
if (!id) return null;
|
|
@@ -966,10 +963,12 @@ function createCrudBatchTransactionInput(inputs) {
|
|
|
966
963
|
extraValues: inputs.some((input) => input.extraValues) ? {} : null
|
|
967
964
|
};
|
|
968
965
|
}
|
|
969
|
-
function
|
|
966
|
+
function readProjectionValue(value) {
|
|
970
967
|
if (!isObjectRecord(value)) return value;
|
|
971
|
-
if ("
|
|
968
|
+
if (typeof value.type === "string" && value.type !== "ref" && "value" in value) return value.value;
|
|
969
|
+
if ("refId" in value && value.refId !== void 0) return value.refId;
|
|
972
970
|
if ("value" in value) return value.value;
|
|
971
|
+
if ("display" in value && value.display !== null && value.display !== void 0) return value.display;
|
|
973
972
|
return value;
|
|
974
973
|
}
|
|
975
974
|
async function enrichCrudRows(ctx, config, idField, rows) {
|
|
@@ -983,14 +982,13 @@ async function enrichCrudRows(ctx, config, idField, rows) {
|
|
|
983
982
|
id: target.id,
|
|
984
983
|
entityIds
|
|
985
984
|
});
|
|
986
|
-
const metadata = await provider.getMetadata?.({ id: target.id });
|
|
987
985
|
return rows.map((row) => {
|
|
988
986
|
const rowId = row[idField];
|
|
989
987
|
const projected = rowId !== null && rowId !== void 0 ? projections[String(rowId)] : void 0;
|
|
990
988
|
if (!projected || Object.keys(projected).length === 0) return row;
|
|
991
989
|
return {
|
|
992
990
|
...row,
|
|
993
|
-
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field,
|
|
991
|
+
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field, readProjectionValue(value)]))
|
|
994
992
|
};
|
|
995
993
|
});
|
|
996
994
|
}
|
|
@@ -1026,26 +1024,46 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1026
1024
|
filters: baseFilters
|
|
1027
1025
|
};
|
|
1028
1026
|
const provider = resolveCrudExtensions(ctx, config);
|
|
1029
|
-
const
|
|
1030
|
-
if (
|
|
1031
|
-
if (
|
|
1027
|
+
const extensionSearch = search && !searchDisabled;
|
|
1028
|
+
if (!provider?.buildConditions) {
|
|
1029
|
+
if (extensionFilters.length > 0 || extensionSearch && provider?.searchEntityIds) throw new TRPCError({
|
|
1032
1030
|
code: "PRECONDITION_FAILED",
|
|
1033
|
-
message: "CRUD extension
|
|
1031
|
+
message: "CRUD extension queries require a provider with buildConditions; upgrade the Host/provider"
|
|
1034
1032
|
});
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1033
|
+
if (extensionSearch && !buildCrudSearchCondition({
|
|
1034
|
+
table,
|
|
1035
|
+
search,
|
|
1036
|
+
searchColumns
|
|
1037
|
+
})) throw new TRPCError({
|
|
1038
|
+
code: "PRECONDITION_FAILED",
|
|
1039
|
+
message: "CRUD search is not available"
|
|
1040
|
+
});
|
|
1041
|
+
return {
|
|
1042
|
+
...input,
|
|
1043
|
+
filters: baseFilters
|
|
1044
|
+
};
|
|
1041
1045
|
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1046
|
+
if (extensionFilters.length === 0 && !extensionSearch) return {
|
|
1047
|
+
...input,
|
|
1048
|
+
filters: baseFilters
|
|
1049
|
+
};
|
|
1050
|
+
const entityId = getTableColumn(table, idField);
|
|
1051
|
+
if (!entityId) throw new TRPCError({
|
|
1052
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
1053
|
+
message: "CRUD identity column is unavailable"
|
|
1054
|
+
});
|
|
1055
|
+
const conditions = await provider.buildConditions({
|
|
1044
1056
|
id: target.id,
|
|
1045
|
-
|
|
1046
|
-
|
|
1057
|
+
entityId,
|
|
1058
|
+
filters: extensionFilters,
|
|
1059
|
+
...input.joinOperator ? { joinOperator: input.joinOperator } : {},
|
|
1060
|
+
...extensionSearch ? { search } : {}
|
|
1047
1061
|
});
|
|
1048
|
-
|
|
1062
|
+
if (extensionFilters.length > 0 && !conditions.filter) throw new TRPCError({
|
|
1063
|
+
code: "PRECONDITION_FAILED",
|
|
1064
|
+
message: "CRUD extension filter condition is unavailable"
|
|
1065
|
+
});
|
|
1066
|
+
if (extensionSearch && !conditions.search && !buildCrudSearchCondition({
|
|
1049
1067
|
table,
|
|
1050
1068
|
search,
|
|
1051
1069
|
searchColumns
|
|
@@ -1053,37 +1071,25 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1053
1071
|
code: "PRECONDITION_FAILED",
|
|
1054
1072
|
message: "CRUD search is not available"
|
|
1055
1073
|
});
|
|
1056
|
-
const matchedSets = (await Promise.all(matchRequests)).map((matchedIds$1) => new Set(matchedIds$1));
|
|
1057
|
-
const effectiveInput = extensionSearchIds === void 0 ? input : {
|
|
1058
|
-
...input,
|
|
1059
|
-
[CRUD_EXTENSION_SEARCH_IDS]: extensionSearchIds
|
|
1060
|
-
};
|
|
1061
|
-
if (matchedSets.length === 0) return {
|
|
1062
|
-
...effectiveInput,
|
|
1063
|
-
filters: baseFilters
|
|
1064
|
-
};
|
|
1065
|
-
let matchedIds = matchedSets[0] ? [...matchedSets[0]] : [];
|
|
1066
|
-
for (const set of matchedSets.slice(1)) matchedIds = matchedIds.filter((id) => set.has(id));
|
|
1067
1074
|
return {
|
|
1068
|
-
...
|
|
1069
|
-
filters:
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
variant: "multiSelect",
|
|
1073
|
-
operator: "inArray",
|
|
1074
|
-
filterId: CRUD_EXTENSION_FILTER_ID
|
|
1075
|
-
}]
|
|
1075
|
+
...input,
|
|
1076
|
+
filters: baseFilters,
|
|
1077
|
+
...extensionFilters.length > 0 && conditions.filter ? { [CRUD_EXTENSION_FILTER]: conditions.filter } : {},
|
|
1078
|
+
...extensionSearch && conditions.search ? { [CRUD_EXTENSION_SEARCH]: conditions.search } : {}
|
|
1076
1079
|
};
|
|
1077
1080
|
}
|
|
1078
|
-
function
|
|
1081
|
+
function combineExtensionFilter(base, input) {
|
|
1082
|
+
const extension = input[CRUD_EXTENSION_FILTER];
|
|
1083
|
+
return input.joinOperator === "or" ? or(base, extension) : and(base, extension);
|
|
1084
|
+
}
|
|
1085
|
+
function buildCrudSearchWithExtensions(table, input, searchColumns) {
|
|
1079
1086
|
const baseCondition = buildCrudSearchCondition({
|
|
1080
1087
|
table,
|
|
1081
1088
|
search: input.search,
|
|
1082
1089
|
searchColumns
|
|
1083
1090
|
});
|
|
1084
|
-
const
|
|
1085
|
-
if (
|
|
1086
|
-
const extensionCondition = inArray(getTableColumn(table, idField), extensionIds);
|
|
1091
|
+
const extensionCondition = input[CRUD_EXTENSION_SEARCH];
|
|
1092
|
+
if (extensionCondition === void 0) return baseCondition;
|
|
1087
1093
|
return baseCondition ? or(baseCondition, extensionCondition) : extensionCondition;
|
|
1088
1094
|
}
|
|
1089
1095
|
function buildCrudSearchCondition({ table, search, searchColumns }) {
|
|
@@ -1286,8 +1292,8 @@ function createCrudRouter(config) {
|
|
|
1286
1292
|
const doList = async (listInput$1) => {
|
|
1287
1293
|
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, searchColumns, listInput$1);
|
|
1288
1294
|
const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
|
|
1289
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1290
|
-
const
|
|
1295
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1296
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1291
1297
|
table,
|
|
1292
1298
|
filters: validatedFilters,
|
|
1293
1299
|
joinOperator: effectiveInput.joinOperator,
|
|
@@ -1301,7 +1307,9 @@ function createCrudRouter(config) {
|
|
|
1301
1307
|
});
|
|
1302
1308
|
},
|
|
1303
1309
|
resolveDateRange: ctx.resolveDateRange
|
|
1304
|
-
}) : void 0
|
|
1310
|
+
}) : void 0;
|
|
1311
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1312
|
+
const where = buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1305
1313
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1306
1314
|
if (where) query = query.where(where);
|
|
1307
1315
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
|
|
@@ -1571,8 +1579,8 @@ function createCrudRouter(config) {
|
|
|
1571
1579
|
...exportInput$1
|
|
1572
1580
|
});
|
|
1573
1581
|
const effectiveLimit = Math.min(Math.max(effectiveInput.limit ?? maxExportSize, 1), maxExportSize);
|
|
1574
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1575
|
-
const
|
|
1582
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1583
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1576
1584
|
table,
|
|
1577
1585
|
filters: validatedFilters,
|
|
1578
1586
|
joinOperator: effectiveInput.joinOperator ?? "and",
|
|
@@ -1586,7 +1594,9 @@ function createCrudRouter(config) {
|
|
|
1586
1594
|
});
|
|
1587
1595
|
},
|
|
1588
1596
|
resolveDateRange: ctx.resolveDateRange
|
|
1589
|
-
}) : void 0
|
|
1597
|
+
}) : void 0;
|
|
1598
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1599
|
+
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1590
1600
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1591
1601
|
if (where) query = query.where(where);
|
|
1592
1602
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, false);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordrhyme/auto-crud-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"description": "tRPC server utilities for auto-crud - automatic CRUD routers for Drizzle ORM",
|
|
6
6
|
"author": "wordrhyme",
|
|
7
7
|
"license": "MIT",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"date-fns": "^4.
|
|
30
|
+
"date-fns": "^4.4.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"@trpc/server": "^11.0.0",
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"typescript": "^5.9.3",
|
|
49
49
|
"vitest": "^4.0.18",
|
|
50
50
|
"zod": "^4.3.6",
|
|
51
|
-
"@internal/eslint-config": "0.3.0",
|
|
52
51
|
"@internal/prettier-config": "0.0.1",
|
|
53
52
|
"@internal/tsconfig": "0.1.0",
|
|
54
53
|
"@internal/tsdown-config": "0.1.0",
|
|
54
|
+
"@internal/eslint-config": "0.3.0",
|
|
55
55
|
"@internal/vitest-config": "0.1.0"
|
|
56
56
|
},
|
|
57
57
|
"prettier": "@internal/prettier-config",
|