I’m looking for a way to do a bulk update on my MariaDB. I use the Node.js mariaDB plugin and HAPI.js with Handlebars. I’m very new at javascript but got already a far way on my little project to do some research on working dogs (Belgium Malinois). It’s for myself to learn working with Javascript, Node and MariaDB.
My issue:
I have a webpage with multiple parameters to edit through a form.
The database: aped_db.parameters
TABLE `parameters` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_general_ci',
`description` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_general_ci',
`opt0` VARCHAR(50) NOT NULL DEFAULT 'Unknown' COLLATE 'utf8mb4_general_ci',
`opt1` VARCHAR(50) NOT NULL DEFAULT 'Very bad' COLLATE 'utf8mb4_general_ci',
`opt2` VARCHAR(50) NOT NULL DEFAULT 'Bad' COLLATE 'utf8mb4_general_ci',
`opt3` VARCHAR(50) NOT NULL DEFAULT 'Ok' COLLATE 'utf8mb4_general_ci',
`opt4` VARCHAR(50) NOT NULL DEFAULT 'Good' COLLATE 'utf8mb4_general_ci',
`opt5` VARCHAR(50) NOT NULL DEFAULT 'Very good' COLLATE 'utf8mb4_general_ci',
`multiplier` FLOAT NULL DEFAULT '1',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `name` (`name`) USING BTREE
)
The HTML code in short
{{#each parlist}}
<input type="hidden" name="id" value="{{this.id}}" form="form-edit">
<td style="text-align:center;">{{this.id}}</td>
<td><input type="text" name="name" value="{{this.name}}" form="form-edit"></td>
<td><input type="text" name="description" value="{{this.description}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt0}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt1}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt2}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt3}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt4}}" form="form-edit"></td>
<td><input type="text" name="opt0" value="{{this.opt5}}" form="form-edit"></td>
<td><input type="text" name="multiplier" value="{{this.multiplier}}" form="form-edit"></td>
{{/each}}
The received payload to process into the table parameters:
req.payload = {
id: [ '1', '3', '' ],
name: [ 'Social', 'Work ethic', 'Dominance' ],
description: [ 'Desc 1', 'Desc 2', 'Desc 3'],
opt0: [ 'Unknown', 'Unknown', 'Unknown' ],
opt1: [ 'Very bad', 'Very bad', 'Very bad' ],
opt2: [ 'Bad', 'Bad', 'Bad' ],
opt3: [ 'Ok', 'Ok', 'Ok' ],
opt4: [ 'Good', 'Good', 'Good' ],
opt5: [ 'Very good', 'Very good', 'Very good' ],
multiplier: [ '1', '1', '1' ]
}
In the above payload 2 parameters are existing ones, and the third one needs to be a new INSERT.
The issue I have is, that for each key there is an array. I would have expected an array for each row.
Parameters id ‘1’ & ‘3’ are existing ones to update, par id ” is a new one to insert.
Because the payload gives an array for each key, I’m not sure how to proceed. How to convert the payload to something more usable.
I tried looking on google, but didn’t find a good example that I could follow (that I understand). I’m very new at this.
Anyone can put me on the right track?
Thx