Files
outline/app/models/decorators/Relation.ts
T
Tom MoorandClaude Opus 5.5 5647621224 chore: Prune redundant comments and type suppressions (#14124)
* chore: Prune redundant comments and normalize lint directives

Removes comments that restate the following line and stale TODOs, drops
the passive event listener feature check now that all supported browsers
support it, and renames eslint directives to oxlint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

* fix: Evaluate restricted team domains at validation time

The restricted email provider list was captured when the model loaded, so
it could not follow the hosting environment and the tests that covered it
never reached the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

* chore: Replace type suppressions with accurate types

Adds a Concrete helper type for registries of abstract classes, declares
missing Sequelize and refractor typings, uses public ProseMirror APIs, a
typed mock context helper in middleware tests, and a StaticLink component
for sidebar rows being edited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-17 17:07:39 -04:00

182 lines
6.0 KiB
TypeScript

import invariant from "invariant";
import { singular } from "pluralize";
import { toError } from "@shared/utils/error";
import type Model from "../base/Model";
import Logger from "~/utils/Logger";
/** The behavior of a relationship on deletion */
type DeleteBehavior = "cascade" | "null" | "ignore";
/** The behavior of a relationship on archival */
type ArchiveBehavior = "cascade" | "null" | "ignore";
type RelationOptions<T = Model> = {
/** Whether this relation is required. */
required?: boolean;
/** If true, this relation is an array of IDs (one-to-many). */
multiple?: boolean;
/** Behavior of this model when relationship is deleted. */
onDelete?: DeleteBehavior | ((item: T) => DeleteBehavior);
/** Behavior of this model when relationship is archived. */
onArchive?: ArchiveBehavior | ((item: T) => ArchiveBehavior);
};
type RelationProperties<T = Model> = {
/** The name of the property on the model that stores the ID of the relation. */
idKey: keyof T;
/** A function that returns the class of the relation. */
relationClassResolver: () => typeof Model;
/** Options for the relation. */
options: RelationOptions<T>;
};
type InverseRelationProperties = RelationProperties & {
/** The name of the model class that owns this relation */
modelName: string;
};
const relations = new Map<string, Map<string, RelationProperties>>(new Map());
/**
* Returns the inverse relation properties for the given model class.
*
* @param targetClass The model class to get inverse relations for.
* @returns A map of inverse relation properties keyed by the property name.
*/
export const getInverseRelationsForModelClass = (
targetClass: Pick<typeof Model, "modelName">
) => {
const inverseRelations = new Map<string, InverseRelationProperties>();
relations.forEach((relation, modelName) => {
relation.forEach((properties, propertyName) => {
try {
const resolvedClass = properties.relationClassResolver();
if (
resolvedClass &&
resolvedClass.modelName &&
resolvedClass.modelName === targetClass.modelName
) {
inverseRelations.set(`${modelName}-${propertyName}`, {
...properties,
modelName,
});
}
} catch (error) {
Logger.error(
`Error resolving relation ${modelName}.${propertyName} for target ${targetClass.modelName}:`,
toError(error)
);
}
});
});
return inverseRelations;
};
export const getRelationsForModelClass = (targetClass: typeof Model) =>
relations.get(targetClass.modelName);
/**
* A decorator that records this key as a relation field on the model.
* Properties decorated with @Relation will merge and read their data from
* the associated store.
*
* @param classResolver A function that returns the class of the relation
* @param options Optional options for the relation definition
*/
export default function Relation<T extends typeof Model>(
classResolver: () => T,
options?: RelationOptions
) {
return function (target: Model, propertyKey: string) {
const idKey = options?.multiple
? `${String(singular(propertyKey))}Ids`
: `${String(propertyKey)}Id`;
// Register relation options to determine how to update relations when a model is deleted.
if (options) {
const modelName = (target.constructor as typeof Model).modelName;
const configForClass = relations.get(modelName) ?? new Map();
configForClass.set(propertyKey, {
options,
relationClassResolver: classResolver,
idKey,
});
relations.set(modelName, configForClass);
}
Object.defineProperty(target, propertyKey, {
get() {
const relationClassName = classResolver().modelName;
const store =
this.store.rootStore.getStoreForModelName(relationClassName);
invariant(store, `Store for ${relationClassName} not found`);
if (options?.multiple) {
const ids: string[] | undefined = this[idKey];
if (!Array.isArray(ids) || ids.length === 0) {
return [];
}
return ids.map((id) => store.get(id)).filter(Boolean);
} else {
const id: string | undefined = this[idKey];
if (!id) {
return undefined;
}
return store.get(id);
}
},
set(
newValue:
| Model
| Partial<Model>
| Array<Model | Partial<Model>>
| undefined
) {
if (options?.multiple) {
if (!newValue) {
this[idKey] = [];
if (options?.required) {
throw new Error(
`Cannot set required ${String(
propertyKey
)} to undefined or empty array`
);
}
return;
}
const values = Array.isArray(newValue) ? newValue : [newValue];
this[idKey] = values.map((v) => v.id);
const relationClassName = classResolver().modelName;
const store =
this.store.rootStore.getStoreForModelName(relationClassName);
invariant(store, `Store for ${relationClassName} not found`);
values.forEach((v) => store.add(v));
} else {
if (Array.isArray(newValue)) {
throw new Error(
`Cannot set array value to single relation property ${String(
propertyKey
)}`
);
}
this[idKey] = newValue ? newValue.id : undefined;
if (newValue) {
const relationClassName = classResolver().modelName;
const store =
this.store.rootStore.getStoreForModelName(relationClassName);
invariant(store, `Store for ${relationClassName} not found`);
store.add(newValue);
} else if (options?.required) {
throw new Error(
`Cannot set required ${String(propertyKey)} to undefined`
);
}
}
},
enumerable: true,
configurable: true,
});
};
}