11import { extname } from 'path' ;
2+ import { fileURLToPath } from 'url' ;
3+ import { readFileSync } from 'fs' ;
24import {
35 DatabaseEngine ,
46 DatabaseEngineTransaction ,
@@ -11,6 +13,10 @@ import {
1113} from '@databases/migrations-base' ;
1214import { ConnectionPool , Queryable , Transaction } from '@databases/pg' ;
1315
16+ const packageVersion : string = JSON . parse (
17+ readFileSync ( fileURLToPath ( import . meta. resolve ( '../package.json' ) ) , 'utf8' ) ,
18+ ) . version ;
19+
1420export interface MigrationsConfig {
1521 migrationsDirectory : string ;
1622 /**
@@ -36,9 +42,9 @@ export default class PostgresDatabaseEngine
3642 this . directory = new DirectoryContext (
3743 config . migrationsDirectory ,
3844 // load migration:
39- (
45+ async (
4046 migrationFileName : string ,
41- ) : Result < Migration , MigrationWithNoValidExport > => {
47+ ) : Promise < Result < Migration , MigrationWithNoValidExport > > => {
4248 switch ( extname ( migrationFileName ) ) {
4349 case '.sql' :
4450 return Result . ok ( async ( db : Transaction ) => {
@@ -47,10 +53,16 @@ export default class PostgresDatabaseEngine
4753 case '.js' :
4854 case '.mjs' :
4955 case '.jsx' :
50- return getExport ( require ( migrationFileName ) , migrationFileName ) ;
56+ return getExport (
57+ await import ( migrationFileName ) ,
58+ migrationFileName ,
59+ ) ;
5160 case '.ts' :
5261 case '.tsx' :
53- return getExport ( require ( migrationFileName ) , migrationFileName ) ;
62+ return getExport (
63+ await import ( migrationFileName ) ,
64+ migrationFileName ,
65+ ) ;
5466 default :
5567 throw new Error (
5668 `Unsupported extension "${ extname ( migrationFileName ) } "` ,
@@ -63,7 +75,7 @@ export default class PostgresDatabaseEngine
6375 readonly databaseName = 'Postgres' ;
6476 readonly packageName = '@databases/pg-migrations' ;
6577 readonly cliName = 'pg-migrations' ;
66- readonly packageVersion : string = require ( '../package.json' ) . version ;
78+ readonly packageVersion : string = packageVersion ;
6779
6880 async checkDatabaseVersion ( ) : Promise < Result < void , DatabaseVersionError > > {
6981 const [ major , minor ] = await getPgVersion ( this . _connection ) ;
@@ -203,9 +215,9 @@ export default class PostgresDatabaseEngine
203215 } ) ;
204216 }
205217
206- loadMigration (
218+ async loadMigration (
207219 migrationFileName : string ,
208- ) : Result < Migration , MigrationWithNoValidExport > {
220+ ) : Promise < Result < Migration , MigrationWithNoValidExport > > {
209221 switch ( extname ( migrationFileName ) ) {
210222 case '.sql' :
211223 return Result . ok ( async ( db : Transaction ) => {
@@ -214,10 +226,10 @@ export default class PostgresDatabaseEngine
214226 case '.js' :
215227 case '.mjs' :
216228 case '.jsx' :
217- return getExport ( require ( migrationFileName ) , migrationFileName ) ;
229+ return getExport ( await import ( migrationFileName ) , migrationFileName ) ;
218230 case '.ts' :
219231 case '.tsx' :
220- return getExport ( require ( migrationFileName ) , migrationFileName ) ;
232+ return getExport ( await import ( migrationFileName ) , migrationFileName ) ;
221233 default :
222234 throw new Error (
223235 `Unsupported extension "${ extname ( migrationFileName ) } "` ,
0 commit comments