Skip to content

Commit

Permalink
chore: making it a bit more strict null friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Jun 12, 2024
1 parent 4328f1b commit 3cfe700
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
16 changes: 8 additions & 8 deletions src/linq/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface IEnumerable<TEntity> extends Iterable<TEntity>, AsyncIterable<T
* @param predicate javascript expression
* @param parameters any javascript parameters has to be declared
*/
where(predicate: (it: TEntity, ...param: any[]) => boolean, ...parameters: any[]): this
where(predicate: (it: Required<TEntity>, ...param: any[]) => boolean, ...parameters: any[]): this

orderBy(property: (it: TEntity) => void): this
orderBy(property: keyof TEntity): this
Expand Down Expand Up @@ -142,8 +142,8 @@ export interface IEnumerable<TEntity> extends Iterable<TEntity>, AsyncIterable<T
*/
select<TResult>(selector: (it: TEntity) => TResult): IEnumerable<TResult>

first(items?: Array<TEntity>): TEntity
firstAsync(items?: Array<TEntity>): Promise<TEntity>
first(items?: Array<TEntity>): TEntity | null
firstAsync(items?: Array<TEntity>): Promise<TEntity | null>

toArray(items: Array<TEntity>): Array<TEntity>
toArray(): Array<TEntity>
Expand Down Expand Up @@ -222,7 +222,7 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* @param predicate javascript expression
* @param parameters any javascript parameters has to be declared
*/
public where(predicate: (it: TEntity, ...param: any[]) => boolean, ...parameters: any[]): this
public where(predicate: (it: Required<TEntity>, ...param: any[]) => boolean, ...parameters: any[]): this
public where(): this {
let predicate: any = arguments[0],
parameters: Array<any> = [];
Expand All @@ -232,11 +232,11 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>

switch (typeof predicate) {
case 'string':
this._operations.add(new WhereOperator<TEntity>('OData', predicate));
this._operations.add(new WhereOperator<Required<TEntity>>('OData', predicate));
break;

case 'function':
this._operations.add(new WhereOperator<TEntity>('Javascript', predicate, ...parameters));
this._operations.add(new WhereOperator<Required<TEntity>>('Javascript', predicate, ...parameters));
break;

default:
Expand Down Expand Up @@ -451,7 +451,7 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
return enumerable
}

public first(items?: Array<TEntity>): TEntity {
public first(items?: Array<TEntity>): TEntity | null {
if (items)
this.from(items);

Expand All @@ -462,7 +462,7 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
return null;
}

public async firstAsync(items?: Array<TEntity>): Promise<TEntity> {
public async firstAsync(items?: Array<TEntity>): Promise<TEntity | null> {
if (items)
this.from(items);

Expand Down
44 changes: 16 additions & 28 deletions src/repository/baserepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { ILogicalExpression, LogicalExpression, LogicalOperatorType } from './..
export { IEnumerable, IRecordSetMeta }

export interface IBaseRepository<TEntity, TEntityId> {
create(entity: TEntity, meta?: IRecordSetMeta): Promise<TEntity>
create(entity: TEntity, meta?: IRecordSetMeta): Promise<TEntity | null>
create(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity | null>
create<T extends TEntity>(entity: T, meta?: IRecordSetMeta): Promise<T | null>

read(id: TEntityId, meta?: IRecordSetMeta): Promise<TEntity>
read(id?: TEntityId, meta?: IRecordSetMeta): Promise<TEntity | null>
readAll(query: IEnumerable<TEntity>, meta?: IRecordSetMeta): Promise<TEntity[]>

update(entity: TEntity, meta?: IRecordSetMeta): Promise<boolean>
update(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity | boolean | null>
delete(entity: TEntity, meta?: IRecordSetMeta): Promise<boolean>
}

Expand All @@ -31,35 +33,21 @@ abstract class BaseRepository<TEntity, TEntityId> implements IBaseRepository<TEn

}

abstract read(id: TEntityId): Promise<TEntity>
abstract read(id: Partial<TEntityId>): Promise<TEntity>
abstract read(id: TEntityId, meta?: IRecordSetMeta): Promise<TEntity>
abstract read(id: Partial<TEntityId>, meta?: IRecordSetMeta): Promise<TEntity>
abstract read(id: TEntityId, meta?: IRecordSetMeta): Promise<TEntity>

abstract read(id?: TEntityId, meta?: IRecordSetMeta): Promise<TEntity | null>
abstract read(id?: Partial<TEntityId>, meta?: IRecordSetMeta): Promise<TEntity | null>

// ((t) => t.gender == 'female' && t.age >= 16)({gender: 'female', age: 17}) => true
// ((t) => t.gender == 'female' && t.age >= 16).toString() => (t) => t.gender == \'female\' && t.age >= 16
abstract readAll(query: IEnumerable<TEntity>): Promise<TEntity[]>
abstract readAll(query: IEnumerable<TEntity>, meta?: IRecordSetMeta): Promise<TEntity[]>
abstract readAll(query: IEnumerable<TEntity>, meta?: IRecordSetMeta, parent?: IParentOptions): Promise<TEntity[]>

abstract create(entity: TEntity): Promise<TEntity>
abstract create(entity: Partial<TEntity>): Promise<TEntity>
abstract create(entity: TEntity, meta?: IRecordSetMeta): Promise<TEntity>
abstract create(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity>
abstract create(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity>

abstract update(entity: TEntity): Promise<boolean>
abstract update(entity: Partial<TEntity>): Promise<boolean>
abstract update(entity: TEntity, meta?: IRecordSetMeta): Promise<boolean>
abstract update(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<boolean>
abstract update(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<boolean>

abstract delete(entity: TEntity): Promise<boolean>
abstract delete(entity: Partial<TEntity>): Promise<boolean>
abstract readAll(query?: IEnumerable<TEntity>, meta?: IRecordSetMeta, parent?: IParentOptions): Promise<TEntity[]>

abstract create(entity: TEntity, meta?: IRecordSetMeta): Promise<TEntity | null>
abstract create(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity | null>

abstract update(entity: TEntity, meta?: IRecordSetMeta): Promise<TEntity | boolean | null>
abstract update(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<TEntity | boolean | null>

abstract delete(entity: TEntity, meta?: IRecordSetMeta): Promise<boolean>
abstract delete(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<boolean>
abstract delete(entity: Partial<TEntity>, meta?: IRecordSetMeta): Promise<boolean>

public beginTransaction(): Promise<void> {
return Promise.resolve();
Expand Down

0 comments on commit 3cfe700

Please sign in to comment.