所以我已经让它工作了,但是在一个非循环的模型加载器中。我忽略了定义文档,
https://sequelize.org/master/manual/typescript.html#usage-of--code-sequelize-define--code-
这里是冗长的课堂方法:https://sequelize.org/master/manual/typescript.html#usage
我将完成设置 2 个模型及其关联的过程,希望能帮助那些试图将 Typescript 与 Sequelize v5 集成的人。
肯定会喜欢这种方法的反馈。
让我们从用户和相关身份(用于访问 API)的类开始
/src/db/models/user.ts
import { Sequelize, Model, DataTypes, BuildOptions } from 'sequelize';
import { Association, HasManyGetAssociationsMixin, HasManyAddAssociationMixin, HasManyHasAssociationMixin, HasManyCountAssociationsMixin, HasManyCreateAssociationMixin } from 'sequelize';
import { Identity } from './identity';
export class User extends Model {
public id!: string; // Note that the `null assertion` `!` is required in strict mode.
public active!: boolean;
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getIdentities!: HasManyGetAssociationsMixin<Identity>; // Note the null assertions!
public addIdentity!: HasManyAddAssociationMixin<Identity, number>;
public hasIdentity!: HasManyHasAssociationMixin<Identity, number>;
public countIdentities!: HasManyCountAssociationsMixin;
public createIdentity!: HasManyCreateAssociationMixin<Identity>;
// You can also pre-declare possible inclusions, these will only be populated if you
// actively include a relation.
public readonly identities?: Identity[]; // Note this is optional since it's only populated when explicitly requested in code
public static associations: {
identities: Association<User, Identity>;
};
}
export function initUser(sequelize: Sequelize): void {
User.init({
id: {
type: DataTypes.UUID,
primaryKey: true,
},
active: {
type:DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: 'User',
sequelize: sequelize, // this bit is important
});
}
export function associateUser(): void {
// Here we associate which actually populates out pre-declared `association` static and other methods.
User.hasMany(Identity, {
sourceKey: 'id',
foreignKey: 'UserId',
as: 'identities' // this determines the name in `associations`!
});
}
/src/db/models/identity.ts
import { Sequelize, Model, DataTypes, BuildOptions } from 'sequelize';
import { Association, HasOneGetAssociationMixin, HasOneCreateAssociationMixin } from 'sequelize';
import { User } from './user'
import * as bcrypt from "bcryptjs";
export class Identity extends Model {
public id!: string; // Note that the `null assertion` `!` is required in strict mode.
public username!: string;
public password!: string;
public UserId: string;
public active!: boolean;
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getUser!: HasOneGetAssociationMixin<User>; // Note the null assertions!
// You can also pre-declare possible inclusions, these will only be populated if you
// actively include a relation.
public readonly user?: User; // Note this is optional since it's only populated when explicitly requested in code
public static associations: {
user: Association<Identity, User>;
};
public validatePassword(password: string) : boolean {
return bcrypt.compareSync(password, this.password)
}
}
export function initIdentity(sequelize: Sequelize): void {
Identity.init({
id: {
type: DataTypes.UUID,
primaryKey: true,
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false
},
UserId: {
type: DataTypes.UUID,
allowNull: true
},
active: {
type:DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: 'Identity',
sequelize: sequelize, // this bit is important
});
}
export function associateIdentity(): void {
// Here we associate which actually populates out pre-declared `association` static and other methods.
Identity.belongsTo(User, {targetKey: 'id'});
}
因此,在此之后,我们已经声明了与 Sequelize 和数据库相关的所有“虚拟”成员和函数。此外还有 init<model> && associate<model> 函数,它们将用于将所有内容联系在一起。
注意您可能会注意到,在identity.ts 中,在关联中使用的是 UserId 而不是 userId。出于某种原因,它一直假设关联将通过 UserId,即使我使用了 userId。进行查询时,它抱怨没有列 UserId (但 userId )。因此,将其更新为大写“U”解决了它。 我不确定它为什么现在这样做。
现在把它们联系在一起
/src/db/index.ts
import { initUser, associateUser } from "./user";
import { initIdentity, associateIdentity } from "./identity";
const Sequelize = require('sequelize');
const env = process.env.NODE_ENV || 'development';
const config = require(`${__dirname}/../config/config.json`)[env];
interface DB {
[key: string]: any;
}
const sequelize = new Sequelize(config.database, config.username, config.password, config);
initUser(sequelize);
initIdentity(sequelize)
associateUser();
associateIdentity();
const db = {
sequelize,
Sequelize,
User: sequelize.models.User,
Identity: sequelize.models.Identity
}
module.exports = db;
平时要做的模型加载,进入目录,找到所有模型,然后将它们导入到sequelize中。现在就像我之前说的,尝试在模型类中使用 define 会在尝试通过此模型加载器时导致问题,因为非 Typescript 版本总是寻找 *.js 而不是 *.ts。更改为 *.ts 会使 define 调用中的所有内容都崩溃。 (更不用说,因为所有这些代码都会被转译成 js 文件,这不会反过来导致问题吗?)
但是正如你所看到的,我是手动而不是循环执行所有操作。可能有更好的循环方式来做到这一点,但目前这已经足够了。
通过调用它们的init<model> 函数来初始化模型。初始化后通过associate<model>的函数调用创建它们的关联
在启动我的快速服务器之前,我需要索引文件,这一切都开始了。轰隆隆。
关于我的方法的其他注意事项
我不想安装超出我需要的软件包。所以我避开了 sequelize-typescript 和 sequelize-typescript-cli。这意味着我所有的种子文件和迁移文件都需要手工制作,不使用 cli(真的没那么糟糕),并且不是 *.ts 而是 *.js。
示例:
20191017135846-create-identity.js
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable({tableName:'Identity'}, {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
autoIncrement: false,
primaryKey: true,
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
UserId: {
type: Sequelize.UUID,
references: {
model: 'User', // name of Target model
key: 'id', // key in Target model that we're referencing
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
active: {
type: Sequelize.BOOLEAN,
defaultValue: true,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
},
})
},
down: (queryInterface) => {
return queryInterface.dropTable({tableName:'Identity', schema:'public'})
}
}
20191015141822-seed-users.js
'use strict'
var moment = require('moment');
var uuidv4 = require('uuid/v4');
const bcrypt = require('bcryptjs');
module.exports = {
up: async (queryInterface) => {
// User
const user1Id = uuidv4();
await queryInterface.bulkInsert('User',
[
{
id:user1Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
await queryInterface.bulkInsert('Identity',
[
{
id:uuidv4(),
username: "user1",
password: bcrypt.hashSync('password', bcrypt.genSaltSync(10)),
UserId: user1Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
const user2Id = uuidv4();
await queryInterface.bulkInsert('User',
[
{
id:user2Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
await queryInterface.bulkInsert('Identity',
[
{
id:uuidv4(),
username: "user2",
password: bcrypt.hashSync('password', bcrypt.genSaltSync(10)),
UserId: user2Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
const user3Id = uuidv4();
await queryInterface.bulkInsert('User',
[
{
id:user3Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
await queryInterface.bulkInsert('Identity',
[
{
id:uuidv4(),
username: "user3",
password: bcrypt.hashSync('password', bcrypt.genSaltSync(10)),
UserId: user3Id,
createdAt: new Date( moment.utc().format() ),
updatedAt: new Date( moment.utc().format() )
}
],
)
},
down: async (queryInterface) => {
await queryInterface.bulkDelete({ tableName: 'User'}, null, {})
}
}
此时你可以运行哪个
sequelize db:migrate
sequelize db:seed:all
一切正常,可以访问数据库。
现在使用类/打字稿我注意到将模型添加到和导出的 db 对象是多余的......
我可以通过导入访问所需的模型
从“../db/models/user”导入{用户}
或者
要求('./db/models/index')
然后我可以执行 User.findAll() 或使用其他导入 db.User.findAll()