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

Add Yarn workspaces supports for ormconfig.js location #2805

Closed
gutenye opened this issue Sep 16, 2018 · 19 comments
Closed

Add Yarn workspaces supports for ormconfig.js location #2805

gutenye opened this issue Sep 16, 2018 · 19 comments

Comments

@gutenye
Copy link

gutenye commented Sep 16, 2018

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

The ormconfig.js location is determined by app-root-path, when using yarn workspaces, it's not accurate anymore.

Given

ROOT/
  package.json     // {"workspaces": ["server"] }
  server1/
    package.json   // {"dependencies": {"typeorm": "latest"}}
    ormconfig.js

After yarn, it creates

ROOT/
  node_modules/{typeorm, app-root-path,...}
  server1/     // no node_modules anymore

So it reads ROOT/ormconfig.js instead of ROOT/server1/ormconfig.js

Possible solution: try process.cwd() first

@TheAifam5
Copy link

Any update on this?

@aaronjensen
Copy link

I created a repo reproducing the problem: https://github.com/aaronjensen/typeorm-monorepo-repro

Many projects use https://github.com/davidtheclark/cosmiconfig. Perhaps typeorm could as well?

@ChristianIvicevic
Copy link

A workaround for the time being is the use of a .env file in the folder of your package. One of the caveats is that you cannot use multiple connections that way.

Nevertheless I'd appreciate support for process.cwd() / ormconfig.json as well.

@Ponjimon
Copy link

What is the workaround for this? Just drop the JSON file into the project root?

@TheAifam5
Copy link

If someone is interested in workaround, check out here : inxilpro/node-app-root-path#31 (comment)

@gho1b
Copy link

gho1b commented Sep 7, 2019

Hi, i got same issue with this config. Then i make a connection helper to read the config file based on server package because i use monorepo in my project.

import {join} from 'path';
import { readJson } from 'fs-extra';
import { ConnectionOptions } from 'typeorm';


export const getConnection = async (): Promise<ConnectionOptions> => {
  const configs: ConnectionOptions[] =  await readJson(join(process.cwd(), '/ormconfig.json'));
  const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'default';
  const config = configs.find(config => config.name === env);
  if(!config) {
    throw Error('Connection Not Found');
  }
  return {...config, name: undefined};
}

export default getConnection;

Then call the function to initiate connection config

const config = await getConnection().then(connection => {
      return { ...connection, entities: [UserSchema] };
    });
    await createConnection(config);

@vscncls
Copy link

vscncls commented Jan 5, 2020

Any update on this?

@Baterka
Copy link

Baterka commented Jan 31, 2020

Does anyone have idea how to fix this better than #2805 (comment)

@vscncls
Copy link

vscncls commented Jan 31, 2020

@Baterka my solution was adding nohoist for typeorm in the monorepo package.json

"nohoist": [
  "**/typeorm/**",
  "**/typeorm"
]

brandonchinn178 pushed a commit to brandonchinn178/worshipmate that referenced this issue Mar 25, 2020
brandonchinn178 pushed a commit to brandonchinn178/worshipmate that referenced this issue Apr 18, 2020
brandonchinn178 pushed a commit to brandonchinn178/worshipmate that referenced this issue Apr 19, 2020
brandonchinn178 pushed a commit to brandonchinn178/worshipmate that referenced this issue Apr 19, 2020
@zhangpengchen
Copy link

@Baterka my solution was adding nohoist for typeorm in the monorepo package.json

"nohoist": [
  "**/typeorm/**",
  "**/typeorm"
]

Thanks, it took me a hour to find this.

terzhang added a commit to terzhang/kekat-mono that referenced this issue Aug 12, 2020
ormconfig.js location is inaccurate with yarn workspace. typeorm/typeorm#2805
brandonchinn178 added a commit to brandonchinn178/worshipmate that referenced this issue May 8, 2021
@octogonz
Copy link

I created a repo reproducing the problem: https://github.com/aaronjensen/typeorm-monorepo-repro

