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
4 changes: 4 additions & 0 deletions src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export class DocumentCollection<DocumentType> {

return documents
}

count(): number {
return this.documents.size
}
}
4 changes: 4 additions & 0 deletions src/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class Store<TypesMap extends Record<string, any>> {
return this.collections.get(type).find(predicate)
}

count<Type extends keyof TypesMap>(type: Type): number {
return this.collections.get(type).count()
}

reset(): void {
this.collections = new CollectionStorage<TypesMap>(this.schema)
}
Expand Down
21 changes: 21 additions & 0 deletions tests/count.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Store } from '../src'
import { schema, TypesMap } from './fixtures'
import { toCollection } from './utils'
import { postFactory, userFactory } from './utils/factories'

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

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

it('should return the count of stored documents for given type', () => {
expect(store.count('User')).toEqual(0)
expect(store.count('Post')).toEqual(0)

toCollection(() => store.create('User', userFactory()), 5)
toCollection(() => store.create('Post', postFactory()), 5)

expect(store.count('User')).toEqual(5)
expect(store.count('Post')).toEqual(5)
})