YuriGor / mongoose-slug-updater

Schema-based slug plugin for Mongoose - single/compound - unique over collection/group - nested docs/arrays - relative/abs paths - sync on change: create/save/update/updateOne/updateMany/findOneAndUpdate tracked - $set operator - counter/shortId
MIT License
40 stars 13 forks source link

Concatenated slugs #9

Closed FarazPatankar closed 5 years ago

FarazPatankar commented 5 years ago

Hey,

I just found this library and it seems great so far. I had a quick question, would it be possible to concatenate two values to form my slug?

As an example, let's assume I am building a job board and I want the slug to be the job title followed by the company name. Could I do something like slug: 'title', 'at', 'company.name'.

Thanks!

YuriGor commented 5 years ago

Hi @FarazPatankar! Yes, it's called compound slug

FarazPatankar commented 5 years ago

Hey @YuriGor,

Thanks for the quick response but I already tried it and it didn't work. Even in this example, it doesn't, right?

YuriGor commented 5 years ago

Most of the tests use compound slugs, so I expect it should work.

If something doesn't work for you there are two cases:

YuriGor commented 5 years ago
FarazPatankar commented 5 years ago

I feel like I am missing something. Here's the code:

const JobSchema = new Schema({
  title: {
    type: String, required: true,
  },
  slug: {
    type: String,
    slug: ['title', 'company.name'],
    uniqueSlug: true,
  },
  company: { type: Schema.Types.ObjectId, ref: 'Company' },
});

Shouldn't this add a slug that includes both the job title and the company name? I am only getting the job title as of now.

Does it not work because I am referencing another model? Does it only work for subdocuments?

YuriGor commented 5 years ago

I've never tested Mongoose Population feature, but show me the rest of the code. Do you have some data loaded already before you are trying to update/create a document with a slug? Ah, you also want the slug to be unique - this will definitely not work. You may try to use forceIdSlug: true flag and make sure documents are loaded/filled before you are trying to generate a slug.

FarazPatankar commented 5 years ago

That's all the code I have actually. I am using Forest Admin to add/update the data. It works fine if I use two fields from the JobSchema itself.

I also tried removing doing it with the uniqueSlug property but that didn't help either.

YuriGor commented 5 years ago

Well, I can't help you with it right now, refs are not supported yet. And if I'll implement this feature(I don't promise), it will not be soon. I expect it will be difficult to keep slug synced with fields from separate collections linked by ref.

In your case i'd recommend to have slug field in each collection, in the job and company separately. If job slug will be unique - you still will be able to search by this slug, and company slug will be used as an url decoration only, separated by slash: suberjob.com/my-company/my-job-wsg535