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

safe-auth-sequelize

v0.1.4

Published

Sequelize storage backend for safe-auth.

Downloads

5

Readme

Build Status codecov npm version

What's safe-auth-sequelize

It's Sequelize adapter for safe-auth. So it enables Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server database engines for safe-auth as its storage backend. It's written in TypeScript with %100 test code coverage.

Requirements

safe-auth-sequelize needs and is tested against Sequelize 5.1 or higher

How to use

It provides a function that should be called with an instance of sequelize:

import {SequelizeAccessToken, SequelizeUser} from 'safe-auth-sequelize';

Models should get initialized (like any other sequelize model) before being used. Initialization can be done explicitly:

SequelizeUser.init({}, {sequelize});
SequelizeAccessToken.init({}, {sequelize});
SequelizeUser.associate(sequelize.models);
SequelizeAccessToken.associate(sequelize.models);

or if there's a script that automates this process for all sequelize models, it should work for these two models too and manual initialization won't be required.

Extending models

Most of the time it's needed to extend the SequelizeUser model (or even the SequelizeAccessToken model), to do so new classes should be defined extending the code classes of safe-auth-sequelize. Also the core class shouldn't get initialized and the initialization should happen for the inherited class instead.

For example here a User class is inheriting from the core SequelizeUser class. It's connected to a Organization model.

import {SequelizeAccessToken, SequelizeUser} from 'safe-auth-sequelize';

class Organization extends Sequelize.Model {
    public id!: number;
    public name!: string;
}
Organization.init({
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
    },
    name: {
        type: new DataTypes.STRING(128),
        allowNull: false,
    },
}, {sequelize})
// Organization doesn't have an associate method

class User extends SequelizeUser {
    public firstname!: string;
    public lastname!: string;
    public organization!: Organization // A class in your codebase

    public static associate(models: {
        User?: typeof SequelizeUser;
        AccessToken?: typeof SequelizeAccessToken;
    }): void {
        super.associate();
        SequelizeAccessToken.belongsTo(Organization, {
            as: 'organization',
            foreignKey: 'organizationId',
            targetKey: 'id',
        });
    }
}

User.init({
    firstname: {
        type: new DataTypes.STRING(128),
        allowNull: false,
    },
    lastname: {
        type: new DataTypes.STRING(128),
        allowNull: false,
    },
}, {sequelize})
SequelizeAccessToken.init({}, {sequelize});
User.associate(sequelize.models);
SequelizeAccessToken.associate(sequelize.models);

Note that you don't need to define internal fields of safe-auth-sequelize, you just need to define your own custom fields in the init function call (here firstname and lastname).

Migrations

We provide a sample migration in the github repository, you can download it to your migrations directory. You need to modify it in case you're extending (inheriting from) the default models.

Reporting bugs

You can report issues/bugs in the github repository of the project: https://github.com/evenset/safe-auth-sequelize/issues