MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / getUniqueFields

Function getUniqueFields

packages/language/src/utils.ts:298–312  ·  view source on GitHub ↗
(model: DataModel)

Source from the content-addressed store, hash-verified

296 * TODO: merge this with {@link getModelUniqueFields}
297 */
298export function getUniqueFields(model: DataModel) {
299 const uniqueAttrs = model.attributes.filter(
300 (attr) => attr.decl.ref?.name === '@@unique' || attr.decl.ref?.name === '@@id',
301 );
302 return uniqueAttrs.map((uniqueAttr) => {
303 const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
304 if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {
305 return [];
306 }
307
308 return fieldsArg.value.items
309 .filter((item): item is ReferenceExpr => isReferenceExpr(item))
310 .map((item) => resolved(item.target) as DataField);
311 });
312}
313
314/**
315 * Finds the first ancestor of the given AST node that satisfies the given predicate function. Returns `undefined` if no such ancestor is found.

Callers 1

validateRelationFieldMethod · 0.90

Calls 4

isArrayExprFunction · 0.90
isReferenceExprFunction · 0.90
findMethod · 0.80
resolvedFunction · 0.70

Tested by

no test coverage detected