Giter Site home page Giter Site logo

Comments (8)

sgronblo avatar sgronblo commented on July 29, 2024 25

I figure out another workaround. You can use yarn's nohoist option to make sure that app-root-path doesn't get hosted to the top "project root" directory.

If you put this in your subproject/package.json and re-install packages (I usually do yarn install --force) you should end up with app-root-path in subproject/node_modules which then should cause a correct lookup and find your ormconfig.js:

{
  "workspaces": {
    "nohoist": [
      "typeorm/**",
      "typeorm"
    ]
  }
}

from node-app-root-path.

jbreeden avatar jbreeden commented on July 29, 2024 13

I just bumped into this as well. As a workaround, you can use the ConnectionOptionsReader directly to get options from any folder/file you like.

My connection stuff is still a bit messy, but I'll post it here as an example. Note that I apply the name "default" to my config so the connectionOptionsReader.get call will work.

import * as fs from 'fs';
import * as path from 'path';
import { createConnection, Connection, ConnectionOptionsReader } from "typeorm";

const connectionOptionsReader = new ConnectionOptionsReader({
    // TODO: Maybe look in every folder up from CWD
    root: process.cwd()
});

let connectionOptions;
let connection;

export default async function connect (): Promise<Connection> {
    if (!connection) {
        connectionOptions = {
            ...(await connectionOptionsReader.get(process.env.TYPEORM_CONNECTION || 'default')),
            entities: [__dirname + "/entity/**/*.js"],
            migrations: [__dirname + "/migration/**/*.js"],
            subscribers: [__dirname + "/subscriber/**/*.js"]
        };
        connection = createConnection(connectionOptions);
    }

    return connection;
}

Checking in process.cwd() seemed to mimic the behavior of the typeorm cli commands (like typeorm migration:generate), so it was convenient to have this behavior be consistent.

I also found it convenient to set the entities and other paths here so that I could consume this package from other repos, with their own ormconfig.js files, and not have to keep setting up these paths. Not sure if I'm doing all of this right, but at least I can load my configs in my monorepo now!

Hope that's helpful

from node-app-root-path.

inxilpro avatar inxilpro commented on July 29, 2024 1

Oof, that seems like a nightmare to resolve. I think the first step is to try to enumerate all the different cases that we need to account for and the expected behavior. Maybe you could start there and see if anyone has comments?

from node-app-root-path.

nwittwer avatar nwittwer commented on July 29, 2024

I recently ran into this issue with Typeorm specifically as well.

@sgronblo's solution was great for Yarn v1, but if you're using Yarn v2–v3, you will likely need a .yarnrc.yml (I added it to my workspace directory) with the following:

nodeLinker: node-modules
nmHoistingLimits: workspaces

Now run yarn and you should see the node_modules folder inside your workspace directory, with Typeorm in node_modules as well.

(You can now safely remove the deprecated nohoist code from your root package.json)


nohoist is deprecated in v2+ and replaced by nmHoistingLimits (Docs).

This article also explains more about Yarn's decision to change away from nohoist.

P.S. if you're switching from Yarn v1 to v2+, I recommend reading their step-by-step upgrade guide.

from node-app-root-path.

inxilpro avatar inxilpro commented on July 29, 2024

Does anyone have any recommendations on how to handle workspaces?

from node-app-root-path.

inxilpro avatar inxilpro commented on July 29, 2024

I suppose we could read the package.json file in the discovered root path, and look for a "workspaces" key, and adjust the behavior if that exists…

from node-app-root-path.

Brianzchen avatar Brianzchen commented on July 29, 2024

@inxilpro This is my goto package and just realised it's not bulletproof under yarn workspaces! Would be keen to help with this.

regarding looking for workspaces. Child projects can also have workspaces such as for nohoist

"workspaces": {
  "nohoist": ["react-native", "react-native/**"]
}

Do you have any other ideas of how we could solve this?

from node-app-root-path.

Brianzchen avatar Brianzchen commented on July 29, 2024

It should be safe to just check the package.json if there's workspaces that's Array.isArray() or workspaces.packages that's Array.isArray() we can safely assume this is the root package of the mono repo. Should work well for yarn which is what I'm familiar with.

And if not, we keep traversing up the tree until we find a package.json that fits the criteria

from node-app-root-path.

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.