Browse Source

chore: improve query filtering

e22m4u 2 years ago
parent
commit
5e95f22d78
2 changed files with 66 additions and 1 deletions
  1. 6 1
      src/mongodb-adapter.js
  2. 60 0
      src/mongodb-adapter.spec.js

+ 6 - 1
src/mongodb-adapter.js

@@ -364,8 +364,13 @@ export class MongodbAdapter extends Adapter {
    * @private
    * @private
    */
    */
   _buildQuery(modelName, clause) {
   _buildQuery(modelName, clause) {
+    if (clause == null) return;
+    if (typeof clause !== 'object' || Array.isArray(clause))
+      throw new InvalidArgumentError(
+        'The provided option "where" should be an Object, but %v given.',
+        clause,
+      );
     const query = {};
     const query = {};
-    if (!clause || typeof clause !== 'object') return query;
     const idPropName = this._getIdPropName(modelName);
     const idPropName = this._getIdPropName(modelName);
     Object.keys(clause).forEach(key => {
     Object.keys(clause).forEach(key => {
       let cond = clause[key];
       let cond = clause[key];

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

@@ -433,6 +433,66 @@ describe('MongodbAdapter', function () {
     });
     });
   });
   });
 
 
+  describe('_buildQuery', function () {
+    it('requires the second argument to be an object', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
+      const throwable = v => () => A._buildQuery('model', v);
+      const error = v =>
+        format(
+          'The provided option "where" should be an Object, but %s given.',
+          v,
+        );
+      expect(throwable('str')).to.throw(error('"str"'));
+      expect(throwable('')).to.throw(error('""'));
+      expect(throwable(10)).to.throw(error('10'));
+      expect(throwable(0)).to.throw(error('0'));
+      expect(throwable(true)).to.throw(error('true'));
+      expect(throwable(false)).to.throw(error('false'));
+      expect(throwable({foo: 'bar'})()).to.be.eql({foo: 'bar'});
+      expect(throwable({})()).to.be.eql({});
+      expect(throwable(undefined)()).to.be.undefined;
+      expect(throwable(null)()).to.be.undefined;
+    });
+
+    it('converts the property names to column names', async function () {
+      const schema = createSchema();
+      schema.defineModel({
+        name: 'model',
+        datasource: 'mongodb',
+        properties: {
+          foo: {
+            type: DataType.STRING,
+            columnName: 'bar',
+          },
+          baz: {
+            type: DataType.STRING,
+            columnName: 'qux',
+          },
+        },
+      });
+      const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
+      const res = A._buildQuery('model', {foo: 'a1', baz: null});
+      expect(res).to.be.eql({bar: 'a1', qux: null});
+    });
+
+    it('converts strings of the ObjectId to instances', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
+      const oid1 = new ObjectId();
+      const oid2 = new ObjectId();
+      const id1 = String(oid1);
+      const id2 = String(oid2);
+      const res = A._buildQuery('model', {foo: id1, bar: id2});
+      expect(res.foo).to.be.instanceof(ObjectId);
+      expect(res.bar).to.be.instanceof(ObjectId);
+      expect(res.foo).to.be.eql(oid1);
+      expect(res.bar).to.be.eql(oid2);
+    });
+  });
+
   describe('create', function () {
   describe('create', function () {
     it('generates a new identifier when a value of a primary key is not provided', async function () {
     it('generates a new identifier when a value of a primary key is not provided', async function () {
       const schema = createSchema();
       const schema = createSchema();