Giter Club home page Giter Club logo

Comments (5)

bpatram avatar bpatram commented on May 24, 2024 4

I'm also affected by this. When loading the seed and factories an assumption is made that the path is relative and never absolute.

export const loadFiles = (filePattern: string[]): string[] => {
return filePattern
.map(pattern => glob.sync(path.join(process.cwd(), pattern)))
.reduce((acc, filePath) => acc.concat(filePath), [])
}
as the OP noticed... that join with process.cwd() on line 8

If we dig into TypeORM's implementation in how they resolve files (like entities, migrations, etc) you'll notice they do a simple check to see if the path is absolute (ie, the path has a leading slash), and if it is then they don't prepend to it.

https://github.com/typeorm/typeorm/blob/442bff950704443e0b42fd0f950180d062f8389f/src/connection/ConnectionOptionsReader.ts#L145-L153

TypeORM seeding should most likely also follow this convention. Another potential issue I see is that this library prepends relative paths with the current working directory (cwd) rather than from the package root directory (or the directory containing the ormconfig file). This will most likely burn people using yarn workspaces or lerna or those running typeorm-seeding from a different directory (ie, npm run --prefix different/path ...)

from typeorm-seeding.

diegoazh avatar diegoazh commented on May 24, 2024 1

Hello @Pegase745 use resolve like this

import { resolve } from "path";

export const ORMConfig = {
 // ...
  seeds: [
    resolve(__dirname, './seeds/**/*.seed.ts'),
  ],
  factories: [
    resolve(__dirname, './factories/**/*.factory.ts'),
  ],
// ...
};

from typeorm-seeding.

hirsch88 avatar hirsch88 commented on May 24, 2024 1

hi @bpatram thanks for the tip. :-)

I now use the ConnectionOptionReader of TypeORM.
Check out the new version 1.4.0

from typeorm-seeding.

bpatram avatar bpatram commented on May 24, 2024

@diegoazh your solution will not work since the paths defined must be relative, not absolute ones like you get when calling resolve.

@Pegase745 Here is the hacky workaround I am using until this issue is resolved. Using your example, you can do something like:

// ormconfig.js
module.exports = {
  seeds: [path.relative(process.cwd(), `${__dirname}/seeds/*.js`)],
  // ...
};

Obviously, this example is a bit contrived... but in some cases this is needed (like when the root to your config is different than your cwd (eg, monorepos).


Since the seeds and factories paths are always going to be prefixed with the cwd we can just work backwards from there in order to still resolve using your absolute path. We can use Node's builtin path.relative to give us a path to our absolute path relative to the cwd.

from typeorm-seeding.

aledoroshenko avatar aledoroshenko commented on May 24, 2024

I'm having the same issue with this config:

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'postgres',
  database: 'payments',
  entities: [__dirname + '/../**/*.entity.{js,ts}'],
  synchronize: true,
};

// Need to export like this to make typeorm-seeding work, otherwise driver:undefined
// https://github.com/typeorm/typeorm/issues/4068
module.exports = typeOrmConfig

After yarn run seed:run (which is ts-node ./node_modules/typeorm-seeding/dist/cli.js seed --configName typeorm.config.ts) it stuck on a database connection step and fail with this error:

✖ Database connection failed! Check your typeORM config file.
../nest-master/integration/typeorm/src/photo/photo.entity.ts:1:56 - error TS2307: Cannot find module 'typeorm' or its corresponding type declarations.

1 import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
                                                         ~~~~~~~~~

error Command failed with exit code 1.

If i remove __dirname everything works.

from typeorm-seeding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.