Skip to content

Commit fce022c

Browse files
authored
Merge pull request #3 from monolithst/name
Name
2 parents d585dbe + 8555a6c commit fce022c

6 files changed

Lines changed: 39 additions & 10 deletions

File tree

features/step_definitions/steps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createOrm, Orm, PrimaryKeyUuidProperty, queryBuilder, TextProperty } fr
66
const SeedData = {
77
SeedData1: () => {
88
return {
9-
'functional-models-orm-memory-test-1-models': {
9+
'functional-models-orm-memory/Test1Models': {
1010
'29a766b5-e77b-4099-a7f2-61cda0a29cc3': {
1111
id: '29a766b5-e77b-4099-a7f2-61cda0a29cc3',
1212
name: 'my-name-1',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functional-models-orm-memory",
3-
"version": "3.0.0",
3+
"version": "3.0.2",
44
"description": "An in-memory datastore adapter for functional-models",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -97,7 +97,7 @@
9797
},
9898
"dependencies": {
9999
"date-fns": "^3.6.0",
100-
"functional-models": "^3.0.12",
100+
"functional-models": "^3.6.4",
101101
"lodash": "^4.17.21"
102102
}
103103
}

src/datastoreAdapter.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@ import {
1212
} from 'functional-models'
1313
import clone from 'lodash/clone'
1414
import merge from 'lodash/merge'
15-
import { filterResults } from './lib'
15+
import { defaultCollectionName, filterResults } from './lib'
1616

1717
type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] }
1818

1919
type Props = {
2020
seedData?: Record<string, Record<string | number, any>>
21+
getCollectionNameForModel?: <T extends DataDescription>(
22+
model: ModelType<T>
23+
) => string
2124
}
2225

23-
const create = ({ seedData }: Props = {}): WithRequired<
24-
DatastoreAdapter,
25-
'count'
26-
> => {
26+
const create = ({
27+
seedData,
28+
getCollectionNameForModel = defaultCollectionName,
29+
}: Props = {}): WithRequired<DatastoreAdapter, 'count'> => {
2730
const database: Record<string, Record<string | number, any>> = clone(
2831
seedData
2932
) || {}
3033

3134
const _getRecords = <TData extends DataDescription>(
3235
model: ModelType<TData>
3336
) => {
34-
const name = model.getName()
37+
const name = getCollectionNameForModel(model)
3538
if (!(name in database)) {
3639
// eslint-disable-next-line functional/immutable-data
3740
database[name] = {}

src/lib.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
EqualitySymbol,
99
isALinkToken,
1010
isPropertyBasedQuery,
11+
ModelType,
1112
OrmSearch,
1213
PropertyQuery,
1314
Query,
@@ -23,6 +24,10 @@ const _emptyValueWrapper =
2324
const isEmptyCheck = property.value === undefined || property.value === null
2425
const subfunc = func(property)
2526
return (obj: object) => {
27+
// 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
28+
if (property.equalitySymbol === EqualitySymbol.ne) {
29+
return subfunc(obj)
30+
}
2631
// @ts-ignore
2732
const value = obj[property.key]
2833
const valueIsEmpty = value === undefined || value === null
@@ -62,6 +67,8 @@ const _checks = {
6267
searchValue > dataValue,
6368
[EqualitySymbol.lte]: (searchValue: number, dataValue: number) =>
6469
searchValue >= dataValue,
70+
[EqualitySymbol.ne]: (searchValue: number, dataValue: number) =>
71+
searchValue !== dataValue,
6572
}
6673

6774
const _numberCompare = _emptyValueWrapper((property: PropertyQuery) => {
@@ -189,4 +196,10 @@ const filterResults = <T extends DataDescription>(
189196
return databaseEntries.filter(func)
190197
}
191198

199+
export const defaultCollectionName = <T extends DataDescription>(
200+
model: ModelType<T>
201+
) => {
202+
return model.getName()
203+
}
204+
192205
export { filterResults }

test/src/datastoreAdapter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { create } from '../../src/datastoreAdapter'
99

1010
const getSeedData1 = () => ({
11-
'functional-models-orm-memory-test-1-models': {
11+
'functional-models-orm-memory/Test1Models': {
1212
'29a766b5-e77b-4099-a7f2-61cda0a29cc3': {
1313
id: '29a766b5-e77b-4099-a7f2-61cda0a29cc3',
1414
name: 'my-name-1',

test/src/lib.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ describe('/src/lib.ts', () => {
254254
const expected = 2
255255
assert.deepEqual(actual, expected)
256256
})
257+
it('should return 4 results with TestData1 when searching != 7', () => {
258+
const actual = filterResults(
259+
queryBuilder()
260+
.property('aNumber', 7, {
261+
type: DatastoreValueType.number,
262+
equalitySymbol: EqualitySymbol.ne,
263+
})
264+
.compile(),
265+
TestData1
266+
)
267+
const expected = 4
268+
assert.deepEqual(actual.length, expected)
269+
})
257270
it('should return 4 results with TestData1 when searching > 1', () => {
258271
const actual = filterResults(
259272
queryBuilder()

0 commit comments

Comments
 (0)