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

graphql-sequelize-helper

v0.0.5

Published

graphql-sequelize-helper transform Sequelize models to GraphQL schemas that is compatible with Relay. * <a href="http://docs.sequelizejs.com/">Sequelize</a> is a promise-based Node.js ORM for Postgres, MySQL, SQLite and Microsoft SQL Server. It features s

Downloads

17

Readme

graphql-sequelize-helper

graphql-sequelize-helper transform Sequelize models to GraphQL schemas that is compatible with Relay.

  • Sequelize is a promise-based Node.js ORM for Postgres, MySQL, SQLite and Microsoft SQL Server. It features solid transaction support, relations, read replication and more.
  • GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

Install

  • npm install graphql-sequelize-helper
  • yarn add graphql-sequelize-helper

Useage

// define sequelize model
import gsh from 'graphql-sequelize-helper'

const UserType = gsh.modelRef('User')

export default (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    username: {
      type: DataTypes.STRING(25),
      unique: true,
      comment: 'name of the user...',
      allowNull: false
    },
    phoneNumber: {
      type: DataTypes.STRING(25),
      field: 'phone_number',  // db field name
      unique: true,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING(64),
      unique: true,
      validate: {
        isEmail: {
          args: true,
          msg: 'Invalid email',
        },
      },
      allowNull: false
    },
    password: {
      type: DataTypes.STRING,
      validate: {
        len: {
          args: [5, 100],
          msg: 'The password needs to be between 5 and 100 characters long',
        },
        // custom validate
        startWithLetter: (value) => {
          if(/^[a-z, A-A]/.test(value) === false) throw new Error('password must start with a letter')
        }
      },
    },
  }, {
    hooks: {
      afterValidate: async (user) => {
        const hashedPassword = await bcrypt.hash(user.password, 12);
        // eslint-disable-next-line no-param-reassign
        user.password = hashedPassword;
      },
    }
  })
  
  // build association
  User.associate = (models) => {
    User.hasMany(models.Order, {
      foreignKey: {
        name: 'userId',   // graphql field
        field: 'user_id'  // db field
      },
      hooks: true,
      onDelete: 'CASCADE'
    })
  }
  
  // some config
  User.config = {
    description: 'user model',   // graphql description,
    crud: {
      update: false
    }
  }
  
  // build custom queries, mutations, subscriptions, model methods, static methods
  User.graphql = {
    methods: {},
    statics: {},
    queries: {},
    mutations: {
      updateUser: {
        description: 'set the password',
        inputFields: {
          id: {
            $type: UserType,
            required: true
          },
          password: {
            $type: String,
            required: true,
            description: 'password...'
          }
        },
        outputFields: {
          changedUser: UserType
        },
        mutateAndGetPayload: async (args, context, info) => {
          const { User } = context.models
          const user = await User.findById(args.id)
          if (user === null) throw new Error('no user found')
          delete args.id

          if (user) {
            await user.update(args)
          }

          return {
            changedUser: user
          }
        }
      },
    },
    links: {}
  }
}