Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch insert of array data into mysql database using objection.js

I have an array

newData = [{ sId: 'XXXXXX', itemlName: 'LSストレッ', iCode: 'XXXXXX', iType: '', skus: 'XXXXXXX', iLevel: 'L2', cCode: '88', cName: 'Other', sCode: '999', sName: 'No Control', pLengthCode: '988', core: 'N', sCode: '1', dCode: 'XX', gDeptCode: 'XX', gDeptName: 'Women\\\'s Items', rCode: 'jqs' },{ sId: 'XXXXXX', itemlName: 'LSストレッ', iCode: 'XXXXXX', iType: '', skus: 'XXXXXXX', iLevel: 'L2', cCode: '88', cName: 'Other', sCode: '999', sName: 'No Control', pLengthCode: '988', core: 'N', sCode: '1', dCode: 'XX', gDeptCode: 'XX', gDeptName: 'Women\\\'s Items', rCode: 'jqs' }]

I wants to insert newData in to mysql database using objection.js. But when I run my node application , I got the error saying:

Error: batch insert only works with Postgresql

My insert code is -:

samplemodel.query().insert( newData );

How can I perform the batch insertion of array data in mysql data base using objection.js?

like image 839
Johncy Binoy Avatar asked Dec 01 '22 13:12

Johncy Binoy


1 Answers

Error: batch insert only works with Postgresql

Well, that means that you need Postgres's instead of Mysql to make it work.

Edit1:

From the docs:

If you are using Postgres the inserts are done in batches for maximum performance. On other databases the rows need to be inserted one at a time. This is because postgresql is the only database engine that returns the identifiers of all inserted rows and not just the first or the last one.

That means that if you are using mysql you should do

samplemodel.query().insert(data);

For each element in the "newData" array.

like image 63
Borjante Avatar answered Dec 05 '22 11:12

Borjante