diff --git a/features/step_definitions/steps.ts b/features/step_definitions/steps.ts index 2d43b98..c2bdf42 100644 --- a/features/step_definitions/steps.ts +++ b/features/step_definitions/steps.ts @@ -6,7 +6,7 @@ import { createOrm, Orm, PrimaryKeyUuidProperty, queryBuilder, TextProperty } fr const SeedData = { SeedData1: () => { return { - 'functional-models-orm-memory-test-1-models': { + 'functional-models-orm-memory/Test1Models': { '29a766b5-e77b-4099-a7f2-61cda0a29cc3': { id: '29a766b5-e77b-4099-a7f2-61cda0a29cc3', name: 'my-name-1', diff --git a/package.json b/package.json index 5eb9f48..177560a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models-orm-memory", - "version": "3.0.0", + "version": "3.0.2", "description": "An in-memory datastore adapter for functional-models", "main": "index.js", "types": "index.d.ts", @@ -97,7 +97,7 @@ }, "dependencies": { "date-fns": "^3.6.0", - "functional-models": "^3.0.12", + "functional-models": "^3.6.4", "lodash": "^4.17.21" } } diff --git a/src/datastoreAdapter.ts b/src/datastoreAdapter.ts index 0076217..b894c40 100644 --- a/src/datastoreAdapter.ts +++ b/src/datastoreAdapter.ts @@ -12,18 +12,21 @@ import { } from 'functional-models' import clone from 'lodash/clone' import merge from 'lodash/merge' -import { filterResults } from './lib' +import { defaultCollectionName, filterResults } from './lib' type WithRequired = T & { [P in K]-?: T[P] } type Props = { seedData?: Record> + getCollectionNameForModel?: ( + model: ModelType + ) => string } -const create = ({ seedData }: Props = {}): WithRequired< - DatastoreAdapter, - 'count' -> => { +const create = ({ + seedData, + getCollectionNameForModel = defaultCollectionName, +}: Props = {}): WithRequired => { const database: Record> = clone( seedData ) || {} @@ -31,7 +34,7 @@ const create = ({ seedData }: Props = {}): WithRequired< const _getRecords = ( model: ModelType ) => { - const name = model.getName() + const name = getCollectionNameForModel(model) if (!(name in database)) { // eslint-disable-next-line functional/immutable-data database[name] = {} diff --git a/src/lib.ts b/src/lib.ts index 00bd699..6c43f41 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -8,6 +8,7 @@ import { EqualitySymbol, isALinkToken, isPropertyBasedQuery, + ModelType, OrmSearch, PropertyQuery, Query, @@ -23,6 +24,10 @@ const _emptyValueWrapper = const isEmptyCheck = property.value === undefined || property.value === null const subfunc = func(property) return (obj: object) => { + // If we are searching for a value that is not equal to the given value, return true if the value is not equal to the given value + if (property.equalitySymbol === EqualitySymbol.ne) { + return subfunc(obj) + } // @ts-ignore const value = obj[property.key] const valueIsEmpty = value === undefined || value === null @@ -62,6 +67,8 @@ const _checks = { searchValue > dataValue, [EqualitySymbol.lte]: (searchValue: number, dataValue: number) => searchValue >= dataValue, + [EqualitySymbol.ne]: (searchValue: number, dataValue: number) => + searchValue !== dataValue, } const _numberCompare = _emptyValueWrapper((property: PropertyQuery) => { @@ -189,4 +196,10 @@ const filterResults = ( return databaseEntries.filter(func) } +export const defaultCollectionName = ( + model: ModelType +) => { + return model.getName() +} + export { filterResults } diff --git a/test/src/datastoreAdapter.test.ts b/test/src/datastoreAdapter.test.ts index 15ec9f5..482a080 100644 --- a/test/src/datastoreAdapter.test.ts +++ b/test/src/datastoreAdapter.test.ts @@ -8,7 +8,7 @@ import { import { create } from '../../src/datastoreAdapter' const getSeedData1 = () => ({ - 'functional-models-orm-memory-test-1-models': { + 'functional-models-orm-memory/Test1Models': { '29a766b5-e77b-4099-a7f2-61cda0a29cc3': { id: '29a766b5-e77b-4099-a7f2-61cda0a29cc3', name: 'my-name-1', diff --git a/test/src/lib.test.ts b/test/src/lib.test.ts index 3d0c438..5f581ec 100644 --- a/test/src/lib.test.ts +++ b/test/src/lib.test.ts @@ -254,6 +254,19 @@ describe('/src/lib.ts', () => { const expected = 2 assert.deepEqual(actual, expected) }) + it('should return 4 results with TestData1 when searching != 7', () => { + const actual = filterResults( + queryBuilder() + .property('aNumber', 7, { + type: DatastoreValueType.number, + equalitySymbol: EqualitySymbol.ne, + }) + .compile(), + TestData1 + ) + const expected = 4 + assert.deepEqual(actual.length, expected) + }) it('should return 4 results with TestData1 when searching > 1', () => { const actual = filterResults( queryBuilder()