UNALE可以连接到Typeorm的Postgres
我正在使用Nestjs将TypeOm作为数据库软件包连接到数据库。 我的Postgres正在Docker运行,IP 172.17.0.3:5432
sudo docker run -name postgre -e postgres_user = admin -e -e postgres_password =密码-P 5432:5432 -V /data/postgres:/var/lib/postgresql/data -d postgres
这是我的存储模块文件:
import { Module } from '@nestjs/common';
import { storageConfig, StorageService } from './storage.service';
import { TypeOrmModule} from '@nestjs/typeorm'
import { User } from './entities/user.entity';
@Module({
providers: [StorageService],
imports: [
TypeOrmModule.forRoot({
driver: 'pg',
type: 'postgres',
host: '172.17.0.3',
port: 5432,
username: 'admin',
password: 'password',
database: 'user_storage',
schema: 'public',
entities: [User]
})
]
})
export class StorageModule {}
现在,我尝试导入users.module.ts
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { TypeOrmModule } from '@nestjs/typeorm'
@Module({
imports: [TypeOrmModule.forRoot()],
controllers: [UsersController],
providers: [UsersService]
})
export class UsersModule {}
,我在服务中使用存储库:
import { Injectable } from '@nestjs/common';
import { UserRequest, UserResponse } from './user.models';
import {DataSource, Repository } from 'typeorm'
import { User } from 'src/storage/entities/user.entity';
@Injectable()
export class UsersService {
private readonly _users: Repository<User>
constructor(private ds: DataSource) {
this._users = this.ds.getRepository(User)
}
async create(req: UserRequest) {
const user = this._users.create()
user.id = 1
user.name = req.name
user.age = req.age
user.address = req.address
user.notes = ''
const saveduser = await this._users.save(user)
return saveduser.id;
}
}
但是我随时会遇到此错误我运行:
[Nest] 36982 - 07/05/2022, 12:02:19 PM LOG [NestFactory] Starting Nest application...
[Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (/projects/hw/src/driver/DriverFactory.ts:72:23)
at new DataSource (/projects/hw/src/data-source/DataSource.ts:139:43)
at createTypeormDataSource (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:172:23)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:176:46)
at Generator.next (<anonymous>)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:16:12)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:174:76
at Observable._subscribe (/projects/hw/node_modules/rxjs/src/internal/observable/defer.ts:55:15) [Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... TypeError: this.postgres.Pool is not a constructor
at PostgresDriver.createPool (/projects/hw/src/driver/postgres/PostgresDriver.ts:1461:22)
at PostgresDriver.connect (/projects/hw/src/driver/postgres/PostgresDriver.ts:340:38)
at DataSource.initialize (/projects/hw/src/data-source/DataSource.ts:232:27)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:179:38)
at Generator.next (<anonymous>)
是什么原因引起了这个问题,该如何解决?
I'm using nestJS to connect to the database with TypeORM as the db package.
My postgres is running in docker with ip 172.17.0.3: 5432
sudo docker run --name postgre -e POSTGRES_USER=admin -e
POSTGRES_PASSWORD=password -p 5432:5432 -v
/data/postgres:/var/lib/postgresql/data -d postgres
Here is my storage module file:
import { Module } from '@nestjs/common';
import { storageConfig, StorageService } from './storage.service';
import { TypeOrmModule} from '@nestjs/typeorm'
import { User } from './entities/user.entity';
@Module({
providers: [StorageService],
imports: [
TypeOrmModule.forRoot({
driver: 'pg',
type: 'postgres',
host: '172.17.0.3',
port: 5432,
username: 'admin',
password: 'password',
database: 'user_storage',
schema: 'public',
entities: [User]
})
]
})
export class StorageModule {}
Now with this I tried importing the users.module.ts
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { TypeOrmModule } from '@nestjs/typeorm'
@Module({
imports: [TypeOrmModule.forRoot()],
controllers: [UsersController],
providers: [UsersService]
})
export class UsersModule {}
and I use repository in a service:
import { Injectable } from '@nestjs/common';
import { UserRequest, UserResponse } from './user.models';
import {DataSource, Repository } from 'typeorm'
import { User } from 'src/storage/entities/user.entity';
@Injectable()
export class UsersService {
private readonly _users: Repository<User>
constructor(private ds: DataSource) {
this._users = this.ds.getRepository(User)
}
async create(req: UserRequest) {
const user = this._users.create()
user.id = 1
user.name = req.name
user.age = req.age
user.address = req.address
user.notes = ''
const saveduser = await this._users.save(user)
return saveduser.id;
}
}
But I keep getting this error whenever I run:
[Nest] 36982 - 07/05/2022, 12:02:19 PM LOG [NestFactory] Starting Nest application...
[Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (/projects/hw/src/driver/DriverFactory.ts:72:23)
at new DataSource (/projects/hw/src/data-source/DataSource.ts:139:43)
at createTypeormDataSource (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:172:23)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:176:46)
at Generator.next (<anonymous>)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:16:12)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:174:76
at Observable._subscribe (/projects/hw/node_modules/rxjs/src/internal/observable/defer.ts:55:15) [Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... TypeError: this.postgres.Pool is not a constructor
at PostgresDriver.createPool (/projects/hw/src/driver/postgres/PostgresDriver.ts:1461:22)
at PostgresDriver.connect (/projects/hw/src/driver/postgres/PostgresDriver.ts:340:38)
at DataSource.initialize (/projects/hw/src/data-source/DataSource.ts:232:27)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:179:38)
at Generator.next (<anonymous>)
What is causing this issue and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于仍在挣扎的任何人来说,另一个解决方案是使用commonjs语法导入您的ormconfig.js模块,并将其插入Typeorm的第一个参数。发生这种情况的原因是因为Typeorm在应用这些设置时不支持Typescript,因此使用关键字 import 导入ormconfig.js模块不起作用,因为在Vanilla nodejs中这样做的正确方法是使用要求。另外,不要忘记更改您的ormconfig.ts-&gt; ormconfig.js:
For anyone still struggling, another solution for this is to import your ormconfig.js module using commonjs syntax and insert it as the first parameter for TypeORM's forRoot. The reason this happens is because TypeORM does not support TypeScript when applying these settings, so importing your ormconfig.js module using the keyword import won't work, because the correct way of doing that in vanilla NodeJS is using require. Also, don't forget to change your ormconfig.ts -> ormconfig.js:
经过大量调试,我能够找到自己的错误。在users.module.ts中,我有此代码:
我用
typeormmodule.forroot()
用storageModule
开始工作。问题在于,使用typeormmodule.forroot()
作为空白,它无法使用storageModule中提供的任何配置
正确的一个:
After much debugging, I was able to find my mistake. in the users.module.ts I had this code:
I replaced
TypeOrmModule.forRoot()
withStorageModule
and it started working. The problem was that withTypeOrmModule.forRoot()
as blank, it wasn't able to utilize any configuration that was provided inStorageModule
The correct one is below: