Skip to content

Model setup

Next, in the model, import the attachment decorator, Attachmentable mixin and the Attachment type from the package.

Make sure NOT to use the @column decorator when using the @attachment decorator.

ts
import { BaseModel } from '@adonisjs/lucid/orm'
import { compose } from '@adonisjs/core/helpers'
import { attachment, Attachmentable } from '@jrmc/adonis-attachment'
import type { Attachment } from '@jrmc/adonis-attachment/types/attachment'

class User extends compose(BaseModel, Attachmentable) { 
  @attachment() 
  declare avatar: Attachment
}

Specifying subfolder

You can also store files inside the subfolder by defining the folder property as follows.

ts
class User extends BaseModel {
  @attachment({ folder: 'uploads/avatars' }) 
  declare avatar: Attachment
}

Specifying variants

Generate variants

ts
class User extends BaseModel {
  @attachment({
    variants: ['thumbnail', 'medium', 'large'] 
  })
  declare avatar: Attachment
}

Specifying disk

You can specify type of disk to use, default is defined in default adonis/drive config

ts
class User extends BaseModel {
  @attachment({ disk: 's3' }) 
  declare avatar: Attachment
}

Specifying preComputeUrl

You can enabled pre compute the URLs after SELECT queries, default is false

ts
class User extends BaseModel {
  @attachment({ preComputeUrl: true }) 
  declare avatar: Attachment
}

Specifying meta

You can disabled meta generation, default is true

ts
class User extends BaseModel {
  @attachment({ meta: false }) 
  declare avatar: Attachment
}

Specifying rename

You can disabled rename file, default is true

ts
class User extends BaseModel {
  @attachment({ rename: false }) 
  declare avatar: Attachment
}