Looks like app-root-path 3.x has fixed their algorithm to detect PNPM and Yarn Plug'n'Play installations. And the latest version version of TypeORM has upgraded to app-root-path 3.x. Simply upgrading the TypeORM dependency fixes @aaronjensen's repro.

Seems like this issue is resolved?

@imnotjames
Copy link
Contributor

imnotjames commented Jun 26, 2021

I created a repo reproducing the problem: https://github.com/aaronjensen/typeorm-monorepo-repro

Looks like app-root-path 3.x has fixed their algorithm to detect PNPM and Yarn Plug'n'Play installations. And the latest version version of TypeORM has upgraded to app-root-path 3.x. Simply upgrading the TypeORM dependency fixes @aaronjensen's repro.

Seems like this issue is resolved?

we've had that version since 2019 - if it's been a problem since then I think there's other issues.

However, if you've confirmed that it's working, yay! We can maybe close this out?

@imnotjames
Copy link
Contributor

I take the silence as "it works".

With that in mind I'm closing it. Feel free to reopen or open a new issue if I've misunderstood.

@Karabur
Copy link

Karabur commented Aug 2, 2021

TypeORM: 0.2.36
app-root-path: 3.0.0

Repo with Yarn PnP
@imnotjames I can confirm problem still here.

Issue about supporting yarn workspaces in app-root-path still open inxilpro/node-app-root-path#31.
I am not sure why @octogonz have seen it as solved, that module didnt get any update for last 2 years

@imnotjames
Copy link
Contributor

inxilpro/node-app-root-path@20420ae This doesn't add support for pnpm?

@Karabur
Copy link

Karabur commented Aug 2, 2021

Perhaps it does, but root of the issue not pnpm (I am not even sure it is related, but didnt check).

it is monorepo with workspaces which gives a problem. I think having plain old node_modules with modules hoisted to the root folder will trigger same error.
app-root-path works relative to node_modules folder where library resides, but relative to actual project.

so, having that structure:

root
  - package.json (workspace)
  - node_modules
        - app-root-path (hoisted from server)
        - typeorm (hoisted from server)
  server
      - package.json which includes typeorm

app-root-path will give root
but it is expected to give root/server as there is the package which uses typeorm resides, and process.cwd() usually points to root/server

@jamezrin
Copy link

jamezrin commented Oct 26, 2021

Is there a way to specify the path of the file to the underlying function to load the connection options in the getConnectionOptions function?

IMO there should be a loadConnectionOptions that has a the file to load as an argument, or an overload of the getConnectionOptions function.

@jamezrin
Copy link

Actually, the base path can already be passed to the ConnectionOptionsReader, see this example:

async function findPackageRoot(): Promise<string | null> {
  for (const dir of module.paths) {
    if (await fs.stat(dir).catch((e) => null)) {
      return path.dirname(dir);
    }
  }

  return null;
}

async function startApp() {
  // read connection options from ormconfig file (or ENV variables)
  const connectionOptionsReader = new ConnectionOptionsReader({
    root: await findPackageRoot(),
  });

  // do something with connectionOptions,
  // for example append a custom naming strategy or a custom logger
  // Object.assign(connectionOptions, { namingStrategy: new MyNamingStrategy() });
  const connectionOptions = await connectionOptionsReader.get('default');

  // create a connection using modified connection options
  const connection = await createConnection(connectionOptions);
}

@andreyselin
Copy link

Looks like deprecated hoisting does no longer work in my yarn v3.1.0.
nmHoistingLimits: workspaces does the thing but it fills additional node_modules folder on nested level.

I found following simple workaround:

# package.json scripts section
# Instead of calling this
"migration:create": "ts-node node_modules/typeorm/cli.js --config src/config/ormconfig.ts migration:create -n ${0}",
# I run this:
"migration:create": "ts-node tools/typeOrmCli.ts --config src/config/ormconfig.ts migration:create -n ${0}"

Note that I made tools/typeOrmCli.ts wrapper file with following contents:

import cli from 'typeorm/cli.js';
void cli;

This will call the cli from the folder of application and build correct path string for migration file.

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