Started new project with \'nest new\' command. Works fine until I add entity file to it.
Got following error:
import { Entity, Column, Primary
You need to have a something.module.ts for every section of your app. It works like Angular. This is setup with GraphQL resolvers and service. REST is a bit different with a controller. Each module will probably have an entity and if GraphQL, projects.schema.graphql.
projects.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectsService } from './projects.service';
import { Projects } from './projects.entity';
import { ProjectsResolvers } from './projects.resolvers';
@Module({
imports: [
TypeOrmModule.forFeature([Projects])],
providers: [
ProjectsService,
ProjectsResolvers
],
})
export class ProjectsModule {}
As Jay McDoniel explained in his answer, the problem seems to be the pattern matching of entity files in ormconfig.json
file: Probably a typescript file (module) is imported from a javascript file (presumably a previously transpiled typescript file).
It should be sufficient to remove an existing ts
glob pattern in the ormconfig.json
, so that TypeORM will only load javascript files. The path to the entity files should be relative to the working directory where node is executed.
"entities" : [
"dist/entity/**/*.js"
],
"migrations" : [
"dist/migration/**/*.js"
],
"subscribers": [
"dist/subscriber/**/*.js"
],
Actually, typeorm was designed to work with javascript by default.
To run the migrations with typescript, you must tell typeorm to do it.
Just put in your package.json, in the scripts part this line below:
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
and then, try to migrate again:
yarn typeorm migration:run
This worked for me - no changes needed to your ormconfig.js
. Run from your root directory where the node_modules
are:
ts-node ./node_modules/typeorm/cli.js migration:generate -n <MirgrationName> -c <ConnectionType>
Example:
ts-node ./node_modules/typeorm/cli.js migration:create -n AuthorHasMultipleBooks -c development
Defining the entities property in ormconfig.json
as mentioned in the official documentation resolved this issue for me.
// This is your ormconfig.json file
...
"entities": ["dist/**/*.entity{.ts,.js}"]
...
In the TypeORM documentation, i found a specific section for Typescript.
This section says:
Install ts-node globally:
npm install -g ts-node
Add typeorm command under scripts section in package.json
"scripts" { ... "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js" }
Then you may run the command like this:
npm run typeorm migration:run
If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:
npm run typeorm migration:generate -- -n migrationNameHere
This works with my file config:
{
"type": "postgres",
"host": "yourhost",
"port": 5423,
"username": "username",
"password": "password",
"database": "your_db",
"synchronize": true,
"entities": [
"src/modules/**/*.entity.{ts,js}"
],
"migrations": [
"src/migrations/**/*.{ts,js}"
],
"cli": {
"entitiesDir": "src/modules",
"migrationsDir": "src/migrations"
}
}
Then you can run the generate command.