我正在用 Graphql 在 Node 中创建我的第一个应用程序,现在我只写了这段代码:
import 'reflect-metadata';
const express = require('express');
import { Express } from 'express';
import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import { buildSchema } from 'type-graphql';
import { ActivityResolver } from './resolvers/activity';
import { createConnection } from 'typeorm';
const main = async () => {
// Setup database connection
const conn = await createConnection({
type: 'postgres',
database: 'deliveryapi',
entities: [],
logging: true,
synchronize: true,
username: process.env.USERNAME || 'postgres',
password: process.env.PASSWORD || 'Juventus',
port: 5432,
});
// Create express application for GraphQL server
const apollo: ApolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [ActivityResolver],
validate: false,
}),
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
})
const app: Express = express();
// All application required varibles
const PORT = process.env.PORT || 5000;
// Middleware and routes
await apollo.start();
apollo.applyMiddleware({ app });
app.use(express.json());
app.get('/', (req, res) => {res.send("Hello World")});
// Start server
app.listen(PORT, () => {console.log(`Server started on port ${PORT} and connected to database`)});
}
main().catch(err => {console.error("Error: ", err)});
问题是,在 VSCode 中,这一行有问题:
import { createConnection } from 'typeorm';
。它已被删除并表示已弃用,我不明白为什么会影响代码。我正在使用打字稿。
有人可以解释一下吗?谢谢
自
0.3.0
版本 typeorm
以来,createConnection
已弃用,取而代之的是新的 DataSoure
API。请参阅 0.3.0
的变更日志 https://github.com/typeorm/typeorm/blob/master/CHANGELOG.md
当我在玩 typeORM 时,刚刚发现你可以把它放到你的
index.ts
文件中
import { AppDataSource } from "./data-source";
AppDataSource.initialize().then(() => {
console.log("Data Source has been initialized!");}).catch((err) => {
console.error("Error during Data Source initialization", err);});