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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 131x 131x 131x 8323x 7560x 763x 572522x 243465x 243465x 107450x 107450x 136015x 328985x 136015x 329057x 7560x 7560x 763x 763x 763x 763x 3684x 3864x 19x 3845x 305x 3540x 180x 763x 4371x 3x 329057x 329057x 329057x 1x 329056x 329056x 329056x 6x 6x 6x 329050x 329050x 329056x 328971x 328971x 19x 19x 19x 18x 19x 360x 360x 350x 10x 6x 4x 4x 44x 42x 2x 1x 2x 2x 2x 2x 3x 6x 6x 5x | import { SqlExpression } from '../expressions/methods.ts';
import { InstanceState } from './instance-state.ts';
import type { ModelRegistry } from './model-registry.ts';
import type { ModelValues } from './model-values.ts';
import type { AnyModel, Values } from './options.ts';
/** Tells model instances apart from plain values (injected to avoid importing `Model`). */
export type InstanceCheck = (value: unknown) => value is AnyModel;
/** Options of `instance.get()`. */
export type GetOptions = Readonly<{ plain?: boolean; raw?: boolean; clone?: boolean }>;
/** Options of `instance.set()`. */
export type SetOptions = Readonly<{ raw?: boolean }>;
/**
* Attribute access of model instances (Sequelize v6 `get` / `set` / `setDataValue` / `changed` /
* `previous` / `equals`): custom getters and setters, plain conversion of nested instances and
* change tracking.
*/
export class InstanceValues {
readonly #registry: ModelRegistry;
readonly #values: ModelValues;
readonly #isInstance: InstanceCheck;
/**
* @param registry - Model definitions.
* @param values - Value equality.
* @param isInstance - Checks whether a value is a model instance.
*/
constructor(registry: ModelRegistry, values: ModelValues, isInstance: InstanceCheck) {
this.#registry = registry;
this.#values = values;
this.#isInstance = isInstance;
}
/**
* One attribute value (custom getter applied unless `raw`), or all values.
*
* @param instance - Model instance.
* @param keyOrOptions - Attribute name, or options for all values.
* @param options - `raw: true` skips the custom getter.
* @returns Attribute value or values object.
*/
get(
instance: AnyModel,
keyOrOptions: string | number | symbol | GetOptions | undefined,
options: SetOptions,
): unknown {
if (typeof keyOrOptions === 'string') {
return this.#getOne(instance, keyOrOptions, options);
}
return this.#getAll(instance, keyOrOptions);
}
/**
* Sets one attribute (custom setter applied unless `raw`) or several.
*
* @param instance - Model instance.
* @param keyOrValues - Attribute name or values.
* @param valueOrOptions - Value, or options when setting several values.
* @param maybeOptions - Options when setting one value.
*/
set(
instance: AnyModel,
keyOrValues: string | number | symbol | Values,
valueOrOptions: unknown,
maybeOptions: SetOptions | undefined,
): void {
if (typeof keyOrValues === 'object') {
const options = (valueOrOptions ?? {}) as SetOptions;
if (options.raw === true) {
// Raw values bypass setters, so they can be copied in one step (as v6 does).
Object.assign(instance.dataValues as Values, keyOrValues);
return;
}
Object.entries(keyOrValues).forEach(([key, value]) =>
instance.set(key, value as never, options),
);
return;
}
this.#setOne(instance, String(keyOrValues), valueOrOptions, maybeOptions ?? {});
}
#getOne(instance: AnyModel, key: string, options: SetOptions): unknown {
const getter = this.#registry.definition(instance.constructor).attributes.get(key)?.get;
return getter !== undefined && options.raw !== true
? (getter as (this: unknown) => unknown).call(instance)
: (instance.dataValues as Values)[key];
}
#getAll(instance: AnyModel, options: GetOptions | number | symbol | undefined): Values {
const raw = typeof options === 'object' && options.raw === true;
const plain = typeof options === 'object' && options.plain === true;
const keys = new Set([
...(raw ? [] : this.#getterKeys(instance)),
...Object.keys(instance.dataValues),
]);
return Object.fromEntries(
[...keys].map((key) => [
key,
this.#plain(raw ? (instance.dataValues as Values)[key] : instance.get(key), plain),
]),
);
}
#plain(value: unknown, plain: boolean): unknown {
if (!plain) {
return value;
}
if (this.#isInstance(value)) {
return value.get({ plain: true });
}
return Array.isArray(value) && value.some((item) => this.#isInstance(item))
? value.map((item) => this.#plain(item, plain))
: value;
}
/** v6 `get()` lists every attribute with a custom getter, also those without a stored value (VIRTUAL). */
#getterKeys(instance: AnyModel): string[] {
return [...this.#registry.definition(instance.constructor).attributes]
.filter(([, attribute]) => attribute.get !== undefined)
.map(([name]) => name);
}
#setOne(instance: AnyModel, key: string, value: unknown, options: SetOptions): void {
Iif (options.raw === true) {
(instance.dataValues as Values)[key] = value;
return;
}
const attribute = this.#registry.definition(instance.constructor).attributes.get(key);
if (attribute === undefined) {
return;
}
const values = instance.dataValues as Values;
const original = values[key];
if (attribute.set !== undefined) {
(attribute.set as (this: unknown, value: unknown) => void).call(instance, value);
this.#track(instance, key, original, values[key]);
return;
}
this.#track(instance, key, original, value);
values[key] = value;
}
/** v6 `set`: an attribute that differs from its value before the call stays changed for good. */
#track(instance: AnyModel, key: string, original: unknown, value: unknown): void {
if (value instanceof SqlExpression || !this.#values.isEqual(value, original)) {
(instance._previousDataValues as Values)[key] = original;
InstanceState.of(instance).mark(key, true);
}
}
/**
* Writes a value without custom setters, tracking the change.
*
* @param instance - Model instance.
* @param key - Attribute name.
* @param value - New value.
*/
setDataValue(instance: AnyModel, key: string | number | symbol, value: unknown): void {
const name = String(key);
const state = InstanceState.of(instance);
if (
!this.#values.isEqual((instance._previousDataValues as Values)[name], value) ||
value instanceof SqlExpression
) {
state.mark(name, true);
}
(instance.dataValues as Values)[name] = value;
}
/**
* Lists changed attributes, checks one, or marks one as changed / unchanged.
*
* @param instance - Model instance.
* @param key - Attribute name.
* @param value - Changed flag.
* @returns Names (or `false`), a flag, or `undefined` when marking.
*/
changed(instance: AnyModel, key: string | undefined, value: boolean | undefined): unknown {
const state = InstanceState.of(instance);
if (key === undefined) {
return state.changed.size === 0 ? false : [...state.changed];
}
if (value === undefined) {
return state.changed.has(key);
}
state.mark(key, value);
return undefined;
}
/**
* Previous value of an attribute, or all previous values of changed attributes.
*
* @param instance - Model instance.
* @param key - Attribute name.
* @returns Previous value(s).
*/
previous(instance: AnyModel, key: string | number | symbol | undefined): unknown {
if (key !== undefined) {
return (instance._previousDataValues as Record<string | number | symbol, unknown>)[key];
}
return Object.fromEntries(
[...InstanceState.of(instance).changed].map((name) => [
name,
(instance._previousDataValues as Values)[name],
]),
);
}
/**
* Row condition of an instance: primary key (and optionally version) values keyed by column name.
*
* @param instance - Model instance.
* @param checkVersion - Include the version attribute.
* @returns Where object, or the `whereCollection` option for models without a primary key.
*/
where(instance: AnyModel, checkVersion: boolean): Values | undefined {
const definition = this.#registry.definition(instance.constructor);
const names = [
...definition.primaryKeys,
...(checkVersion && definition.version !== undefined ? [definition.version] : []),
];
Iif (definition.primaryKeys.length === 0) {
return (definition.options as Readonly<Record<string, unknown>>)['whereCollection'] as
| Values
| undefined;
}
return Object.fromEntries(
names.map((name) => [
definition.attributes.get(name)?.field ?? name,
(instance.dataValues as Values)[name],
]),
);
}
/**
* Compares primary keys of two instances of the same model.
*
* @param instance - Model instance.
* @param other - Instance to compare with.
* @returns True when `other` is an instance of the model and all primary keys are equal.
*/
equals(instance: AnyModel, other: AnyModel): boolean {
const definition = this.#registry.definition(instance.constructor);
return (
other instanceof (instance.constructor as abstract new (...args: never[]) => unknown) &&
definition.primaryKeys.every((name) =>
this.#values.isEqual(
(instance.dataValues as Values)[name],
(other.dataValues as Values)[name],
),
)
);
}
}
|