adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.08k stars 195 forks source link

feat: add meta to all relations #1042

Closed zaosoula closed 4 days ago

zaosoula commented 4 months ago

πŸ”— Linked issue

❓ Type of change

πŸ“š Description

Add the meta property to relations like on classic column to convey additional data for third party libraries

πŸ“ Checklist

thetutlage commented 1 week ago

Hello @zaosoula

Can you please share what use-cases we are trying to serve by adding the meta property to relationships?

zaosoula commented 1 week ago

@thetutlage Hi, we use meta to define the options of editable field for the frontend, it allow use to automaticaly generate view, and forms based on the model

import { BaseModel, column, manyToMany } from '@adonisjs/lucid/orm'
import { compose } from '@adonisjs/core/helpers'
import { Filterable } from 'adonis-lucid-filter'
import ProductFilter from '#filters/product_filter'
import { CommonModel } from '#mixins/common_model'
import { EntityModel } from '#mixins/entity_model'
import User from '#models/user'
import ProductCategory from './product_category.js'
import type { ManyToMany } from '@adonisjs/lucid/types/relations'

export default class Product extends compose(
  BaseModel,
  Filterable,
  CommonModel({ userModel: User }),
  EntityModel({ userModel: User })
) {
  static $filter = () => ProductFilter

  @column({
    meta: {
      label: 'Nom',
      type: 'text',
      summary: true,
    },
  })
  declare name: string

  @column({
    meta: {
      label: 'RΓ©current',
      type: 'checkbox',
      summary: true,
    },
  })
  declare isRecurrent: boolean

  @manyToMany(() => ProductCategory, {
    meta: {
      label: 'CatΓ©gories',
      type: 'autocomplete',
      options: {
        resource: 'product-category',
        label: 'name',
      },
      summary: true,
    },
  })
  declare categories: ManyToMany<typeof ProductCategory>
}
thetutlage commented 4 days ago

Thanks πŸ‘