Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RepositoryNotFoundError : TypeORM

I am trying to get the following example working:

https://github.com/typeorm/javascript-example/tree/master/src/app3-es6

I am running into the following error:

Error
    at new RepositoryNotFoundError (...\node_modules\typeorm\connection\error\RepositoryNotFoundError.js:24:23)
    at Connection.findRepositoryAggregator (...\node_modules\typeorm\connection\Connection.js:513:19)
    at Connection.getRepository (...\node_modules\typeorm\connection\Connection.js:405:21)
    at ...\index.js:27:37
name: 'RepositoryNotFoundError',
  message: 'No repository for "Post" was found. Looks like this entity is not registered in current "default" connection?'

here is index.js

const typeorm = require("typeorm"); // import * as typeorm from "typeorm";
const Post = require("./model/Post"); // import {Post} from "./model/Post";
// import Post from './model/Post.js';
const Category = require("./model/Category"); // import {Category} from "./model/Category";

typeorm.createConnection({
    driver: {
        type: "oracle",
        host: "localhost",
        port: 1521,
        username: "uname",
        password: "pwd",
        sid: "dev"
    },
    entities: [
        __dirname + "/entity/*.js"
    ],
    autoSchemaSync: true
}).then(function (connection) {
    console.log(connection);

    let post = new Post.Post();
    post.title = "Control flow based type analysis";
    post.text = "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.";
    post.categories = [new Category.Category(0, "TypeScript"), new Category.Category(0, "Programming")];

    let postRepository = connection.getRepository(Post.Post);
    postRepository.persist(post)
        .then(function(savedPost) {
            console.log("Post has been saved: ", savedPost);
            console.log("Now lets load all posts: ");

            return postRepository.find();
        })
        .then(function(allPosts) {
            console.log("All posts: ", allPosts);
        });
}).catch(function(error) {
    console.log("Error: ", error);
});

Post.js in /model/

/*export */ class Post {
    constructor(id, title, text, categories) {
        this.id = id;
        this.title = title;
        this.text = text;
        this.categories = categories;
    }
}

module.exports = {
    Post: Post
};

Category.js

/*export */ class Category {
    constructor(id, name) {
        this.id = id;
        this.name = name;
    }
}

module.exports = {
    Category: Category
};

PostSchema.js in /entity/

const Post = require("../model/Post"); // import {Post} from "../model/Post";
const Category = require("../model/Category"); // import {Category} from "../model/Category";
const PostSchema = {
    target: Post,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        title: {
            type: "string"
        },
        text: {
            type: "text"
        }
    },
    relations: {
        categories: {
            target: Category,
            type: "many-to-many",
            joinTable: true,
            cascadeInsert: true
        }
    }
};

module.exports = {
    PostSchema: PostSchema
};

CategorySchema.js

const Category = require("../model/Category"); // import {Category} from "../model/Category";
const CategorySchema = {
    target: Category,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        name: {
            type: "string"
        }
    }
};

module.exports = {
    CategorySchema: CategorySchema
};

i dont know what i am doing wrong

like image 635
Shodhan Manda Avatar asked Aug 31 '17 20:08

Shodhan Manda


3 Answers

It looks like your entity import is not working. If you import via the wildcard:

entities: [
    __dirname + "/entity/*.js"
],`

Make sure your model is compiled to js. You also could just import

createConnection({ 
    ..., 
    entities: [
        Post,
        ...
    ],}).then(...)
like image 81
wenzel Avatar answered Nov 18 '22 07:11

wenzel


For those who are using typescript and experience this problem: Be reminded that you need to include both ts and js file suffixes when specifying the entities-path:

  • ts used when locally running with ts-node
  • js used when having built for production via tsc.

Code:

import * as path from 'path';
// ...
entities: [
    // assuming _dirname is your project root
    path.resolve(__dirname, '**/*.entity{.ts,.js}'),
],
like image 13
B12Toaster Avatar answered Nov 18 '22 05:11

B12Toaster


I had the same problem for months and finally figured out what I was doing wrong.
When you import Entities, make sure the file names are EXACTLY matching. It's not going to throw any errors, but during the run time, it's going to throw the above error.
Ex. In the entity or model classes, if we import like this,

import { FooClass } from "./foo-Class.model";

it's different from

import { FooClass } from "./foo-class.model";

It won't show any errors, but when you try to call the table, it will show the exact same error.

like image 6
Buzzzzzzz Avatar answered Nov 18 '22 07:11

Buzzzzzzz