如何使用 Typeorm 创建与 Postgres 的连接
Posted
技术标签:
【中文标题】如何使用 Typeorm 创建与 Postgres 的连接【英文标题】:How can I create a Connection to Postgres using Typeorm 【发布时间】:2020-08-30 04:50:37 【问题描述】:我正在关注 Ben Awad 于 1/2019 年发布的名为 TypeGraphQL 的 YouTube 视频系列。第一个视频建立了开发环境,第二个视频介绍了 Typeorm 和 PostgreSQL。在视频的这一点上,事情很简单。该代码定义了一个实体并创建了一个连接,该连接应导致代码在启动时创建表。 我在启动时收到此错误:
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node src/index.ts`
/Users/jboss/projects/ts-graphql/src/index.ts:141
export EntityManager from "./entity-manager/EntityManager";
^
TypeError: Cannot set property EntityManager of #<Object> which has only a getter
at Object.<anonymous> (/Users/jboss/projects/ts-graphql/src/index.ts:141:9)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
at Module.load (internal/modules/cjs/loader.js:914:32)
at Function.Module._load (internal/modules/cjs/loader.js:822:14)
at Module.require (internal/modules/cjs/loader.js:956:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/Users/jboss/projects/ts-graphql/src/index.ts:5:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (/Users/jboss/projects/ts-graphql/node_modules/ts-node/src/index.ts:858:23)
[nodemon] app crashed - waiting for file changes before starting...
这里是 tsconfig.json
"compilerOptions":
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"declaration": false,
"composite": false,
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"baseUrl": ".",
"rootDir": "src"
,
"exclude": ["node_modules"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
这里是 package.json:
"name": "ts-graphql",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies":
"apollo-server-express": "^2.13.1",
"bcryptjs": "^2.4.3",
"express": "^4.17.1",
"graphql": "^15.0.0",
"pg": "^8.1.0",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.0.0-rc.1",
"typeorm": "^0.2.24"
,
"devDependencies":
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.6",
"@types/graphql": "^14.5.0",
"@types/node": "^14.0.1",
"nodemon": "^2.0.3",
"ts-node": "^8.10.1",
"typescript": "^3.9.2"
,
"scripts":
"start": "nodemon --exec ts-node src/index.ts"
这里是 ormconfig.json:
"name": "default",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "docker",
"database": "typegraphql",
"synchronize": true,
"logging": true,
"entities": [
"src/entity/**/*.*"
]
还有 index.ts:
import ApolloServer from "apollo-server-express";
import * as Express from "express";
import buildSchema, Resolver, Query from 'type-graphql';
import createConnection from "typeorm";
@Resolver()
class HelloResolver
@Query(() => String, name: 'helloWorld')
async hello()
return "Hi from Typescript GraphQL!";
let main = async () =>
await createConnection();
const schema = await buildSchema(
resolvers: [HelloResolver]
);
const apolloServer = new ApolloServer( schema )
const app = Express();
apolloServer.applyMiddleware( app );
app.listen(4000, () =>
console.log('Server started on http://localhost:4000');
);
main();
最后是 User.ts:
import Entity, PrimaryGeneratedColumn, Column, BaseEntity from "typeorm";
@Entity( schema: "public" )
export class User extends BaseEntity
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column("text", unique: true)
email: string;
@Column()
password: string;
谁能告诉我如何让它工作?我不认为我理解这个问题。
【问题讨论】:
【参考方案1】:这是昨天在 Typeorm 项目中报告的一个错误。我花了一天时间才找到它。在他们的 Slack 中找到它。爱松弛。 https://github.com/typeorm/typeorm/issues/6054 我的解决方案是:
npm install tslib@1.11.2
然后将以下内容添加到 package.json 中:
"resolutions":
"ts.lib": "1.11.2"
,
然后用yarn重新开始。
似乎 tslib 1.12 引起了一些问题。
【讨论】:
以上是关于如何使用 Typeorm 创建与 Postgres 的连接的主要内容,如果未能解决你的问题,请参考以下文章
如何以正确的方式将@admin-bro/nestjs 与@admin-bro/typeorm 和postgres 一起使用?
节点服务器无法通过 Docker 连接到 Postgres,使用 TypeORM
如何在 typeorm、postgres 中组合 3 列唯一的?