mongo-sql icon indicating copy to clipboard operation
mongo-sql copied to clipboard

Add support for compound `UNIQUE` and `PRIMARY KEY` constraints

Open brianc opened this issue 9 years ago • 1 comments

I'm not 100% sure this is the correct approach or if all the formatting is okay. This is a first crack at adding table constraints to support natural keys.

brianc avatar Jun 15 '16 01:06 brianc

This is perfect! And it made me remember some things that will make it more robust.

So, this lib needs to be refactored. There's too many concepts:

  • Query Types
  • Query Helpers
  • Conditional Helpers
  • Update Helpers
  • Action Helpers
  • Column Definition Helpers

When I first wrote MoSQL, I thought there'd be a unified helper interface, but as time went on, I ended up with more and more specialized helpers.

The Column Definition Helpers can assist us here:

var columnDefs = require('../../lib/column-def-helpers');

...

return Object
  .keys( constraints )
  .filter( function( k ){
    return columnDefs.has( k );
  })
  .map( function( k ){
    return columnDefs.get( k ).fn( constraints[ k ], values, query );
  })
  .reduce( function( result, constraint ){
    result.push( constraint );
    return result;
  }, [] )
  .join(', ')

I think that code would work I'm not sure. But my main point would be to use the column-def-helpers module and that will actually give you primary key(...), unique(...) and check(...)!

jrf0110 avatar Jun 15 '16 14:06 jrf0110