You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

mysql-bricks

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysql-bricks - npm Package Compare versions

Comparing version

to
1.1.1

16

index.js

@@ -64,4 +64,11 @@ 'use strict';

return 'ON DUPLICATE KEY UPDATE ' + this._onDuplicateColumns.map(function(col) {
col = sql._handleColumn(col, null);
return `${col} = VALUES(${col})`;
if (col.constructor === "".constructor) { // is string
col = sql._handleColumn(col, null);
return `${col} = VALUES(${col})`;
} else if (col.constructor === {}.constructor && Object.keys(col).length > 0) { // is column-value pair object
let colKey = Object.keys(col)[0];
let colValue = col[colKey];
colKey = sql._handleColumn(colKey, null);
return `${colKey} = ${colValue}`;
}
}).join(', ');

@@ -81,8 +88,3 @@ },

// TODO: The ORDER BY and LIMIT clauses of the UPDATE and DELETE statements + tests
// TODO: extend on duplicate key update - allow expression
// Replace ?
module.exports = mysqlBricks;
{
"name": "mysql-bricks",
"version": "1.1.0",
"version": "1.1.1",
"description": "sql-bricks extension for MySQL",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -29,7 +29,22 @@ # mysql-bricks

INSERT INTO main.some_table (id, name, age, grade)
VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
ON DUPLICATE KEY UPDATE grade = VALUES(grade)
*/
```
ON DUPLICATE KEY UPDATE clause can also contain a column-value pair, where the value is a free text sql.
Note that free-text sql will not be automatically quoted if it contains reserved words.
```javascript
let values = [[123, 'Daniel', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]];
let query = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter')
.values(values)
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }])
.toString();
/*
INSERT INTO main.some_table (id, name, age, grade, counter)
VALUES (123, \'Daniel\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0)
ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1
*/
```
#### INSERT IGNORE ...

@@ -45,3 +60,3 @@

INSERT IGNORE INTO main.some_table (id, name, age, grade)
VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
*/

@@ -48,0 +63,0 @@ ```

@@ -48,2 +48,18 @@ 'use strict';

it("should return correct insert query when using '.onDuplicateKeyUpdate' option with column-value pair", function () {
let values = [[123, 'Moshe', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade, counter) ' +
'VALUES (123, \'Moshe\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0) ' +
'ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter')
.values(values)
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }])
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore '.onDuplicateKeyUpdate' when cols is null", function () {

@@ -50,0 +66,0 @@