我为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"
有人可以告诉我我的代码有什么问题吗?
答案 0 :(得分:0)
我有一个解决方案: 当我不使用typescript-sequelize框架时,我必须使用其他类型的类。 sequelize-typescript框架使用注释。如果涉及到IBM DB2 sequelize连接器,则不可能:
下面是我必须实现的类,我意识到自己这种类型的代码与打字稿标准没有太大关系:
import { Model, DataTypes, BuildOptions } from 'sequelize-ibmi';
import * as config from 'config';
import * as atob from 'atob';
import { LoggerSligro } from '../../api/utils/logger';
const Sequelize = require('sequelize-ibmi');
//IBM AS400 (let op gebruikersnaam in hoofdletters, ACS met ODBC moet geinstalleerd zijn inclusief sequalize-ibmi en odbc van npm lib)
export const sequelize = new Sequelize({
dialect: 'ibmi',
odbcConnectionString: 'DSN=SLIGRO20;UID=PITTENSB;PWD=bwnnlbs197',
define: {
timestamps: false
}
});
export class Afdeling extends Model {
public afdeling!: string;
public groep1!: string;
}
Afdeling.init({
afdeling: {
type: DataTypes.STRING,
allowNull: false
},
groep1: {
type: DataTypes.STRING
}
}, { sequelize});
export class Activiteit extends Model {
public activiteit: string;
public afdelingId: number;
}
Activiteit.init({
activiteit: {
type: DataTypes.STRING,
allowNull: false
},
afdelingId : {
type: DataTypes.INTEGER,
allowNull: false,
/* references: {
model: Afdeling,
key: 'id'
} */
},
}, { sequelize});
Afdeling.hasMany(Activiteit, {foreignKey: 'afdelingId', sourceKey: 'id'});
Activiteit.belongsTo(Afdeling, {foreignKey: 'afdelingId', targetKey: 'id'});