Install sails-mongo
masterTo install the Sails.js/Waterline MongoDB adapter, use npm:
$ npm install sails-mongorepository·master·Indexed 19 days ago
https://github.com/balderdashy/sails-mongoA MongoDB adapter for Sails.js and Waterline ORM (version 2.1.2) that provides access to MongoDB databases. It implements core Waterline interface methods including create, update, destroy, find, count, sum, and avg, though join and setPhysicalSequence are not supported. Tested with MongoDB versions 3.6, 4.0, and 4.2, it utilizes MongoDB 3.5.x connection options and provides Docker Compose scripts for local development and testing.
To install the Sails.js/Waterline MongoDB adapter, use npm:
$ npm install sails-mongoThe repository provides Docker Compose scripts to manage a local MongoDB instance for development and testing, avoiding the need to install MongoDB directly on your host machine. The Docker instance runs on the standard port 27017.
npm run start-mongodbnpm testnpm run stop-mongodbnpm run start-mongodb: Starts a MongoDB Docker instance.npm run stop-mongodb: Stops the MongoDB Docker instance.npm run mongodb-shell: Runs the MongoDB shell CLI, connected to the Docker instance.npm run docker-test: Runs the test suite in a single run using a Docker instance (automatically starts and stops it).npm run docker: Opens a Docker instance and a shell for interactive development.# Example workflow
npm run start-mongodb
npm test
npm run stop-mongodbThe sails-mongo adapter does not implement an optimized join method for use with .populate().
Instead, Waterline will automatically fall back to its built-in join polyfill, known as "polypopulate". This mechanism performs multiple queries to the adapter and joins the resulting records in-memory within the application layer.
You can use the provided docker-compose.yml to set up a testing environment that includes a MongoDB instance and a Node.js container configured to run the sails-mongo test suite.
When running via Docker Compose, the following environment variables are configured within the adapter service to facilitate testing:
WATERLINE_ADAPTER_TESTS_DATABASE: Set to sails-mongo.WATERLINE_ADAPTER_TESTS_URL: Set to mongo/testdb.WATERLINE_ADAPTER_TESTS_HOST: Set to mongo.NODE_ENV: Set to test.The mongo service uses the mongo:7 image and exposes port 27017 on the host.
docker-compose upThe sails-mongo adapter implements various Waterline interface methods. Note that join and setPhysicalSequence are currently not supported.
| Method | Status | Layer |
|---|---|---|
validateModelDef | Implemented | Modeled |
createRecord | Implemented | Modeled (DML) |
createEachRecord | Implemented | Modeled (DML) |
updateRecords | Implemented | Modeled (DML) |
destroyRecords | Implemented | Modeled (DML) |
findRecords | Implemented | Modeled (DQL) |
join | not supported | Modeled (DQL) |
countRecords | Implemented | Modeled (DQL) |
sumRecords | Implemented | Modeled (DQL) |
avgRecords | Implemented | Modeled (DQL) |
definePhysicalModel | Implemented | Migratable |
dropPhysicalModel | Implemented | Migratable |
setPhysicalSequence | not supported | Migratable |
This adapter has been tested with MongoDB versions 3.6, 4.0, and 4.2.
It utilizes MongoDB 3.5.x connection options. If you are upgrading from an older version of MongoDB, be aware of updated, changed, new, or deprecated connection options.
The teardown method is used to unregister a datastore and destroy its connection manager. This is typically called when the server is shutting down or during test cleanup.
datastoreName (String): The unique identity of the datastore to unregister.done (Function): Callback (err).The adapter implements several methods from the node-machine/driver-interface for managing database connections:
createManager: Creates a new connection manager for a datastore.destroyManager: Destroys an existing connection manager.getConnection: Retrieves a connection from the manager.releaseConnection: Returns a connection to the manager.Use drop to remove a physical model (collection) and all its records from the database. This is used for schema migrations and is idempotent.
If the collection does not exist, the method will not return an error (it handles the MongoDB ns not found error by treating it as a successful operation).
/**
* Drop a physical model (table/etc.) from the database, including all of its records.
* This is idempotent.
*
* @param {String} datastoreName The name of the datastore containing the table to drop.
* @param {String} tableName The name of the table to drop.
* @param {Ref} unused Currently unused.
* @param {Function} done Callback
* @param {Error?}
*/
drop: function (datastoreName, tableName, unused, done) { ... }Use destroy to remove one or more records from the datastore.
Depending on the value of query.meta.fetch, the callback may return the array of physical records that were destroyed as the second argument. If fetch is not requested, you should exclude the second argument or return undefined in the callback.
/**
* @param {String} datastoreName The name of the datastore to perform the query on.
* @param {Dictionary} query The stage-3 query to perform.
* @param {Function} done Callback
* @param {Error?}
* @param {Array?}
*/
destroy: buildStdAdapterMethod(...)Use createEach to create multiple new records in the datastore.
Depending on the value of query.meta.fetch, the callback may return the array of physical records that were created as the second argument. If fetch is not requested, you should exclude the second argument or return undefined in the callback.
/**
* @param {String} datastoreName The name of the datastore to perform the query on.
* @param {Dictionary} query The stage-3 query to perform.
* @param {Function} done Callback
* @param {Error?}
* @param {Array?}
*/
createEach: buildStdAdapterMethod(...)