sails-mongo

repository·master·Indexed 19 days ago

https://github.com/balderdashy/sails-mongo

A 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.

Tokens
3.9K
Snippets
14
Records
21
Agent score
65%

What's inside sails-mongo

  1. Manage MongoDB for development using Docker

    master

    The 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.

    Development Workflow

    1. Start the instance: npm run start-mongodb
    2. Run tests: npm test
    3. Stop the instance: npm run stop-mongodb

    Available Scripts

    • npm 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-mongodb
  2. Configure sails-mongo datastores

    master
    After installation, you must connect the adapter to one or more of your app's datastores by configuring them in your Sails.js application settings. Refer to the official Sails documentation for detailed datastore configuration.
  3. Note on Joins and Population in sails-mongo

    master

    The 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.

  4. Run tests using Docker Compose

    master

    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 up
  5. Supported and unsupported adapter methods

    master

    The sails-mongo adapter implements various Waterline interface methods. Note that join and setPhysicalSequence are currently not supported.

    MethodStatusLayer
    validateModelDefImplementedModeled
    createRecordImplementedModeled (DML)
    createEachRecordImplementedModeled (DML)
    updateRecordsImplementedModeled (DML)
    destroyRecordsImplementedModeled (DML)
    findRecordsImplementedModeled (DQL)
    joinnot supportedModeled (DQL)
    countRecordsImplementedModeled (DQL)
    sumRecordsImplementedModeled (DQL)
    avgRecordsImplementedModeled (DQL)
    definePhysicalModelImplementedMigratable
    dropPhysicalModelImplementedMigratable
    setPhysicalSequencenot supportedMigratable
  6. Tear down a datastore with teardown()

    master

    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.

    Parameters

    • datastoreName (String): The unique identity of the datastore to unregister.
    • done (Function): Callback (err).
  7. Connection Management API

    master

    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.
  8. Drop a physical model with drop()

    master

    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) { ... }
  9. Destroy records with destroy()

    master

    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(...)
  10. Create multiple records with createEach()

    master

    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(...)