Skip to content

Commit

Permalink
Changed Save to return ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonpereyra committed Aug 26, 2019
1 parent 5d9eef0 commit 0c9b70b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Changed
- `insert` returns ID inserted.
- `save` returns ID inserted / updated.

## [1.3.2] - 2019-08-08
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const config = {

- `model`: a Model instance with the *database*, *tables*, *fields*, *joins* and other data.
- `item`: *type* `OBJECT`,the object to be saved.
- **Returns**, `Promise` with `true` if the object was saved correctly.
- **Returns**, `ID` of the object inserted / updated.

* `multiInsert(model, items)` **ASYNCHRONOUS**, Performs an Insert of multiple objects. Duplicate Objects updates it.

Expand Down Expand Up @@ -151,7 +151,7 @@ movieItem = {

try {
movieResponse = await mysql.insert(movieModel, movieItem);
// Response: TRUE
// Response: 1
console.log('Movie Saved'); // Print in Console
} catch (error) {
console.log('These Movie can\'t be saved.');
Expand Down Expand Up @@ -208,7 +208,7 @@ movieItem = {
try {

movieResponse = await mysql.save(movieModel, movieItem);
// Insert and Response: TRUE
// Insert and Response: 2
console.log('Movie Saved'); // Print in Console

} catch(error) {
Expand Down
7 changes: 4 additions & 3 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class MySQL {
* Insert an item into the database
* @param {instance} model - Model instance
* @param {object} item - The item to insert
* @returns {Promise<boolean>} True if success
* @returns {Insert} ID
*/
async insert(model, item) {

Expand Down Expand Up @@ -112,7 +112,7 @@ class MySQL {
* Save a new items or update it in the SQL database.
* @param {instance} model Model Class Instance
* @param {object} item object to saved
* @returns {Promise<boolean>} True if success
* @returns {Integer} ID
*/
async save(model, item) {

Expand All @@ -126,7 +126,8 @@ class MySQL {
const queryBuilder = new QueryBuilder(this.knex, model);
const [result] = await queryBuilder.save(item);

return !!result[this.constructor.affectedRows];
// If ID is not Auto-Incremental 'insertId0 is 0 when insert. Anything else 'insertId' is ID inserted
return result.insertId || item.id;

} catch(error) {
throw new MySQLError(error.message, MySQLError.codes.INVALID_SAVE);
Expand Down
76 changes: 50 additions & 26 deletions tests/mysql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('MySQL module', function() {

const result = await mysql.insert(dummyModel, item);

assert.equal(result, item.id);
assert.strictEqual(result, item.id);
});

it('should return ID if try to insert a new Item with Auto-Incremental ID', async function() {
Expand All @@ -82,7 +82,7 @@ describe('MySQL module', function() {

const result = await mysql.insert(dummyModel, item);

assert.equal(result, itemIdGenerated);
assert.strictEqual(result, itemIdGenerated);
});

it('should return ID if try to insert a new Item which has ID with Auto-Incremental ID', async function() {
Expand All @@ -98,7 +98,7 @@ describe('MySQL module', function() {

const result = await mysql.insert(dummyModel, item);

assert.equal(result, item.id);
assert.strictEqual(result, item.id);
});

it('should throw MySqlError if try to insert an item that already exist', async function() {
Expand All @@ -113,32 +113,56 @@ describe('MySQL module', function() {
await assert.rejects(mysql.insert(dummyModel, item), { code: MySQLError.codes.INVALID_INSERT });
});

it('should return true if try to save a new item', async function() {
it('should return ID if try to save a new item with ID no auto-incremental', async function() {

const item = {
id: 2,
superhero: 'batman'
};

sandbox.stub(QueryBuilder.prototype, 'save').callsFake(() => {
return [{ affectedRows: 1, insertId: 0 }];
});

const result = await mysql.save(dummyModel, {
id: 2,
const result = await mysql.save(dummyModel, item);

assert.strictEqual(result, item.id);
});

it('should return ID if try to save a new item with ID auto-incremental', async function() {

const itemIDGenerated = 2;

const item = {
superhero: 'batman'
};

sandbox.stub(QueryBuilder.prototype, 'save').callsFake(() => {
return [{ affectedRows: 1, insertId: itemIDGenerated }];
});

assert.equal(result, true);
const result = await mysql.save(dummyModel, item);

assert.strictEqual(result, itemIDGenerated);
});

it('should return true if try to save an old item', async function() {
it('should return ID if try to save an existing item', async function() {

const item = {
id: 1,
superhero: 'hulk'
};

sandbox.stub(QueryBuilder.prototype, 'save').callsFake(() => {
return [{ affectedRows: 2 }];
return [{ affectedRows: 2, insertId: item.id }];
});

const result = await mysql.save(dummyModel, {
id: 1,
superhero: 'hulk'
});

assert.equal(result, true);
assert.strictEqual(result, item.id);
});


Expand All @@ -155,7 +179,7 @@ describe('MySQL module', function() {

const result = await mysql.update(dummyModel, fields, filters);

assert.equal(result, 2);
assert.strictEqual(result, 2);
});

it('should return 0 if try to update using filters don\'t match any item', async function() {
Expand All @@ -171,7 +195,7 @@ describe('MySQL module', function() {

const result = await mysql.update(dummyModel, fields, filters);

assert.equal(result, 0);
assert.strictEqual(result, 0);
});

it('should return 1 if try to multi-Insert a new Item', async function() {
Expand All @@ -186,7 +210,7 @@ describe('MySQL module', function() {
}]);


assert.equal(result, 1);
assert.strictEqual(result, 1);
});

it('should return the quantity of new items as rows affected if try to multi-Insert new Items', async function() {
Expand All @@ -202,7 +226,7 @@ describe('MySQL module', function() {
]);


assert.equal(result, 3);
assert.strictEqual(result, 3);
});

it('should return the double of quantity of new items as rows affected if try to multi-Insert Items which already exist', async function() {
Expand All @@ -217,7 +241,7 @@ describe('MySQL module', function() {
]);


assert.equal(result, 4);
assert.strictEqual(result, 4);
});

});
Expand Down Expand Up @@ -349,7 +373,7 @@ describe('MySQL module', function() {
});

const testParams = (params, expectedParams) => {
assert.deepEqual(params, expectedParams, 'shouldn\'t modify ofiginal params');
assert.deepStrictEqual(params, expectedParams, 'shouldn\'t modify ofiginal params');
};

it('should return default values getting totals with empty tables', async function() {
Expand All @@ -363,7 +387,7 @@ describe('MySQL module', function() {
total: 0
};

assert.deepEqual(await mysql.getTotals(dummyModel), totalExpected);
assert.deepStrictEqual(await mysql.getTotals(dummyModel), totalExpected);
});

it('Should return empty results and totals with zero values', async function() {
Expand All @@ -374,11 +398,11 @@ describe('MySQL module', function() {

const result = await mysql.get(dummyModel, params);

assert.deepEqual(result, []);
assert.deepStrictEqual(result, []);

const resultTotals = await mysql.getTotals(dummyModel);

assert.deepEqual(resultTotals, { total: 0, pages: 0 });
assert.deepStrictEqual(resultTotals, { total: 0, pages: 0 });

testParams(params, {});
});
Expand All @@ -392,15 +416,15 @@ describe('MySQL module', function() {

const result = await mysql.get(dummyModel, params);

assert.deepEqual(result, [{ result: 1 }, { result: 2 }]);
assert.deepStrictEqual(result, [{ result: 1 }, { result: 2 }]);

testParams(params, originalParams);

stubGet.callsFake(() => [{ count: 650 }]);

const resultTotals = await mysql.getTotals(dummyModel);

assert.deepEqual(resultTotals, {
assert.deepStrictEqual(resultTotals, {
total: 650,
page: 1,
pageSize: 500,
Expand All @@ -417,15 +441,15 @@ describe('MySQL module', function() {

const result = await mysql.get(dummyModel, params);

assert.deepEqual(result, [{ result: 1 }, { result: 2 }]);
assert.deepStrictEqual(result, [{ result: 1 }, { result: 2 }]);

testParams(params, originalParams);

stubGet.callsFake(() => [{ count: 650 }]);

const resultTotals = await mysql.getTotals(dummyModel);

assert.deepEqual(resultTotals, {
assert.deepStrictEqual(resultTotals, {
total: 650,
page: 4,
pageSize: 10,
Expand All @@ -442,15 +466,15 @@ describe('MySQL module', function() {

const result = await mysql.get(dummyModel, params);

assert.deepEqual(result, [{ result: 1 }, { result: 2 }]);
assert.deepStrictEqual(result, [{ result: 1 }, { result: 2 }]);

testParams(params, originalParams);

stubGet.callsFake(() => [{ count: 650 }]);

const resultTotals = await mysql.getTotals(dummyModel);

assert.deepEqual(resultTotals, {
assert.deepStrictEqual(resultTotals, {
total: 650,
page: 65,
pageSize: 10,
Expand Down Expand Up @@ -518,7 +542,7 @@ describe('MySQL module', function() {
filters: { id: 1 }
});

assert.equal(results, 1);
assert.strictEqual(results, 1);
});

it('should throw MySqlError if can not remove', async function() {
Expand Down

0 comments on commit 0c9b70b

Please sign in to comment.