@@ -7,8 +7,6 @@ Sometimes you need to visualize custom columns which do not exist in database.
77For doing this you can use ` virtual ` columns.
88
99``` ts title='./resources/apartments.ts'
10- // diff-add
11- import { AdminForthDataTypes , AdminForthResourcePages } from ' adminforth' ;
1210
1311...
1412resourceId : ' aparts' ,
@@ -71,11 +69,11 @@ columns: [
7169 return iso? .toUpperCase ()? .replace (/ . / g , (char ) => String .fromCodePoint (char .charCodeAt (0 ) + 127397 ));
7270 }
7371< / script>
74- ` ` `
72+ ` ` `
73+
74+ Here is how it looks:
7575
76- To test component open some apartment for edit and change ` description` field to ` US New York` .
77- Here is how it looks:
78- 
76+ 
7977
8078
8179## Virtual columns for editing.
@@ -108,43 +106,46 @@ columns: [
108106]
109107 ` ` `
110108
111- Now to handle virtual ` password` field we use hooks:
109+ Now to handle virtual ` password` field we use hooks:
112110
113111
114- ` ` ` ts title= " ./index.ts"
115- hooks: {
112+ ` ` ` ts title= " ./index.ts"
113+ hooks: {
116114 create: {
117- beforeSave: async ({ record, adminUser, resource }) => {
118- record .password_hash = await AdminForth .Utils .generatePasswordHash (record .password );
119- return { ok: true , error : false };
115+ beforeSave: async ({ record, adminUser, resource }: { record : any, adminUser : AdminUser, resource : AdminForthResource } ) => {
116+ record .passwordHash = await AdminForth .Utils .generatePasswordHash (record .password );
117+ return { ok: true };
120118 }
121119 },
122120 edit: {
123- beforeSave: async ({ record , adminUser, resource}) => {
124- if (record .password ) {
125- record . password_hash = await AdminForth .Utils .generatePasswordHash (record .password );
121+ beforeSave: async ({ updates , adminUser, resource } : { updates : any, adminUser : AdminUser, resource : AdminForthResource }) => {
122+ if (updates .password ) {
123+ updates . passwordHash = await AdminForth .Utils .generatePasswordHash (updates .password );
126124 }
127- return { ok: true , error : false }
125+ return { ok: true }
128126 },
129127 },
130- }
128+ },
131129` ` `
132130
133- Hook still has access to the virtual field ` record .password ` , and we use built-in AdminForth hasher to hash password and write it into
131+ Hook still has access to the virtual field ` updates .password ` , and we use built-in AdminForth hasher to hash password and write it into
134132` password_hash` field which exists in database.
135133
136- After hook is executed, ` record .password ` will be removed from the record since it is virtual, so password itself will not be saved to the database.
134+ After hook is executed, ` updates .password ` will be removed from the record since it is virtual, so password itself will not be saved to the database.
135+
136+
137+ ### Backend-only fields
137138
138139Another important point is that ` hashed_password` field should never be passed to frontend due to security reasons.
139140
140141To do it we have 2 options:
141142
1421431) Do not list ` password_hash` in the ` columns` array of the resource. If AdminForth knows nothing about field
143144it will never pass this field to frontend.
144- 2) Define ` password_hash` but set ` backendOnly`
145+ 2) Define ` password_hash` in columns way but set ` backendOnly` . The scond option is more explicit and should be preferrred
145146
146147` ` ` ts
147- {
148+ {
148149 name: ' password_hash' ,
149150 type: AdminForthDataTypes .STRING ,
150151 showIn: { all: false },
0 commit comments