@hatchifyjs/sequelize-create-with-associations v0.6.6
Sequelize Create With Associations
@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));
7 months ago
8 months ago
10 months ago
10 months ago
10 months ago
7 months ago
7 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago