adonisjs / attachment-lite

Turn any field on your Lucid models to an attachment data type
MIT License
68 stars 20 forks source link

[Adonis Responsive Attachment] preComputeUrls dosn't work when using Database query #15

Closed AT-Lorlando closed 2 years ago

AT-Lorlando commented 2 years ago

Package version

https://github.com/ndianabasi/adonis-responsive-attachment V 1.3.0

Node.js and npm version

Node version: v16.11.0 Npm version: 8.0.0

The error

When getting images from the databases, you can use Image.query() or Database.from(Image.table).

When using Database.from(Image.table), your SGBD return usable timestamp, so it can help to use this method. But, with version V 1.3.0 of this packages, the image's url dosn't compute when using Database.from(Image.table) as you can see below

Sample Code (to reproduce the issue)

Images models:

@responsiveAttachment({
    folder: 'images',
    preComputeUrls: true,
    breakpoints: {
      medium: 'off',
      large: 'off',
      thumbmail: 'off',
    },
  })
  public data: ResponsiveAttachmentContract

Query to fetch an image

  public async show({ params }: HttpContextContract) {
    const image = await Image.findOrFail(params.id)
    return image
  }

Return this result:

{
    "folder_id": 9,
    "id": 1000,
    "title": "IMG_7740.png",
    "data": {
        "name": "images/original_cl3od91bf000argxi5zmohrc5.png",
        "size": 23187.75,
        "hash": "cl3od91bf000argxi5zmohrc5",
        "width": 3861,
        "format": "png",
        "height": 2574,
        "extname": "png",
        "mimeType": "image/png",
        "breakpoints": {
            "thumbnail": {
                "name": "images/thumbnail_cl3od91bf000argxi5zmohrc5.png",
                "hash": "cl3od91bf000argxi5zmohrc5",
                "extname": "png",
                "mimeType": "image/png",
                "format": "png",
                "width": 234,
                "height": 156,
                "size": 96.57,
                "url": "/uploads/images/thumbnail_cl3od91bf000argxi5zmohrc5.png"
            },
            "small": {
                "name": "images/small_cl3od91bf000argxi5zmohrc5.png",
                "hash": "cl3od91bf000argxi5zmohrc5",
                "extname": "png",
                "mimeType": "image/png",
                "format": "png",
                "width": 500,
                "height": 333,
                "size": 428.04,
                "url": "/uploads/images/small_cl3od91bf000argxi5zmohrc5.png"
            }
        },
        "url": "/uploads/images/original_cl3od91bf000argxi5zmohrc5.png"
    },
    "raw": null,
    "created_at": "2022-05-27T13:33:58.194+02:00",
    "updated_at": "2022-05-27T13:33:58.194+02:00"
}

But if you're using this method to fetch from db:

  public async show({ params }: HttpContextContract) {
    const image = await Database.from(Image.table).where('id', params.id).first()    
    return image
  }

The result is:

{
    "folder_id": 9,
    "id": 1000,
    "title": "IMG_7740.png",
    "data": {
        "name": "images/original_cl3od91bf000argxi5zmohrc5.png",
        "size": 23187.75,
        "hash": "cl3od91bf000argxi5zmohrc5",
        "width": 3861,
        "format": "png",
        "height": 2574,
        "extname": "png",
        "mimeType": "image/png",
        "breakpoints": {
            "thumbnail": {
                "name": "images/thumbnail_cl3od91bf000argxi5zmohrc5.png",
                "hash": "cl3od91bf000argxi5zmohrc5",
                "extname": "png",
                "mimeType": "image/png",
                "format": "png",
                "width": 234,
                "height": 156,
                "size": 96.57
            },
            "small": {
                "name": "images/small_cl3od91bf000argxi5zmohrc5.png",
                "hash": "cl3od91bf000argxi5zmohrc5",
                "extname": "png",
                "mimeType": "image/png",
                "format": "png",
                "width": 500,
                "height": 333,
                "size": 428.04
            }
        }
    },
    "raw": null,
    "created_at": "2022-05-27T11:33:58.194Z",
    "updated_at": "2022-05-27T11:33:58.194Z"
}

There is no URL, and the timestamp are TZ

I'm really sorry if this is normal, but, it worked on previous version 1.2.2 so i thought something happened since then

RomainLanz commented 2 years ago

You have an issue with a community package, not attachment-lite. Please, create an issue in their repository.

ndianabasi commented 2 years ago

Hi @AT-Lorlando. Please submit your issue here: https://github.com/ndianabasi/adonis-responsive-attachment/issues