Files
outline/server/models/decorators/CounterCache.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

109 lines
3.8 KiB
TypeScript

import { isNil } from "es-toolkit/compat";
import type {
IncludeOptions,
InferAttributes,
Transaction,
WhereOptions,
} from "sequelize";
import type { ModelClassGetter } from "sequelize-typescript";
import { CacheHelper } from "@server/utils/CacheHelper";
import { RedisPrefixHelper } from "@server/utils/RedisPrefixHelper";
import type Model from "../base/Model";
type RelationOptions = {
/** Reference name used in cache key. */
as: string;
/** The foreign key to use for the relationship query. */
foreignKey: string;
/** Optional include used in the count query for filtering through associations. */
include?: IncludeOptions[];
/** Optional additional where clause used in the count query. */
where?: WhereOptions;
};
/**
* A decorator that caches the count of a relationship and registers model lifecycle hooks
* to invalidate the cache when models are added or removed from the relationship.
*/
export function CounterCache<
TCreationAttributes extends InferAttributes<Model>,
TModelAttributes extends InferAttributes<Model>,
T extends typeof Model,
>(
classResolver: ModelClassGetter<TCreationAttributes, TModelAttributes>,
options: RelationOptions
) {
return function (target: InstanceType<T>, _propertyKey: string) {
const modelClass = classResolver() as typeof Model;
const modelName = target.constructor.name;
const buildCacheKey = (id: unknown) =>
RedisPrefixHelper.getCounterCacheKey(modelName, options.as, String(id));
const computeCount = (id: unknown) =>
modelClass.count({
where: Object.assign({ [options.foreignKey]: id }, options.where),
include: options.include,
distinct: !!options.include,
});
const invalidate = async (
model: InstanceType<T>,
hookOptions?: { transaction?: Transaction | null }
) => {
const cacheKey = buildCacheKey(
model[options.foreignKey as keyof typeof model]
);
const remove = async () => {
await CacheHelper.removeData(cacheKey);
};
// Defer invalidation until after the transaction commits so that a
// rollback does not leave the cache out of sync, and so that a stale
// pre-commit count is not re-cached by a concurrent reader. Walk to
// the parent transaction when nested so the callback isn't lost when
// the savepoint releases without committing the outer transaction.
if (hookOptions?.transaction) {
const transaction =
hookOptions.transaction.parent || hookOptions.transaction;
transaction.afterCommit(remove);
} else {
await remove();
}
};
// The model class is not added to a Sequelize instance until the database
// module is first imported, which is later than decorator evaluation. Poll
// until the model is ready, then register the hooks. Use unref() so the
// pending immediate does not keep the event loop alive in environments
// (such as tests) where the database is never initialized.
const registerHooks = () => {
if (!modelClass.sequelize) {
setImmediate(registerHooks).unref();
return;
}
modelClass.addHook("afterCreate", invalidate);
modelClass.addHook("afterDestroy", invalidate);
};
setImmediate(registerHooks).unref();
return {
get() {
const cacheKey = buildCacheKey(this.id);
return CacheHelper.getData<number>(cacheKey).then((value) => {
if (!isNil(value)) {
return value;
}
return computeCount(this.id).then((count) => {
void CacheHelper.setData(cacheKey, count);
return count;
});
});
},
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- TS rejects PropertyDescriptor as legacy decorator return type; descriptor is consumed by Sequelize at runtime.
} as any;
};
}