Code Monkey home page Code Monkey logo

sequelize-slugify's Introduction

Sequelize Slugify Logo

Sequelize Slugify

What does Sequelize Slugify do?

  • A plugin for Sequelize ORM models to automatically create and update unique slugs

Unit Testing

  • GitHub Actions CI tests
    • PostgreSQL
    • MySQL
    • MariaDB
    • SQLite
    • MSSQL

Releases

  • Semantic Versioning
  • Supporting Node.js v10+

Documentation

sequelize-slugify's People

Contributors

bossyan avatar bpartridge83 avatar corinv avatar gleuch avatar jarrodconnolly avatar joewoodhouse avatar maxdeviant avatar mikefowler avatar oktapodia avatar romanzaycev avatar semantic-release-bot avatar shem8 avatar tobyjsullivan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sequelize-slugify's Issues

transaction support?

It might be a good idea to pass options.transaction to checkSlug. This plugin doesn't seem like it would be reliable for insertions of many records within a single transaction, since it doesn't scope the Model.find to the transaction.

Question: How do I perform a find by slug when I do not have the suglified string?

Scenario:
I have a model called Author like so:

'use strict';
var SequalizeSlugify = require('sequelize-slugify');
module.exports = function (sequelize, DataTypes) {
    var Author = sequelize.define('Author', {
        slug: {
            type: DataTypes.STRING,
            unique: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        }
    }, {
        tableName: 'authors',
        paranoid: true,
        underscored: true,
        classMethods: {
            associate: function (models) {
                Author.belongsToMany(models.Book, {as: 'BookAuthor', through: 'books_authors'});
            }
        }
    });
    SequalizeSlugify.slugifyModel(Author, {
        source: ['name']
    });
    return Author;
};

Now I would like to do a find given an unslugified author name.

Author.FindOne({
    where: {
        slug: SequalizeSlugify.somethingmagical("John Doe")
});

Add unit test support for mariadb

Is your feature request related to a problem? Please describe.
Sequelize itself tests against mariadb, we should add this to our tests.

Describe the solution you'd like
Unit tests all pass.

Describe alternatives you've considered
N/A

Don't work beforeUpdate

Hello, I've found a problem. The hook beforeUpdate don't work for model's method update. I.e. slug's col doesn't update with name.
e.g.:

await Person.update(
    {
      name: name,
      description: description
    },
    { where: { id: id } }
  )

But if I use this code:

  const person = await Person.findByPk(id)
  await person.update({
    name: name,
    description: description
  })

Everything works well.

Documentation link is invalid

  • Node.js: All
  • OS: All
  • Sequelize: Current
  • Database: All

What steps will reproduce the bug?

The Readme.md has a link to documentation pointing to https://sequelize-slugify.nestedquotes.ca/ But the URL is invalid.

How often does it reproduce? Is there a required condition?

NA

What is the expected behavior?

The url should point to a valid location. Or better still point to the internal doc links: for example

What do you see instead?

Additional information

Not working with bulkCreate

hi, I use like this
let slugify = require('sequelize-slugify');

slugify.slugifyModel(Nomenclatures, {
        source: ['name'],
        slugOptions: { lower: true },
        overwrite: false,
        column: 'slug'
    });

In my case slug can not be null
await model.bulkCreate(json_data);

It gives me an error slug can not be null

Update please

Getting the following error:

sequelize deprecated Model.find has been deprecated, please use Model.findOne instead

Can you please update find to findOne?

/**
     * Checks whether or not the slug is already in use.
     *
     * @param slug The slug to check for uniqueness.
     * @return True if the slug is unique, false otherwise.
     */
    var checkSlug = function (slug) {
        var query = { where: {} };
        query.where[slugColumn] = slug;

        return Model.find(query).then(function (model) {
            return model === null;
        });
    };

Clean up unit tests

Is your feature request related to a problem? Please describe.
The unit tests are a mess :) clean them up and separate them into different files if possible.

Describe the solution you'd like
Should be easy to find the tests you want to run.

Describe alternatives you've considered
N/A

vulnerabilities detected

Node: v10.7.0
NPM: v6.4.0

{
...
"sequelize": "^4.38.0",
"sequelize-slugify": "^0.5.0",
...
}

Issue:

