sequelize-typescript

注意:

  • 注意关联的名称 例如hobbies在使用HasMany 会生成createHobby关联方法 建议根据关联来起名称 对多就是负数 对一就是单数
  • 关联关系(@BelongsToMany,@HasMany... )不要加@Column
  • 加了关联关系的类才能使用对应的关联关系操作 没关联的类不能因为被关联而使用关联操作

常见操作

  • CURD
  • c => Model.create();
  • u => Model.update(); instance.save(); instance.update();
  • r =>
    1535300288894.png
  • d => Model.destory();instance.destory();

表、列配置信息

  • 表常用配置:
    1535300562473.png
  • 数据库常用配置:
    1535300980622.png
  • 字段常用配置

    • 主键:(注意 @Column一定要在下面)
      1535301033927.png
  • 外键:
    1535301085507.png
  • 自定义验证:
    1535301987646.png
  • 钩子:
    1535302147260.png
    • 当一条数据被创建或者更新时可以调用这些钩子函数
    • 钩子函数必须是静态函数

关联关系

  1. one to many
@Table
export default class Person extends Model {

  @Column
  name: string;

  @Column
  birthday: Date;

  @HasMany(() => Hobby,"p_id")
  hobbies: Hobby[];
}

@Table
export default class Hobby extends Model {

    @Column
    name: string;

    @ForeignKey(()=>Person)
    @AllowNull
    @Column({field:"p_id"})
    pId: string;

    @BelongsTo(() => Person)
    person: Person;
}

 const pModel = await Person.create({ name: "vijay", birthday: new Date() });
// create
const hModel = await pModel.$create("Hobby", { name: "sport" });
// add
const h = Hobby.build({ name: "sport2" });
await h.save();
await pModel.$add("Hobby",h);

// 常用关联方法
// modelA.$set('Hobbies', [ /* instance */]).then( /* ... */);
// modelA.$add('Hobby'|'Hobbies', /* instance */).then( /* ... */);
// modelA.$get('Hobbies').then( /* ... */);
// modelA.$count('Hobbies').then( /* ... */);
// modelA.$has('Hobbies').then( /* ... */);
// modelA.$remove('Hobby'|'Hobbies', /* instance */ ).then( /* ... */);
// modelA.$create('Hobby', /* value */ ).then( /* ... */);
  1. one to one
@Table
export default class Hobby extends Model {

    @Column
    name: string;

    @ForeignKey(()=>Person)
    @AllowNull
    @Column({field:"p_id"})
    pId: string;

    @BelongsTo(() => Person)
    person: Person;
}

@Table
export default class Person extends Model {

  @Column
  name: string;

  @Column
  birthday: Date;

  @HasOne(() => Hobby,"p_id")
  hobby: Hobby;
}
  1. many to many

    @Table
    export default class Person extends Model {
        @PrimaryKey
        @AutoIncrement
        @Column
        id: number;
    
        @Column
        name: string;
    
        @Column
        birthday: Date;
    
        @BelongsToMany(() => Hobby,()=>HobbyPerson)
        hobbies: Hobby[];
    }
    
    @Table
    export default class HobbyPerson extends Model{
        @PrimaryKey
        @AutoIncrement
        @Column
        id:number;
    
        @ForeignKey(()=>Hobby)
        @AllowNull
        @Column({field:"h_id"})
        hId:number;
    
        @ForeignKey(()=>Person)
        @AllowNull
        @Column({field:"p_id"})
        pId:number;
    }
    
    @Table
    export default class Person extends Model {
        @PrimaryKey
        @AutoIncrement
        @Column
        id: number;
    
        @Column
        name: string;
    
        @Column
        birthday: Date;
    
        @BelongsToMany(() => Hobby,()=>HobbyPerson)
        hobbies: Hobby[];
    }
    

    原生sequelize迁移

  • 将sequelize换成从sequelize-typescript 中导出的

    import { Sequelize } from 'sequelize-typescript';
    // import * as Sequelize from 'sequelize'
    
  • 将后续用sequelize-typescript写的模型类加入到sequlize实例中

    import Car from '../models/Car';
    
    const db = new Sequelize({
        dialect: 'mysql',
        operatorsAliases: true, //是否识别字段别名中的下划线
        database: 'shaliang',
        username: 'root',
        password: 'root',
        define: {
            timestamps: true,//开启时间戳 create_at delete_at update_at
            paranoid: true,//开启假删除
            underscored: true,//下划线
            charset: 'utf8',
            freezeTableName: true//固定表名为单数  默认表名是xxxs
        },
        pool: {
            max: 10,
            min: 0,
            acquire: 30000,
            idle: 10000
        },
        timezone: '+08:00',//更改为北京时区
        // modelPaths: [__dirname + '/../models'] //模型文件地址
    });
    db.addModels([Car]);
    

你可能感兴趣的:(sequelize-typescript)