@@ -9,8 +9,8 @@ import type { BaseModel } from '@adonisjs/lucid/orm'
9
9
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers'
10
10
import type { AttributeOfModelWithAttachment } from '../types/mixin.js'
11
11
12
- import { beforeSave , afterSave , beforeDelete } from '@adonisjs/lucid/orm'
13
- import { persistAttachment , commit , rollback , generateVariants } from '../utils/actions.js'
12
+ import { beforeSave , afterSave , beforeDelete , afterFind , afterFetch , afterPaginate } from '@adonisjs/lucid/orm'
13
+ import { persistAttachment , commit , rollback , generateVariants , computeUrl } from '../utils/actions.js'
14
14
import { clone , getAttachmentAttributeNames } from '../utils/helpers.js'
15
15
import { defaultStateAttributeMixin } from '../utils/default_values.js'
16
16
@@ -20,6 +20,23 @@ export const Attachmentable = <Model extends NormalizeConstructor<typeof BaseMod
20
20
class ModelWithAttachment extends superclass {
21
21
$attachments : AttributeOfModelWithAttachment = clone ( defaultStateAttributeMixin )
22
22
23
+ @afterFind ( )
24
+ static async afterFindHook ( modelInstance : ModelWithAttachment ) {
25
+ const attachmentAttributeNames = getAttachmentAttributeNames ( modelInstance )
26
+
27
+ await Promise . all (
28
+ attachmentAttributeNames . map ( ( attributeName ) => {
29
+ return computeUrl ( modelInstance , attributeName )
30
+ } )
31
+ )
32
+ }
33
+
34
+ @afterFetch ( )
35
+ @afterPaginate ( )
36
+ static async afterFetchHook ( modelInstances : ModelWithAttachment [ ] ) {
37
+ await Promise . all ( modelInstances . map ( ( row ) => this . afterFindHook ( row ) ) )
38
+ }
39
+
23
40
@beforeSave ( )
24
41
static async beforeSaveHook ( modelInstance : ModelWithAttachment ) {
25
42
const attachmentAttributeNames = getAttachmentAttributeNames ( modelInstance )
0 commit comments