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 | 393x 178x 178x 178x 178x 178x 178x 233x 233x 233x 233x 233x 411x 411x 145x 27x | import { inflector as inflection } from '../model/inflector.ts';
import type { AnyModel, ModelStatic } from '../model/options.ts';
import type { AssociationKeys, AssociationKeyFields, TargetKeys } from './association-keys.ts';
import type { AssociationOptions } from './types.ts';
type Model = ModelStatic<AnyModel>;
/** Resolves alias and keys of belongsTo / hasOne / hasMany and adds the foreign key attribute. */
export class OwnerKeys {
readonly #keys: AssociationKeys;
/**
* @param keys - Naming and foreign key attribute rules.
*/
constructor(keys: AssociationKeys) {
this.#keys = keys;
}
/**
* Resolves the alias and keys of a belongsTo association and adds the foreign key attribute
* to the source.
*
* @param source - Model the association is defined on.
* @param target - Associated model.
* @param options - Association options.
* @returns Alias forms and keys.
*/
belongsTo(source: Model, target: Model, options: AssociationOptions): TargetKeys {
const keys = this.#keys;
const name = keys.aliasNames(options.as, keys.modelName(target), false);
const targetKey = options.targetKey ?? keys.primaryKey(target);
const foreignKey =
keys.foreignKeyName(options.foreignKey) ??
keys.camelize(`${name.singular}_${keys.primaryKey(target)}`);
keys.addForeignKey(source, target, targetKey, foreignKey, options);
return { name, keys: this.#fields(source, foreignKey, foreignKey, targetKey) };
}
/**
* Resolves the alias and keys of a hasOne / hasMany association and adds the foreign key
* attribute to the target.
*
* @param multi - True for hasMany.
* @param source - Model the association is defined on.
* @param target - Associated model.
* @param options - Association options.
* @returns Alias forms and keys.
*/
hasOneOrMany(
multi: boolean,
source: Model,
target: Model,
options: AssociationOptions,
): TargetKeys {
const keys = this.#keys;
const sourceKey = options.sourceKey ?? keys.primaryKey(source);
const foreignKey =
keys.foreignKeyName(options.foreignKey) ??
keys.camelize(`${this.#sourcePrefix(multi, source, options)}_${keys.primaryKey(source)}`);
keys.addForeignKey(target, source, sourceKey, foreignKey, options);
return {
name: keys.aliasNames(options.as, keys.modelName(target), multi),
keys: this.#fields(target, foreignKey, sourceKey, foreignKey),
};
}
#fields(
owner: Model,
foreignKey: string,
sourceKey: string,
targetKey: string,
): AssociationKeyFields {
const identifierField = this.#keys.fieldOf(owner, foreignKey);
return {
foreignKey,
identifierField,
sourceKey,
targetKey,
otherKey: undefined,
through: undefined,
};
}
#sourcePrefix(multi: boolean, source: Model, options: AssociationOptions): string {
if (multi) return this.#keys.modelName(source).singular;
return inflection.singularize(typeof options.as === 'string' ? options.as : source.modelName);
}
}
|