Search code examples
deployd

Is there a way in deployd to map a collection to a different endpoint?


I have a collection called customer_devices and I can't change the name. Can I expose it via deployd as /devices ? How?


Solution

  • There are a few ways that I can think of. If you really just want to rename the collection, you can do so from the dashboard, as @thomasb mentioned in his answer.

    Alternatively, you can create a "proxy" event resource devices and forward all queries to customer_devices. For example, in devices/get.js you would say

    dpd.customer_devices.get(query, function(res, err) {
        if (err) cancel(err);
        setResult(res);
    });
    

    Update
    Finally, here is a "hack" to redirect all requests from one resource path to a different path. This is poorly tested so use at your own risk. This requires that you set up your own server as explained here. Once you have that, you can modify the routing behaviour using this snippet:

    server.on('listening', function() {
        var customer_devices = server.router.resources.filter(function (res) {
            return res.path === '/customer_devices';
        })[0];
        // Make a copy of the Object's prototype
        var devices = Object.create(customer_devices);
        // Shallow copy the properties
        devices = extend(devices, customer_devices);
        // Change the routing path
        devices.path = "/devices";
        // Add back to routing cache
        server.router.resources.push(devices);
    });
    

    This will take your customer_devices resource, copy it, change the path, and re-insert it into the cached routing table. I tested it and it works, but I won't guarantee that it's safe or a good idea...