注意:
- 注意關(guān)聯(lián)的名稱 例如
hobbies
在使用HasMany
會(huì)生成createHobby
關(guān)聯(lián)方法建議根據(jù)關(guān)聯(lián)來(lái)起名稱 對(duì)多就是負(fù)數(shù) 對(duì)一就是單數(shù)
- 關(guān)聯(lián)關(guān)系(
@BelongsToMany
,@HasMany
... )不要加@Column
- 加了關(guān)聯(lián)關(guān)系的類才能使用對(duì)應(yīng)的關(guān)聯(lián)關(guān)系操作 沒(méi)關(guān)聯(lián)的類不能因?yàn)楸魂P(guān)聯(lián)而使用關(guān)聯(lián)操作
常見(jiàn)操作
- CURD
- c => Model.create();
- u => Model.update(); instance.save(); instance.update();
-
r =>
- d => Model.destory();instance.destory();
表、列配置信息
-
表常用配置:
-
數(shù)據(jù)庫(kù)常用配置:
-
字段常用配置
-
主鍵:(注意 @Column一定要在下面)
-
主鍵:(注意 @Column一定要在下面)
-
外鍵:
-
自定義驗(yàn)證:
-
鉤子:
- 當(dāng)一條數(shù)據(jù)被創(chuàng)建或者更新時(shí)可以調(diào)用這些鉤子函數(shù)
- 鉤子函數(shù)必須是靜態(tài)函數(shù)
關(guān)聯(lián)關(guān)系
- one to many
@Table
export default class Person extends Model<Person> {
@Column
name: string;
@Column
birthday: Date;
@HasMany(() => Hobby,"p_id")
hobbies: Hobby[];
}
@Table
export default class Hobby extends Model<Hobby> {
@Column
name: string;
@ForeignKey(()=>Person)
@AllowNull
@Column({field:"p_id"})
pId: string;
@BelongsTo(() => Person)
person: Person;
}
const pModel = await Person.create<Person>({ name: "vijay", birthday: new Date() });
// create
const hModel = await pModel.$create<Hobby>("Hobby", { name: "sport" });
// add
const h = Hobby.build({ name: "sport2" });
await h.save();
await pModel.$add<Hobby>("Hobby",h);
// 常用關(guān)聯(lián)方法
// 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( /* ... */);
- one to one
@Table
export default class Hobby extends Model<Hobby> {
@Column
name: string;
@ForeignKey(()=>Person)
@AllowNull
@Column({field:"p_id"})
pId: string;
@BelongsTo(() => Person)
person: Person;
}
@Table
export default class Person extends Model<Person> {
@Column
name: string;
@Column
birthday: Date;
@HasOne(() => Hobby,"p_id")
hobby: Hobby;
}
-
many to many
@Table export default class Person extends Model<Person> { @PrimaryKey @AutoIncrement @Column id: number; @Column name: string; @Column birthday: Date; @BelongsToMany(() => Hobby,()=>HobbyPerson) hobbies: Hobby[]; } @Table export default class HobbyPerson extends Model<HobbyPerson>{ @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<Person> { @PrimaryKey @AutoIncrement @Column id: number; @Column name: string; @Column birthday: Date; @BelongsToMany(() => Hobby,()=>HobbyPerson) hobbies: Hobby[]; }
原生sequelize遷移
-
將sequelize換成從sequelize-typescript 中導(dǎo)出的
import { Sequelize } from 'sequelize-typescript'; // import * as Sequelize from 'sequelize'
-
將后續(xù)用sequelize-typescript寫(xiě)的模型類加入到sequlize實(shí)例中
import Car from '../models/Car'; const db = new Sequelize({ dialect: 'mysql', operatorsAliases: true, //是否識(shí)別字段別名中的下劃線 database: 'shaliang', username: 'root', password: 'root', define: { timestamps: true,//開(kāi)啟時(shí)間戳 create_at delete_at update_at paranoid: true,//開(kāi)啟假刪除 underscored: true,//下劃線 charset: 'utf8', freezeTableName: true//固定表名為單數(shù) 默認(rèn)表名是xxxs }, pool: { max: 10, min: 0, acquire: 30000, idle: 10000 }, timezone: '+08:00',//更改為北京時(shí)區(qū) // modelPaths: [__dirname + '/../models'] //模型文件地址 }); db.addModels([Car]);