1
votes

I'm trying to add an Idea entity to my TypeOrm (configured to mysql), and it seems like the imports refuse to work. With the file added, the typeorm doesn't initialize successfully.

I've reinstalled typeorm package, and I have no idea where to start debugging. I'm following a tutorial ( https://www.youtube.com/watch?v=EHlhvy-fh90), in which this error does NOT appear. I've deviated from it by replacing a postgresql database with a dockerized mysql.

I've had some trouble setting up the node -> mysql connection, eventually opting to run a dockerized mysql 5 to account for auth errors (following this answer: https://stackoverflow.com/a/50167617/9043642). Without the idea.entity.ts file, the connection goes through (with orm), so I wouldn't expect there to be a problem with the database.

This is what my idea.entity.ts file looks like. It effectively follows the example found in documentation (https://docs.nestjs.com/techniques/database). Copy and pasted, the documentation example gives the same error anyway.

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
} from 'typeorm';

@Entity()
export class IdeaEntity {
  @PrimaryGeneratedColumn('uuid') id:string;

  @CreateDateColumn() created:Date;

  @Column('text') idea:string;

  @Column('text') description:string;
}

I'm getting the following error while running

[Nest] 14034   - 07/02/2019, 1:00 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +19ms
[0] /home/miko/node/ideas/src/idea/idea.entity.ts:2
[0] import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
[0]        ^
[0] 
[0] SyntaxError: Unexpected token {
[0]     at Module._compile (internal/modules/cjs/loader.js:718:23)
[0]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
[0]     at Module.load (internal/modules/cjs/loader.js:641:32)
[0]     at Function.Module._load (internal/modules/cjs/loader.js:556:12)
[0]     at Module.require (internal/modules/cjs/loader.js:681:19)
[0]     at require (internal/modules/cjs/helpers.js:16:16)
[0]     at Function.PlatformTools.load (/home/miko/node/ideas/node_modules/typeorm/platform/PlatformTools.js:107:28)
[0]     at /home/miko/node/ideas/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:30:69
[0]     at Array.map (<anonymous>)
[0]     at Object.importClassesFromDirectories (/home/miko/node/ideas/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:30:10)

Notably, the database connection flops. Again, without the idea entity present the connection is fine. The expected result is [InstanceLoader] TypeOrmCoreModule dependencies initialized. How would I successfully create an entity like this?

3
This error is usually caused because of a syntax problem, usually above it in your code. Look at the previous import statements for the problem.Preston

3 Answers

0
votes

Ended up being a messy ormconfig.json file. Make sure it follows format according to: https://github.com/nestjs/nest/blob/master/integration/typeorm/ormconfig.json

0
votes

I know it's a little late, but this could help someone. I had this same mistake and anything worked for me, I ended up cloning a repo from a tutorial and tried to figure out what was different. After some iterations what worked for me was change my original nodemon.json file from this:

{
  "watch": ["dist"],
  "ext": "js",
  "exec": "node dist/main"
}

to this:

{
  "watch": ["src"],
  "ext": "js",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "ts-node -r tsconfig-paths/register src/main.ts"
}

Actually just changing the "exec" parameter worked.

Hope this could be helpful for someone.

0
votes

Add the path of entities as your dist folder

{
    "type":"postgres",
    "host":"localhost",
    "port":"5432",
    "username":process.env.USERNAME,
    "password":process.env.PASSWORD,
    "database":process.env.DATABASE,
    "synchronize":true,
    "logging":true,
    "entities": ["dist/**/**.entity{.ts,.js}"]    
}