From 0a5464522c801e832c2fb9bf61050737651fc987 Mon Sep 17 00:00:00 2001 From: lostfields Date: Wed, 8 Apr 2020 18:39:06 +0200 Subject: [PATCH] better repository usage --- package.json | 2 +- src/linq/base.ts | 4 ++-- src/linq/index.ts | 4 +++- src/repository/repository.ts | 37 +++++++++++++++++++++++------------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index e3aa162..bb8ffe6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tfso/js-enumerable", - "version": "0.0.10", + "version": "0.0.11", "description": "", "main": "lib/index.js", "typings": "lib/index", diff --git a/src/linq/base.ts b/src/linq/base.ts index 27ea0d6..80caee4 100644 --- a/src/linq/base.ts +++ b/src/linq/base.ts @@ -26,7 +26,7 @@ export default abstract class Base implements IEnumerable | AsyncIterable | null = null - protected operators: Array> = [] + public operators: Array> = [] public constructor(items?: Array) public constructor(items?: Iterable) @@ -115,7 +115,7 @@ export default abstract class Base implements IEnumerable while (!(result = await Promise.resolve(iterator.next())).done) { diff --git a/src/linq/index.ts b/src/linq/index.ts index 05e57cc..088f2a7 100644 --- a/src/linq/index.ts +++ b/src/linq/index.ts @@ -1,2 +1,4 @@ export { Enumerable } from './enumerable' -export { IEnumerable } from './base' \ No newline at end of file +export { IEnumerable } from './base' + +export { LinqType } from './operator' \ No newline at end of file diff --git a/src/repository/repository.ts b/src/repository/repository.ts index 0c9d603..487986f 100644 --- a/src/repository/repository.ts +++ b/src/repository/repository.ts @@ -1,27 +1,38 @@ import { IEnumerable } from './../linq' -export interface IRepository extends AsyncIterable { - query(linq?: IEnumerable, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator +export interface IRepository, TEntityId> extends AsyncIterable { + query(enumerable?: IEnumerable, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator - create(item: Partial): Promise + create(item: Partial, meta?: Partial<{ etag: string }>): Promise read(id?: TEntityId): Promise - update(entity: Partial): Promise - delete(id: TEntityId): Promise + update(entity: Partial, meta?: Partial<{ etag: string }>, ...fields: Array): Promise + delete(id: TEntityId, meta?: Partial<{ etag: string }>): Promise + + getIterable(meta?: Partial<{ continuationToken: string }>): AsyncIterable } -export abstract class Repository implements IRepository, AsyncIterable { - abstract query(linq?: IEnumerable, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator +export abstract class Repository, TEntityId> implements IRepository, AsyncIterable { + + abstract query(enumerable?: IEnumerable, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator - abstract async create(item: Partial): Promise + abstract async create(item: Partial, meta?: Partial<{ etag: string }>): Promise abstract async read(id?: TEntityId): Promise - abstract async update(item: Partial): Promise - abstract async delete(id: TEntityId): Promise + abstract async update(item: Partial, meta?: Partial<{ etag: string }>, ...fields: Array): Promise + abstract async delete(id: TEntityId, meta?: Partial<{ etag: string }>): Promise + + public getIterable(meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterable { + return Object.assign(Object.create(Object.getPrototypeOf(this)), this, { + [Symbol.asyncIterator]: (enumerable?: IEnumerable) => { + return this.query(enumerable, meta) + } + }) + } - [Symbol.iterator](...options: any[]): IterableIterator> { + [Symbol.iterator](...options: any[]): IterableIterator { throw new TypeError('Repository is async iterable but it used as iterable') } - [Symbol.asyncIterator](linq?: IEnumerable, meta?: Partial<{ continuationToken: string }>): AsyncIterableIterator { - return this.query(linq, meta) + [Symbol.asyncIterator](enumerable?: IEnumerable, meta?: Partial<{ etag: string, continuationToken: string }>): AsyncIterableIterator { + return this.query(enumerable, meta) } } \ No newline at end of file