简体   繁体   中英

How to use import inside of sequelize model?

I have a model called events.js where Im trying to import a helper function from a utilities file. Im not sure what Im doing wrong, perhaps im missing a loader? or this might not be possible?

the code:

import { getIdField } from 'utils/utilities';

module.exports = function(sequelize, DataTypes) {
  return sequelize.define(
    'events',
    {
      Id: getIdField(),

      Name: {
        type: DataTypes.STRING(150),
        allowNull: false,
        comment: 'null',
      },
      HouseId: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        comment: 'null',
        references: {
          model: 'house',
          key: 'Id',
        },
      },
      Date: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: 'null',
      },
      Active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        comment: 'null',
      },
    },
    {
      tableName: 'events',
    }
  );
};

the error:

SyntaxError: Unexpected token {

在此处输入图像描述

my tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "src",
    "resolveJsonModule": true
  },
  "lib": ["es2015"],
  "include": ["src"]
}

the exported function:

export const getIdField = (name = 'Id') => ({
// return cool stuff
});

Any ideas?

This should work

const utility = require('utils/utilities'); 
utility.getIdField();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM