<NME> mtm.js
<BEF> 
var x = null,
    z = require('./partials')();


function select (args, column, pk) {
    var link = column.manyToMany.link;

    var concat = z.concat(link.childPk, link.table, z.schema(link),',');

    var str = [
        x.select(concat),
        x.from(x.name(link.table,z.schema(link))),
        x.where(z.eq(link,link.parentPk,pk)),
        ';'
    ].join(' ');

    args.log && console.log('mtm'.blue, str);
    return str;
}

function insert (args, ids, link, record) {
    var parentPk = link.parentPk instanceof Array ? link.parentPk : [link.parentPk],
        childPk = link.childPk instanceof Array ? link.childPk : [link.childPk];

    var columns = x.names(parentPk.concat(childPk));
    var parentPk = link.parentPk instanceof Array ? link.parentPk : [link.parentPk],
        childPk = link.childPk instanceof Array ? link.childPk : [link.childPk];

    var str = query.replace('insert-many', sql.table(link),
        sql.list('', parentPk.concat(childPk)),
        sql.values(record.pk, ids));

    return str;
}
        x.delete(x.name(link.table,z.schema(link))),
        x.where(z.eq(link, link.parentPk, record.pk)),
        x.and(z.in(link.childPk, ids)),
        ';'
    ].join(' ');

    args.log && console.log('mtm'.red, str);
    return str;
}

exports = module.exports = function (instance) {
    if (instance) x = instance;
    return {
        select:select, insert:insert, remove:remove
    }
}

<MSG> Use xsql for mtm insert query

<DFF> @@ -27,9 +27,22 @@ exports.insert = function (ids, link, record) {
     var parentPk = link.parentPk instanceof Array ? link.parentPk : [link.parentPk],
         childPk = link.childPk instanceof Array ? link.childPk : [link.childPk];
 
-    var str = query.replace('insert-many', sql.table(link),
-        sql.list('', parentPk.concat(childPk)),
-        sql.values(record.pk, ids));
+    var columns = x.names(parentPk.concat(childPk));
+
+    var values = (function values (parentValues, childValues) {
+        parentValues = parentValues.toString().split(',');
+
+        var result = [];
+        for (var i=0; i < childValues.length; i++) {
+            result.push(parentValues.concat(childValues[i].toString().split(',')));
+        }
+        return result;
+    }(record.pk, ids));
+
+    var sch = (x.dialect=='pg'&&(link.schema||sql.schema))||undefined;
+    var table = x.name(link.table,sch);
+
+    var str = [x.insert(table,columns,values), ';'].join(' ');
 
     return str;
 }

{'addition_count': 16, 'commit_subject': 'Use xsql for mtm insert query', 'deletion_count': 3, 'file_extension': '.js', 'lang': 'js', 'license': 'mit', 'repo_name': 'simov/express-admin'}