Giter VIP home page Giter VIP logo

Comments (14)

jeffijoe avatar jeffijoe commented on July 25, 2024 1

Yes, The default is the current working dir (usually where your package.json is), because Node wont tell us the dir of the file unless you explicitly do so with '__dirname' which you may pass to opts.cwd 😀

This should do the trick:

container.loadModules([
  '../src/lib/*.js',
  '../src/services/*.js'
], { cwd: __dirname })

Also, if you haven't already, I suggest you read up on lifetime management - one of the (IMO) best features of Awilix when writing API's/web apps - as well as the special overload for loadModules:

container.loadModules([
  ['../src/lib/*.js', Lifetime.SINGLETON],
  ['../src/services/*.js', Lifetime.SCOPED]
], { cwd: __dirname })

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024 1

Yeah thats what I was thinking - I've personally noticed more people naming their files after their default export, which is why that is the default convention in Awilix.

If you want to continue using filenames with dashes, you can specify opts.formatName: 'camelCase' for loadModules - it will use the camel-case module to convert your dashed version to camel case. 😄

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024 1

That one was the earliest pattern and was more or less converted from awilix v1 to v2, but I recommend using this instead (it's less confusing IMO)

export default function makeUserService ({ someService }) {
  return {
    getUser: () => someService.doStuff()
  }
}

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024 1

Yes, the last one registered wins.

from awilix.

joeltello avatar joeltello commented on July 25, 2024

does opts.cwd have something to do with this?

from awilix.

joeltello avatar joeltello commented on July 25, 2024

I just changed cwd to __dirname, but didn't help, I still get the same error.

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024

Could I see the contents of the file that contains the function you want to register?

Please keep in mind only the default export is registered when using loadModules.

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024

Here's a working project boilerplate using loadModules: https://github.com/jeffijoe/koa-es7-boilerplate/blob/master/src/lib/configureContainer.js

from awilix.

joeltello avatar joeltello commented on July 25, 2024

Thank you so much. So it was a combination of export default and file name with dashes!

from awilix.

joeltello avatar joeltello commented on July 25, 2024

the file name is not an issue, this is what I am doing:

const { sendValidResponse, sendErrorResponse } = container.resolve('response-handlers')

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024

Right - I'm guessing you just found out that only the default export is registered. 😄

The reason for this is that I don't want to register any other exported members since they are likely just going to pollute the container.

Also, please keep in mind you don't have to use loadModules - the following is perfectly acceptable as well:

// response-handlers
export function sendValidResponse ({ db }) {
  return async (req, res) => {
    res.status(200).send(await db.get('something'))
  }
}

// dependencies.js
import * as handlers from '../src/response-handlers'
import mapValues from 'lodash/mapValues'

const container = createContainer()
  .registerFunction(
    // Map to scoped registration
    mapValues(handlers, (handler) => [handler, { lifetime: Lifetime.SCOPED }])
  )

Basically using lodash/mapValues to register all handlers with scoped lifetime.

Hope this helps. :shipit:

from awilix.

joeltello avatar joeltello commented on July 25, 2024

yes, I was using that version of the code before, but wanted to take advantage of passing the modules as arguments from this example:

export default function (opts) {
  return {
    getUser: getUser.bind(null, opts),
  }
}

from awilix.

joeltello avatar joeltello commented on July 25, 2024

what would happen if I do something like this:

container.loadModules([
  '../src/services/user.js',
  '../src/test/mocks/user.js'
])

will the second user.js overwrite the first?

from awilix.

jeffijoe avatar jeffijoe commented on July 25, 2024

You can use this to your advantage with scoping! E.g. register a default logger, but overwrite it with a scoped version that also collects request and user info. 😎

Welcome to the world of DI in Node with Awilix. 😄

from awilix.

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.