UNPKG

@athenna/database

Version:

The Athenna database handler for SQL/NoSQL.

41 lines (40 loc) 1.51 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 'reflect-metadata'; import { debug } from '#src/debug'; import { Is, Options } from '@athenna/common'; import { Annotation } from '#src/helpers/Annotation'; /** * Create belongs to relation for model class. */ export function BelongsTo(model, options = {}) { return (target, key) => { const Target = target.constructor; options = Options.create(options, { isIncluded: false, // Default will be set later as: RelationModel.schema().getMainPrimaryKeyProperty() primaryKey: undefined, // Default will be set later as: `${String.toCamelCase(RelationModel.name)}Id` foreignKey: undefined }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore options.type = 'belongsTo'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore options.model = Is.String(model) ? () => ioc.safeUse(`App/Models/${model}`).constructor : model; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore options.property = key; debug('registering belongsTo metadata for model %s: %o', Target.name, options); Annotation.defineBelongsToMeta(Target, options); }; }