I am creating a new application using Next.js + TypeORM and encountering an issue with the integration.
RepositoryNotFoundError: No repository for "User" was found. It seems like this entity is not registered in the current "default" connection?
Although I have double-checked my configuration, I keep getting this error. Upon debugging and inspecting the active connection, I can see the entities listed on connection.options.entities
, indicating that they are indeed registered. This problem has me stumped.
Repository Link: https://github.com/kyleect/bolt
Server:
import "reflect-metadata";
import { createServer } from "http";
import { parse } from "url";
import { createConnection } from "typeorm";
import path from "path";
import next from "next";
import { Link } from "../entities/Link";
import { Post } from "../entities/Post";
import { User } from "../entities/User";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app
.prepare()
.then(() =>
createConnection({
type: "sqlite",
database: "local.db",
synchronize: true,
logging: true,
entities: [Link, Post, User],
})
)
.then(() => {
createServer((req, res) => {
handle(req, res, parse(req.url, true));
}).listen(3000, () => {
console.log("> Ready on http://localhost:3000");
});
});
This code snippet shows one of the endpoint handlers. Similar ones exist for all endpoints and they are all causing errors:
const db = await getConnection();
const repo = db.getRepository(User);
const users = await repo.find();
res.json(users);
Entities:
import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToMany,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
} from "typeorm";
import { Post } from "./Post";
@Entity()
export class Link {
@PrimaryGeneratedColumn("uuid")
id: string;
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
@DeleteDateColumn()
deletedDate: Date;
@Column({ type: "varchar" })
url: string;
@OneToMany((type) => Post, (post) => post.link)
posts: Array<Post>;
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
} from "typeorm";
import { User } from "./User";
import { Link } from "./Link";
@Entity()
export class Post {
@PrimaryGeneratedColumn("uuid")
id: string;
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
@DeleteDateColumn()
deletedDate: Date;
@ManyToOne((type) => User, (user) => user.posts)
user: User;
@Column({ type: "varchar" })
title: string;
@Column({ type: "varchar" })
body: string;
@ManyToOne((type) => Link, (link) => link.posts)
link: Link;
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToMany,
DeleteDateColumn,
UpdateDateColumn,
CreateDateColumn,
} from "typeorm";
import { Post } from "./Post";
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
@DeleteDateColumn()
deletedDate: Date;
@Column({ type: "varchar" })
username: string;
@OneToMany((type) => Post, (post) => post.user)
posts: Array<Post>;
}