1
- import { AsyncAdapter , NodeProvider , SyncAdapter } from '@stenodb/node'
1
+ import { AsyncAdapter , NodeProvider } from '@stenodb/node'
2
2
import { targetConstructorToSchema } from 'class-validator-jsonschema'
3
3
import type { StenoOptions } from './types'
4
- import type { Steno } from '@stenodb/node'
4
+ import type { AsyncProvider } from '@stenodb/node'
5
+ import type { ClassEntity } from '@stenodb/utils'
5
6
import type { FastifyInstance } from 'fastify'
6
7
7
8
export class StenoPlugin {
8
9
#fastify: FastifyInstance
9
10
#options: StenoOptions
10
- #provider : NodeProvider
11
- #databases : Map < string , Steno . NodeProvider < any > >
11
+ #db : NodeProvider
12
+ #providers : Map < string , AsyncProvider < any > >
12
13
13
14
constructor ( fastify : FastifyInstance , options : StenoOptions ) {
14
15
this . #fastify = fastify
15
16
this . #options = options
16
- this . #provider = new NodeProvider ( options )
17
- this . #databases = new Map < string , Steno . NodeProvider < any > > ( )
17
+ this . #db = new NodeProvider ( options )
18
+ this . #providers = new Map < string , AsyncProvider < any > > ( )
18
19
this . #fastify. decorate ( 'steno' , { get : this . getDatabase . bind ( this ) } )
19
20
}
20
21
@@ -33,18 +34,17 @@ export class StenoPlugin {
33
34
this . registerEntities ( )
34
35
35
36
for ( const adapter of this . #options. adapters ) {
36
- const db = await this . # provider. create ( adapter )
37
+ let provider : AsyncProvider < any >
37
38
38
- if ( adapter instanceof SyncAdapter ) {
39
- db . read ( )
40
- } else if ( adapter instanceof AsyncAdapter ) {
41
- await db . read ( )
39
+ if ( adapter instanceof AsyncAdapter ) {
40
+ provider = await this . #db. create ( adapter )
41
+ await provider . read ( )
42
42
} else {
43
43
throw new TypeError ( 'Invalid adapter' )
44
44
}
45
45
46
46
this . addSchema ( adapter . entity )
47
- this . #databases . set ( adapter . fileName , db )
47
+ this . #providers . set ( adapter . fileName , provider )
48
48
}
49
49
}
50
50
@@ -55,7 +55,7 @@ export class StenoPlugin {
55
55
}
56
56
}
57
57
58
- private addSchema ( entity : Steno . Entity < any > ) : void {
58
+ private addSchema ( entity : ClassEntity < any > ) : void {
59
59
if ( this . #fastify. getSchema ( entity . name ) ) return
60
60
const schema = targetConstructorToSchema (
61
61
entity ,
@@ -65,9 +65,9 @@ export class StenoPlugin {
65
65
this . #fastify. addSchema ( { ...schema , $id : entity . name } )
66
66
}
67
67
68
- private getDatabase < T extends Steno . Entity < any > > (
68
+ private getDatabase < T extends ClassEntity < any > > (
69
69
name : string
70
- ) : Steno . NodeProvider < T > | undefined {
71
- return this . #databases . get ( name )
70
+ ) : AsyncProvider < T > | undefined {
71
+ return this . #providers . get ( name )
72
72
}
73
73
}
0 commit comments