mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix(db): PK lookups inside patch refresh past the identity map; corrected identity-map notes
This commit is contained in:
@@ -30,14 +30,16 @@ export class CategoriesRepository extends EntityRepository<Categories> {
|
||||
* global") and was deleted rather than kept as dead code (Task 0
|
||||
* re-review, R6; see `_shared/owned-lookup.ts`'s docstring).
|
||||
*
|
||||
* `refresh: true` (Task 0 review, I1 — the ruling every Plan 3 repository
|
||||
* follows for a PK read). For this unrestricted (no `fields` option)
|
||||
* findOne, MikroORM already re-queries and merges fresh data on every call
|
||||
* regardless of `refresh` — its identity-map-skip optimisation only
|
||||
* engages together with a `fields` restriction (the case
|
||||
* `AppSettingsRepository.getValue` actually needed it for). `refresh`
|
||||
* stays on this read to match the blanket ruling and to stay correct if a
|
||||
* future caller adds `fields` here.
|
||||
* `refresh: true` (Task 0 review, I1 — every PK-only `findOne` in a
|
||||
* repository takes it) and it is load-bearing here, not decorative: `{ id
|
||||
* }` is exactly the primary key, so MikroORM answers this call from the
|
||||
* identity map with zero queries whenever the row is already loaded
|
||||
* (Task 3 review, Important 2's correction — the short-circuit is driven
|
||||
* by the filter being PK-only, not by a `fields` restriction, which was
|
||||
* the wrong mechanism this docstring stated before the fix). Without
|
||||
* `refresh`, a raw write to this row earlier in the same request would
|
||||
* make this method return the stale snapshot instead of the current DB
|
||||
* row.
|
||||
*/
|
||||
async findById(id: number): Promise<CategoryRow | null> {
|
||||
const category = await this.findOne({ id }, { refresh: true });
|
||||
@@ -84,13 +86,26 @@ export class CategoriesRepository extends EntityRepository<Categories> {
|
||||
* coalesce-away-empty-string behaviour. Returns `null` when no row matches
|
||||
* `id`, mirroring the legacy re-select's `undefined`.
|
||||
*
|
||||
* The lookup `findOne({ id }, { refresh: true })` is PK-only, so without
|
||||
* `refresh: true` MikroORM would answer it from the identity map instead
|
||||
* of the DB (Task 3 review, Important 1): a category loaded earlier in
|
||||
* this request (list/findById/an earlier patch) would stay pinned to that
|
||||
* stale snapshot, so (a) an untouched column could read stale even though
|
||||
* the DB has since changed underneath it, (b) `assign` could diff a patch
|
||||
* against a stale value that happens to match the new one and emit no
|
||||
* UPDATE at all — reporting success while silently dropping the write —
|
||||
* and (c) a `remove()` earlier in the same request (a `nativeDelete`,
|
||||
* which does not clear the identity map) would let `patch` fabricate a
|
||||
* row for an id that no longer exists instead of returning `null`.
|
||||
* `refresh: true` forces a real re-query every time, closing all three.
|
||||
*
|
||||
* No re-`findOne`/`refresh` after `flush`: `assign` already mutated the
|
||||
* managed entity in place, and no column here is DB-computed on UPDATE
|
||||
* (unlike `created_at` on insert), so the in-memory entity already holds
|
||||
* exactly what was written.
|
||||
* managed (and now guaranteed-fresh) entity in place, and no column here
|
||||
* is DB-computed on UPDATE (unlike `created_at` on insert), so the
|
||||
* in-memory entity already holds exactly what was written.
|
||||
*/
|
||||
async patch(id: number, changes: { name?: string; color?: string; icon?: string }): Promise<CategoryRow | null> {
|
||||
const category = await this.findOne({ id });
|
||||
const category = await this.findOne({ id }, { refresh: true });
|
||||
if (!category) return null;
|
||||
this.assign(category, changes);
|
||||
await this.getEntityManager().flush();
|
||||
|
||||
@@ -33,16 +33,17 @@ export class TagsRepository extends EntityRepository<Tags> {
|
||||
* directly).
|
||||
*
|
||||
* `refresh: true` (Task 0 review, I1 — the ruling every Plan 3 repository
|
||||
* follows for a PK-scoped read). For this unrestricted (no `fields`
|
||||
* option) findOne, MikroORM already re-queries and merges fresh data on
|
||||
* every call regardless of `refresh` — its identity-map-skip optimisation
|
||||
* only engages together with a `fields` restriction (the case
|
||||
* `AppSettingsRepository.getValue` actually needed it for). `refresh`
|
||||
* stays on this read to match the blanket ruling and to stay correct if a
|
||||
* future caller adds `fields` here. `findOwnedByUser`'s optional `options`
|
||||
* parameter (added here, not duplicated — Task 0's helper had no reads
|
||||
* that needed it yet) passes straight through to the underlying
|
||||
* `findOne`.
|
||||
* follows for a PK-scoped read). This call's filter is `{ id, user }`,
|
||||
* not PK-only, so MikroORM's identity-map short-circuit — which fires
|
||||
* only when the filter is exactly the primary key, not (as an earlier
|
||||
* version of this docstring said) when a `fields` restriction is present
|
||||
* (Task 3 review, Important 2's correction) — never engages here anyway:
|
||||
* this call already re-queries and merges fresh data on every call
|
||||
* regardless of `refresh`. `refresh` stays threaded through to match the
|
||||
* blanket ruling and to stay correct if a future caller narrows the
|
||||
* filter to PK-only. `findOwnedByUser`'s optional `options` parameter
|
||||
* (added here, not duplicated — Task 0's helper had no reads that needed
|
||||
* it yet) passes straight through to the underlying `findOne`.
|
||||
*/
|
||||
async findByIdAndUser(id: number, userId: number): Promise<TagRow | null> {
|
||||
const tag = await findOwnedByUser<Tags, 'user'>(this, id, 'user', userId, { refresh: true });
|
||||
@@ -84,12 +85,22 @@ export class TagsRepository extends EntityRepository<Tags> {
|
||||
* coalesce-away-empty-string behaviour. Returns `null` when no row matches
|
||||
* `id`, mirroring the legacy re-select's `undefined`.
|
||||
*
|
||||
* The lookup `findOne({ id }, { refresh: true })` is PK-only, so without
|
||||
* `refresh: true` MikroORM would answer it from the identity map instead
|
||||
* of the DB (Task 3 review, Important 1) — same three consequences as
|
||||
* `CategoriesRepository.patch`: a stale untouched column, a patch that
|
||||
* diffs against a stale value and silently drops the UPDATE, or (since
|
||||
* `remove()` is a `nativeDelete` that does not clear the identity map) a
|
||||
* fabricated row for an id already deleted earlier in the request instead
|
||||
* of `null`. `refresh: true` forces a real re-query every time.
|
||||
*
|
||||
* No re-`findOne`/`refresh` after `flush`, same reasoning as
|
||||
* `CategoriesRepository.patch`: `assign` already mutated the managed
|
||||
* entity in place, and no column here is DB-computed on UPDATE.
|
||||
* `CategoriesRepository.patch`: `assign` already mutated the managed (and
|
||||
* now guaranteed-fresh) entity in place, and no column here is
|
||||
* DB-computed on UPDATE.
|
||||
*/
|
||||
async patch(id: number, changes: { name?: string; color?: string }): Promise<TagRow | null> {
|
||||
const tag = await this.findOne({ id });
|
||||
const tag = await this.findOne({ id }, { refresh: true });
|
||||
if (!tag) return null;
|
||||
this.assign(tag, changes);
|
||||
await this.getEntityManager().flush();
|
||||
|
||||
@@ -73,14 +73,15 @@ export async function listForOwner<T extends { id: unknown }, K extends keyof T,
|
||||
*
|
||||
* `options.refresh` passes straight through to the underlying `findOne`
|
||||
* (Task 0 review, I1's PK-read ruling: a caller whose read can follow a
|
||||
* raw/native write on the same row within the same request sets it). For an
|
||||
* unrestricted call like this one (no `fields` option), MikroORM already
|
||||
* re-queries and merges fresh data on every call regardless of `refresh` —
|
||||
* its identity-map-skip optimisation, and the staleness it can cause without
|
||||
* `refresh`, only engages together with a `fields` restriction (the case
|
||||
* `AppSettingsRepository.getValue` actually hit). `refresh` is still
|
||||
* threaded through here to match the blanket ruling and to stay correct if a
|
||||
* caller ever adds `fields` on top of it.
|
||||
* raw/native write on the same row within the same request sets it). This
|
||||
* filter is `{ id, [ownerField]: ownerId }` — not PK-only — so MikroORM's
|
||||
* identity-map short-circuit never engages here: that short-circuit fires
|
||||
* only when the filter is exactly the primary key (Task 3 review, Important
|
||||
* 2's correction — `fields` has nothing to do with it, unlike what an
|
||||
* earlier version of this docstring claimed). `refresh` is a harmless
|
||||
* no-op on every call this helper serves today; it stays threaded through
|
||||
* to match the blanket ruling and to stay correct if a future caller
|
||||
* narrows the filter to PK-only.
|
||||
*/
|
||||
export async function findOwnedByUser<T extends { id: unknown }, K extends keyof T>(
|
||||
repo: EntityRepository<T>,
|
||||
|
||||
@@ -51,7 +51,11 @@ export class CategoriesService {
|
||||
if (name) changes.name = name;
|
||||
if (color) changes.color = color;
|
||||
if (icon) changes.icon = icon;
|
||||
return (await this.categories.patch(Number(id), changes)) as unknown as Category;
|
||||
// `patch` returns `null` for a non-existent id (Task 3 review, Minor 2);
|
||||
// the legacy re-select returned `undefined` for the same case, and every
|
||||
// caller pre-checks with a 404 first, so `?? undefined` restores that
|
||||
// exact parity for the race-condition path the pre-check doesn't cover.
|
||||
return ((await this.categories.patch(Number(id), changes)) ?? undefined) as unknown as Category;
|
||||
}
|
||||
|
||||
async remove(id: string | number): Promise<void> {
|
||||
|
||||
@@ -49,7 +49,11 @@ export class TagsService {
|
||||
const changes: { name?: string; color?: string } = {};
|
||||
if (name) changes.name = name;
|
||||
if (color) changes.color = color;
|
||||
return (await this.tags.patch(Number(id), changes)) as unknown as Tag;
|
||||
// `patch` returns `null` for a non-existent id (Task 3 review, Minor 2);
|
||||
// the legacy re-select returned `undefined` for the same case, and every
|
||||
// caller pre-checks with a 404 first, so `?? undefined` restores that
|
||||
// exact parity for the race-condition path the pre-check doesn't cover.
|
||||
return ((await this.tags.patch(Number(id), changes)) ?? undefined) as unknown as Tag;
|
||||
}
|
||||
|
||||
async remove(id: string | number): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user