Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 132x 132x 132x 139583x 576720x 4034x 139588x 139588x 139588x 139588x 139588x 329083x 86x 328997x 135786x 193211x 107645x | import type { IncludeNode } from '../associations/include.ts';
const UNCHANGED: ReadonlySet<string> = new Set();
const NO_INCLUDES: readonly IncludeNode[] = [];
// A symbol-keyed own property: invisible to attribute enumeration, JSON and the instance type.
const STATE = Symbol('postgrelize.instanceState');
/**
* Internal state of one model instance: changed attribute names, the include tree it was built
* with, and the write metadata exposed as `wasInserted` / `oldValues`. Stored under a private symbol
* (no attribute-like member, no private field that would break mixin declaration types).
*/
export class InstanceState {
/**
* Creates the state of a new instance.
*
* @param instance - Model instance.
*/
static attach(instance: object): void {
(instance as Record<symbol, InstanceState>)[STATE] = new InstanceState();
}
/**
* State of an instance.
*
* @param instance - Model instance.
* @returns Its state.
*/
static of(instance: object): InstanceState {
return (instance as Record<symbol, InstanceState>)[STATE] as InstanceState;
}
/**
* Whether a value is a model instance (has a state).
*
* @param value - Any value.
* @returns True for model instances.
*/
static has(value: unknown): boolean {
return typeof value === 'object' && value !== null && STATE in value;
}
/**
* Changed attribute names. The set belongs to this instance and grows in place; readers copy it
* (`[...changed]`) when they need a snapshot.
*/
changed: ReadonlySet<string> = UNCHANGED;
/** Include tree of a nested build. */
includes: readonly IncludeNode[] = NO_INCLUDES;
/** Include tree the instance was loaded with by a finder; `reload()` loads it again (v6 `_options.include`). */
loadedIncludes: readonly IncludeNode[] = NO_INCLUDES;
/** Whether the row was inserted (`true`) or updated (`false`) by an `ON CONFLICT` write. */
wasInserted: boolean | undefined = undefined;
/** Pre-statement values from `returningOld`, `null` for inserted rows. */
oldValues: Readonly<Record<string, unknown>> | null | undefined = undefined;
/**
* Marks an attribute as changed or unchanged.
*
* @param name - Attribute name.
* @param changed - Changed flag.
*/
mark(name: string, changed: boolean): void {
if (!changed) {
(this.changed as Set<string>).delete(name);
} else if (this.changed === UNCHANGED) {
this.changed = new Set([name]);
} else {
(this.changed as Set<string>).add(name);
}
}
/** Forgets all changes (after a load or save). */
reset(): void {
this.changed = UNCHANGED;
}
}
|