Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typeorm entities not working with javascript #6997

Open
theabner opened this issue Nov 1, 2020 · 3 comments
Open

Typeorm entities not working with javascript #6997

theabner opened this issue Nov 1, 2020 · 3 comments

Comments

@theabner
Copy link

theabner commented Nov 1, 2020

I'm trying to create a project with typeorm with javascript, a connection works correctly, but typeorm can't find my entities.
I am following the example provided by the type documentation: https://typeorm.io/#/usage-with-javascript

app.js:

typeorm.createConnection({
  type: 'mysql',
  host: process.env.MYSQL_IP, // Docker Env
  port: process.env.MYSQL_INTERNAL_PORT,
  username: process.env.MYSQL_ROOT_USERNAME,
  password: process.env.MYSQL_ROOT_PASSWORD,
  database: process.env.MYSQL_DATABASE,
  synchronize: true,
  entities: [
    require('./Model/TestSchema.js'),
  ],
}).then((connection) => {
  const app = express();

  app.use(cors(CorsConfigs));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(authentication);
  app.use(router);

  const testRepository = connection.getRepository('Category');
  const teste = { name: 'TypeScript' };

  app.listen(process.env.BACKEND_PORT, () => {
    console.log(`Rodando na porta: ${process.env.BACKEND_PORT.toString()}`);
  });
}).catch((error) => {
  console.log('Error: ', error);
});

Model/TestSchema.js:

module.exports = {
  name: 'Category',
  columns: {
    id: {
      primary: true,
      type: 'int',
      generated: true,
    },
    name: {
      type: 'string',
    },
  },
};

the connection is working correctly, the problem and when I run it shows me:

Error: RepositoryNotFoundError: No repository for "Category" was found. Looks like this entity is not registered in current "default" connection?

at new RepositoryNotFoundError (/app/src/error/RepositoryNotFoundError.ts:11:9)

at EntityManager.getRepository (/app/src/entity-manager/EntityManager.ts:919:19)

at Connection.getRepository (/app/src/connection/Connection.ts:346:29)

at /app/src/app.js:32:37

at processTicksAndRejections (internal/process/task_queues.js:93:5)

@edcolvin
Copy link
Contributor

edcolvin commented Nov 7, 2020

I think the documentation is incorrect. You need to pass EntitySchema objects in the entities array.

const typeorm = require("typeorm")
const EntitySchema = typeorm.EntitySchema

and then in the options passed to createConnection:

entities: [
        new EntitySchema(require("./entity/Post")),
        new EntitySchema(require("./entity/Category"))
    ]

I submitted pull request #7031 to fix the example code.
The repo referenced in the documentation shows the correct usage:
https://github.com/typeorm/javascript-example

@matttm
Copy link

matttm commented Jan 31, 2021

@edcolvin

I have a similar error with loading the entities in the orm config, but if I try your way, I get the error: EntitySchema is not a constructor.

@cassioseffrin
Copy link

cassioseffrin commented Oct 30, 2021

I have found a workaround for it. Just rename your ormconfig.json to ormconfig.js. The module detect-ts-node will detect the enviroment and according with it you config will be loaded for dev or production.

#ormconfig.js

const isTS = require('detect-ts-node');
const tsConfig = {
   "type": "mysql",
   "host": "localhost",
   "port": 3306,
   "username": "mysqlUser",
   "password": "mysqlPasswd",
   "database": "bot-provin-produtos",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}
const distConfig = {
   "type": "mysql",
   "host": "localhost",
   "port": 3306,
   "username": "mysqlUser",
   "password": "mysqlPasswd",
   "database": "bot-provin-produtos",
   "synchronize": true,
   "logging": false,
   "entities": [
      "dist/src/entity/**/*.js"
   ],
   "migrations": [
      "dist/src/migration/**/*.js"
   ],
   "subscribers": [
      "dist/src/subscriber/**/*.js"
   ],
   "cli": {
      "entitiesDir": "dist/src/entity",
      "migrationsDir": "dist/src/migration",
      "subscribersDir": "dist/src/subscriber"
   }
}
module.exports = isTS ? tsConfig : distConfig;

So, just build you app with
#tsc
And then run with
#node dist/src/youmainclass.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants