如何在Sequelize中批量插入到多个表

时间:2018-12-22 13:56:22

标签: javascript node.js postgresql sequelize.js

在我的POSTGRESQL数据库中,我有以下表格(3):

*) transaction_status
+----------------------+----------------------------------------+
| status_id            | status_text                            |
+----------------------+----------------------------------------+
| *this is primary key | *this value always unique and non-null |
+----------------------+----------------------------------------+

然后

*) transactions
+----------------------+-----------------------------+------------------+-------------------------------------------------------------------+
| id                   | transaction_title           | transaction_date | transaction_status                                                |
+----------------------+-----------------------------+------------------+-------------------------------------------------------------------+
| *this is primary key | *this value always non-null |                  | *this is foreign key that refer to transaction_status.status_text |
+----------------------+-----------------------------+------------------+-------------------------------------------------------------------+

最后

*) transaction_destination
+----------------------+----------------------------------------------------+-----------------------------+
| id                   | transaction_id                                     | transaction_destination     |
+----------------------+----------------------------------------------------+-----------------------------+
| *this is primary key | *this is foreign key that refer to transactions.id | *this value always non-null |
+----------------------+----------------------------------------------------+-----------------------------+

这是这些表之间的关系:

> ONE transaction_status => MANY transactions
> ONE transactions => MANY transaction_destination

我的目标目标是使用 Sequelize 编写NodeJS脚本,该脚本将使用单个JSON数据结构一次填充这3个表。

这是我的JSON数据结构:

const TData = [
    {
        transaction_title: 'Title here',
        transaction_date: '2018-12-12',
        transaction_status: 'Status A',
        transaction_destination: [
            'Destination 01',
            'Destination 02',
            'Destination 03',
        ]
    },
    {
        transaction_title: 'Another title here',
        transaction_date: '2018-12-13',
        transaction_status: 'Status A',
        transaction_destination: [
            'Destination 03',
            'Destination 04',
            'Destination 05',
        ]
    }
]

我已经已经定义的是:

const TransactionStatus = db.define('transaction_status', {
    statusId: { // Primary Key
        type: Sequelize.INTEGER(2),
        field: 'status_id',
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
    statusText: { // Must be Unique
        type: Sequelize.STRING(45),
        field: 'status_text',
        allowNull: false,
        unique: true,
        validate: {
            notEmpty: true,
        }
    }
});

const Transactions = db.define('transactions', {
    id: { // Primary Key
        type: Sequelize.BIGINT(7),
        field: 'id',
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
    transactionTitle: {
        type: Sequelize.STRING(100),
        field: 'transaction_title',
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
    transactionDate: {
        type: Sequelize.DATEONLY,
        field: 'transaction_date',
        allowNull: false,
        validate: {
            notEmpty: true,
            isDate: true,
            isAfter: "2011-11-05"
        }
    },
    transactionStatus: { // Foreign Key
        type: Sequelize.STRING(25),
        field: 'transaction_status',
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    }
});

const TransactionDestination = db.define('transaction_destination', {
    id: { // Primary Key
        type: Sequelize.BIGINT(15),
        field: 'id',
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
    transactionId: { // Foreign Key
        type: Sequelize.BIGINT(7),
        field: 'transaction_id',
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
    transactionDestination: { //
        type: Sequelize.STRING(100),
        field: 'transaction_destination',
        allowNull: false,
        validate: {
            notEmpty: true,
        }
    },
});

所以,我想在脚本中做的是

db.sync({
        force: true
    })
    .then(() => {
        console.log('DB was synced and dropped old data');
    })
    .then(() => {
        // Do something that will populate all 3 tables using existing JSON data structure, i.e. TData
    })

但这是一些约束

  

请注意,在TData JSON中,未提供transaction_status.status_idtransactions.idtransaction_destination.idtransaction_destination.transaction_id的值,因为它们是PK或FK。

     

提供的TData仅包含2个transactions数据,但实际上它有1000个transactions行/点。

请帮助,这是我第一次使用续集。 谢谢。

0 个答案:

没有答案