@athenna/database
Version:
The Athenna database handler for SQL/NoSQL.
39 lines (38 loc) • 1.24 kB
JavaScript
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { HasManyThroughRelation } from '#src/models/relations/HasManyThrough/HasManyThroughRelation';
export class HasOneThroughRelation {
/**
* Load a has one through relation.
*/
static async load(model, relation) {
await HasManyThroughRelation.load(model, relation);
const arr = model[relation.property] || [];
model[relation.property] = arr[0] ?? null;
return model;
}
/**
* Load all models that has one through relation.
*/
static async loadAll(models, relation) {
const result = await HasManyThroughRelation.loadAll(models, relation);
return result.map(m => {
const arr = m[relation.property] || [];
m[relation.property] = arr[0] ?? null;
return m;
});
}
/**
* Apply a where has relation to the query when the given model
* has one through relation.
*/
static whereHas(Model, query, relation) {
return HasManyThroughRelation.whereHas(Model, query, relation);
}
}