// Return the parsed query parameter from the raw one
const queryRules = {
    limit  : limit  => parseInt(limit, 10),
    offset : offset => parseInt(offset, 10),
    orderBy: order  => order,
    sort   : sort   => sort,
    count  : ()     => true,
    embed  : embed  => {
        if (embed.length === 0) {
            return undefined;
        }

        // ?embed=modelA:modelB:modelC,modelC,modelD
        // Should generate
        // {
        //  modelA: {
        //   modelB: true,
        //   modelC: true
        //  },
        //  modelC: true,
        //  modelD: true
        // }
        let joinObj = {};
        embed
            .split(',')
            .map(model => model.split(':'))
            .forEach(model => {
                if (model.length === 1) {
                    // One element : no submodel
                    joinObj[model[0]] = true;
                } else {
                    // Multiple elements : first is model, nexts are submodels
                    joinObj[model[0]] = {};
                    model.slice(1).forEach(subModel => {
                        joinObj[model[0]][subModel] = true;
                    });
                }
            });

        return joinObj;
    }
};

/**
 * Parses the query to build the future rethink call
 * @param {Request}  req  Express request
 * @param {Response} res  Express response
 * @param {Function} next Next middleware
 */
export default (req, res, next) => {
    let query = {};

    Object.keys(queryRules).forEach(q => {
        let value = (req.query.hasOwnProperty(q)) ? req.query[q].toString() : undefined;

        if (value === undefined) {
            return;
        }
        query[q] = queryRules[q](value);
    });

    req.query = query;

    return next();
};

@@ -272,962 +272,45 @@
  =%3E 
-%7B%0A        if (embed.length === 0) %7B%0A            return undefined;%0A        %7D%0A%0A        // ?embed=modelA:modelB:modelC,modelC,modelD%0A        // Should generate%0A        // %7B%0A        //  modelA: %7B%0A        //   modelB: true,%0A        //   modelC: true%0A        //  %7D,%0A        //  modelC: true,%0A        //  modelD: true%0A        // %7D%0A        let joinObj = %7B%7D;%0A        embed%0A            .split(',')%0A            .map(model =%3E model.split(':'))%0A            .forEach(model =%3E %7B%0A                if (model.length === 1) %7B%0A                    // One element : no submodel%0A                    joinObj%5Bmodel%5B0%5D%5D = true;%0A                %7D else %7B%0A                    // Multiple elements : first is model, nexts are submodels%0A                    joinObj%5Bmodel%5B0%5D%5D = %7B%7D;%0A                    model.slice(1).forEach(subModel =%3E %7B%0A                        joinObj%5Bmodel%5B0%5D%5D%5BsubModel%5D = true;%0A                    %7D);%0A                %7D%0A            %7D);%0A%0A        return joinObj;%0A    %7D
+JSON.parse(decodeURIComponent(embed))
 %0A%7D;%0A

Use thinky embeded form