senecajs / seneca-entity

Entity plugin for seneca
MIT License
13 stars 15 forks source link

Using a dash '-' as part of an entity zone leads to strange behavior (at least using seneca-mongo-store) #48

Open jeromevalentin opened 6 years ago

jeromevalentin commented 6 years ago

The code below should create a collection: 'base_collectionName' in the database associated with zone 'foo-bar'.

let entity = seneca.make('foo-bar', 'base', 'collectionName') 
entity.save$()

Unfortunately, the method parsecanon() in make_entity.js is handling it with a regular expression considering '-' as the empty value of one part of the canon. So the canon which should be

{ zone: 'foo-bar', base: 'base', name: 'collectionName' } 

is not parsed like that and produces

{ zone: '-', base: '-', name: 'foo'}.

I recommend to change the regular expression used in parsecanon as follow to workaround this issue:

...
  var m = /\$?(((?:\w|-)+|-)\/)?((\w+|-)\/)?(\w+|-)/.exec(str)
  if (m) {
    var zi = m[4] == null ? 4 : 2
    var bi = m[4] == null ? 2 : 4
...

By the way, the acceptable character set per zone/base/name are dependent of the underlying database. So I would recommend to make the different part of the regular expression customizable in order to simplify the integration of seneca-entity on existing databases.