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 | 131x 131x 49x 49x 49x 4x 1x 3x 2x 1x 1x 1x 2x 1x 1x 1x 10x 10x 10x 2x 8x 8x 2x 6x 6x 6x 7x 10x 10x 5x 5x 5x 10x 11x 6x 8x 5x 5x | import { Op } from '../expressions/op.ts';
import type { AnyModel, ModelStatic, Values } from '../model/options.ts';
import { Association, type AccessorOptions } from './association.ts';
import { AssociationKeys } from './association-keys.ts';
import { OwnerKeys } from './owner-keys.ts';
import type { AssociationOptions } from './types.ts';
type Instance = AnyModel;
const keys = new AssociationKeys();
const ownerKeys = new OwnerKeys(keys);
/** `source.hasOne(target)` (Sequelize v6 `HasOne`): the foreign key lives on the target. */
export class HasOne<
S extends AnyModel = AnyModel,
T extends AnyModel = AnyModel,
> extends Association<S, T> {
/**
* Adds the foreign key attribute to the target and registers the association.
*
* @param source - Model the association is defined on.
* @param target - Associated model.
* @param options - Association options.
*/
constructor(source: ModelStatic<S>, target: ModelStatic<T>, options: AssociationOptions = {}) {
const resolved = ownerKeys.hasOneOrMany(false, source, target, options);
super('HasOne', source, target, options, resolved.name, resolved.keys);
this.install();
}
/**
* Loads the associated instance (`getX`); given an array of source instances it loads all of them with one query.
*
* @param instance - Source instance, or an array of them.
* @param options - Find options.
* @returns Associated instance, or `null`; for an array, an object keyed by the source key value with the instance or `null`.
*/
get(
instances: readonly Instance[],
options?: AccessorOptions,
): Promise<Record<string, Instance | null>>;
get(instance: Instance, options?: AccessorOptions): Promise<Instance | null>;
get(
instance: Instance | readonly Instance[],
options: AccessorOptions = {},
): Promise<Instance | null | Record<string, Instance | null>> {
if (Array.isArray(instance)) {
return this.#getMany(instance as readonly Instance[], options);
}
return this.target.findOne({
...options,
where: this.andWhere(options.where, this.scope, {
[this.foreignKey]: (instance as Instance).get(this.sourceKey),
}),
} as never);
}
async #getMany(
instances: readonly Instance[],
options: AccessorOptions,
): Promise<Record<string, Instance | null>> {
const values = instances.map((instance) => instance.get(this.sourceKey));
const found = await this.target.findAll({
...options,
where: this.andWhere(options.where, this.scope, { [this.foreignKey]: { [Op.in]: values } }),
} as never);
const result: Record<string, Instance | null> = {};
for (const key of values) {
result[String(key)] = null;
}
for (const row of found as readonly Instance[]) {
result[String(row.get(this.foreignKey))] = row;
}
return result;
}
/**
* Makes `associated` the associated instance, unlinking the current one (`setX`).
*
* @param instance - Source instance.
* @param associated - Instance, primary key value or `null`.
* @param options - Query options.
* @returns The new associated instance, or `null`.
*/
async set(
instance: Instance,
associated: unknown,
options: AccessorOptions = {},
): Promise<Instance | null> {
const value = instance.get(this.sourceKey);
const current = await this.#current(value, options);
if (this.#isLinked(current, associated)) {
return null;
}
await this.#unlink(current, options);
if (associated === null || associated === undefined) {
return null;
}
const next = this.#toInstance(associated);
next.set({ [this.foreignKey]: value, ...this.scope });
return (await next.save(this.passThrough(options) as never)) as Instance;
}
/**
* Creates the associated instance linked to the source (`createX`).
*
* @param instance - Source instance.
* @param values - Values of the new instance.
* @param options - Query options.
* @returns The created instance.
*/
create(
instance: Instance,
values: Values = {},
options: AccessorOptions = {},
): Promise<Instance> {
return this.target.create(
{ ...values, ...this.scope, [this.foreignKey]: instance.get(this.sourceKey) } as never,
this.passThrough(options) as never,
);
}
async #current(value: unknown, options: AccessorOptions): Promise<Instance | null> {
return (await this.target.findOne({
...this.passThrough(options),
where: { [this.foreignKey]: value },
} as never)) as Instance | null;
}
#isLinked(current: Instance | null, associated: unknown): boolean {
if (current === null || associated === null || associated === undefined) {
return false;
}
const pk = keys.primaryKey(this.target);
const other = this.#isInstance(associated) ? associated.get(pk) : associated;
return current.get(pk) === other;
}
#isInstance(value: unknown): value is Instance {
return typeof value === 'object' && value !== null && 'get' in value;
}
#toInstance(associated: unknown): Instance {
return this.#isInstance(associated)
? associated
: (this.target.build({ [keys.primaryKey(this.target)]: associated } as never, {
isNewRecord: false,
}) as Instance);
}
async #unlink(current: Instance | null, options: AccessorOptions): Promise<void> {
if (current !== null) {
current.set(this.foreignKey, null);
await current.save({ ...this.passThrough(options), fields: [this.foreignKey] } as never);
}
}
}
|