@@ -24,72 +24,92 @@ import {
24
24
import { clone } from '../utils/helpers.js'
25
25
import { defaultStateAttributeMixin } from '../utils/default_values.js'
26
26
27
- export const attachment = ( options ?: LucidOptions ) => {
28
- return function ( target : any , attributeName : string ) {
29
- if ( ! target [ optionsSym ] ) {
30
- target [ optionsSym ] = { }
31
- }
32
-
33
- target [ optionsSym ] [ attributeName ] = options
34
-
35
- const Model = target . constructor as LucidModel & {
36
- $attachments : AttributeOfModelWithAttachment
37
- }
38
- Model . boot ( )
27
+ export const bootModel = ( model : LucidModel & {
28
+ $attachments : AttributeOfModelWithAttachment
29
+ } , options ?: LucidOptions ) => {
30
+ model . boot ( )
39
31
40
- Model . $attachments = clone ( defaultStateAttributeMixin )
32
+ model . $attachments = clone ( defaultStateAttributeMixin )
41
33
42
34
/**
43
35
* Registering all hooks only once
44
36
*/
45
-
46
- if ( ! Model . $hooks . has ( 'find' , afterFindHook ) ) {
47
- Model . after ( 'find' , afterFindHook )
37
+ if ( ! model . $hooks . has ( 'find' , afterFindHook ) ) {
38
+ model . after ( 'find' , afterFindHook )
48
39
}
49
- if ( ! Model . $hooks . has ( 'fetch' , afterFetchHook ) ) {
50
- Model . after ( 'fetch' , afterFetchHook )
40
+ if ( ! model . $hooks . has ( 'fetch' , afterFetchHook ) ) {
41
+ model . after ( 'fetch' , afterFetchHook )
51
42
}
52
- if ( ! Model . $hooks . has ( 'paginate' , afterFetchHook ) ) {
53
- Model . after ( 'paginate' , afterFetchHook )
43
+ if ( ! model . $hooks . has ( 'paginate' , afterFetchHook ) ) {
44
+ model . after ( 'paginate' , afterFetchHook )
54
45
}
55
- if ( ! Model . $hooks . has ( 'save' , beforeSaveHook ) ) {
56
- Model . before ( 'save' , beforeSaveHook )
46
+ if ( ! model . $hooks . has ( 'save' , beforeSaveHook ) ) {
47
+ model . before ( 'save' , beforeSaveHook )
57
48
}
58
- if ( ! Model . $hooks . has ( 'save' , afterSaveHook ) ) {
59
- Model . after ( 'save' , afterSaveHook )
49
+ if ( ! model . $hooks . has ( 'save' , afterSaveHook ) ) {
50
+ model . after ( 'save' , afterSaveHook )
60
51
}
61
- if ( ! Model . $hooks . has ( 'delete' , beforeDeleteHook ) ) {
62
- Model . before ( 'delete' , beforeDeleteHook )
52
+ if ( ! model . $hooks . has ( 'delete' , beforeDeleteHook ) ) {
53
+ model . before ( 'delete' , beforeDeleteHook )
63
54
}
55
+ }
64
56
57
+ const makeColumnOptions = ( options ?: LucidOptions ) => {
65
58
const { disk, folder, variants, meta, rename, ...columnOptions } = {
66
- ...defaultOptionsDecorator ,
67
- ...options ,
68
- }
69
-
70
- Model . $addColumn ( attributeName , {
71
- consume : ( value ) => {
72
- if ( value ) {
73
- const attachment = attachmentManager . createFromDbResponse ( value )
74
- attachment ?. setOptions ( { disk, folder, variants } )
59
+ ...defaultOptionsDecorator ,
60
+ ...options ,
61
+ }
62
+ return ( {
63
+ consume : ( value : any ) => {
64
+ if ( value ) {
65
+ const attachment = attachmentManager . createFromDbResponse ( value )
66
+ attachment ?. setOptions ( { disk, folder, variants } )
75
67
76
- if ( options && options ?. meta !== undefined ) {
77
- attachment ?. setOptions ( { meta : options ! . meta } )
78
- }
79
- if ( options && options ?. rename !== undefined ) {
80
- attachment ?. setOptions ( { rename : options ! . rename } )
68
+ if ( options && options ?. meta !== undefined ) {
69
+ attachment ?. setOptions ( { meta : options ! . meta } )
70
+ }
71
+ if ( options && options ?. rename !== undefined ) {
72
+ attachment ?. setOptions ( { rename : options ! . rename } )
73
+ }
74
+ if ( options && options ?. preComputeUrl !== undefined ) {
75
+ attachment ?. setOptions ( { preComputeUrl : options ! . preComputeUrl } )
76
+ }
77
+ return attachment
78
+ } else {
79
+ return null
81
80
}
82
- if ( options && options ?. preComputeUrl !== undefined ) {
83
- attachment ?. setOptions ( { preComputeUrl : options ! . preComputeUrl } )
84
- }
85
- return attachment
86
- } else {
87
- return null
88
- }
89
81
} ,
90
- prepare : ( value ) => ( value ? JSON . stringify ( value . toObject ( ) ) : null ) ,
91
- serialize : ( value ) => ( value ? value . toJSON ( ) : null ) ,
82
+ prepare : ( value : any ) => ( value ? JSON . stringify ( value . toObject ( ) ) : null ) ,
83
+ serialize : ( value : any ) => ( value ? value . toJSON ( ) : null ) ,
92
84
...columnOptions ,
93
- } )
94
- }
85
+ } )
95
86
}
87
+
88
+
89
+ const makeAttachmentDecorator = ( columnOptionsTransformer ?: ( columnOptions : any ) => any ) =>
90
+ ( options ?: LucidOptions ) => {
91
+ return function ( target : any , attributeName : string ) {
92
+ if ( ! target [ optionsSym ] ) {
93
+ target [ optionsSym ] = { }
94
+ }
95
+
96
+ target [ optionsSym ] [ attributeName ] = options
97
+
98
+ const Model = target . constructor as LucidModel & {
99
+ $attachments : AttributeOfModelWithAttachment
100
+ }
101
+
102
+ bootModel ( Model , options )
103
+
104
+ const columnOptions = makeColumnOptions ( options )
105
+ const transformedColumnOptions = columnOptionsTransformer ? columnOptionsTransformer ( columnOptions ) : columnOptions
106
+ Model . $addColumn ( attributeName , transformedColumnOptions )
107
+ }
108
+ }
109
+
110
+
111
+ export const attachment = makeAttachmentDecorator ( )
112
+ export const attachments = makeAttachmentDecorator ( ( columnOptions ) => ( {
113
+ consume : ( value : any [ ] ) => value . map ( columnOptions . consume ) ,
114
+ prepare : ( value : any [ ] ) => ( value ? JSON . stringify ( value . map ( v => v . toObject ( ) ) ) : null ) ,
115
+ } ) )
0 commit comments