@@ -21,38 +21,36 @@ export type AdapterORM = {
2121 metadata : MetadataStorage ;
2222} ;
2323
24- // eslint-disable-next-line max-len
25- const OrmNotFoundError = 'ORM is not set. Make sure to set it before registering the adapter: AdminJS.setORM(mikroOrmInstance)' ;
26-
2724export class Resource extends BaseResource {
28- public static orm : AdapterORM ;
29-
3025 public static validate : any ;
3126
32- private metadata : EntityMetadata | undefined ;
27+ private orm : MikroORM ;
28+
29+ private metadata ?: EntityMetadata ;
3330
3431 private model : EntityClass < AnyEntity > ;
3532
3633 private propertiesObject : Record < string , Property > ;
3734
38- constructor ( model : EntityClass < AnyEntity > ) {
39- super ( model ) ;
35+ constructor ( args : { model : EntityClass < AnyEntity > , orm : MikroORM } ) {
36+ super ( args ) ;
37+
38+ const { model, orm } = args ;
39+ this . orm = orm ;
4040 this . model = model ;
41- this . metadata = Resource . orm ?. metadata ? .find ( model . name ) ;
41+ this . metadata = this . orm . getMetadata ( ) . find ( model . name ) ;
4242 this . propertiesObject = this . prepareProperties ( ) ;
4343 }
4444
45- public static setORM ( orm : MikroORM ) {
46- orm . getMetadata ( ) . decorate ( orm . em ) ;
47- Resource . orm = Resource . stripOrmConfig ( orm ) ;
48- }
49-
5045 public databaseName ( ) : string {
51- return Resource . orm ?. database || 'mikroorm' ;
46+ const {
47+ database,
48+ } = this . orm . config . getDriver ( ) . getConnection ( ) . getConnectionOptions ( ) ;
49+ return database || 'mikroorm' ;
5250 }
5351
5452 public databaseType ( ) : string {
55- return Resource . orm ?. databaseType || this . databaseName ( ) ;
53+ return this . orm . config . getAll ( ) . type || this . databaseName ( ) ;
5654 }
5755
5856 public name ( ) : string {
@@ -76,20 +74,16 @@ export class Resource extends BaseResource {
7674 }
7775
7876 public async count ( filter : Filter ) : Promise < number > {
79- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
80-
81- return Resource . orm . entityManager . getRepository ( this . model ) . count (
77+ return this . orm . em . getRepository ( this . model ) . count (
8278 convertFilter ( filter ) ,
8379 ) ;
8480 }
8581
8682 public async find ( filter : Filter , params : Record < string , any > = { } ) : Promise < Array < BaseRecord > > {
87- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
88-
8983 const { limit = 10 , offset = 0 , sort = { } } = params ;
9084 const { direction, sortBy } = sort as { direction : 'asc' | 'desc' , sortBy : string } ;
9185
92- const results = await Resource . orm . entityManager
86+ const results = await this . orm . em
9387 . getRepository ( this . model )
9488 . find (
9589 convertFilter ( filter ) , {
@@ -105,11 +99,9 @@ export class Resource extends BaseResource {
10599 }
106100
107101 public async findOne ( id : string | number ) : Promise < BaseRecord | null > {
108- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
109-
110- const result = await Resource . orm . entityManager
102+ const result = await this . orm . em
111103 . getRepository ( this . model )
112- . findOne ( id as any ) ; // mikroorm has incorrect types for findOne
104+ . findOne ( id as any ) ; // mikroorm has incorrect types for ` findOne`
113105
114106 if ( ! result ) return null ;
115107
@@ -119,22 +111,18 @@ export class Resource extends BaseResource {
119111 public async findMany (
120112 ids : Array < string | number > ,
121113 ) : Promise < Array < BaseRecord > > {
122- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
123-
124114 const pk = this . metadata ?. primaryKeys [ 0 ] ;
125115 if ( ! pk ) return [ ] ;
126116
127- const results = await Resource . orm . entityManager
117+ const results = await this . orm . em
128118 . getRepository ( this . model )
129119 . find ( { [ pk ] : { $in : ids } } ) ;
130120
131121 return results . map ( ( result ) => new BaseRecord ( wrap ( result ) . toJSON ( ) , this ) ) ;
132122 }
133123
134124 public async create ( params : Record < string , any > ) : Promise < Record < string , any > > {
135- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
136-
137- const instance = Resource . orm ?. entityManager
125+ const instance = this . orm . em
138126 . getRepository ( this . model )
139127 . create ( flat . unflatten ( params ) ) ;
140128
@@ -146,9 +134,7 @@ export class Resource extends BaseResource {
146134 }
147135
148136 public async update ( pk : string | number , params : Record < string , any > = { } ) : Promise < Record < string , any > > {
149- if ( ! Resource . orm ) throw new Error ( OrmNotFoundError ) ;
150-
151- const instance = await Resource . orm ?. entityManager
137+ const instance = await this . orm . em
152138 . getRepository ( this . model )
153139 . findOne ( pk as any ) ; // mikroorm has incorrect types for findOneOrFail
154140
@@ -164,32 +150,15 @@ export class Resource extends BaseResource {
164150 }
165151
166152 public async delete ( id : string | number ) : Promise < void > {
167- if ( ! Resource . orm ) return ;
168-
169- await Resource . orm ?. entityManager
153+ await this . orm . em
170154 . getRepository ( this . model )
171155 . nativeDelete ( id as any ) ; // mikroorm has incorrect types for nativeDelete
172156 }
173157
174- public static isAdapterFor ( rawResource : EntityClass < AnyEntity > ) : boolean {
175- try {
176- if ( Resource . orm === null ) return false ;
177- const metadata = Resource . orm ?. metadata ?. find ( rawResource . name ) ;
178- return ! ! metadata ;
179- } catch ( e ) {
180- return false ;
181- }
182- }
183-
184- public static stripOrmConfig ( orm : MikroORM ) {
185- const {
186- database,
187- } = orm . config . getDriver ( ) . getConnection ( ) . getConnectionOptions ( ) ;
188- const databaseType = orm . config . getAll ( ) . type ;
189- const entityManager = orm . em ;
190- const metadata = orm . getMetadata ( ) ;
158+ public static isAdapterFor ( args ?: { model ?: EntityClass < AnyEntity > , orm ?: MikroORM } ) : boolean {
159+ const { model, orm } = args ?? { } ;
191160
192- return { database , databaseType , entityManager , metadata } ;
161+ return ! ! model ?. name && ! ! orm ?. getMetadata ?. ( ) . find ?. ( model . name ) ;
193162 }
194163
195164 async validateAndSave ( instance : Loaded < AnyEntity > ) : Promise < void > {
@@ -210,7 +179,7 @@ export class Resource extends BaseResource {
210179 }
211180 }
212181 try {
213- await Resource . orm ?. entityManager . persistAndFlush ( instance ) ;
182+ await this . orm . em . persistAndFlush ( instance ) ;
214183 } catch ( error ) {
215184 if ( error . name === 'QueryFailedError' ) {
216185 throw new ValidationError ( {
0 commit comments