Ili*_*ntz 5 javascript postgresql node.js typescript typeorm
我已经使用 TypeORM 设置了一个 Typescript 项目,但在编译时遇到了一些问题。
我的包结构是这样的:
root
??? db
? ??? migrations
? ? ??? a_migration.ts
? ??? connection
? ? ??? config.ts <- ormconfig
? ? ??? encrypt.ts
? ? ??? index.ts <- creates the connection
??? src
? ??? card
? ? ??? entity.ts
??? package.json
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)
我的 config.ts 是:
root
??? db
? ??? migrations
? ? ??? a_migration.ts
? ??? connection
? ? ??? config.ts <- ormconfig
? ? ??? encrypt.ts
? ? ??? index.ts <- creates the connection
??? src
? ??? card
? ? ??? entity.ts
??? package.json
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
export = {
type: 'postgres',
host: POSTGRES_HOST,
port: POSTGRES_PORT,
username: POSTGRES_USER,
password: POSTGRES_PASS,
database: POSTGRES_DB,
synchronize: true,
logging: false,
entities: ['**src/**/*entity.{ts,js}'],
migrations: ['**/migrations/*.{ts,js}'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'db/migrations',
},
namingStrategy: new SnakeNamingStrategy(),
};
Run Code Online (Sandbox Code Playgroud)
我尝试用+替换**实体和迁移中的前缀,但是 typeorm 无法检测到迁移文件和实体。我知道这与解析代码在文件夹下的路径有关,但我不确定如何解决这个问题。path.join__dirnamebuild
如果我像这样生活,CLI 适用于执行应用程序的未编译代码 (ts),nodemon但不适用于已编译的 (js) 代码。
我从编译的 js 中得到的错误是:
SyntaxError: Cannot use import statement outside a module
任何帮助表示赞赏,非常感谢!
应为您的实体和迁移提供构建目录而不是 src 目录。
并且synchronize应该设置为 false。
尝试更新您的config.ts如下内容:
export = {
type: 'postgres',
host: POSTGRES_HOST,
port: POSTGRES_PORT,
username: POSTGRES_USER,
password: POSTGRES_PASS,
database: POSTGRES_DB,
synchronize: false,
logging: false,
entities: ['build/src/**/*entity.{ts,js}'],
migrations: ['build/db/migrations/*.{ts,js}'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'db/migrations',
},
namingStrategy: new SnakeNamingStrategy(),
};
Run Code Online (Sandbox Code Playgroud)
entities请注意和属性的变化migrations。如果这不起作用,很可能是因为我指定的路径不在目录内build。在这种情况下,请根据需要进行更改。
希望这对您有帮助!干杯!