首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可能在NodeJS中使用Sequelize typescript而不使用sequelize-typescript框架?

有没有可能在NodeJS中使用Sequelize typescript而不使用sequelize-typescript框架?
EN

Stack Overflow用户
提问于 2019-12-24 19:10:29
回答 1查看 2.9K关注 0票数 1

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

代码语言:javascript
复制
import { Table, Column, Model, HasMany, AllowNull } from 'sequelize-typescript';

@Table
export class Locatie extends Model<Locatie> {

  @AllowNull(false)
  @Column
  locatie: string;
  .........
  ......... 
}

我的第一个问题是,在使用'sequelize‘npm库和'@types/sequelize’时,是否可以使用typescript编码?(这意味着我们必须排除sequelize-typescript框架)

在我的代码中,我不能让它运行,这是我的初始化连接和启动数据库的代码:(到mssql数据库的连接是没有问题的,但是如果我想创建一个表,我会得到一个错误)

代码语言:javascript
复制
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

代码语言:javascript
复制
//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中:

代码语言:javascript
复制
"@types/sequelize": "^4.27.48",
"sequelize": "^5.7.6",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"

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

EN

回答 1

Stack Overflow用户

发布于 2020-01-01 01:47:39

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

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

代码语言:javascript
复制
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'});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59467917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档