Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/step_definitions/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -97,7 +97,7 @@
},
"dependencies": {
"date-fns": "^3.6.0",
"functional-models": "^3.0.12",
"functional-models": "^3.6.4",
"lodash": "^4.17.21"
}
}
15 changes: 9 additions & 6 deletions src/datastoreAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@ 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, K extends keyof T> = T & { [P in K]-?: T[P] }

type Props = {
seedData?: Record<string, Record<string | number, any>>
getCollectionNameForModel?: <T extends DataDescription>(
model: ModelType<T>
) => string
}

const create = ({ seedData }: Props = {}): WithRequired<
DatastoreAdapter,
'count'
> => {
const create = ({
seedData,
getCollectionNameForModel = defaultCollectionName,
}: Props = {}): WithRequired<DatastoreAdapter, 'count'> => {
const database: Record<string, Record<string | number, any>> = clone(
seedData
) || {}

const _getRecords = <TData extends DataDescription>(
model: ModelType<TData>
) => {
const name = model.getName()
const name = getCollectionNameForModel(model)
if (!(name in database)) {
// eslint-disable-next-line functional/immutable-data
database[name] = {}
Expand Down
13 changes: 13 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EqualitySymbol,
isALinkToken,
isPropertyBasedQuery,
ModelType,
OrmSearch,
PropertyQuery,
Query,
Expand All @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -189,4 +196,10 @@ const filterResults = <T extends DataDescription>(
return databaseEntries.filter(func)
}

export const defaultCollectionName = <T extends DataDescription>(
model: ModelType<T>
) => {
return model.getName()
}

export { filterResults }
2 changes: 1 addition & 1 deletion test/src/datastoreAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 13 additions & 0 deletions test/src/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down