gpt4 book ai didi

node.js - 如何扩展Sequelize (v5) 的数据类型?

转载 作者:行者123 更新时间:2023-12-03 22:35:56 25 4
gpt4 key购买 nike

我试着像官方文档那样做。
https://sequelize.org/v5/manual/data-types.html#extending-datatypes

但是好像没有调用toSql函数。

我究竟做错了什么?

const Sequelize = require("sequelize");

function sequelizeAdditions(Sequelize) {
const DataTypes = Sequelize.DataTypes;
class NEWTYPE extends DataTypes.ABSTRACT {
toSql() {
console.log("run toSql -------------");
return "INTEGER(11)";
}
}

DataTypes.NEWTYPE = NEWTYPE;

DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = "NEWTYPE";
Sequelize.NEWTYPE = Sequelize.Utils.classToInvokable(DataTypes.NEWTYPE);
};

sequelizeAdditions(Sequelize);

const db = new Sequelize({
dialect: "sqlite",
storage: "./db.sqlite"
});

const table = db.define("test", {
data: {
type: Sequelize.NEWTYPE
}
});

table.sync({alter: true});

最佳答案

也许这是一个错误(从 5.21.5 版开始)。
我在这里找到了一个临时解决方案。 https://github.com/sequelize/sequelize/issues/11177#issuecomment-596244206

一个工作示例:

const Sequelize = require("sequelize");
const { classToInvokable } = require('sequelize/lib/utils/classToInvokable');

function sequelizeAdditions(Sequelize) {
const DataTypes = Sequelize.DataTypes;
// Note here
const ABSTRACT = DataTypes.ABSTRACT.prototype.constructor;

class NEWTYPE extends ABSTRACT {
toSql() {
console.log("run toSql -------------");
return "INTEGER(11)";
}
}

DataTypes.NEWTYPE = NEWTYPE;

DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = "NEWTYPE";
Sequelize.NEWTYPE = Sequelize.Utils.classToInvokable(DataTypes.NEWTYPE);
};

sequelizeAdditions(Sequelize);

const db = new Sequelize({
dialect: "sqlite",
storage: "./db.sqlite"
});

const table = db.define("test", {
data: {
type: Sequelize.NEWTYPE
}
});

table.sync({alter: true});

关于node.js - 如何扩展Sequelize (v5) 的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60615291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com