|
| 1 | +import { lowerCaseFirst } from '@zenstackhq/common-helpers'; |
| 2 | +import type { QueryOptions } from '@zenstackhq/orm'; |
| 3 | +import { ExpressionUtils, type AttributeApplication, type SchemaDef } from '@zenstackhq/orm/schema'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Checks if a model is included based on slicing options. |
| 7 | + */ |
| 8 | +export function isModelIncluded(modelName: string, queryOptions?: QueryOptions<any>): boolean { |
| 9 | + const slicing = queryOptions?.slicing; |
| 10 | + if (!slicing) return true; |
| 11 | + |
| 12 | + const excluded = slicing.excludedModels as readonly string[] | undefined; |
| 13 | + if (excluded?.includes(modelName)) return false; |
| 14 | + |
| 15 | + const included = slicing.includedModels as readonly string[] | undefined; |
| 16 | + if (included && !included.includes(modelName)) return false; |
| 17 | + |
| 18 | + return true; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Checks if a CRUD operation is included for a model based on slicing options. |
| 23 | + */ |
| 24 | +export function isOperationIncluded(modelName: string, op: string, queryOptions?: QueryOptions<any>): boolean { |
| 25 | + const slicing = queryOptions?.slicing; |
| 26 | + if (!slicing?.models) return true; |
| 27 | + |
| 28 | + const modelKey = lowerCaseFirst(modelName); |
| 29 | + const modelSlicing = (slicing.models as Record<string, any>)[modelKey] ?? (slicing.models as any).$all; |
| 30 | + if (!modelSlicing) return true; |
| 31 | + |
| 32 | + const excluded = modelSlicing.excludedOperations as readonly string[] | undefined; |
| 33 | + if (excluded?.includes(op)) return false; |
| 34 | + |
| 35 | + const included = modelSlicing.includedOperations as readonly string[] | undefined; |
| 36 | + if (included && !included.includes(op)) return false; |
| 37 | + |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Checks if a procedure is included based on slicing options. |
| 43 | + */ |
| 44 | +export function isProcedureIncluded(procName: string, queryOptions?: QueryOptions<any>): boolean { |
| 45 | + const slicing = queryOptions?.slicing; |
| 46 | + if (!slicing) return true; |
| 47 | + |
| 48 | + const excluded = slicing.excludedProcedures as readonly string[] | undefined; |
| 49 | + if (excluded?.includes(procName)) return false; |
| 50 | + |
| 51 | + const included = slicing.includedProcedures as readonly string[] | undefined; |
| 52 | + if (included && !included.includes(procName)) return false; |
| 53 | + |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Checks if a field should be omitted from the output schema based on queryOptions.omit. |
| 59 | + */ |
| 60 | +export function isFieldOmitted(modelName: string, fieldName: string, queryOptions?: QueryOptions<any>): boolean { |
| 61 | + const omit = queryOptions?.omit as Record<string, Record<string, boolean>> | undefined; |
| 62 | + return omit?.[modelName]?.[fieldName] === true; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Returns the list of model names from the schema that pass the slicing filter. |
| 67 | + */ |
| 68 | +export function getIncludedModels(schema: SchemaDef, queryOptions?: QueryOptions<any>): string[] { |
| 69 | + return Object.keys(schema.models).filter((name) => isModelIncluded(name, queryOptions)); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Checks if a filter kind is allowed for a specific field based on slicing options. |
| 74 | + */ |
| 75 | +export function isFilterKindIncluded( |
| 76 | + modelName: string, |
| 77 | + fieldName: string, |
| 78 | + filterKind: string, |
| 79 | + queryOptions?: QueryOptions<any>, |
| 80 | +): boolean { |
| 81 | + const slicing = queryOptions?.slicing; |
| 82 | + if (!slicing?.models) return true; |
| 83 | + |
| 84 | + const modelKey = lowerCaseFirst(modelName); |
| 85 | + const modelSlicing = (slicing.models as Record<string, any>)[modelKey] ?? (slicing.models as any).$all; |
| 86 | + if (!modelSlicing?.fields) return true; |
| 87 | + |
| 88 | + const fieldSlicing = modelSlicing.fields[fieldName] ?? modelSlicing.fields.$all; |
| 89 | + if (!fieldSlicing) return true; |
| 90 | + |
| 91 | + const excluded = fieldSlicing.excludedFilterKinds as readonly string[] | undefined; |
| 92 | + if (excluded?.includes(filterKind)) return false; |
| 93 | + |
| 94 | + const included = fieldSlicing.includedFilterKinds as readonly string[] | undefined; |
| 95 | + if (included && !included.includes(filterKind)) return false; |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Extracts a "description" from `@@meta("description", "...")` or `@meta("description", "...")` attributes. |
| 102 | + */ |
| 103 | +export function getMetaDescription(attributes: readonly AttributeApplication[] | undefined): string | undefined { |
| 104 | + if (!attributes) return undefined; |
| 105 | + for (const attr of attributes) { |
| 106 | + if (attr.name !== '@meta' && attr.name !== '@@meta') continue; |
| 107 | + const nameArg = attr.args?.find((a) => a.name === 'name'); |
| 108 | + if (!nameArg || ExpressionUtils.getLiteralValue(nameArg.value) !== 'description') continue; |
| 109 | + const valueArg = attr.args?.find((a) => a.name === 'value'); |
| 110 | + if (valueArg) { |
| 111 | + return ExpressionUtils.getLiteralValue(valueArg.value) as string | undefined; |
| 112 | + } |
| 113 | + } |
| 114 | + return undefined; |
| 115 | +} |
0 commit comments