rubydoc / mongoose-friendly-id

Slug for mongoose
4 stars 0 forks source link

Unique slug #1

Open anas10 opened 8 years ago

anas10 commented 8 years ago

Is it possible to handle the case where a slug already exists and generate a unique slug by adding a number at the end for example ?

The friendly slug is very handy but not unique.

rubydoc commented 8 years ago

Hi anans10. I have not touched mongoose for a while, but I think it is possible following this. It is written by icedcoffeescript.

slug = require 'mongoose-friendly-id'
schema.plugin slug, (model) ->
  slugged = "#{model.name}--#{model.job}" 
  await schema. findByFriendlyId slugged, defer(err, user)
  if user 
    "#{slugged}-#{model.id}"
  else
    slugged