@wordrhyme/auto-crud-server 1.5.1 → 2.0.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 +12 -0
- package/dist/index.cjs +75 -55
- package/dist/index.d.cts +30 -2
- package/dist/index.d.ts +30 -2
- package/dist/index.js +75 -55
- package/package.json +5 -5
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;
|
|
@@ -875,9 +872,15 @@ function splitCrudExtensionWriteInput(inputData, tableColumnNames) {
|
|
|
875
872
|
baseValues: {},
|
|
876
873
|
extraValues: null
|
|
877
874
|
};
|
|
875
|
+
const rawValues = { ...inputData };
|
|
876
|
+
delete rawValues.__crudExtensionProjection;
|
|
877
|
+
if (isObjectRecord(rawValues.ext)) {
|
|
878
|
+
rawValues.ext = { ...rawValues.ext };
|
|
879
|
+
delete rawValues.ext.__crudExtensionProjection;
|
|
880
|
+
}
|
|
878
881
|
const baseValues = {};
|
|
879
882
|
const extraValues = {};
|
|
880
|
-
for (const [key, value] of Object.entries(
|
|
883
|
+
for (const [key, value] of Object.entries(rawValues)) {
|
|
881
884
|
if (key === "ext") {
|
|
882
885
|
if (isObjectRecord(value)) Object.assign(extraValues, value);
|
|
883
886
|
continue;
|
|
@@ -887,7 +890,7 @@ function splitCrudExtensionWriteInput(inputData, tableColumnNames) {
|
|
|
887
890
|
}
|
|
888
891
|
return {
|
|
889
892
|
data: baseValues,
|
|
890
|
-
rawValues
|
|
893
|
+
rawValues,
|
|
891
894
|
baseValues,
|
|
892
895
|
extraValues: Object.keys(extraValues).length > 0 ? extraValues : null
|
|
893
896
|
};
|
|
@@ -997,10 +1000,12 @@ function createCrudBatchTransactionInput(inputs) {
|
|
|
997
1000
|
extraValues: inputs.some((input) => input.extraValues) ? {} : null
|
|
998
1001
|
};
|
|
999
1002
|
}
|
|
1000
|
-
function
|
|
1003
|
+
function readProjectionValue(value) {
|
|
1001
1004
|
if (!isObjectRecord(value)) return value;
|
|
1002
|
-
if ("
|
|
1005
|
+
if (typeof value.type === "string" && value.type !== "ref" && "value" in value) return value.value;
|
|
1006
|
+
if ("refId" in value && value.refId !== void 0) return value.refId;
|
|
1003
1007
|
if ("value" in value) return value.value;
|
|
1008
|
+
if ("display" in value && value.display !== null && value.display !== void 0) return value.display;
|
|
1004
1009
|
return value;
|
|
1005
1010
|
}
|
|
1006
1011
|
async function enrichCrudRows(ctx, config, idField, rows) {
|
|
@@ -1014,14 +1019,17 @@ async function enrichCrudRows(ctx, config, idField, rows) {
|
|
|
1014
1019
|
id: target.id,
|
|
1015
1020
|
entityIds
|
|
1016
1021
|
});
|
|
1017
|
-
const metadata = await provider.getMetadata?.({ id: target.id });
|
|
1018
1022
|
return rows.map((row) => {
|
|
1019
1023
|
const rowId = row[idField];
|
|
1020
1024
|
const projected = rowId !== null && rowId !== void 0 ? projections[String(rowId)] : void 0;
|
|
1021
1025
|
if (!projected || Object.keys(projected).length === 0) return row;
|
|
1022
1026
|
return {
|
|
1023
1027
|
...row,
|
|
1024
|
-
|
|
1028
|
+
__crudExtensionProjection: Object.fromEntries(Object.entries(projected).flatMap(([field, value]) => isObjectRecord(value) && (value.type === "ref" || typeof value.type !== "string" && "refId" in value) ? [[field, {
|
|
1029
|
+
refId: readProjectionValue(value) ?? null,
|
|
1030
|
+
display: typeof value.display === "string" ? value.display : null
|
|
1031
|
+
}]] : [])),
|
|
1032
|
+
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field, readProjectionValue(value)]))
|
|
1025
1033
|
};
|
|
1026
1034
|
});
|
|
1027
1035
|
}
|
|
@@ -1057,26 +1065,46 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1057
1065
|
filters: baseFilters
|
|
1058
1066
|
};
|
|
1059
1067
|
const provider = resolveCrudExtensions(ctx, config);
|
|
1060
|
-
const
|
|
1061
|
-
if (
|
|
1062
|
-
if (
|
|
1068
|
+
const extensionSearch = search && !searchDisabled;
|
|
1069
|
+
if (!provider?.buildConditions) {
|
|
1070
|
+
if (extensionFilters.length > 0 || extensionSearch && provider?.searchEntityIds) throw new __trpc_server.TRPCError({
|
|
1063
1071
|
code: "PRECONDITION_FAILED",
|
|
1064
|
-
message: "CRUD extension
|
|
1072
|
+
message: "CRUD extension queries require a provider with buildConditions; upgrade the Host/provider"
|
|
1065
1073
|
});
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1074
|
+
if (extensionSearch && !buildCrudSearchCondition({
|
|
1075
|
+
table,
|
|
1076
|
+
search,
|
|
1077
|
+
searchColumns
|
|
1078
|
+
})) throw new __trpc_server.TRPCError({
|
|
1079
|
+
code: "PRECONDITION_FAILED",
|
|
1080
|
+
message: "CRUD search is not available"
|
|
1081
|
+
});
|
|
1082
|
+
return {
|
|
1083
|
+
...input,
|
|
1084
|
+
filters: baseFilters
|
|
1085
|
+
};
|
|
1072
1086
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1087
|
+
if (extensionFilters.length === 0 && !extensionSearch) return {
|
|
1088
|
+
...input,
|
|
1089
|
+
filters: baseFilters
|
|
1090
|
+
};
|
|
1091
|
+
const entityId = getTableColumn(table, idField);
|
|
1092
|
+
if (!entityId) throw new __trpc_server.TRPCError({
|
|
1093
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
1094
|
+
message: "CRUD identity column is unavailable"
|
|
1095
|
+
});
|
|
1096
|
+
const conditions = await provider.buildConditions({
|
|
1075
1097
|
id: target.id,
|
|
1076
|
-
|
|
1077
|
-
|
|
1098
|
+
entityId,
|
|
1099
|
+
filters: extensionFilters,
|
|
1100
|
+
...input.joinOperator ? { joinOperator: input.joinOperator } : {},
|
|
1101
|
+
...extensionSearch ? { search } : {}
|
|
1078
1102
|
});
|
|
1079
|
-
|
|
1103
|
+
if (extensionFilters.length > 0 && !conditions.filter) throw new __trpc_server.TRPCError({
|
|
1104
|
+
code: "PRECONDITION_FAILED",
|
|
1105
|
+
message: "CRUD extension filter condition is unavailable"
|
|
1106
|
+
});
|
|
1107
|
+
if (extensionSearch && !conditions.search && !buildCrudSearchCondition({
|
|
1080
1108
|
table,
|
|
1081
1109
|
search,
|
|
1082
1110
|
searchColumns
|
|
@@ -1084,37 +1112,25 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1084
1112
|
code: "PRECONDITION_FAILED",
|
|
1085
1113
|
message: "CRUD search is not available"
|
|
1086
1114
|
});
|
|
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
1115
|
return {
|
|
1099
|
-
...
|
|
1100
|
-
filters:
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
variant: "multiSelect",
|
|
1104
|
-
operator: "inArray",
|
|
1105
|
-
filterId: CRUD_EXTENSION_FILTER_ID
|
|
1106
|
-
}]
|
|
1116
|
+
...input,
|
|
1117
|
+
filters: baseFilters,
|
|
1118
|
+
...extensionFilters.length > 0 && conditions.filter ? { [CRUD_EXTENSION_FILTER]: conditions.filter } : {},
|
|
1119
|
+
...extensionSearch && conditions.search ? { [CRUD_EXTENSION_SEARCH]: conditions.search } : {}
|
|
1107
1120
|
};
|
|
1108
1121
|
}
|
|
1109
|
-
function
|
|
1122
|
+
function combineExtensionFilter(base, input) {
|
|
1123
|
+
const extension = input[CRUD_EXTENSION_FILTER];
|
|
1124
|
+
return input.joinOperator === "or" ? (0, drizzle_orm.or)(base, extension) : (0, drizzle_orm.and)(base, extension);
|
|
1125
|
+
}
|
|
1126
|
+
function buildCrudSearchWithExtensions(table, input, searchColumns) {
|
|
1110
1127
|
const baseCondition = buildCrudSearchCondition({
|
|
1111
1128
|
table,
|
|
1112
1129
|
search: input.search,
|
|
1113
1130
|
searchColumns
|
|
1114
1131
|
});
|
|
1115
|
-
const
|
|
1116
|
-
if (
|
|
1117
|
-
const extensionCondition = (0, drizzle_orm.inArray)(getTableColumn(table, idField), extensionIds);
|
|
1132
|
+
const extensionCondition = input[CRUD_EXTENSION_SEARCH];
|
|
1133
|
+
if (extensionCondition === void 0) return baseCondition;
|
|
1118
1134
|
return baseCondition ? (0, drizzle_orm.or)(baseCondition, extensionCondition) : extensionCondition;
|
|
1119
1135
|
}
|
|
1120
1136
|
function buildCrudSearchCondition({ table, search, searchColumns }) {
|
|
@@ -1317,8 +1333,8 @@ function createCrudRouter(config) {
|
|
|
1317
1333
|
const doList = async (listInput$1) => {
|
|
1318
1334
|
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, searchColumns, listInput$1);
|
|
1319
1335
|
const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
|
|
1320
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1321
|
-
const
|
|
1336
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1337
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1322
1338
|
table,
|
|
1323
1339
|
filters: validatedFilters,
|
|
1324
1340
|
joinOperator: effectiveInput.joinOperator,
|
|
@@ -1332,7 +1348,9 @@ function createCrudRouter(config) {
|
|
|
1332
1348
|
});
|
|
1333
1349
|
},
|
|
1334
1350
|
resolveDateRange: ctx.resolveDateRange
|
|
1335
|
-
}) : void 0
|
|
1351
|
+
}) : void 0;
|
|
1352
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1353
|
+
const where = buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1336
1354
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1337
1355
|
if (where) query = query.where(where);
|
|
1338
1356
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
|
|
@@ -1602,8 +1620,8 @@ function createCrudRouter(config) {
|
|
|
1602
1620
|
...exportInput$1
|
|
1603
1621
|
});
|
|
1604
1622
|
const effectiveLimit = Math.min(Math.max(effectiveInput.limit ?? maxExportSize, 1), maxExportSize);
|
|
1605
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1606
|
-
const
|
|
1623
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1624
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1607
1625
|
table,
|
|
1608
1626
|
filters: validatedFilters,
|
|
1609
1627
|
joinOperator: effectiveInput.joinOperator ?? "and",
|
|
@@ -1617,7 +1635,9 @@ function createCrudRouter(config) {
|
|
|
1617
1635
|
});
|
|
1618
1636
|
},
|
|
1619
1637
|
resolveDateRange: ctx.resolveDateRange
|
|
1620
|
-
}) : void 0
|
|
1638
|
+
}) : void 0;
|
|
1639
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1640
|
+
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1621
1641
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1622
1642
|
if (where) query = query.where(where);
|
|
1623
1643
|
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,47 @@ 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. Envelopes with type "ref" or a legacy untyped refId
|
|
235
|
+
* also expose the resolved raw ID and display in
|
|
236
|
+
* __crudExtensionProjection on each row, allowing consumers to show the
|
|
237
|
+
* existing reference label without requiring access to the target's search API.
|
|
238
|
+
*/
|
|
230
239
|
readProjection?: (input: {
|
|
231
240
|
id: string;
|
|
232
241
|
entityIds: string[];
|
|
233
242
|
fields?: string[];
|
|
234
243
|
}) => Promise<Record<string, Record<string, unknown>>>;
|
|
244
|
+
/**
|
|
245
|
+
* Trusted server-side provider only: compose tenant-scoped SQL predicates.
|
|
246
|
+
* No matching ID arrays are materialized. Conditions join the base query
|
|
247
|
+
* before permissions, count, pagination and export limits are applied.
|
|
248
|
+
* Non-empty filters require a filter condition; a search with no matches
|
|
249
|
+
* should return SQL false. Never accept SQL supplied by an HTTP client.
|
|
250
|
+
*/
|
|
251
|
+
buildConditions?: (input: {
|
|
252
|
+
id: string;
|
|
253
|
+
entityId: AnyColumn;
|
|
254
|
+
filters: CrudExtensionFilter[];
|
|
255
|
+
joinOperator?: 'and' | 'or';
|
|
256
|
+
search?: string;
|
|
257
|
+
}) => Promise<{
|
|
258
|
+
filter?: SQL;
|
|
259
|
+
search?: SQL;
|
|
260
|
+
}>;
|
|
261
|
+
/** Legacy API; list/export require buildConditions for extension matching. */
|
|
235
262
|
matchEntityIds?: (input: {
|
|
236
263
|
id: string;
|
|
237
264
|
filters: CrudExtensionFilter[];
|
|
238
265
|
joinOperator?: 'and' | 'or';
|
|
239
266
|
limit?: number;
|
|
240
267
|
}) => Promise<string[]>;
|
|
268
|
+
/** Legacy ID lookup; not used by the list/export query path. */
|
|
241
269
|
searchEntityIds?: (input: {
|
|
242
270
|
id: string;
|
|
243
271
|
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,47 @@ 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. Envelopes with type "ref" or a legacy untyped refId
|
|
236
|
+
* also expose the resolved raw ID and display in
|
|
237
|
+
* __crudExtensionProjection on each row, allowing consumers to show the
|
|
238
|
+
* existing reference label without requiring access to the target's search API.
|
|
239
|
+
*/
|
|
231
240
|
readProjection?: (input: {
|
|
232
241
|
id: string;
|
|
233
242
|
entityIds: string[];
|
|
234
243
|
fields?: string[];
|
|
235
244
|
}) => Promise<Record<string, Record<string, unknown>>>;
|
|
245
|
+
/**
|
|
246
|
+
* Trusted server-side provider only: compose tenant-scoped SQL predicates.
|
|
247
|
+
* No matching ID arrays are materialized. Conditions join the base query
|
|
248
|
+
* before permissions, count, pagination and export limits are applied.
|
|
249
|
+
* Non-empty filters require a filter condition; a search with no matches
|
|
250
|
+
* should return SQL false. Never accept SQL supplied by an HTTP client.
|
|
251
|
+
*/
|
|
252
|
+
buildConditions?: (input: {
|
|
253
|
+
id: string;
|
|
254
|
+
entityId: AnyColumn;
|
|
255
|
+
filters: CrudExtensionFilter[];
|
|
256
|
+
joinOperator?: 'and' | 'or';
|
|
257
|
+
search?: string;
|
|
258
|
+
}) => Promise<{
|
|
259
|
+
filter?: SQL;
|
|
260
|
+
search?: SQL;
|
|
261
|
+
}>;
|
|
262
|
+
/** Legacy API; list/export require buildConditions for extension matching. */
|
|
236
263
|
matchEntityIds?: (input: {
|
|
237
264
|
id: string;
|
|
238
265
|
filters: CrudExtensionFilter[];
|
|
239
266
|
joinOperator?: 'and' | 'or';
|
|
240
267
|
limit?: number;
|
|
241
268
|
}) => Promise<string[]>;
|
|
269
|
+
/** Legacy ID lookup; not used by the list/export query path. */
|
|
242
270
|
searchEntityIds?: (input: {
|
|
243
271
|
id: string;
|
|
244
272
|
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;
|
|
@@ -844,9 +841,15 @@ function splitCrudExtensionWriteInput(inputData, tableColumnNames) {
|
|
|
844
841
|
baseValues: {},
|
|
845
842
|
extraValues: null
|
|
846
843
|
};
|
|
844
|
+
const rawValues = { ...inputData };
|
|
845
|
+
delete rawValues.__crudExtensionProjection;
|
|
846
|
+
if (isObjectRecord(rawValues.ext)) {
|
|
847
|
+
rawValues.ext = { ...rawValues.ext };
|
|
848
|
+
delete rawValues.ext.__crudExtensionProjection;
|
|
849
|
+
}
|
|
847
850
|
const baseValues = {};
|
|
848
851
|
const extraValues = {};
|
|
849
|
-
for (const [key, value] of Object.entries(
|
|
852
|
+
for (const [key, value] of Object.entries(rawValues)) {
|
|
850
853
|
if (key === "ext") {
|
|
851
854
|
if (isObjectRecord(value)) Object.assign(extraValues, value);
|
|
852
855
|
continue;
|
|
@@ -856,7 +859,7 @@ function splitCrudExtensionWriteInput(inputData, tableColumnNames) {
|
|
|
856
859
|
}
|
|
857
860
|
return {
|
|
858
861
|
data: baseValues,
|
|
859
|
-
rawValues
|
|
862
|
+
rawValues,
|
|
860
863
|
baseValues,
|
|
861
864
|
extraValues: Object.keys(extraValues).length > 0 ? extraValues : null
|
|
862
865
|
};
|
|
@@ -966,10 +969,12 @@ function createCrudBatchTransactionInput(inputs) {
|
|
|
966
969
|
extraValues: inputs.some((input) => input.extraValues) ? {} : null
|
|
967
970
|
};
|
|
968
971
|
}
|
|
969
|
-
function
|
|
972
|
+
function readProjectionValue(value) {
|
|
970
973
|
if (!isObjectRecord(value)) return value;
|
|
971
|
-
if ("
|
|
974
|
+
if (typeof value.type === "string" && value.type !== "ref" && "value" in value) return value.value;
|
|
975
|
+
if ("refId" in value && value.refId !== void 0) return value.refId;
|
|
972
976
|
if ("value" in value) return value.value;
|
|
977
|
+
if ("display" in value && value.display !== null && value.display !== void 0) return value.display;
|
|
973
978
|
return value;
|
|
974
979
|
}
|
|
975
980
|
async function enrichCrudRows(ctx, config, idField, rows) {
|
|
@@ -983,14 +988,17 @@ async function enrichCrudRows(ctx, config, idField, rows) {
|
|
|
983
988
|
id: target.id,
|
|
984
989
|
entityIds
|
|
985
990
|
});
|
|
986
|
-
const metadata = await provider.getMetadata?.({ id: target.id });
|
|
987
991
|
return rows.map((row) => {
|
|
988
992
|
const rowId = row[idField];
|
|
989
993
|
const projected = rowId !== null && rowId !== void 0 ? projections[String(rowId)] : void 0;
|
|
990
994
|
if (!projected || Object.keys(projected).length === 0) return row;
|
|
991
995
|
return {
|
|
992
996
|
...row,
|
|
993
|
-
|
|
997
|
+
__crudExtensionProjection: Object.fromEntries(Object.entries(projected).flatMap(([field, value]) => isObjectRecord(value) && (value.type === "ref" || typeof value.type !== "string" && "refId" in value) ? [[field, {
|
|
998
|
+
refId: readProjectionValue(value) ?? null,
|
|
999
|
+
display: typeof value.display === "string" ? value.display : null
|
|
1000
|
+
}]] : [])),
|
|
1001
|
+
...Object.fromEntries(Object.entries(projected).map(([field, value]) => [field, readProjectionValue(value)]))
|
|
994
1002
|
};
|
|
995
1003
|
});
|
|
996
1004
|
}
|
|
@@ -1026,26 +1034,46 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1026
1034
|
filters: baseFilters
|
|
1027
1035
|
};
|
|
1028
1036
|
const provider = resolveCrudExtensions(ctx, config);
|
|
1029
|
-
const
|
|
1030
|
-
if (
|
|
1031
|
-
if (
|
|
1037
|
+
const extensionSearch = search && !searchDisabled;
|
|
1038
|
+
if (!provider?.buildConditions) {
|
|
1039
|
+
if (extensionFilters.length > 0 || extensionSearch && provider?.searchEntityIds) throw new TRPCError({
|
|
1032
1040
|
code: "PRECONDITION_FAILED",
|
|
1033
|
-
message: "CRUD extension
|
|
1041
|
+
message: "CRUD extension queries require a provider with buildConditions; upgrade the Host/provider"
|
|
1034
1042
|
});
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1043
|
+
if (extensionSearch && !buildCrudSearchCondition({
|
|
1044
|
+
table,
|
|
1045
|
+
search,
|
|
1046
|
+
searchColumns
|
|
1047
|
+
})) throw new TRPCError({
|
|
1048
|
+
code: "PRECONDITION_FAILED",
|
|
1049
|
+
message: "CRUD search is not available"
|
|
1050
|
+
});
|
|
1051
|
+
return {
|
|
1052
|
+
...input,
|
|
1053
|
+
filters: baseFilters
|
|
1054
|
+
};
|
|
1041
1055
|
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1056
|
+
if (extensionFilters.length === 0 && !extensionSearch) return {
|
|
1057
|
+
...input,
|
|
1058
|
+
filters: baseFilters
|
|
1059
|
+
};
|
|
1060
|
+
const entityId = getTableColumn(table, idField);
|
|
1061
|
+
if (!entityId) throw new TRPCError({
|
|
1062
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
1063
|
+
message: "CRUD identity column is unavailable"
|
|
1064
|
+
});
|
|
1065
|
+
const conditions = await provider.buildConditions({
|
|
1044
1066
|
id: target.id,
|
|
1045
|
-
|
|
1046
|
-
|
|
1067
|
+
entityId,
|
|
1068
|
+
filters: extensionFilters,
|
|
1069
|
+
...input.joinOperator ? { joinOperator: input.joinOperator } : {},
|
|
1070
|
+
...extensionSearch ? { search } : {}
|
|
1047
1071
|
});
|
|
1048
|
-
|
|
1072
|
+
if (extensionFilters.length > 0 && !conditions.filter) throw new TRPCError({
|
|
1073
|
+
code: "PRECONDITION_FAILED",
|
|
1074
|
+
message: "CRUD extension filter condition is unavailable"
|
|
1075
|
+
});
|
|
1076
|
+
if (extensionSearch && !conditions.search && !buildCrudSearchCondition({
|
|
1049
1077
|
table,
|
|
1050
1078
|
search,
|
|
1051
1079
|
searchColumns
|
|
@@ -1053,37 +1081,25 @@ async function applyCrudExtensionFilters(ctx, config, table, idField, filterable
|
|
|
1053
1081
|
code: "PRECONDITION_FAILED",
|
|
1054
1082
|
message: "CRUD search is not available"
|
|
1055
1083
|
});
|
|
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
1084
|
return {
|
|
1068
|
-
...
|
|
1069
|
-
filters:
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
variant: "multiSelect",
|
|
1073
|
-
operator: "inArray",
|
|
1074
|
-
filterId: CRUD_EXTENSION_FILTER_ID
|
|
1075
|
-
}]
|
|
1085
|
+
...input,
|
|
1086
|
+
filters: baseFilters,
|
|
1087
|
+
...extensionFilters.length > 0 && conditions.filter ? { [CRUD_EXTENSION_FILTER]: conditions.filter } : {},
|
|
1088
|
+
...extensionSearch && conditions.search ? { [CRUD_EXTENSION_SEARCH]: conditions.search } : {}
|
|
1076
1089
|
};
|
|
1077
1090
|
}
|
|
1078
|
-
function
|
|
1091
|
+
function combineExtensionFilter(base, input) {
|
|
1092
|
+
const extension = input[CRUD_EXTENSION_FILTER];
|
|
1093
|
+
return input.joinOperator === "or" ? or(base, extension) : and(base, extension);
|
|
1094
|
+
}
|
|
1095
|
+
function buildCrudSearchWithExtensions(table, input, searchColumns) {
|
|
1079
1096
|
const baseCondition = buildCrudSearchCondition({
|
|
1080
1097
|
table,
|
|
1081
1098
|
search: input.search,
|
|
1082
1099
|
searchColumns
|
|
1083
1100
|
});
|
|
1084
|
-
const
|
|
1085
|
-
if (
|
|
1086
|
-
const extensionCondition = inArray(getTableColumn(table, idField), extensionIds);
|
|
1101
|
+
const extensionCondition = input[CRUD_EXTENSION_SEARCH];
|
|
1102
|
+
if (extensionCondition === void 0) return baseCondition;
|
|
1087
1103
|
return baseCondition ? or(baseCondition, extensionCondition) : extensionCondition;
|
|
1088
1104
|
}
|
|
1089
1105
|
function buildCrudSearchCondition({ table, search, searchColumns }) {
|
|
@@ -1286,8 +1302,8 @@ function createCrudRouter(config) {
|
|
|
1286
1302
|
const doList = async (listInput$1) => {
|
|
1287
1303
|
const effectiveInput = await applyCrudExtensionFilters(ctx, config, table, idField, filterableColumns, searchColumns, listInput$1);
|
|
1288
1304
|
const offset = (effectiveInput.page - 1) * effectiveInput.perPage;
|
|
1289
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1290
|
-
const
|
|
1305
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1306
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1291
1307
|
table,
|
|
1292
1308
|
filters: validatedFilters,
|
|
1293
1309
|
joinOperator: effectiveInput.joinOperator,
|
|
@@ -1301,7 +1317,9 @@ function createCrudRouter(config) {
|
|
|
1301
1317
|
});
|
|
1302
1318
|
},
|
|
1303
1319
|
resolveDateRange: ctx.resolveDateRange
|
|
1304
|
-
}) : void 0
|
|
1320
|
+
}) : void 0;
|
|
1321
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1322
|
+
const where = buildWhere(ctx, "list", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1305
1323
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1306
1324
|
if (where) query = query.where(where);
|
|
1307
1325
|
const orderByExpressions = buildOrderByExpressions(effectiveInput.sort, true);
|
|
@@ -1571,8 +1589,8 @@ function createCrudRouter(config) {
|
|
|
1571
1589
|
...exportInput$1
|
|
1572
1590
|
});
|
|
1573
1591
|
const effectiveLimit = Math.min(Math.max(effectiveInput.limit ?? maxExportSize, 1), maxExportSize);
|
|
1574
|
-
const validatedFilters = effectiveInput.filters?.filter((filter) =>
|
|
1575
|
-
const
|
|
1592
|
+
const validatedFilters = effectiveInput.filters?.filter((filter) => validateColumn(filter.id, filterableColumns));
|
|
1593
|
+
const filterCondition = validatedFilters?.length ? filterColumns({
|
|
1576
1594
|
table,
|
|
1577
1595
|
filters: validatedFilters,
|
|
1578
1596
|
joinOperator: effectiveInput.joinOperator ?? "and",
|
|
@@ -1586,7 +1604,9 @@ function createCrudRouter(config) {
|
|
|
1586
1604
|
});
|
|
1587
1605
|
},
|
|
1588
1606
|
resolveDateRange: ctx.resolveDateRange
|
|
1589
|
-
}) : void 0
|
|
1607
|
+
}) : void 0;
|
|
1608
|
+
const searchCondition = buildCrudSearchWithExtensions(table, effectiveInput, searchColumns);
|
|
1609
|
+
const where = buildWhere(ctx, "export", combineConditions(combineExtensionFilter(filterCondition, effectiveInput), searchCondition));
|
|
1590
1610
|
let query = ctx.db.select().from(table).$dynamic();
|
|
1591
1611
|
if (where) query = query.where(where);
|
|
1592
1612
|
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.1",
|
|
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",
|
|
@@ -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",
|
|
53
52
|
"@internal/tsconfig": "0.1.0",
|
|
54
|
-
"@internal/
|
|
55
|
-
"@internal/vitest-config": "0.1.0"
|
|
53
|
+
"@internal/prettier-config": "0.0.1",
|
|
54
|
+
"@internal/vitest-config": "0.1.0",
|
|
55
|
+
"@internal/tsdown-config": "0.1.0"
|
|
56
56
|
},
|
|
57
57
|
"prettier": "@internal/prettier-config",
|
|
58
58
|
"scripts": {
|