【发布时间】:2020-04-15 11:39:45
【问题描述】:
我有一个用于 sequelize-typescript 实现的有效解决方案。没问题,但我们必须使用数据库 IBM DB2 (AS400)。而且我们不能使用 sequelize-typescript,因为 IBM DB2 接口不支持这样的构造:
import { Table, Column, Model, HasMany, AllowNull } from 'sequelize-typescript';
@Table
export class Locatie extends Model<Locatie> {
@AllowNull(false)
@Column
locatie: string;
.........
.........
}
我的第一个问题是在使用 'sequelize' npm lib 和 '@types/sequelize' 时是否可以使用打字稿编码? (也就是说我们要排除 sequelize-typescript 框架)
在我的代码中我无法让它运行,这是我用于初始化连接和启动数据库的代码: (与 mssql 数据库的连接没有问题,但如果我想创建一个表,则会出现错误)
import { Sequelize } from 'sequelize';
import { Locatie } from '../model/locatie.model';
export class OrmDB {
sequelize = new Sequelize({
host: 'localhost',
port: 1433,
database: 'testabcd',
dialect: 'mssql',
username: 'sa',
password: 'secret',
storage: ':memory:',
logging: false,
define: {
timestamps: false
}
});
initialize() {
this.sequelize
.authenticate()
.then(() => {
console.log('Connection to database is ok');
//create table: When I add this code block that throws an error like this:
// \node_modules\sequelize\lib\model.js:919... throw new Error('No Sequelize instance passed');
Locatie.sync().then(() => {
return Locatie.create({
locatie: 'test',
opmerking: 'verzin'
});
});
//end create table
})
.catch(err => {
console.log('Unable to connect to the database', err);
});
//this runs without a problem but table is not created.
/* this.sequelize.sync().then(() => {
console.log(' It works, table Locatie is NOT created or synced ??');
}); */
}
}
这是我的模型,我尝试使用 require 和 import 被注释掉但没有任何效果。 (文件名是locatie.model.ts
//import { Sequelize } from 'sequelize';
//import * as Sequelize from 'sequelize'
const Sequelize = require('sequelize');
const Model = Sequelize.Model;
export class Locatie extends Model{}
Locatie.init({
locatie: {
type: Sequelize.STRING,
allowNull: false
},
opmerking: {
type: Sequelize.STRING,
},
create_door: {
type: Sequelize.STRING,
},
create_dat: {
type: Sequelize.DATE,
},
wijz_door: {
type: Sequelize.STRING,
},
wijz_dat: {
type: Sequelize.DATE,
}
},{ Sequelize, modelName: 'locatie'});
这是在我的 package.json 中:
"@types/sequelize": "^4.27.48",
"sequelize": "^5.7.6",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
谁能告诉我我的代码有什么问题?
【问题讨论】:
-
我还添加了导入以及类和连接参数
-
Sequelize 不支持 DB2 方言。观看此视频:github.com/sequelize/sequelize/issues/6263
-
IBM DB2 AS400 支持 Sequelize:npmjs.com/package/sequelize-ibmi。但不支持 sequelize-typescript 库。
标签: node.js sequelize.js sequelize-typescript