sequelize-7

📁 totophe/skills 📅 10 days ago
3
总安装量
3
周安装量
#62528
全站排名
安装命令
npx skills add https://github.com/totophe/skills --skill sequelize-7

Agent 安装分布

amp 3
claude-code 3
github-copilot 3
codex 3
kimi-cli 3
gemini-cli 3

Skill 文档

Sequelize v7 — Claude Skill

Key Differences from Sequelize v6

  • Package renamed: sequelize → @sequelize/core
  • Dialects are separate packages (e.g., @sequelize/postgres, @sequelize/sqlite3)
  • Decorator-based model definitions (recommended over Model.init())
  • Constructor only accepts a single options object (no URL string as first arg)
  • dialectOptions removed — settings go in top-level options
  • CLS transactions enabled by default (no manual setup needed)
  • Default association names are now camelCase (userId not UserId)
  • sequelize.transaction() only creates managed transactions; use sequelize.startUnmanagedTransaction() for unmanaged
  • JSON null vs SQL NULL distinction: inserting null into JSON stores JSON 'null', not SQL NULL
  • Minimum: Node >= 18, TypeScript >= 5.0

Skill Files

File Contents
getting-started.md Installation, connection, TypeScript setup, logging
models.md Model definitions, data types, decorators, timestamps, naming, validation, indexes
querying.md CRUD operations, operators, WHERE clauses, raw SQL, JSON querying, subqueries
associations.md HasOne, HasMany, BelongsTo, BelongsToMany, eager loading
advanced.md Transactions, hooks, scopes, connection pool, paranoid models, optimistic locking, read replication, migrations

Quick Reference — Common Patterns

Minimal Model (TypeScript + Decorators)

import { Model, InferAttributes, InferCreationAttributes, CreationOptional, DataTypes } from '@sequelize/core';
import { Attribute, PrimaryKey, AutoIncrement, NotNull } from '@sequelize/core/decorators-legacy';

export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  @Attribute(DataTypes.INTEGER)
  @PrimaryKey
  @AutoIncrement
  declare id: CreationOptional<number>;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare name: string;

  @Attribute(DataTypes.STRING)
  declare email: string | null;

  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

Initialize Sequelize

import { Sequelize } from '@sequelize/core';
import { PostgresDialect } from '@sequelize/postgres';

const sequelize = new Sequelize({
  dialect: PostgresDialect,
  host: 'localhost',
  port: 5432,
  database: 'mydb',
  user: 'user',
  password: 'pass',
  models: [User],
});

Basic CRUD

// Create
const user = await User.create({ name: 'Alice' });

// Read
const users = await User.findAll({ where: { name: 'Alice' } });
const one = await User.findByPk(1);

// Update
await user.update({ name: 'Bob' });
await User.update({ name: 'Bob' }, { where: { id: 1 } });

// Delete
await user.destroy();
await User.destroy({ where: { id: 1 } });

Association with Eager Loading

const posts = await Post.findAll({
  include: ['comments'],
  where: { authorId: 1 },
});

Managed Transaction

const result = await sequelize.transaction(async () => {
  const user = await User.create({ name: 'Alice' });
  await Profile.create({ userId: user.id, bio: 'Hello' });
  return user;
});