11import path from 'path' ;
2- import knexModule , { Knex } from 'knex' ;
2+ import { fileURLToPath } from 'url' ;
3+ import knex from 'knex' ;
4+ import type { Knex } from 'knex' ;
35import { AGServer } from 'socketcluster-server' ;
46
7+ // eslint-disable-next-line @typescript-eslint/ban-types
8+ type KnexFunction = < TRecord extends { } = any , TResult = unknown [ ] > (
9+ config : Knex . Config | string
10+ ) => Knex < TRecord , TResult > ;
11+
512export default function connector ( options : AGServer . AGServerOptions ) {
613 const dbOptions = options . dbOptions as Knex . Config ;
714 dbOptions . useNullAsDefault = true ;
815 if ( ! ( dbOptions as any ) . migrate ) {
9- return knexModule ( dbOptions ) ;
16+ return ( knex as unknown as KnexFunction ) ( dbOptions ) ;
1017 }
1118
12- dbOptions . migrations = { directory : path . resolve ( __dirname , 'migrations' ) } ;
13- dbOptions . seeds = { directory : path . resolve ( __dirname , 'seeds' ) } ;
14- const knex = knexModule ( dbOptions ) ;
19+ dbOptions . migrations = {
20+ directory : path . join (
21+ path . dirname ( fileURLToPath ( import . meta. url ) ) ,
22+ 'migrations'
23+ ) ,
24+ } ;
25+ dbOptions . seeds = {
26+ directory : path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , 'seeds' ) ,
27+ } ;
28+ const knexInstance = ( knex as unknown as KnexFunction ) ( dbOptions ) ;
1529
1630 /* eslint-disable no-console */
17- knex . migrate
31+ knexInstance . migrate
1832 . latest ( { loadExtensions : [ '.js' ] } )
1933 . then ( function ( ) {
20- return knex . seed . run ( { loadExtensions : [ '.js' ] } ) ;
34+ return knexInstance . seed . run ( { loadExtensions : [ '.js' ] } ) ;
2135 } )
2236 . then ( function ( ) {
2337 console . log ( ' \x1b[0;32m[Done]\x1b[0m Migrations are finished\n' ) ;
@@ -27,5 +41,5 @@ export default function connector(options: AGServer.AGServerOptions) {
2741 } ) ;
2842 /* eslint-enable no-console */
2943
30- return knex ;
44+ return knexInstance ;
3145}
0 commit comments