前言:TypeORM 0.3以上版别弃用了getConnectionManagercreateConnection等一系列api,运用 DataSource 代替原本的 Connection

详情查看 CHANGELOG.md

1. 处理代码更新时的报错

文件: pages/index.tsx完成数据库衔接

import {GetServerSideProps, NextPage} from "next";
import {AppDataSource} from "@/src/data-source"
import {User} from "@/src/entity/User";
type Props = {
  name: string
}
const Posts: NextPage<Props> = (props) => {
  return (
    <div> 你好 {props.name} </div>
  );
}
export default Posts;
export const getServerSideProps: GetServerSideProps = async (context) => {
  // createConnection 被弃用,运用 initialize 创建衔接
  const connection = await dataSource.initialize()
  return {
    props: { name: 'Bumble' }
  };
};

以上代码能够成功衔接数据库,可是在开发环境中,代码更新时会呈现以下报错

Cannot create a "default" connection because connection to the database already established.

错误信息提示您正在测验创建一个名为”default”的衔接,但因为现已树立了与数据库的衔接,所以无法创建新衔接。
因而需求加判别,判别是否已有衔接,复用衔接。

处理方法:在代码中增加判别

export const getServerSideProps: GetServerSideProps = async (context) => {
  const UserRepository = AppDataSource.isInitialized
    ? AppDataSource.getRepository(User)
    : (await AppDataSource.initialize()).getRepository(User);
  return {
    props: { name: 'Bumble' }
  };
};

上述代码运用 isInitialized 判别是否已有衔接,已有衔接就运用getRepository获取 User 实体的存储库,无衔接则先树立衔接再获取实体存储库。

2. 处理报错 no metadata for User

上述代码完成了数据库衔接,接下来完成数据查询。
查询 User 表的数据,代码完成:

export const getServerSideProps: GetServerSideProps = async (context) => {
  const UserRepository = AppDataSource.isInitialized
    ? AppDataSource.getRepository(User)
    : (await AppDataSource.initialize()).getRepository(User);
  // 运用 find() 就能够完成查询,可是此时会报错
  const users = await UserRepository.find()
  console.log(users)
  return {
    props: { name: 'Bumble' }
  };
};

报错内容

error - EntityMetadataNotFoundError: No metadata for "User" was found.

错误原因 TypeORM 无法找到与 “User” 实体相关的元数据

处理方法:增加与 “User” 实体相关的元数据

export const getServerSideProps: GetServerSideProps = async (context) => {
  // @ts-ignore
  const dataSource = new DataSource({
    ...AppDataSource.options,
    entities: [User],
  });
  const UserRepository = dataSource.isInitialized
    ? dataSource.getRepository(User)
    : (await dataSource.initialize()).getRepository(User);
  const users = await UserRepository.find()
  console.log(users) // 成功打印出 User 表的一切数据
  return {
    props: { name: 'Bumble' }
  };
};

以上代码就能够成功打印出User表的一切数据了

3. 总结:完好代码优化

有多个实体时,代码会呈现冗余,能够封装方法以优化代码。
以下是优化后的完好代码

import {GetServerSideProps, NextPage} from "next";
import {AppDataSource} from "@/src/data-source"
import {User} from "@/src/entity/User";
import {Post} from "@/src/entity/Post";
import {Comment} from "@/src/entity/Comment";
import {DataSource} from "typeorm";
type Props = {
  name: string
}
const Posts: NextPage<Props> = (props) => {
  return (
    <div> 你好 {props.name} </div>
  );
}
export default Posts;
export const getServerSideProps: GetServerSideProps = async (context) => {
  const UserRepository = await handleGetRepository(User)
  const PostRepository = await handleGetRepository(Post)
  const users = await UserRepository.find()
  const posts = await PostRepository.find()
  console.log(users, posts)
  return {
    props: {
      name: 'Bumble'
    }
  };
};
const handleGetRepository = async (entity) => {
  // @ts-ignore
  const dataSource = new DataSource({
    ...AppDataSource.options,
    entities: [User, Post, Comment],
  });
  return dataSource.isInitialized
    ? dataSource.getRepository(entity)
    : (await dataSource.initialize()).getRepository(entity);
}