Skip to content

Commit

Permalink
better repository usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Apr 8, 2020
1 parent 0559b6d commit 0a54645
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tfso/js-enumerable",
"version": "0.0.10",
"version": "0.0.11",
"description": "",
"main": "lib/index.js",
"typings": "lib/index",
Expand Down
4 changes: 2 additions & 2 deletions src/linq/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default abstract class Base<TEntity extends any> implements IEnumerable<T
private iterableName: string | null = null
private items: Iterable<TEntity> | AsyncIterable<TEntity> | null = null

protected operators: Array<LinqOperator<TEntity>> = []
public operators: Array<LinqOperator<TEntity>> = []

public constructor(items?: Array<TEntity>)
public constructor(items?: Iterable<TEntity>)
Expand Down Expand Up @@ -115,7 +115,7 @@ export default abstract class Base<TEntity extends any> implements IEnumerable<T
}
}

let iterator = iterate(this.items instanceof Repository ? this.items.query(this) : this.items[Symbol.asyncIterator](), this.operators),
let iterator = iterate(this.items instanceof Repository ? this.items[Symbol.asyncIterator](this) : this.items[Symbol.asyncIterator](), this.operators),
result: IteratorResult<TEntity>

while (!(result = await Promise.resolve(iterator.next())).done) {
Expand Down
4 changes: 3 additions & 1 deletion src/linq/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { Enumerable } from './enumerable'
export { IEnumerable } from './base'
export { IEnumerable } from './base'

export { LinqType } from './operator'
37 changes: 24 additions & 13 deletions src/repository/repository.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
import { IEnumerable } from './../linq'

export interface IRepository<TEntity, TEntityId> extends AsyncIterable<TEntity> {
query(linq?: IEnumerable<TEntity>, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator<TEntity>
export interface IRepository<TEntity extends Record<string, any>, TEntityId> extends AsyncIterable<TEntity> {
query(enumerable?: IEnumerable<TEntity>, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator<TEntity>

create(item: Partial<TEntity>): Promise<TEntity>
create(item: Partial<TEntity>, meta?: Partial<{ etag: string }>): Promise<TEntity>
read(id?: TEntityId): Promise<TEntity>
update(entity: Partial<TEntity>): Promise<TEntity>
delete(id: TEntityId): Promise<boolean>
update(entity: Partial<TEntity>, meta?: Partial<{ etag: string }>, ...fields: Array<keyof TEntity>): Promise<TEntity>
delete(id: TEntityId, meta?: Partial<{ etag: string }>): Promise<boolean>

getIterable(meta?: Partial<{ continuationToken: string }>): AsyncIterable<TEntity>
}

export abstract class Repository<TEntity, TEntityId> implements IRepository<TEntity, TEntityId>, AsyncIterable<TEntity> {
abstract query(linq?: IEnumerable<TEntity>, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator<TEntity>
export abstract class Repository<TEntity extends Record<string, any>, TEntityId> implements IRepository<TEntity, TEntityId>, AsyncIterable<TEntity> {

abstract query(enumerable?: IEnumerable<TEntity>, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator<TEntity>

abstract async create(item: Partial<TEntity>): Promise<TEntity>
abstract async create(item: Partial<TEntity>, meta?: Partial<{ etag: string }>): Promise<TEntity>
abstract async read(id?: TEntityId): Promise<TEntity>
abstract async update(item: Partial<TEntity>): Promise<TEntity>
abstract async delete(id: TEntityId): Promise<boolean>
abstract async update(item: Partial<TEntity>, meta?: Partial<{ etag: string }>, ...fields: Array<keyof TEntity>): Promise<TEntity>
abstract async delete(id: TEntityId, meta?: Partial<{ etag: string }>): Promise<boolean>

public getIterable(meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterable<TEntity> {
return Object.assign(Object.create(Object.getPrototypeOf(this)), this, {
[Symbol.asyncIterator]: (enumerable?: IEnumerable<TEntity>) => {
return this.query(enumerable, meta)
}
})
}

[Symbol.iterator](...options: any[]): IterableIterator<Promise<TEntity>> {
[Symbol.iterator](...options: any[]): IterableIterator<TEntity> {
throw new TypeError('Repository is async iterable but it used as iterable')
}

[Symbol.asyncIterator](linq?: IEnumerable<TEntity>, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator<TEntity> {
return this.query(linq, meta)
[Symbol.asyncIterator](enumerable?: IEnumerable<TEntity>, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator<TEntity> {
return this.query(enumerable, meta)
}
}

0 comments on commit 0a54645

Please sign in to comment.