Skip to content

Commit

Permalink
fix(sql): use tsx instead of ts-node for ts runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Sep 17, 2024
1 parent 8d7ec6b commit 80948d8
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"tsx": "^4.19.1",
"typedoc": "^0.23.17",
"typescript": "~5.4.5"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/sql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@deepkit/logger": "^1.0.1-alpha.13",
"@deepkit/orm": "^1.0.1-alpha.13",
"@deepkit/stopwatch": "^1.0.1-alpha.13",
"@deepkit/type": "^1.0.1-alpha.13"
"@deepkit/type": "^1.0.1-alpha.13",
"tsx": "4.19.1"
},
"dependencies": {
"@types/sqlstring": "^2.2.1",
Expand Down
25 changes: 12 additions & 13 deletions packages/sql/src/migration/migration-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ export class MigrationProvider {
return migrationsPerDatabase;
}

private async registerTsNode() {
private async registerTsx() {
const esm = isEsm();
const { register } = await import('ts-node');
register({
esm,
compilerOptions: {
experimentalDecorators: true,
module: esm ? 'ESNext' : 'CommonJS',
},
transpileOnly: true,
});
if (esm) {
// @ts-ignore
const { register } = await import('tsx/esm/api');
register();
} else {
const { register } = require('tsx/cjs/api');
register();
}
}

async addDatabase(path: string): Promise<void> {
await this.registerTsNode();
await this.registerTsx();

const exports = Object.values((await import(path) || {}));
const exports = Object.values((await import(join(process.cwd(), path)) || {}));
if (!exports.length) {
throw new Error(`No database found in path ${path}`);
}
Expand Down Expand Up @@ -100,7 +99,7 @@ export class MigrationProvider {

const files = await glob('**/*.ts', { cwd: migrationDir });

await this.registerTsNode();
await this.registerTsx();

for (const file of files) {
const path = join(process.cwd(), migrationDir, file);
Expand Down
Loading

0 comments on commit 80948d8

Please sign in to comment.