Code Monkey home page Code Monkey logo

mongoose-slugger's People

Contributors

arslanlineupr avatar dependabot[bot] avatar qqilihq avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

mongoose-slugger's Issues

Add a `maxAttempts` parameter

The settings should have an optional maxAttempts parameter, which allows to limit the number of retries in order to avoid long or endless loops.

Non-Unique Slug Generation Issue

Description:

Issue:

I have encountered an issue with the "mongoose-slugger-plugin" where slugs are not being generated as expected. The plugin is supposed to generate unique slugs for Mongoose documents, but in my testing, I've found that non-unique slugs are being generated under certain circumstances.

Steps to Reproduce:

  • Create a Mongoose schema with the "mongoose-slugger-plugin" configured.
  • Use the plugin to create documents, ensuring that there are potential conflicts in slug generation.
  • Observe that in some cases, non-unique slugs are generated, violating the expected behavior.

Expected Behavior:

I expect the "mongoose-slugger-plugin" to consistently generate unique slugs for Mongoose documents, even when conflicts exist.

Actual Behavior:

Non-unique slugs are generated in some cases, leading to slug collisions.

Additional Information:

  • I have configured the plugin according to the provided usage instructions.
  • This issue occurs intermittently and is not consistently reproducible, making it challenging to pinpoint the exact cause.
  • I'm using the latest version of the "mongoose-slugger-plugin"
  • I have attached a code snippet of my schema and plugin configuration for reference.
import mongoose from "mongoose";
import { sluggerPlugin } from "mongoose-slugger-plugin";

const { Schema, model } = mongoose;

const schema = new Schema<any>(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "User",
    },
    title: {
      type: String,
      required: true,
    },
    slug: {
      type: String,
      unique: true,
      require: true,
    },
    description: {
      type: String,
    },
    assets: {
      type: [{ type: String, required: true }],
      validate: {
        validator: function (items: []) {
          return items && items.length > 0;
        },
        message: "At least one asset is required",
      },
    },
  },
  { timestamps: true }
);

schema.index(
  { slug: 1, title: 1, description: 1 },
  { name: "search", unique: true }
);

schema.plugin(sluggerPlugin, {
  slugPath: "slug",
  generateFrom: ["title"],
  index: "search",
});

Environment:

  • Node.js version: 19.8.1
  • Mongoose version: 7.5.1
  • Operating System: macOs

Please let me know if you need any additional information to investigate this issue.

Check code coverage

Shippable currently shows something around 45 percent, but it should actually be 90 percent.

Mongoose 6?

Can't install this now with latest mongoose at the package requires 5.0. Will it work with 6?

Hide non-public functionality

Currently, the plugin exposes internal functions which should probably not be visible to the outside:

bildschirmfoto 2018-04-21 um 13 04 46

The following properties should be invisible (how can this be done?):

  • createDefaultGenerator
  • extractIndexNameFromError
  • saveSlugWithRetries (maybe?!?)

Maximum length of slugs

It should be possible to configure a maximum length for the generated slugs. Ideas:

  1. Implement this in the generator function (either a default, or entirely leave this to the client)
  2. Add a maxLength parameter
  3. Use mongoose's maxlength validator

Make `wrap` function more generic (TS)

Instead of

export declare function wrap<D extends Document>(model: Model<D>): Model<D>;

… it should probably rather be …:

export declare function wrap<D extends Document, M extends Model<D>>(model: M): M;

So that it plays will e.g. with @casl/mongoose.

Remove unique requirement

The slugs I generate I don't want to necessarily be unique since they are accessed through another path /. However this plugin requires all slugs to be unique

(Error Code 11000) How to create slug with exists documents

Hello friends!
I am facing a problem when using with Typegoose, I am trying to allow users to create the same documents, but when using this plugin, I am getting error code 11000 when creating the document I am creating already existed before.
Here is my code, if I'm wrong please help me
Model.ts
image

Error message:
image

Thank all!

Check, whether generated slug is actually unique

One could stupidly do something like this:

schema.plugin(slugger.plugin, new slugger.SluggerOptions({
  generator: (doc, attempt) => doc.whatever,
  // …
}));

The generated slug would not change (as the attempt is not considered) and produce an infinite loop (see also #2).

Add some checking logic, which verifies that the generated slug is actually unique before retrying.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.