UNPKG

@athenna/database

Version:

The Athenna database handler for SQL/NoSQL.

39 lines (38 loc) 1.38 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 { Annotation } from '#src/helpers/Annotation'; import { Is, Options, String } from '@athenna/common'; /** * Create has one relation for model class. */ export function HasOne(model, options = {}) { return (target, key) => { const Target = target.constructor; options = Options.create(options, { isIncluded: false, primaryKey: Target.schema().getMainPrimaryKeyProperty(), foreignKey: `${String.toCamelCase(Target.name)}Id` }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore options.type = 'hasOne'; // 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 hasOne metadata for model %s: %o', Target.name, options); Annotation.defineHasOneMeta(Target, options); }; }