gpt4 book ai didi

node.js - 是否可以在不使用 sequelize-typescript 框架的情况下在 NodeJS 中使用 Sequelize typescript?

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

我有一个用于 sequelize-typescript 实现的工作解决方案。没问题,但我们必须使用数据库 IBM DB2 (AS400)。而且我们不能使用 sequelize-typescript 因为 IBM DB2 接口(interface)不支持这样的构造:

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' 时是否可以使用 typescript 编码?
(这意味着我们必须排除 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"

谁能告诉我我的代码有什么问题?

最佳答案

我有一个解决方案:
当我不使用 typescript-sequelize 框架时,我必须使用其他类型的类。 sequelize-typescript 框架使用注释。如果涉及到 IBM DB2 sequelize 连接器,这是不可能的:

下面是我必须实现的类,我意识到这种代码与 typescript 标准没有太大关系:

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'});


关于node.js - 是否可以在不使用 sequelize-typescript 框架的情况下在 NodeJS 中使用 Sequelize typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59467917/

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