From 5d9eef08733af9af2f4d487452944f3176483e5e Mon Sep 17 00:00:00 2001 From: gastonpereyra Date: Mon, 26 Aug 2019 10:54:45 -0300 Subject: [PATCH] Changed Insert to return ID --- CHANGELOG.md | 4 ++++ README.md | 2 +- lib/mysql.js | 5 +++-- tests/mysql-test.js | 47 +++++++++++++++++++++++++++++++++++++++------ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f326d5..177de9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## Unreleased +### Changed +- `insert` returns ID inserted. + ## [1.3.2] - 2019-08-08 ### Fixed - Updated `@janiscommerce/query-builder` version diff --git a/README.md b/README.md index 81e0655..684beee 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ const config = { - `model`: a Model instance with the *database*, *tables*, *fields*, *joins* and other data. - `item`: *type* `OBJECT`, the object to be inserted. - - **Returns**, `boolean` if the object was inserted correctly returns `true`. + - **Returns**, `ID` of the object inserted. * `save(model, item)` **ASYNCHRONOUS**, Saved an individual object in the database. Duplicate Objects updates it. diff --git a/lib/mysql.js b/lib/mysql.js index 3fde116..b52ed81 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -98,8 +98,9 @@ class MySQL { const queryBuilder = new QueryBuilder(this.knex, model); // Insert could response with an Array with 0 value if Primary Key is not AutoIncremental // or Primary Key value if is ID and Integer and AutoIncremental - const result = await queryBuilder.insert(item); - return !!result; + const [result] = await queryBuilder.insert(item); + + return result || item.id; } catch(error) { throw new MySQLError(error.message, MySQLError.codes.INVALID_INSERT); diff --git a/tests/mysql-test.js b/tests/mysql-test.js index c934339..0f3e7c2 100644 --- a/tests/mysql-test.js +++ b/tests/mysql-test.js @@ -52,18 +52,53 @@ describe('MySQL module', function() { sandbox.restore(); }); - it('should return true if try to insert a new Item', async function() { + it('should return ID if try to insert a new Item with no Auto-Incremental ID', async function() { + + const item = { + id: 1, + superhero: 'superman' + }; sandbox.stub(QueryBuilder.prototype, 'insert').callsFake(() => { - return [0]; + return [0]; // Knex return this if no auto-incremental ID }); - const result = await mysql.insert(dummyModel, { - id: 1, - superhero: 'superman' + const result = await mysql.insert(dummyModel, item); + + assert.equal(result, item.id); + }); + + it('should return ID if try to insert a new Item with Auto-Incremental ID', async function() { + + const itemIdGenerated = 10; + + const item = { + superhero: 'supergirl' + }; + + sandbox.stub(QueryBuilder.prototype, 'insert').callsFake(() => { + return [itemIdGenerated]; // Knex return this if no auto-incremental ID }); - assert.equal(result, true); + const result = await mysql.insert(dummyModel, item); + + assert.equal(result, itemIdGenerated); + }); + + it('should return ID if try to insert a new Item which has ID with Auto-Incremental ID', async function() { + + const item = { + id: 20, + superhero: 'Wolverine' + }; + + sandbox.stub(QueryBuilder.prototype, 'insert').callsFake(() => { + return [item.id]; // Knex return this if no auto-incremental ID + }); + + const result = await mysql.insert(dummyModel, item); + + assert.equal(result, item.id); }); it('should throw MySqlError if try to insert an item that already exist', async function() {