Skip to content

Commit

Permalink
Changed Insert to return ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonpereyra committed Aug 26, 2019
1 parent 14044a4 commit 5d9eef0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 41 additions & 6 deletions tests/mysql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5d9eef0

Please sign in to comment.