Browse Source

fix: an error of "unknown id" on patching with no changes

e22m4u 2 years ago
parent
commit
77eff83b37
2 changed files with 34 additions and 4 deletions
  1. 4 4
      src/mongodb-adapter.js
  2. 30 0
      src/mongodb-adapter.spec.js

+ 4 - 4
src/mongodb-adapter.js

@@ -579,8 +579,8 @@ export class MongodbAdapter extends Adapter {
     modelData[idPropName] = id;
     modelData[idPropName] = id;
     const tableData = this._toDatabase(modelName, modelData);
     const tableData = this._toDatabase(modelName, modelData);
     const table = this._getCollection(modelName);
     const table = this._getCollection(modelName);
-    const {modifiedCount} = await table.replaceOne({_id: id}, tableData);
-    if (modifiedCount < 1)
+    const {matchedCount} = await table.replaceOne({_id: id}, tableData);
+    if (matchedCount < 1)
       throw new InvalidArgumentError('Identifier %v is not found.', String(id));
       throw new InvalidArgumentError('Identifier %v is not found.', String(id));
     const projection = this._buildProjection(
     const projection = this._buildProjection(
       modelName,
       modelName,
@@ -605,8 +605,8 @@ export class MongodbAdapter extends Adapter {
     delete modelData[idPropName];
     delete modelData[idPropName];
     const tableData = this._toDatabase(modelName, modelData);
     const tableData = this._toDatabase(modelName, modelData);
     const table = this._getCollection(modelName);
     const table = this._getCollection(modelName);
-    const {modifiedCount} = await table.updateOne({_id: id}, {$set: tableData});
-    if (modifiedCount < 1)
+    const {matchedCount} = await table.updateOne({_id: id}, {$set: tableData});
+    if (matchedCount < 1)
       throw new InvalidArgumentError('Identifier %v is not found.', String(id));
       throw new InvalidArgumentError('Identifier %v is not found.', String(id));
     const projection = this._buildProjection(
     const projection = this._buildProjection(
       modelName,
       modelName,

+ 30 - 0
src/mongodb-adapter.spec.js

@@ -1299,6 +1299,21 @@ describe('MongodbAdapter', function () {
         .findOne({_id: oid});
         .findOne({_id: oid});
       expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
       expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
     });
     });
+
+    it('does not throws an error if nothing changed', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const rep = schema.getRepository('model');
+      const created = await rep.create({foo: 10});
+      const id = created[DEF_PK];
+      const replaced = await rep.replaceById(id, {foo: 10});
+      expect(replaced).to.be.eql({[DEF_PK]: id, foo: 10});
+      const oid = new ObjectId(id);
+      const rawData = await MDB_CLIENT.db()
+        .collection('model')
+        .findOne({_id: oid});
+      expect(rawData).to.be.eql({_id: oid, foo: 10});
+    });
   });
   });
 
 
   describe('patchById', function () {
   describe('patchById', function () {
@@ -1651,6 +1666,21 @@ describe('MongodbAdapter', function () {
         .findOne({_id: oid});
         .findOne({_id: oid});
       expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
       expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
     });
     });
+
+    it('does not throws an error if nothing changed', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const rep = schema.getRepository('model');
+      const created = await rep.create({foo: 10});
+      const id = created[DEF_PK];
+      const patched = await rep.patchById(id, {foo: 10});
+      expect(patched).to.be.eql({[DEF_PK]: id, foo: 10});
+      const oid = new ObjectId(id);
+      const rawData = await MDB_CLIENT.db()
+        .collection('model')
+        .findOne({_id: oid});
+      expect(rawData).to.be.eql({_id: oid, foo: 10});
+    });
   });
   });
 
 
   describe('find', function () {
   describe('find', function () {