Skip to content

Commit

Permalink
feat: use start/count instead of bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
akdasa committed May 23, 2023
1 parent 7c49e88 commit 2282ac3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/persistence/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Aggregate, AnyIdentity } from '@lib/domain/models'
import { Query } from '@lib/persistence'

export interface ResultSetSlice {
start: number
count: number
}

/**
* Result set.
*/
Expand All @@ -12,7 +17,7 @@ export class ResultSet<TEntity extends Aggregate<AnyIdentity>> {
*/
constructor(
public readonly entities: readonly TEntity[],
public readonly bookmark?: string,
public readonly slice: ResultSetSlice,
) { }
}

Expand Down
7 changes: 4 additions & 3 deletions lib/persistence/memory/InMemoryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class InMemoryRepository<
// options?: QueryOptions,
): Promise<ResultSet<TAggregate>> {
return new ResultSet(
Array.from(this.entities.values())
Array.from(this.entities.values()),
{ start: 0, count: this.entities.size }
)
}

Expand All @@ -38,11 +39,11 @@ export class InMemoryRepository<
query: Query<TAggregate>,
options?: QueryOptions,
): Promise<ResultSet<TAggregate>> {
const startIndex = parseInt(options?.bookmark ?? '0', 10)
const startIndex = options?.skip || 0
const entities = Array.from(this.entities.values()).slice(startIndex)
const result = this.processor.execute(query, entities)
return new ResultSet<TAggregate>(
result, (startIndex + entities.length).toString()
result, { start: startIndex, count: result.length }
)
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akdasa-studios/framework",
"version": "0.5.1",
"version": "0.5.2",
"description": "Framework to build every app",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/persistence/memory/InMemoryRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('InMemoryRepository', () => {
let result = await repository.find(query)

// act:
result = await repository.find(query, { bookmark: result.bookmark })
result = await repository.find(query, { skip: result.slice.count })

// assert:
expect(result.entities).toEqual([])
Expand Down

0 comments on commit 2282ac3

Please sign in to comment.