theorye / feathers-chat-rethinkdb

A Feathers real-time chat application using rethinkdb
MIT License
2 stars 0 forks source link

Adding unique emails #3

Open theorye opened 7 years ago

theorye commented 7 years ago

ATM, there is nothing to ensure that emails are unique

Suggested code:

r.branch(r.table("users").getAll(email, {index: "email"}).isEmpty(),
         r.table("users").insert(user),
         {})
genyded commented 7 years ago

That will work assuming the index is there, but should probably be combined with something that checks if the query contains {index: ...} and if so making sure the index exists with something like this before attempting to use the index:

 r.db('somedb').table('users').indexList().contains('email')
  .do(function (indexExists) {
    return r.branch(indexExists, { created: 0 }, r.db('somedb').table('users').indexCreate('email'))
  })

That of course has to be abstracted the way db and table are now in feathers-rethink to automate it.

theorye commented 7 years ago

@genyded

Should I make this its own service or use hooks?

genyded commented 7 years ago

I guess that depends on if you want it always, or sometimes. If always make it a hook that always runs. If sometimes, either it's own service that you can call it independently when needed, or a hook with an option to specify whether or not to run it for any given call. The real question is whether or not there would ever be any scenario where you might want to allow duplicates for any reason - even temporarily like maybe for a data merge or something.