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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 8323x 572522x 572522x 5x 19x 19x 360x 44x 6x 3x 1x 2x 1x 451x | import { instanceValues, valueCloner } from './model-services.ts';
import type { Model } from './model.ts';
import type { AnyModel, Values } from './options.ts';
type GetOptions = Readonly<{ plain?: boolean; raw?: boolean; clone?: boolean }>;
/**
* Value access of model instances: `get`, `set`, `getDataValue`, `setDataValue`, `changed`, `previous`, `equals`, `toJSON`.
*
* @internal
*/
export class ModelInstanceValues<TAttributes extends object = Values> {
/** Current attribute values. */
declare dataValues: TAttributes;
/**
* Returns all attribute values of the instance; no query is run.
*
* @param options - `plain: true` returns a copy of all values with custom getters applied; `raw: true` skips custom getters.
* @returns Values object.
* @example
* ```ts
* const values = user.get({ plain: true });
* ```
*/
get(options?: GetOptions): TAttributes;
/**
* Returns one attribute value, applying its custom getter unless `raw` is set.
*
* @param key - Attribute name.
* @param options - `raw: true` skips the custom getter.
* @returns Attribute value.
* @example
* ```ts
* const email = user.get('email');
* ```
*/
get<K extends keyof this>(key: K, options?: GetOptions): this[K];
/**
* Returns the value of an attribute or association that is addressed by a plain string, for
* example an included association or a dynamic key.
*
* @param key - Attribute or association name.
* @param options - `raw: true` skips the custom getter.
* @returns The value, untyped.
* @example
* ```ts
* const posts = user.get('Posts');
* ```
*/
get(key: string, options?: GetOptions): unknown;
get(
keyOrOptions?: string | number | symbol | GetOptions,
options: Readonly<{ raw?: boolean }> = {},
): unknown {
return instanceValues.get(this as unknown as AnyModel, keyOrOptions, options);
}
/**
* Sets one attribute value, applying its custom setter unless `raw` is set. Nothing is written to
* the database until {@link Model.save}; the attribute is marked as changed when the value differs.
*
* @param key - Attribute name.
* @param value - New value.
* @param options - `raw: true` skips the custom setter and change tracking.
* @returns This instance.
* @example
* ```ts
* user.set('email', 'ada@example.com');
* await user.save();
* ```
*/
set<K extends keyof TAttributes>(
key: K,
value: TAttributes[K],
options?: Readonly<{ raw?: boolean }>,
): this;
/**
* Sets several attribute values at once, applying custom setters unless `raw` is set.
*
* @param values - Attribute values.
* @param options - `raw: true` skips custom setters and change tracking.
* @returns This instance.
* @example
* ```ts
* user.set({ email: 'ada@example.com', active: true });
* ```
*/
set(values: Partial<TAttributes> | Values, options?: Readonly<{ raw?: boolean }>): this;
set(
keyOrValues: string | number | symbol | Values,
valueOrOptions?: unknown,
maybeOptions?: Readonly<{ raw?: boolean }>,
): this {
instanceValues.set(this as unknown as AnyModel, keyOrValues, valueOrOptions, maybeOptions);
return this;
}
/**
* Reads the stored value of an attribute, bypassing its custom getter.
*
* @param key - Attribute name.
* @returns Raw value.
* @example
* ```ts
* const stored = user.getDataValue('email');
* ```
*/
getDataValue<K extends keyof TAttributes>(key: K): TAttributes[K] {
return this.dataValues[key];
}
/**
* Writes the stored value of an attribute, bypassing its custom setter, and marks it as changed.
*
* @param key - Attribute name.
* @param value - New value.
* @returns This instance.
* @example
* ```ts
* user.setDataValue('email', 'ada@example.com');
* ```
*/
setDataValue<K extends keyof TAttributes>(key: K, value: TAttributes[K]): this {
instanceValues.setDataValue(this as unknown as AnyModel, key, value);
return this;
}
/**
* Lists the attributes changed since the instance was loaded or last saved.
*
* @returns Changed attribute names, or `false` when nothing changed.
* @example
* ```ts
* user.set('email', 'ada@example.com');
* const changedFields = user.changed();
* ```
*/
changed(): (keyof TAttributes & string)[] | false;
/**
* Checks whether one attribute changed since the instance was loaded or last saved.
*
* @param key - Attribute name.
* @returns True when changed.
*/
changed(key: keyof TAttributes & string): boolean;
/**
* Marks an attribute as changed or unchanged, which controls whether {@link Model.save} writes it.
*
* @param key - Attribute name.
* @param value - Changed flag.
*/
changed(key: keyof TAttributes & string, value: boolean): void;
/**
* Checks whether one attribute changed, addressed by a key of the model class (Sequelize v6 signature, so `keyof M` works in generic code).
*
* @param key - Attribute name.
* @returns True when changed.
*/
changed<K extends keyof this>(key: K): boolean;
/**
* Marks an attribute as changed or unchanged, addressed by a key of the model class (Sequelize v6 signature).
*
* @param key - Attribute name.
* @param value - Changed flag.
*/
changed<K extends keyof this>(key: K, value: boolean): void;
changed(key?: string | number | symbol, value?: boolean): unknown {
return instanceValues.changed(this as unknown as AnyModel, key as string | undefined, value);
}
/**
* Returns the previous values of all changed attributes.
*
* @returns Previous values keyed by attribute name.
* @example
* ```ts
* user.set('email', 'ada@example.com');
* const before = user.previous();
* ```
*/
previous(): Partial<TAttributes>;
/**
* Returns the value an attribute had before it was changed.
*
* @param key - Attribute name.
* @returns Previous value.
* @example
* ```ts
* user.set('email', 'ada@example.com');
* const before = user.previous('email');
* ```
*/
previous<K extends keyof TAttributes>(key: K): TAttributes[K] | undefined;
previous<K extends keyof TAttributes>(
key?: K,
): TAttributes[K] | Partial<TAttributes> | undefined {
return instanceValues.previous(this as unknown as AnyModel, key) as
| TAttributes[K]
| Partial<TAttributes>
| undefined;
}
/**
* Compares the primary keys of two instances.
*
* @param other - Instance to compare with.
* @returns True when all primary key values are equal.
* @example
* ```ts
* const other = User.build({ id: user.get('id') });
* const same = user.equals(other);
* ```
*/
equals(other: Model): boolean {
return instanceValues.equals(this as unknown as AnyModel, other);
}
/**
* Tells whether the instance equals any of the given instances (same model and primary keys).
*
* @param others - Instances to compare with.
* @returns True when at least one equals this instance.
* @example
* ```ts
* const known = user.equalsOneOf(users);
* ```
*/
equalsOneOf(others: readonly Model[]): boolean {
return others.some((other) => this.equals(other));
}
/**
* Alias of {@link Model.set} for several values (Sequelize v6 `setAttributes`).
*
* @param updates - Attribute values.
* @returns This instance.
* @example
* ```ts
* user.setAttributes({ email: 'ada@example.com' });
* ```
*/
setAttributes(updates: Partial<TAttributes> | Values): this {
return this.set(updates);
}
/**
* Condition that selects this row: its primary key values keyed by column name.
*
* @param checkVersion - Also match the optimistic-locking `version` value.
* @returns The where object (the model's `whereCollection` option when it has no primary key).
* @example
* ```ts
* const where = user.where();
* ```
*/
where(checkVersion?: boolean): Record<string, unknown> | undefined {
return instanceValues.where(this as unknown as AnyModel, checkVersion === true);
}
/**
* Describes the instance as `[object SequelizeInstance:Model]`.
*
* @returns The description.
* @example
* ```ts
* String(user); // '[object SequelizeInstance:User]'
* ```
*/
toString(): string {
return `[object SequelizeInstance:${(this.constructor as { name: string }).name}]`;
}
/**
* Returns a deep copy of the values with custom getters applied, used by `JSON.stringify`.
*
* @returns Plain object.
* @example
* ```ts
* const json = JSON.stringify(user);
* ```
*/
toJSON(): TAttributes {
return valueCloner.clone(this.get({ plain: true }));
}
}
|