這里有點想法。我是 Nestjs(或一般的節點后端)世界的新手,直到對 db 進行任何處理之前,這都是小菜一碟。
我已明確關閉同步:
.env
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=redacted
TYPEORM_USERNAME=redacted
TYPEORM_PASSWORD=redacted
TYPEORM_DATABASE=redacted
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=false
我將上述 .env 檔案引入如下:
const DatabaseConfig = () => ({
type: 'postgres',
host: process.env.TYPEORM_HOST,
port: parseInt(process.env.TYPEORM_PORT),
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
logging: true,
entities: [
"dist/**/*.entity{.ts,.js}"
],
synchronize: process.env.TYPEORM_SYNCHRONIZE||false,
migrationsTableName: 'typeorm_migrations', // this field will be used to create the table by name of 'typeorm_migrations'. You can name it whatever you want. But make sure to use the sensible name
migrations: [
"dist/src/common/persistence/migrations/*{.ts,.js}" // This is the path to the migration files created by typeorm cli. You don't have to create dist folder. When you save file, compiled files will be stored in dist folder
],
cli: {
migrationsDir: "src/common/persistence/migrations" // This path will be used by typeorm cli when we create a new migration
}
});
export default DatabaseConfig;
通過“Appconfig”實體
const AppConfig = () => ({
environment: (process.env.NODE_ENVIRONMENT) ? process.env.NODE_ENVIRONMENT : 'development' ,
port: 3000,
database: {
...DatabaseConfig()
}
});
export default AppConfig;
像這樣全部拉入 AppModule:
@Module({
imports: [
DemoModule,
CommonModule,
ConfigModule.forRoot(
{
isGlobal: false,
load: [AppConfig]
}),
TypeOrmModule.forRootAsync({
imports: [
ConfigModule
],
useFactory: (configService: ConfigService) => {
return configService.get<ConnectionOptions>('database');
},
inject: [
ConfigService
]
})
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
TypeORM 如何在每次運行時使用物體檔案夾中的物體創建表?
query: CREATE TABLE "basic_phone_check" ("id" SERIAL NOT NULL, "country" integer NOT NULL, "phoneNumber" character varying NOT NULL, "givenName" character
varying NOT NULL, "familyName" character varying NOT NULL, "dob" TIMESTAMP NOT NULL, "timeCreated" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_3c09423ecba40b5709e30b6061e" PRIMARY KEY ("id"))
非常感謝
uj5u.com熱心網友回復:
此行可能會導致問題:
synchronize: process.env.TYPEORM_SYNCHRONIZE||false,
因為process.env.TYPEORM_SYNCHRONIZE可能是字串而不是布林值并且"false"是真實的
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395780.html
標籤:节点.js PostgreSQL的 嵌套 打字机 node.js-typeorm
下一篇:Postgresql合并列
