# mongoose-slug-maker

> Mongoose plugin to create slugs and unique slugs in any field

Latest version **1.0.8** (published 2020-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-slug-maker
pnpm add mongoose-slug-maker
yarn add mongoose-slug-maker
bun add mongoose-slug-maker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2020-05-22 |
| First published | 2020-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.12.7 |
| Dependencies | 2 |
| Unpacked size | 64.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | webafra |
| Maintainers | webafra |
| Keywords | slug, persian ( farsi ) slug, arabic slug, url, permalink, slugify, seo url, mongoose, mongoose automatic slug, maker, unique, seo friendly url, user friendly url, nice looking url, pretty url, clean url, static url, nice url |

## Links

- npm: https://www.npmjs.com/package/mongoose-slug-maker
- Repository: https://github.com/webafra/mongoose-slug-maker
- Homepage: https://github.com/webafra/mongoose-slug-maker#readme
- Issues: https://github.com/webafra/mongoose-slug-maker/issues
- npm.io page: https://npm.io/package/mongoose-slug-maker

## Dependencies (2)

- [async](https://npm.io/package/async.md) ^1.5.0
- [shortid](https://npm.io/package/shortid.md) ^2.2.4

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.8 (latest) — 2020-05-22
- 1.0.7 — 2020-05-22
- 1.0.6 — 2020-05-22
- 1.0.5 — 2020-05-22
- 1.0.4 — 2020-05-22

## README

# mongoose-slug-maker

Mongoose plugin for creating slugs based on mongoose schema fields. For example you can create a slug based on a document's title and author's name: _my-post-title-kevin-roosevelt_, or unique slugs based on just the title: _my-post-title-Nyiy4wW9l_.
support persian ( farsi ) , Arabic and other language url slug

# Optimize
[webafra web development team](https://webafra.com)

## Installation

The best way to install it is using **npm**

```sh
npm install mongoose-slug-maker --save
```

## Loading

```js
var slug = require('mongoose-slug-maker');
```

## Initialization

```js
var mongoose = require('mongoose');
mongoose.plugin(slug);
```

## Usage

This plugin is based on the idea of using the **mongoose schema** as the way to check the use of slug fields.

The plugin checks and updates automatically the *slug field* with the correct slug.

### Basic Usage

If you only want to create the slug based on a simple field.

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    mongoose.plugin(slug),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        slug: { type: String, slug: "title" }
});
```


### Multiple slug fields

You can add as many slug fields as you wish

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    mongoose.plugin(slug),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        subtitle: String,
        slug: { type: String, slug: "title" },
        slug2: { type: String, slug: "title" },
        slug3: { type: String, slug: "subtitle" }
});
```


### Multiple fields to create the slug

If you want, you can use more than one field in order to create a new slug field.

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    mongoose.plugin(slug),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        subtitle: String,
        slug: { type: String, slug: ["title", "subtitle"] }
});
```


### Unique slug field

To create a unique slug field, you must only add add the *unique: true* parameter in the path (also, this way the default mongo unique index gets created)

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    mongoose.plugin(slug),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        subtitle: String,
        slug: { type: String, slug: ["title", "subtitle"], unique: true }
});
```

If _unique_ is set, the plugin searches in the mongo database, and if the slug already exists in the collection, it appends to the slug a separator (default: "-") and a random string (generated with the shortid module).

**example random**

```js
mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you'

mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you-Nyiy4wW9l'

mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you-NJeskEPb5e'
```

Alternatively you can modify this behaviour and instead of appending a random string, an incremental counter will be used. For that to happen, you must use the parameter *slug_padding_size* specifying the total length of the counter:  

**example counter**

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    mongoose.plugin(slug),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        subtitle: String,
        slug: { type: String, slug: ["title", "subtitle"], slug_padding_size: 4,  unique: true }
});

mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you'

mongoose.model('Resource').create({
    title: 'سلام دنیا',
    subtitle: "لورم اپسون با تولید سادگی"
}) // slug -> 'سلام-دنیا'



mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you-0001'

mongoose.model('Resource').create({
    title: 'Am I wrong, fallin\' in love with you!',
    subtitle: "tell me am I wrong, well, fallin' in love with you"
}) // slug -> 'am-i-wrong-fallin-in-love-with-you-0002'
```


### Choose your own options

You can change any options adding to the plugin

```js
var mongoose = require('mongoose'),
    slug = require('mongoose-slug-maker'),
    options = {
        separator: "-",
        lang: "en",
        truncate: 120
    },
    mongoose.plugin(slug, options),
    Schema = mongoose.Schema,
    schema = new Schema({
        title: String,
        subtitle: String,
        slug: { type: String, slug: ["title", "subtitle"], unique: true }
});
```

You can find more options in the [speakingURL's npm page](https://www.npmjs.com/package/speakingurl)

## Support

This plugin is proudly supported by [Kubide](http://kubide.es/)

## Thanks to 
[Kubide](https://github.com/Kubide/mongoose-slug-generator#readme)

---
_Source: https://npm.io/package/mongoose-slug-maker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
