Skip to content

Commit

Permalink
Merge pull request #25 from janis-commerce/JCN-449-skip-modified-data
Browse files Browse the repository at this point in the history
Jcn 449 skip modified data
  • Loading branch information
jormaechea committed Sep 21, 2023
2 parents 68ec920 + 96d2992 commit a1eb7a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ const items = await myModel.get({ filters: { foo: 'bar' }});

#### Parameters
- `params` optional parameters to define some behavior of the query
- `skipAutomaticSetModifiedData`: _Boolean_. When receive as **true**, the fields `dateModified` and `userModified` are not updated automatically.

#### Example
```js
Expand Down
10 changes: 6 additions & 4 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,12 @@ class Model {
const db = await this.getDb();
this.validateMethodImplemented(db, 'update');

if(Array.isArray(values))
values.push(this.addModifiedData());
else
this.addModifiedData(values);
if(!params?.skipAutomaticSetModifiedData) {
if(Array.isArray(values))
values.push(this.addModifiedData());
else
this.addModifiedData(values);
}

this.setExecutionStart();

Expand Down
12 changes: 12 additions & 0 deletions tests/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,18 @@ describe('Model', () => {
}, {}, undefined);
});

it('Should skip the automatically fields `dateModified` and `userModified` if the flag `skipAutomaticSetModifiedData` is setted', async () => {

sinon.stub(DBDriver.prototype, 'update')
.resolves();

await myClientModel.update({ some: 'data' }, {}, { skipAutomaticSetModifiedData: true });

sinon.assert.calledOnceWithExactly(DBDriver.prototype.update, myClientModel, {
some: 'data'
}, {}, { skipAutomaticSetModifiedData: true });
});

it('Should add the userModified field when session exists (data is array)', async () => {

sinon.stub(DBDriver.prototype, 'update')
Expand Down

0 comments on commit a1eb7a3

Please sign in to comment.