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
14 changes: 12 additions & 2 deletions src/DocumentCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document } from './types'
import { Document, PredicateFunction } from './types'
import { DOCUMENT_KEY } from './constants'

export class DocumentCollection<DocumentType> {
Expand All @@ -13,7 +13,17 @@ export class DocumentCollection<DocumentType> {
return document
}

find(): DocumentType | undefined {
findFirst(
predicate?: PredicateFunction<DocumentType>,
): DocumentType | undefined {
if (predicate) {
for (const document of this.documents.values()) {
if (predicate(document)) {
return document
}
}
}

return this.documents.values().next().value
}
}
9 changes: 6 additions & 3 deletions src/Store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLSchema } from 'graphql'
import { Schema } from './types'
import { PredicateFunction, Schema } from './types'
import { CollectionStorage } from './CollectionStorage'
import { resolveGraphQLSchema } from './resolveGraphQLSchema'
import { createDocument } from './createDocument'
Expand All @@ -25,8 +25,11 @@ export class Store<TypesMap extends Record<string, any>> {
return this.collections.get(type).create(document)
}

findFirstOrThrow<Type extends keyof TypesMap>(type: Type): TypesMap[Type] {
const document = this.collections.get(type).find()
findFirstOrThrow<Type extends keyof TypesMap>(
type: Type,
predicate?: PredicateFunction<TypesMap[Type]>,
): TypesMap[Type] {
const document = this.collections.get(type).findFirst(predicate)

if (!document) {
throw new Error('Document not found.')
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export type Document<DocumentType> = DocumentType & {
[DOCUMENT_KEY]: string
[DOCUMENT_TYPE]: string
}

export type PredicateFunction<T> = (data: T) => boolean
51 changes: 51 additions & 0 deletions tests/findFirstOrThrow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Store } from '../src'
import { schema, TypesMap } from './fixtures'
import { postFactory, userFactory } from './utils/factories'

const store = new Store<TypesMap>({
schema,
})

afterEach(() => store.reset())

it('should return the first document', () => {
const user = store.create('User', userFactory())
store.create('User', userFactory())
store.create('User', userFactory())

expect(store.findFirstOrThrow('User')).toEqual(user)

const post = store.create('Post', postFactory())
store.create('Post', postFactory())
store.create('Post', postFactory())

expect(store.findFirstOrThrow('Post')).toEqual(post)
})

it('should throw error if first user is undefined', () => {
expect(() => {
store.findFirstOrThrow('User')
}).toThrowError('Document not found.')

expect(() => {
store.findFirstOrThrow('Post')
}).toThrowError('Document not found.')
})

it('can find specific document with a predicate function', () => {
store.create('User', userFactory())
store.create('User', userFactory())
const user = store.create('User', userFactory({ username: 'john.doe' }))

expect(
store.findFirstOrThrow('User', (user) => user.username === 'john.doe'),
).toEqual(user)

store.create('Post', postFactory())
store.create('Post', postFactory())
const post = store.create('Post', postFactory({ title: 'Hello World!' }))

expect(
store.findFirstOrThrow('Post', (post) => post.title === 'Hello World!'),
).toEqual(post)
})