Sequelize : MODEL.bulkCreate() - Is it possible to pass custom options like MODEL.Create()?

25 Views Asked by At

I'm building a table manager, so I have columns, rows and cells defined in my DB to store the table structure and datas.

I'm using hooks to automatically create cells when a row is added to the table.

Usage of the Model.create() with custom options :

const createOneWithDatasToImport = async(body,datas=undefined,colsDefs=undefined)=>{
  try {
    return await models.Table_Rows.create(body,{dataToImport:datas,columnsDefinitions:colsDefs});
  } catch (error) {
    throw new Error("Error | Logic | Table_Rows | createOneWithDatasToImport | " + error);
  }
}

afterCreate hook receiving the options :

sequelize.models.Table_Rows.afterCreate(async (row,options) => {
        const { createOne:createCell} = require('../../logic/tables/table_data_cells.logic');

        const dataRow = options.dataToImport;
        const colDefsFromImport = options.columnsDefinitions;

        //We need to create one cell for each column of the table
        if(!(dataRow === undefined || colDefsFromImport === undefined)){
            //row.TableDataId
            //We have datas to import to imported columns, so we fill the new cells with the data
            return Promise.all(
                colDefsFromImport.map(async(_colDef) => {
                    new Promise(async(resolve,reject) => {
                        if(_colDef.importOptions.createNewColumn || _colDef.importOptions.importToExistingCol){
                            var _cell = {
                                datas:'',
                                TableDataId:row.TableDataId,
                                TableColumnId:_colDef.importOptions.columnDef._id,
                                TableRowId:row._id,
                                status:"None",
                                wordsCount:0
                            }
                            if(dataRow[_colDef.importOptions.name]){
                                _cell.datas = dataRow[_colDef.importOptions.name]
                            }
                            if(dataRow[_colDef.importOptions.name+'_WC']){
                                _cell.wordsCount = dataRow[_colDef.importOptions.name+'_WC']
                            }
                            await createCell(_cell).then(()=>{resolve();})
                        }
                        else resolve();
                    })
                })
            )
        }
        else{
            //Basic row creation
            return await row.getTable_Data().then(async(table_data) => {
                await table_data.getTable_Columns().then(async(cols) => {
                    Promise.all(
                        cols.map(async(col) => {
                            new Promise(async(resolve,reject) =>{
                                var _cell = {
                                    datas:'',
                                    TableDataId:table_data._id,
                                    TableColumnId:cols._id,
                                    TableRowId:_createdRow._id,
                                    status:"None",
                                    wordsCount:0
                                }
                                await createCell(_cell).then(()=>{resolve();})
                            })
                        })
                    )
                })
            })
        }
    })

As you can see in the code above, I give "dataToImport" and "columnsDefinitions" to the options object when I create a row with datas to import to the cells. It works perfectly.

Now, what I want to do is to be able to use the bulkCreate() method and keep my hook running well. Does anyone knows if it's possible to pass custom options, for each row in the array of the bulkCreate() so my hook could run well and does what he has to do?

I've been searching a solution on the web but didn't find anything relevant.

I think I can bypass this issue by adding fields to my table Table_Rows to store related datas in order to use it later in my hook but I think this is not the best way to do it and I wish you guys to share your experience to inform me if I can do it another way?

Regards.

0

There are 0 best solutions below