@e22m4u/js-repository-mongodb-adapter 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository-mongodb-adapter",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "MongoDB адаптер для @e22m4u/js-repository",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -579,8 +579,8 @@ export class MongodbAdapter extends Adapter {
579
579
  modelData[idPropName] = id;
580
580
  const tableData = this._toDatabase(modelName, modelData);
581
581
  const table = this._getCollection(modelName);
582
- const {modifiedCount} = await table.replaceOne({_id: id}, tableData);
583
- if (modifiedCount < 1)
582
+ const {matchedCount} = await table.replaceOne({_id: id}, tableData);
583
+ if (matchedCount < 1)
584
584
  throw new InvalidArgumentError('Identifier %v is not found.', String(id));
585
585
  const projection = this._buildProjection(
586
586
  modelName,
@@ -605,8 +605,8 @@ export class MongodbAdapter extends Adapter {
605
605
  delete modelData[idPropName];
606
606
  const tableData = this._toDatabase(modelName, modelData);
607
607
  const table = this._getCollection(modelName);
608
- const {modifiedCount} = await table.updateOne({_id: id}, {$set: tableData});
609
- if (modifiedCount < 1)
608
+ const {matchedCount} = await table.updateOne({_id: id}, {$set: tableData});
609
+ if (matchedCount < 1)
610
610
  throw new InvalidArgumentError('Identifier %v is not found.', String(id));
611
611
  const projection = this._buildProjection(
612
612
  modelName,
@@ -1299,6 +1299,21 @@ describe('MongodbAdapter', function () {
1299
1299
  .findOne({_id: oid});
1300
1300
  expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
1301
1301
  });
1302
+
1303
+ it('does not throws an error if nothing changed', async function () {
1304
+ const schema = createSchema();
1305
+ schema.defineModel({name: 'model', datasource: 'mongodb'});
1306
+ const rep = schema.getRepository('model');
1307
+ const created = await rep.create({foo: 10});
1308
+ const id = created[DEF_PK];
1309
+ const replaced = await rep.replaceById(id, {foo: 10});
1310
+ expect(replaced).to.be.eql({[DEF_PK]: id, foo: 10});
1311
+ const oid = new ObjectId(id);
1312
+ const rawData = await MDB_CLIENT.db()
1313
+ .collection('model')
1314
+ .findOne({_id: oid});
1315
+ expect(rawData).to.be.eql({_id: oid, foo: 10});
1316
+ });
1302
1317
  });
1303
1318
 
1304
1319
  describe('patchById', function () {
@@ -1651,6 +1666,21 @@ describe('MongodbAdapter', function () {
1651
1666
  .findOne({_id: oid});
1652
1667
  expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
1653
1668
  });
1669
+
1670
+ it('does not throws an error if nothing changed', async function () {
1671
+ const schema = createSchema();
1672
+ schema.defineModel({name: 'model', datasource: 'mongodb'});
1673
+ const rep = schema.getRepository('model');
1674
+ const created = await rep.create({foo: 10});
1675
+ const id = created[DEF_PK];
1676
+ const patched = await rep.patchById(id, {foo: 10});
1677
+ expect(patched).to.be.eql({[DEF_PK]: id, foo: 10});
1678
+ const oid = new ObjectId(id);
1679
+ const rawData = await MDB_CLIENT.db()
1680
+ .collection('model')
1681
+ .findOne({_id: oid});
1682
+ expect(rawData).to.be.eql({_id: oid, foo: 10});
1683
+ });
1654
1684
  });
1655
1685
 
1656
1686
  describe('find', function () {