2

I tried MongoDB Stitch earlier this year, and at the time it did not feel like a finished product (for example, apps cannot be renamed). I am giving it another go, and this time I am interested to see how I could create automated tests for my Stitch functions using Jest (this also may not be straightforward).

I have noticed that the Functions section has a DependenciesBeta tab. Here one may zip up NPM modules in a tarball, and they will become available in the Stitch JS environment. I wonder if I could use this to circumvent the import difficulties I am experiencing with the functions system - instead I could make (untested) lightweight calls from Functions to Dependencies, and then just test the dependencies.

However, I want to be able to import my app automatically using the console command, to deploy automatically in a CI pipeline. For this to work, import/export would need to include dependencies as well, but the file-format docs do not mention dependencies. Is there any support for syncing dependencies from the console, as part of an app import?

Wan B.
  • 18,367
  • 4
  • 54
  • 71
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

Is there any support for syncing dependencies from the console, as part of an app import?

Yes, you can import dependencies using mongodb-stitch-cli (v1.10+).

To upload external dependencies:

  • First need a local node_modules folder containing at least one Node.js package. If the node_modules folder does not already exist, npm install <package> will automatically creates it.

  • Next you need to package them up in an archive so you can upload them to Stitch:

    tar -czf node_modules.tgz node_modules/
    

    Other supported formats/extensions are : .zip, .tar, .gz, .tgz

  • Next you can place the archive into the functions directory in the application file schema. i.e.

    ├── functions/
    │   └── <function name>/
    │       ├── config.json
    │       └── source.js
    │   └── node_modules.tgz
    
  • Execute the import command with --include-dependencies, i.e:

    stitch-cli import --app-id <APP_ID> --path ./your_app --include-dependencies
    
    Creating draft for app...
    Draft created successfully...
    Importing app...
    Deploying app...
    Deploying app...
    Done.
    Importing hosting assets...
    Done.
    

Please note that currently stitch-cli does not support export for dependencies yet.

See also Stitch: Upload External Dependencies to upload from the UI.

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • That's great, thanks Wan! Apologies, I did not spot it was in the docs - or perhaps that is a new addition? – halfer Jan 24 '20 at 21:13
  • It is interesting that zip files can be no larger than 10M - I have seen Node dependency trees much larger than that, but I assume they compress well. – halfer Jan 24 '20 at 21:13
  • 1
    Incidentally, since you work for Mongo, you may be interested in my [Dockerised Stitch Console](https://github.com/halfer/docker-stitch-cli) - it's a way to containerise the console utility for people who don't want to run it on their host directly. – halfer Jan 24 '20 at 21:15
  • 2
    Hi @halfer, it's not entirely in docs yet. The documentation that I posted is mostly for the UI import but may add some information. Yes, it should compress well. Thanks I've starred your GitHub project for future reference. – Wan B. Jan 27 '20 at 22:10