npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@techntools/sequelize-to-openapi

v0.4.9

Published

OpenAPI 3 schemas from Sequelize models

Downloads

9

Readme

Sequelize To OpenAPI

Based on sequelize-to-json-schemas

With following changes:

  1. Rewrite in TypeScript
  2. Added OpenAPI validations based on Sequelize validations
  3. Only OpenAPI is supported.
  4. Focused only on Sequelize 6

Installation

npm install --save @techntools/sequelize-to-openapi

Usage

import { SchemaManager, OpenApiStrategy } from '@techntools/sequelize-to-openapi'

const schemaManager = new SchemaManager
const strategy = new OpenApiStrategy

oapi.schema('User', schemaManager.generate(UserModel, strategy))

Configuration Options

Pass (per) model options to the generate() method:

const userSchema = schemaManager.generate(userModel, strategy, {
    exclude: ['someAttribute'],
    include: ['someAttribute'],
    associations: true,
    excludeAssociations: ['someAssociation'],
    includeAssociations: ['someAssociation'],
});

title and description are dropped

jsonSchema and schema works the same as sequelize-to-json-schemas

Unsupported Types

  • GEOMETRY
  • RANGE
  • ABSTRACT
  • GEOGRAPHY
  • HSTORE

While sequelize-to-json-schemas throws error for these, this package simply ignores them so that you can use generated schema for rest of the types and support these types the way you see fit

Validators

Following validators are supported:

| Sequelize | OpenAPI Keyword | | :-------- | :-------------------------- | | min | minimum | | max | maximum | | len | minLength/maxLength | | notEmpty | minLength | | notIn | { not: { enum: [] } } |

| Sequelize | OpenAPI Format | | :-------- | :------------- | | isEmail | email | | isUrl | url | | isUUID | uuid |

| Sequelize | OpenAPI Pattern | | :------------- | :------------------------------------------------------------------------------------------ | | isAlpha | ^[a-zA-Z]+$ | | isNumeric | ^[0-9]+$ | | isAlphanumeric | ^[a-zA-Z0-9]+$ | | isLowercase | ^[a-z]+$ | | isUppercase | ^[A-Z]+$ | | contains | ^.*' + val + '.*$ | | notContains | ^(?!.*' + val + ').*$With array:^(?!.*(' + val.args.join('\|') + ')).*$ |

| Sequelize | AJV | | -------------------------------------- | ------------------------------------------------------ | | is as string | { regexp: '' } | | is as RegExp | { regexp: '' } | | is as { args: '', msg } | { regexp: '' } | | is as { args: RegExp, msg } | { regexp: '' } | | is as { args: ['pat', 'f'], msg } | { regexp: { pattern: pat, flag: f }} | | is as array | { regexp: { pattern: val[0], flag: val[1] }} | | not as string | { not: { regexp: '' }} | | not as RegExp | { not: { regexp: '' }} | | not as { args: '', msg } | { not: { regexp: '' }} | | not as { args: RegExp, msg } | { not: { regexp: '' }} | | not as { args: ['pat', 'f'], msg } | { not: { regexp: { pattern: pat, flag: f }}} | | not as array | { not: { regexp: { pattern: val[0], flag: val[1] }}} |

Case with regular expression

Flags such as i, g etc. are not supported in OpenAPI. Sequelize can use string or RegExp class for regex. So, to avoid these limitations, I have used regexp keyword from ajv-keywords package for is and not validators.

This makes generated OpenAPI schema not fully compliant with the standard. But you can drop those validators if you face issues.

Demo

Check my repo for usage of the package. It uses sequelize-typescript.

Visit the OpenAPI documentation powered by scalar

express-openapi generates OpenAPI spec

License

This project is released under MIT LICENSE

Contributing

  1. Keep the changes small
  2. Add the tests
  3. Existing tests have to pass

Credits

Full credits to the authors and contributors of sequelize-to-json-schemas for the great work