【问题标题】:Is it possible to use Sequelize typescript in NodeJS without using sequelize-typescript framework?是否可以在不使用 sequelize-typescript 框架的情况下在 NodeJS 中使用 Sequelize typescript?
【发布时间】: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"

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

【问题讨论】:

标签: node.js sequelize.js sequelize-typescript


【解决方案1】:

我有一个解决方案: 当我不使用 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'});


【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2020-05-17
    • 2017-04-06
    • 1970-01-01
    • 2020-02-04
    • 2020-10-23
    • 2016-08-11
    • 2020-01-29
    • 2020-03-24
    相关资源
    最近更新 更多