Skip to content

Commit

Permalink
added save method
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Detering authored and Brian Detering committed Aug 2, 2015
1 parent 0b11248 commit 36fb2db
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Brian Detering
Copyright (c) 2015 Brian Detering

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ sql.insert('fruit', [
sql.replace('fruit', { uniqueKey: 5, name: 'plum', isRipe: false, color: 'brown' });
```

###save(table, insertObject, \*callback)
Inserts a new row if no duplicate unique or primary keys
are found, else it updates that row.
```sql
INSERT INTO fruit (uniqueKey, isRipe) VALUES (5, 0)
ON DUPLICATE KEY UPDATE uniqueKey=5, isRipe=0
```
```javascript
sql.save('fruit', { uniqueKey: 5, isRipe: false });
```

###update(table, setValues, \*whereEqualsObject, \*callback)
```javascript
sql.update('fruit', { isRipe: false }, { name: 'grape' })
Expand Down
24 changes: 24 additions & 0 deletions mysql-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ var createMySQLWrap = function (connection, options) {
);
};

self.save = function (table, rowRaw, callback) {
var prepareSaveRows = function () {
var insertRow = prepareInsertRows(rowRaw);
var setValues = [];
var setSQL = _.map(rowRaw, function (val, key) {
setValues.push(key, val);
return '?? = ?';
}).join(', ');

return {
sql: insertRow.sql + ' ON DUPLICATE KEY UPDATE ' + setSQL,
values: insertRow.values.concat(setValues)
};
};

var row = prepareSaveRows();

return self.query(
'INSERT INTO ?? ' + row.sql,
[table].concat(row.values),
callback
);
};

self.update = function (table, setData, whereEquals, callback) {
var prepareSetRows = function (setData) {
var values = [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mysql-wrap",
"version": "0.5.2",
"version": "0.5.3",
"description": "Mysql interface and helper functions, wrapping node-mysql",
"main": "mysql-wrap.js",
"scripts": {
Expand Down
42 changes: 42 additions & 0 deletions test/shared_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,48 @@ module.exports = function (connection, createNodeMySQL) {
.done();
};

self.save = function (test) {
test.expect(3);
var self = this;
self.sql.save('table', { unique: 'd', field: 'baz' })
.then(function (res) {
test.strictEqual(res.affectedRows, 1, 'returns affectedRows');
test.strictEqual(res.insertId, 4, 'returns insert id');
connection.query(
'SELECT * FROM `table` WHERE `id` = 4',
function (err, res) {
test.deepEqual(res, [
{ id: 4, unique: 'd', field: 'baz' }
], 'inserts into database');
test.done();
}
);
})
.done();
};

self.save_updates_row_with_same_unique_key = function (test) {
test.expect(1);
var self = this;
self.sql.replace('table', { unique: 'd', field: 'baz' })
.then(function () {
return self.sql.replace('table', { unique: 'd', field: 'bar' });
})
.then(function () {
connection.query(
'SELECT * FROM `table` WHERE `unique` = "d"',
function (err, res) {
test.deepEqual(res, [
{ id: 5, unique: 'd', field: 'bar' }
], 'replaces existing row');
test.done();
}
);
})
.done();
};


self.update = function (test) {
test.expect(1);
var self = this;
Expand Down

0 comments on commit 36fb2db

Please sign in to comment.