UNPKG

@athenna/database

Version:

The Athenna database handler for SQL/NoSQL.

55 lines (54 loc) 1.78 kB
/** * @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 { String } from '@athenna/common'; export class BelongsToRelation { /** * Get the options with defined default values. */ static options(relation) { const RelationModel = relation.model(); relation.primaryKey = relation.primaryKey || RelationModel.schema().getMainPrimaryKeyProperty(); relation.foreignKey = relation.foreignKey || `${String.toCamelCase(RelationModel.name)}Id`; return relation; } /** * Load a belongs to relation. */ static async load(model, relation) { this.options(relation); model[relation.property] = await relation .model() .query() .where(relation.primaryKey, model[relation.foreignKey]) .when(relation.closure, relation.closure) .find(); return model; } /** * Load all models that belongs to relation. */ static async loadAll(models, relation) { this.options(relation); const foreignValues = models.map(model => model[relation.foreignKey]); const results = await relation .model() .query() .whereIn(relation.primaryKey, foreignValues) .when(relation.closure, relation.closure) .findMany(); const map = new Map(); results.forEach(result => map.set(result[relation.primaryKey], result)); return models.map(model => { model[relation.property] = map.get(model[relation.foreignKey]); return model; }); } }