-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use import statement outside a module #324
Comments
Hey @nodegin you can use this lib as an executor, mine was fixed when use this lib. package.json: "typeorm": "tsx ./node_modules/typeorm/cli",
"seeding": "tsx ./node_modules/@jorgebodega/typeorm-seeding/dist/cli",
"migration:generate": "npm run typeorm -- -d ./src/db/typeorm.config.ts migration:generate ./src/db/migrations/$npm_config_name",
"migration:run": "npm run typeorm -- -d ./src/db/typeorm.config.ts migration:run",
"migration:revert": "npm run typeorm -- -d ./src/db/typeorm.config.ts migration:revert",
"seed:run": "npm run seeding -- seed -d ./src/db/typeorm.config.ts ./src/db/seeds/*.ts" terminal: npm run seed:run |
v7 switched from CommonJS to ESM. It is not mentioned in the breaking changes, but it is a breaking change for projects that are using CommonJS |
As far as I tried, I did not change anything to be ESM-only (TypeORM is still supporting CommonJS). If you are sure of this problem, a PR would be appreciated. |
Introduced by PR #303 in This change replaced the Probably the best option for consistency would be to use |
Normally since it's a CLI tool, it shouldn't matter whether it's CommonJS or ESM, but it does if you are forced to call Later edit: |
I'll update the |
Since neither In general, I would not change from |
|
You may don't need anything at all. at least some typescript runner eg // create-users.ts
import dataSource from "@demo-app/backend/db/seeds/datasource";
import { UsersEntity } from "@demo-app/backend/src/users/users.entity";
const users: Partial<UsersEntity>[] = [ {
email: "test@test.gmail.com",
password: "test",
} ];
await dataSource.createEntityManager().save(
users.map( user => Object.assign( new UsersEntity(), user ) )
); // datasource.ts
import { DataSource } from "typeorm";
import DatabaseConfig from "@demo-app/backend/config/database.config";
const dataSource = await ( new DataSource( DatabaseConfig ) ).initialize();
export default dataSource; // database.config.ts
import { UsersEntity } from "@demo-app/backend/src/users/users.entity";
import type { TypeOrmModuleOptions } from "@nestjs/typeorm";
const config : TypeOrmModuleOptions = {
type: "sqlite",
database: "db/database.sqlite",
logging: "all",
logger: "advanced-console",
};
function shouldSynchronize( config: TypeOrmModuleOptions ) {
// If database exists, do not synchronize
if ( "sqlite" === config.type ) {
return ! require( "fs" ).existsSync( config.database );
}
// TODO: Implement for other database types
return false;
}
export default {
...config,
entities: [ UsersEntity ],
synchronize: shouldSynchronize( config ),
}; bun run --bun create-users.ts |
FYI Or rather, TypeORM has some incorrect |
Thank you, we can follow this temp solution to fix this |
diff --git a/dist/utils/commandUtils.js b/dist/utils/commandUtils.js
index 0a922d09f28843382e947501307f775b4181a1f3..febe6d3d13660918072d81c6de0aad19228f1356 100644
--- a/dist/utils/commandUtils.js
+++ b/dist/utils/commandUtils.js
@@ -8,7 +8,7 @@ async function loadDataSource(dataSourceFilePath) {
return CommandUtils_1.CommandUtils.loadDataSource(dataSourceFilePath);
}
async function loadSeeders(seederPaths) {
- const seederFileExports = (await Promise.all(seederPaths.map((seederFile) => import(seederFile))))
+ const seederFileExports = (await Promise.all(seederPaths.map((seederFile) => Promise.resolve().then(() => require(seederFile)))))
.map((seederExport) => seederExport.default?.default ?? seederExport.default)
.filter((seederExport) => Boolean(seederExport));
if (seederFileExports.length === 0) { Instead of the dynamic import, the actual solution would be to use Or even better, we could completely rely on TypeORM for loading the classes from the globs, using |
After upgrading to latest release (7.0.0), I got this error:
The text was updated successfully, but these errors were encountered: