How to require scripts in Mongo Shell CLI - mongodb

I'd like to be able to import a library of common tools that I use to make working with MongoDB easier. However, I haven't discovered an easy to to import external scripts into the Mongo CLI. Ideally, this would work similar to Node.js require.
How can I get require working in Mongo CLI by default?
Or, is there some other way to solve external library dependencies?

There are several options for loading JavaScript into the mongo shell:
1) Save the JavaScript to .mongorc.js in your home directory.
2) Save the JavaScript to the global mongorc.js file (MongoDB 2.6+) which is evaluated before the .mongorc.js file in your home directory.
3) Use load('filename.js') from within the shell (or within one of the mongorc.js files).
4) Store your JavaScript on the server using the system.js collection and db.loadServerScripts().
For common JavaScript functions, the first option is the most typical approach.

Related

Distribute shell scripts using setuptools and pyproject.toml

I'm trying to distribute a shell script along with a Python package. Ideally, the shell script is installed when I run pip install my_package. I read from this SO that, my expected behavior is exactly what the scripts keyword of setuptools.setup provides. E.g. the script my_script will be installed with the following setup.py script:
setup(
...
scripts=['my_script'],
...
)
However, I cannot use the above method for two reasons:
the official doc did not mention this behavior. I don't know if I can continue to do this way.
my whole project is built on pyproject.toml, without setup.py. Although pyproject.toml has provided a [project.scripts] table, as explained in the setuptools official doc, the scripts can only be python functions instead of shell scripts.
For completeness, in my case, the shell script reads git status and sets environment variables, which will be read from within my python project. The shell script and my python project are bonded so tightly that I would rather not split them into two projects.
I have also tried to use a python function to execute the shell script, e.g.
[project.scripts]
my_script = 'my_project:my_func'
def my_func():
subprocess.run(...)
The problem with this solution is that every time I run my_script, my_project is loaded and the loading process is really slow.
Maybe a link in the comments leads to this information already. Anyway, I think it is worth posting that scripts = [...] in setup.py can be written in pyproject.toml as:
[tool.setuptools]
script-files = ["scripts/myscript1", "scripts/myscript2"]
However, this feature is deprecated. I hope the authors of the packaging tools will recognize the problem with shell scripts and deal with it.
Link: setuptools docs
I'm not exactly sure it will work for you case, but I solved this by creating a "shim" setup.py file (it has an added benefit of being able to install your project in edit mode).
It usually just calls setup(), but it was possible to pass the scripts argument:
"""Shim setup file to allow for editable install."""
from setuptools import setup
if __name__ == "__main__":
setup(scripts=["myscript"])
Everything else was loaded from pyproject.toml.

How can you run a config script when installing a perl module?

I've been searching for a couple of hours and I'm coming up empty trying to find a solution. I'm using Dist::Zilla. I have a module that uses a simple config file in .ini format located in the module's share/ directory. When my module is installed, I'd like the install script to prompt the user for configuration options and save the user's options in the config file. Then, using File::UserConfig, it will copy the file over to the user's configuration directory where it can be loaded by the module when it runs.
Someone had suggested the Dist::Zilla::Plugin::MakeMaker::Custom module but I know next to nothing about MakeMaker and how I might write a custom one to kick off the configuration script.
I'm surprised I can't find anything that makes this easy to do. Perhaps I'm searching on the wrong keywords?
You had discussed this in IRC, and the gist is:
You cannot rely on the installation process allowing any interaction, as a large amount of installations are via cpanm which is non-interactive and hides output from Makefile.PL by default. This is because users don't like having to configure things, and as an example, a Carton deployment is frequently non-interactive by its nature. You can allow configuration via environment variables recognized by your Makefile.PL to get around this.
You can document to install using the --interactive option for cpanm in order to respond to prompts in your Makefile.PL, injected into the generated file using the [MakeMaker::Awesome] plugin.
You can include a script with the distribution that will set up the configuration so the user can do it themselves separate from the installation.

How do I build an installer for my perl application with DB?

I've been searching for a while and I cannot find an easy solution for building an install package on linux for a perl application I've built. My application is a mojolicious application and I am using DBIx::Schema. I need to accomplish the following;
import my DB schema into a database
check for and install any missing perl modules
copy my actual application to a location.
possibly set my application to run as a service.
This is rather trival on windows, but I can't seem to find a clean solution to do this on Linux. Is the easiest approach to just write another perlscript to do the install?
thanks.
Try to use Rex or checkout this SO question Is there a Perl or Lua alternative to Capistrano?.
To deploy your DBIx::Class::Schema you can use $schema->deploy in an install script. It uses SQL::Translator to generate the SQL statements for your RDBMS of choice.
Another possibility to generate the SQL statements in the app build process and store them in text files per supported RDBMS using $schema->deployment_statements.
Many people package their apps like CPAN modules so they can't be installed using the CPAN toolkit apps like cpan, cpanminus or cpanplus.

How to run server-side code programmatically in MongoDB

I need to define some js codes and run them in the server side. I know how to run them thru the mongo shell but I need to do this programmatically. How can I do this?
You can use grails drivers like mongodb/gmongo. For example I use the MapReduceCommand
to execute map/reduce command with Javascript syntax.
Enjoy
After some Internet search, I ran into a PHP solution from http://pointbeing.net/weblog/2010/08/getting-started-with-stored-procedures-in-mongodb.html

Working with nodejs and mongodb

Are there any libraries or frameworks that allow me to work with mongodb and nodejs that don't require a npm install ? More precisely I'm working on windows.For example i managed to find a library for working with websockets(https://github.com/ncr/node.ws.js) and simply requires you to include the file.
Is it absolutely necessary to use a library ? I am asking because all the tutorials use one, doesn't node have a module for this ?
You don't need npm to use most modules - you can download them (e.g. from their GitHub page as a zip file) and then put them in your project folder. Then require them:
var mongodb = require('../lib/module-folder-name');
Some useful libraries:
MongoDB native driver:
https://github.com/christkv/node-mongodb-native
Mongoose, a higher level ORM for MongoDB:
https://github.com/learnboost/mongoose/
evilcelery has the best answer +1 from me;
Most anything you run across for npm will work just as he said, and lib is the best convention to do it with.
To expand on his links a bit The module list he refereed to is found:
https://github.com/joyent/node/wiki/modules
http://search.npmjs.org/ is a bit more user-friendly.
Also if you wish to include things globally similar to npm you can do what it does with things like html and put it in the lib dir where you originally compiled it with the Makefile (note: you may not need to rebuild it but I beleave you do)
There are a lot of mongodb related projects/libs enjoy!
interestingly:
Blockquote Contrary to the belief of many, "npm" is not in fact an abbreviation for "Node Package Manager". It is a recursive bacronymic abbreviation for "npm is not an acronym".
source: https://github.com/isaacs/npm/blob/master/doc/faq.md#readme
You can use builtin net.sockets module
var net = require('net');
var server = net.createServer(); // to listen
var socket = net.socket; // to connect
It might you to work with any network application and write raw requests.
A lot of modules written on js so you can not to install them with npm, but require from your project folder.
I haven't tried it, but this should enable you to get node packages with windows: https://github.com/japj/ryppi . It will require you to download Python.