node.js – how to create a unique slug using Postgres and TypeORM?

I am trying to create a unique slug and save to database. But I am not getting a logic here. I am trying to clear it-

suppose I receive a title field from frontend-

{title: this is the blog title}

For this title slug will be this-is-the-blog-title

Here, If title will be same every time, then I need to create slug like this for newer document-

this-is-the-blog-title-1
this-is-the-blog-title-2
this-is-the-blog-title-3
this-is-the-blog-title-4
this-is-the-blog-title-5
this-is-the-blog-title-6
this-is-the-blog-title-7
this-is-the-blog-title-8
this-is-the-blog-title-9
........................

But I am not getting any idea how can I do that.

Here is my try-

const slugTitle = slugify(this.title)
//this-is-blog-title
const exist = await this.model.findOne({slug: slugTitle})
if(!exist) {
    await this.model.create({slug: slugTitle})
} else {
    await this.model.create({slug: slugTitle + ""}) //Here what I do, I am not getting any idea
}

Read more here: Source link