11/* eslint-disable no-param-reassign */
2- import { BaseEntity , In } from 'typeorm'
3- import { BaseResource , ValidationError , Filter , BaseRecord } from 'admin-bro'
2+ import { BaseEntity } from 'typeorm'
3+ import { BaseResource , ValidationError , Filter , BaseRecord , flat } from 'admin-bro'
44
55import { Property } from './Property'
66import { convertFilter } from './utils/convertFilter'
@@ -51,29 +51,6 @@ export class Resource extends BaseResource {
5151 } ) )
5252 }
5353
54- public async populate (
55- baseRecords : Array < BaseRecord > ,
56- property : Property ,
57- ) : Promise < Array < BaseRecord > > {
58- const fks : Array < any > = baseRecords . map ( ( baseRecord ) => baseRecord . params [ property . name ( ) ] )
59-
60- const instances = await this . model . findByIds ( fks )
61- const instancesRecord : Record < string , BaseEntity > = { }
62- instances . forEach ( ( instance ) => {
63- if ( instance . hasId ( ) ) {
64- instancesRecord [ ( instance as any ) . id ] = instance
65- }
66- } )
67-
68- baseRecords . forEach ( ( baseRecord ) => {
69- const fk = baseRecord . params [ property . name ( ) ]
70- const instance = instancesRecord [ fk ]
71- // eslint-disable-next-line no-param-reassign
72- if ( instance ) { baseRecord . populated [ property . name ( ) ] = new BaseRecord ( instance , this ) }
73- } )
74- return baseRecords
75- }
76-
7754 public async find (
7855 filter : Filter ,
7956 params ,
@@ -100,7 +77,7 @@ export class Resource extends BaseResource {
10077 }
10178
10279 public async findMany ( ids : Array < string | number > ) : Promise < Array < BaseRecord > > {
103- const instances = await this . model . find ( { where : { id : In ( ids ) } } )
80+ const instances = await this . model . findByIds ( ids )
10481 return instances . map ( ( instance ) => new BaseRecord ( instance , this ) )
10582 }
10683
@@ -154,17 +131,17 @@ export class Resource extends BaseResource {
154131 private prepareParams ( params : Record < string , any > ) : Record < string , any > {
155132 const preparedParams : Record < string , any > = { ...params }
156133
157- for ( const key in params ) {
158- const param = params [ key ]
159- const property = this . property ( key )
134+ this . properties ( ) . forEach ( ( property ) => {
135+ const param = flat . get ( preparedParams , property . path ( ) )
136+ const key = property . path ( )
160137
161138 // eslint-disable-next-line no-continue
162- if ( ! ( property && param !== undefined ) ) continue
139+ if ( param === undefined ) { return }
163140
164141 const type = property . type ( )
165142
166143 if ( type === 'mixed' ) {
167- preparedParams [ key ] = JSON . parse ( param )
144+ preparedParams [ key ] = param
168145 }
169146
170147 if ( type === 'number' ) {
@@ -184,7 +161,7 @@ export class Resource extends BaseResource {
184161 preparedParams [ property . column . propertyName ] = { id }
185162 }
186163 }
187- }
164+ } )
188165 return preparedParams
189166 }
190167
0 commit comments