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

@hatchifyjs/sequelize-create-with-associations

v0.6.6

Published

![build workflow](https://github.com/bitovi/sequelize-create-with-associations/actions/workflows/build.yml/badge.svg) <span class="badge-npmversion"><a href="https://npmjs.org/package/@hatchifyjs/sequelize-create-with-associations" title="View this projec

Downloads

4,464

Readme

Sequelize Create With Associations

build workflow

@hatchifyjs/sequelize-create-with-associations is a handy package that extends Sequelize's to simplify the associations.

Key Features

  • Create associations without providing an includes declaration
  • Create associations with update

Need help or have questions?

This project is supported by Bitovi, a Node consultancy. You can get help or ask questions on our:

Or, you can hire us for training, consulting, or development. Set up a free consultation.

Getting Started

In this example, we'll be using sqlite3 as an in-memory data store, but you can use any data store supported by Sequelize.

We'll start by installing dependencies.

npm i sequelize @hatchifyjs/sequelize-create-with-associations
npm i sqlite3 -D

Now we're going to setup our Sequelize instance and setup our plugin.

const { Sequelize, DataTypes } = require("sequelize");
const { extendSequelize } = require("@hatchifyjs/sequelize-create-with-associations");

extendSequelize(Sequelize);

const sequelize = new Sequelize("sqlite::memory:", {
  logging: false,
});

Now create some Models to represent your data.

const User = sequelize.define("User", {
  name: DataTypes.STRING,
});

const Skill = sequelize.define("Skill", {
  name: DataTypes.STRING,
});

const UserSkill = sequelize.define("UserSkill", {
  userId: DataTypes.INTEGER,
  skillId: DataTypes.INTEGER,
  selfGranted: DataTypes.BOOLEAN,
});

User.belongsToMany(Skill, {
  as: "skills",
  foreignKey: "userId",
  through: UserSkill,
});

Skill.belongsToMany(User, {
  as: "users",
  foreignKey: "skillId",
  through: UserSkill,
});

Finally, we will use sequelize's sync method to migrate our Model schemas to the database.

Note: this approach should probably not be used in production applications. Consider using migrations instead.

await sequelize.sync();

Now we're ready to start leveraging this plugin. Try creating a user with a skill.

await User.create({
  name: "Justin",
  skills: [{ name: "Programming" }],
});

Or update a record with associations.

await User.update(
  {
    name: "Kevin",
    skills: [{ id: 7 }],
  },
  { where: { id: 1 } },
);

Or even bulk create a record with associations.

await User.bulkCreate([
  {
    name: "John",
    skills: [{ id: 7 }],
  },
  {
    name: "Jane",
    skills: [{ name: "Gaming", through: { selfGranted: true } }],
  },
]);

Now let's put that all together.

const { Sequelize, DataTypes } = require("sequelize");
const { extendSequelize } = require("@hatchifyjs/sequelize-create-with-associations");

// extend Sequelize
extendSequelize(Sequelize);

// create your Sequelize instance
const sequelize = new Sequelize("sqlite::memory:", {
  logging: false,
});

// define your models
const User = sequelize.define("User", {
  name: DataTypes.STRING,
});

const Skill = sequelize.define("Skill", {
  name: DataTypes.STRING,
});

const UserSkill = sequelize.define("UserSkill", {
  userId: DataTypes.INTEGER,
  skillId: DataTypes.INTEGER,
  selfGranted: DataTypes.BOOLEAN,
});

User.belongsToMany(Skill, {
  as: "skills",
  foreignKey: "userId",
  through: UserSkill,
});

Skill.belongsToMany(User, {
  as: "users",
  foreignKey: "skillId",
  through: UserSkill,
});

(async function main() {
  // sync schemas to DB
  await sequelize.sync();

  // seed some data
  const cooking = await Skill.create({ name: "Cooking" });

  // create a record and associate existing data or create data on the fly
  const justin = await User.create({
    name: "Justin",
    skills: [{ name: "Programming" }, { id: cooking.id }],
  });

  // Update a record and setup an association
  await User.update(
    {
      name: "Kevin",
      skills: [{ id: cooking.id }],
    },
    { where: { id: justin.id } },
  );

  // Bulk create some records with associations
  await User.bulkCreate([
    {
      name: "John",
      skills: [{ id: cooking.id }],
    },
    {
      name: "Jane",
      skills: [{ name: "Gaming", through: { selfGranted: true } }],
    },
  ]);
})().catch((err) => console.error(err));