diff --git a/packages/database/src/__tests__/operator/ne.test.ts b/packages/database/src/__tests__/operator/ne.test.ts new file mode 100644 index 000000000..59027ff98 --- /dev/null +++ b/packages/database/src/__tests__/operator/ne.test.ts @@ -0,0 +1,33 @@ +import { mockDatabase } from '../index'; +import Database from '../../database'; + +describe('ne operator', () => { + let db: Database; + let Test; + beforeEach(async () => { + db = mockDatabase({}); + + Test = db.collection({ + name: 'tests', + fields: [{ type: 'string', name: 'name' }], + }); + + await db.sync(); + }); + + afterEach(async () => { + await db.close(); + }); + + it('should ne with null', async () => { + await db.getRepository('tests').create({}); + + const results = await db.getRepository('tests').count({ + filter: { + 'name.$ne': '123', + }, + }); + + expect(results).toEqual(1); + }); +}); diff --git a/packages/database/src/operators/index.ts b/packages/database/src/operators/index.ts index 011a19b63..f746f04b4 100644 --- a/packages/database/src/operators/index.ts +++ b/packages/database/src/operators/index.ts @@ -4,4 +4,5 @@ export default { ...require('./array').default, ...require('./empty').default, ...require('./string').default, + ...require('./ne').default, }; diff --git a/packages/database/src/operators/ne.ts b/packages/database/src/operators/ne.ts new file mode 100644 index 000000000..7997d34db --- /dev/null +++ b/packages/database/src/operators/ne.ts @@ -0,0 +1,12 @@ +import { Op } from 'sequelize'; + +export default { + $ne(val, ctx) { + return { + [Op.or]: { + [Op.ne]: val, + [Op.is]: null, + }, + }; + }, +};