Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sayburgh-solutions/mongoose-simple-slugify
A simple language agnostic mongoose plug-in for generating unique slugs from a given string.
https://github.com/sayburgh-solutions/mongoose-simple-slugify
mongoose mongoose-plugin slugify
Last synced: 2 months ago
JSON representation
A simple language agnostic mongoose plug-in for generating unique slugs from a given string.
- Host: GitHub
- URL: https://github.com/sayburgh-solutions/mongoose-simple-slugify
- Owner: sayburgh-solutions
- Created: 2020-12-09T13:52:18.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-08-04T10:34:27.000Z (over 3 years ago)
- Last Synced: 2024-09-28T20:42:05.718Z (3 months ago)
- Topics: mongoose, mongoose-plugin, slugify
- Language: JavaScript
- Homepage:
- Size: 295 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongoose-simple-slugify ![npm](https://img.shields.io/npm/v/mongoose-simple-slugify) ![npm](https://img.shields.io/npm/dw/mongoose-simple-slugify)
A simple language agnostic `mongoose` plugin for generating unique slugs from a given string.
## Installation
Installation is as simple as any other `npm` package:```
$ npm install mongoose-simple-slugify
```## Usage
Given you have a schema in a file `models/post.js` that has a `title` and you want to generate a slug using that `title`, the corresponding code will be as follows:```js
// models/post.js// regular mongoose stuff
const mongoose = require('mongoose');
const { Schema } = mongoose;// require mongoose-simple-slugify in your schema
const slugify = require('mongoose-simple-slugify');const Post = mongoose.model(
'Post',
new Schema(
{
// title of the post
title: {
type: String,
required: true,
},
// slug generated for the post
slug: {
source: 'title', // source for generating the slug
type: String,
unique: true,
},body: {
type: String,
required: true,
},
},{ timestamps: true },
).plugin(slugify), // registering the plugin
);module.exports = Post;
```
That's all you need. Now everytime you create a new post a slug will be generated automatically. In case the generated slug is already in the database, a cryptographically correct random string will be appended to the slug.