Mar*_*ard 7 node.js sequelize.js typescript
我正在创建一个sequelize存储库,并且我有一个扩展sequelize模型的对象。
我能够执行以下操作将模型保存到数据库。
repository.create(myModel) // myModel being an instance of MyModel which extends sequelize model
Run Code Online (Sandbox Code Playgroud)
现在我在打字稿中收到以下错误:
Argument of type 'MyModel' is not assignable to parameter of type 'CreationAttributes<MyModel>'.
Type 'MyModel' is not assignable to type 'Omit<any, string>'.
Index signature for type 'number' is missing in type 'MyModel'.ts(2345)
Run Code Online (Sandbox Code Playgroud)
我正在做一些搜索,建议添加到 MyModel:
[key: number]: number;
Run Code Online (Sandbox Code Playgroud)
当我这样做时,错误更改为以下内容:
Argument of type 'MyModel' is not assignable to parameter of type 'CreationAttributes<MyModel>'.
Type 'MyModel' is not assignable to type 'Omit<any, string>'.
Index signature for type 'symbol' is missing in type 'MyModel'.ts(2345)
Run Code Online (Sandbox Code Playgroud)
我可以通过更改创建调用来解决该错误:
repository.create({...myModel})
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我指出有关ts(2345)
它是什么的文档,也许如何忽略它。或者以正确的方式解决问题?
在我看来,使用扩展运算符似乎有点混乱,但如果这是正确的解决方案,那就没问题了。
型号定义:
import {
Column, DataType, ForeignKey, Model, Sequelize, Table,
} from 'sequelize-typescript';
import MediaResource from './media_resource';
import User from './users';
@Table({
tableName: 'videos',
timestamps: true,
version: true,
})
export default class Video extends Model {
@Column({
primaryKey: true,
autoIncrement: true,
type: DataType.INTEGER,
defaultValue: Sequelize.literal("nextval('videos_id_seq'::regclass)"),
})
id?: number;
@ForeignKey(() => MediaResource)
@Column({
field: 'media_resource_id',
allowNull: false,
type: DataType.INTEGER,
})
mediaResourceId?: number;
@Column({
allowNull: false,
type: DataType.STRING(191),
})
title?: string;
@Column({
allowNull: false,
type: DataType.STRING(2000),
})
url?: string;
@Column({
allowNull: true,
type: DataType.STRING(512),
})
description?: string;
@Column({
allowNull: false,
type: DataType.BOOLEAN,
})
is_3d?: boolean;
@Column({
allowNull: false,
type: DataType.BOOLEAN,
})
is_360?: boolean;
@ForeignKey(() => User)
@Column({
field: 'user_id',
allowNull: false,
type: DataType.INTEGER,
})
userId?: number;
@Column({
field: 'created_at',
allowNull: false,
type: DataType.DATE,
})
createdAt?: Date;
@Column({
field: 'updated_at',
allowNull: false,
type: DataType.DATE,
})
updatedAt?: Date;
}
Run Code Online (Sandbox Code Playgroud)
存储库创建签名:
const db = DB.getInstance();
const videosRepository = db.getRepository(Video);
const transaction = await db.transaction();
try {
const saved = await videosRepository.create({ ...video }, { transaction });
await transaction.commit();
return saved;
} catch (err) {
await transaction.rollback();
if (err instanceof Error) logger.error(err.message);
throw new InternalServerError();
}
Run Code Online (Sandbox Code Playgroud)
我们有一个 Sequelize 单例,这就是 db,视频对象是从快速请求主体创建的。
这也曾经有效,我可以添加 ts-ignore 注释,它会起作用。
续集:6.15.1续集打字稿:2.1.2打字稿:4.5.5
所以我遇到了这个问题的两种解决方案,第一个是如上所述:
repository.create({ ...myModel })
第二个是:
repository.create(myModel as any)
这是我的解决方案......
export default class Video extends Model<InferAttributes<Video>, InferCreationAttributes<Video>> {
https://sequelize.org/master/manual/typescript.html
归档时间: |
|
查看次数: |
1393 次 |
最近记录: |