$ npm audit
                       === npm audit security report ===

                                 Manual Review
             Some vulnerabilities require your attention to resolve
          Visit https://go.npm.me/audit-guide for additional guidance

  Moderate        Regular Expression Denial of Service
  Package         slug
  Patched in      No patch available
  Dependency of   sequelize-slugify
  Path            sequelize-slugify > slug
  More info       https://nodesecurity.io/advisories/537

Programmatically disabled the plugin if user provides a custom slug

Hey guys, and thanks for building the plugin. I was wondering if there's a way to programmatically disable
sequelize-slugify if the user provide a custom slug.

Basically, I have a use-case where users are allowed to create their own custom slugs, e.g. for articles, and if one is provided, I would like this plugin to simply do nothing.

Is there a way to do this now, and if not, how can we help to implement this feature? I don't mind fiddling with the plugin and doing a PR.

Cheers

Add unit test support for mssql

Is your feature request related to a problem? Please describe.
Sequelize itself tests against mssql, we should add this to our tests.

Describe the solution you'd like
Unit tests all pass.

Describe alternatives you've considered
N/A

Not working with DataTypes.VIRTUAL

  • Node.js: v14.16.1
  • OS: Darwin Lucass-MacBook-Pro.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:31 PDT 2021; root:xnu-7195.121.3~9/RELEASE_ARM64_T8101 arm64
  • Sequelize: 6.6.4
  • Database: PostgresSQL 13

What steps will reproduce the bug?

Page.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: UUIDV4,
        allowNull: false,
        primaryKey: true
      },
      title: {
        type: DataTypes.JSONB,
        allowNull: false
      },
      _title: {
        type: DataTypes.VIRTUAL,
        get() {
          const title: any = this.get('title');
          return title[langs[0]];
        }
      },
    },
    {
      sequelize,
      tableName: 'pages',
      modelName: 'Page'
    }
  );

  SequelizeSlugify.slugifyModel(Page, { source: ['_title'] });

How often does it reproduce? Is there a required condition?

What is the expected behavior?

The slug column is not getting _title Virtual column as the documentation says.

Screen Shot 2021-07-09 at 14 49 01

What do you see instead?

Additional information

Typescript support

  • Node.js: 14.17.5

What steps will reproduce the bug?

Install sequelize-slugify to a typescript project

What is the expected behavior?

Types are defined

What do you see instead?

src/posts/post.entity.ts:11:30 - error TS2307: Cannot find module 'sequelize-slugify' or its corresponding type declarations.
2021-08-17T08:13:24.667115708Z 
2021-08-17T08:13:24.667126875Z 11 import SequelizeSlugify from 'sequelize-slugify';
2021-08-17T08:13:24.667267208Z                                 ~~~~~~~~~~~~~~~~~~~

Additional information

I found that there are no types in the installed package.
image

Enhanced suffix generation

I think it would be nice to have a list of suffix fields that would be used to create uniqueness before reverting to appending numbers to the slugs.

Similar to the source field, it would take an array of fields that it would use when creating a suffix.

SequelizeSlugify.slugifyModel(Movie, {
    source: ['title'],
    suffixSource: ['year'],
    overwrite: false
});

A case where this would be useful would be for something like movies, where two movies have the same title but are released in different years. Obviously, you could just include make the source ['title', 'year'] right off the bat, but then each slug would be required to have the year in it, even if there is no collision.

Example:

// added first
{ title: 'Gladiator', year: 2000, slug: 'gladiator' }

// added second (use the suffixSource)
{ title: 'Gladiator', year: 1992, slug: 'gladiator-1992' }

// added third (fall back to default suffix)
{ title: 'Gladiator', year: 1992, slug: 'gladiator-1992-1' }

Slug replacement for incremental counter

When we specify replacement in the slugOptions, it only replaces within the slug and not the incremental counter.

Example I used to slugify a model:

SequelizeSlugify.slugifyModel(People, {
    source: ['first_name', 'last_name'],
    slugOptions: {
        replacement: '_'
    },
    overwrite: false,
    column: 'identifier'
});

This will generate first_last. If it already exist, it will generate first_last-1.

I'd like to replace the separator for the counter to first_last_1, first_last_2, etc..

Option to do slugification before validation is done by sequelize

Hello Jarrod,

Since I've changed allowNull property of slug column to false, sequelize is throwing SequelizeValidationError: notNull Violation: MyModel.slug cannot be nullError

If we can execute handleSlugify on beforeValidate hook then the above issue will be solved.

Thank you.
Best regards,
Tanmay

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.