goodybag / mongo-sql

An extensible SQL generation library for JavaScript with a focus on introspectibility
321 stars 72 forks source link

Create Index query type #117

Open jrf0110 opened 9 years ago

jrf0110 commented 9 years ago

http://www.postgresql.org/docs/9.3/static/sql-createindex.html

Meaglin commented 9 years ago

don't forget the 'drop-index', i have been using the following in my current code base:

mosql.registerQueryType('create-index', 'create {unique} index {name} ON {table} ({fields})');
mosql.registerQueryType('drop-index', 'drop index {ifExists} {name} ON {table}');
mosql.registerQueryHelper('unique', function(unique, values, query) {
    return unique ? 'unique' : '';
});
mosql.registerQueryHelper('name', function(name, values, query) {
    return require('mongo-sql/lib/utils').quoteObject(name);
});
mosql.registerQueryHelper('fields', function(fields, values, query) {
    return fields.map(function(name) { return require('mongo-sql/lib/utils').quoteObject(name); }).join(', ');
});
mosql.registerQueryHelper('index', function(index, values, query) {
    var util = require('mongo-sql/lib/utils');
    if(!index || !index.length) {
        return '';
    }
    return index.map(function(index) {
        var rt = ', ';
        if(index.unique) {
            rt += 'unique';
        } else {
            rt += 'key';
        }
        rt += ' ' + util.quoteObject(index.name);
        rt += ' (' + index.fields.map(function(field) { return util.quoteObject(field); }).join(', ') + ')';
        return rt;
    }).join('');
});
jrf0110 commented 9 years ago

Beautiful!