Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

FRONTEND-2309 :: Feature :: Remove getAll and putAll #142

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
120 changes: 18 additions & 102 deletions packages/core/src/repository/cache.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@ export class CacheSyncOperation implements Operation {

export interface ObjectValidator {
isObjectValid<T>(object: T): boolean;
orioljp marked this conversation as resolved.
Show resolved Hide resolved
isArrayValid<T>(array: T[]): boolean;
}

export class DefaultObjectValidator implements ObjectValidator {
isObjectValid<T>(_object: T): boolean {
return true;
}
isArrayValid<T>(array: T[]): boolean {
if (array.length === 0) {
return false;
}
for (const element of array) {
if (!this.isObjectValid(element)) {
export class ArrayValidator implements ObjectValidator {
constructor(private readonly validator: ObjectValidator) {}

public isObjectValid<T>(array: T): boolean {
if (Array.isArray(array)) {
if (array.length === 0) {
return false;
}
for (const element of array) {
if (!this.validator.isObjectValid(element)) {
orioljp marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}
return true;
}
return false;
}
}

export class DefaultObjectValidator implements ObjectValidator {
isObjectValid<T>(_object: T): boolean {
return true;
}
}
Expand Down Expand Up @@ -109,71 +116,6 @@ export class CacheRepository<T> implements Repository<T> {
}
}

/**
* @deprecated please use get with an array type instead
*/
public async getAll(query: Query, operation: Operation): Promise<T[]> {
console.warn('getAll is deprecated. Please use get instead');
switch (operation.constructor) {
case DefaultOperation:
return this.getAll(query, new CacheSyncOperation());
case MainOperation:
return this.getMain.getAll(query);
case CacheOperation:
return this.getCache
.getAll(query)
.then((values: T[]) => {
if (values == null) {
throw new NotFoundError();
}
if (!this.validator.isArrayValid(values)) {
throw new NotValidError();
}
return values;
})
.catch((err: Error) => {
const op = operation as CacheOperation;
if (err instanceof NotValidError && op.fallback(err)) {
return this.getCache.getAll(query);
} else {
throw err;
}
});
case MainSyncOperation:
return this.getMain.getAll(query).then((values: T[]) => {
return this.putCache.putAll(values, query);
});
case CacheSyncOperation:
return this.getCache
.getAll(query)
.then((values: T[]) => {
if (values == null) {
throw new NotFoundError();
}
if (!this.validator.isArrayValid(values)) {
throw new NotValidError();
}
return values;
})
.catch((err: Error) => {
if (err instanceof NotValidError || err instanceof NotFoundError) {
return this.getAll(query, new MainSyncOperation()).catch((finalError: Error) => {
const op = operation as CacheSyncOperation;
if (op.fallback(finalError)) {
return this.getCache.getAll(query);
} else {
throw finalError;
}
});
} else {
throw err;
}
});
default:
throw new OperationNotSupportedError();
}
}

public async put(value: T | undefined, query: Query, operation: Operation): Promise<T> {
switch (operation.constructor) {
case DefaultOperation:
Expand All @@ -195,32 +137,6 @@ export class CacheRepository<T> implements Repository<T> {
}
}

/**
* @deprecated please use put with an array type instead
*/
public async putAll(values: T[] | undefined, query: Query, operation: Operation): Promise<T[]> {
console.warn('putAll is deprecated. Please use put instead');

switch (operation.constructor) {
case DefaultOperation:
return this.putAll(values, query, new MainSyncOperation());
case MainOperation:
return this.putMain.putAll(values, query);
case CacheOperation:
return this.putCache.putAll(values, query);
case MainSyncOperation:
return this.putMain.putAll(values, query).then((array: T[]) => {
return this.putCache.putAll(array, query);
});
case CacheSyncOperation:
return this.putCache.putAll(values, query).then((array: T[]) => {
return this.putMain.putAll(array, query);
});
default:
throw new OperationNotSupportedError();
}
}

public async delete(query: Query, operation: Operation): Promise<void> {
switch (operation.constructor) {
case DefaultOperation:
Expand Down
27 changes: 0 additions & 27 deletions packages/core/src/repository/data-source/data-source-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,10 @@ export class DataSourceMapper<In, Out> implements DataSource<Out> {
return this.getMapper.get(query);
}

/**
* @deprecated please use get with an array type instead
*/
public getAll(query: Query): Promise<Out[]> {
console.warn('getAll is deprecated. Please use get instead');
return this.getMapper.getAll(query);
}

public put(value: Out | undefined, query: Query): Promise<Out> {
return this.putMapper.put(value, query);
}

/**
* @deprecated please use put with an array type instead
*/
public putAll(values: Out[] | undefined, query: Query): Promise<Out[]> {
console.warn('putAll is deprecated. Please use put instead');
return this.putMapper.putAll(values, query);
}

public delete(query: Query): Promise<void> {
return this.deleteDataSource.delete(query);
}
Expand All @@ -69,11 +53,6 @@ export class GetDataSourceMapper<In, Out> implements GetDataSource<Out> {
const result: In = await this.getDataSource.get(query);
return this.toOutMapper.map(result);
}

public async getAll(query: Query): Promise<Out[]> {
const results: In[] = await this.getDataSource.getAll(query);
return results.map((r: In) => this.toOutMapper.map(r));
}
}

/**
Expand All @@ -95,10 +74,4 @@ export class PutDataSourceMapper<In, Out> implements PutDataSource<Out> {
const result: In = await this.putDataSource.put(mapped, query);
return this.toOutMapper.map(result);
}

public async putAll(values: Out[] | undefined, query: Query): Promise<Out[]> {
const mapped: In[] | undefined = values ? values.map((v) => this.toInMapper.map(v)) : undefined;
const results: In[] = await this.putDataSource.putAll(mapped, query);
return results.map((r: In) => this.toOutMapper.map(r));
}
}
8 changes: 0 additions & 8 deletions packages/core/src/repository/data-source/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import { Query } from '..';

export interface GetDataSource<T> {
get: (query: Query) => Promise<T>;
/**
* @deprecated please use get with an array type instead
*/
getAll: (query: Query) => Promise<T[]>;
}

export interface PutDataSource<T> {
put: (value: T | undefined, query: Query) => Promise<T>;
/**
* @deprecated please use put with an array type instead
*/
putAll: (values: T[] | undefined, query: Query) => Promise<T[]>;
}

export interface DeleteDataSource {
Expand Down
64 changes: 0 additions & 64 deletions packages/core/src/repository/data-source/in-memory.data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { DeviceConsoleLogger, Logger } from '../../helpers';

export class InMemoryDataSource<T> implements DataSource<T> {
private objects: Map<string, T> = new Map();
orioljp marked this conversation as resolved.
Show resolved Hide resolved
private arrays: Map<string, T[]> = new Map();

constructor(private readonly logger: Logger = new DeviceConsoleLogger(undefined, 'InMemoryDataSource')) {}

Expand All @@ -29,38 +28,6 @@ export class InMemoryDataSource<T> implements DataSource<T> {
}
}

/**
* @deprecated please use get with an array type instead
*/
public async getAll(query: Query): Promise<T[]> {
console.warn('getAll is deprecated. Please use get instead');

if (query instanceof IdsQuery) {
return query.keys.map((key) => {
if (!this.objects.has(key)) {
throw new NotFoundError();
}

// SAFETY `as T`: `undefined` case handled above
return this.objects.get(key) as T;
});
} else if (query instanceof KeyQuery) {
if (!this.arrays.has(query.key)) {
throw new NotFoundError();
}

// SAFETY `as T[]`: `undefined` case handled above
return this.arrays.get(query.key) as T[];
} else if (query instanceof AllObjectsQuery) {
return [
...Array.from(this.objects.values()),
...Array.from(this.arrays.values()).flatMap((values) => values),
];
}

throw new QueryNotSupportedError();
}

public async put(value: T | undefined, query: Query): Promise<T> {
if (typeof value === 'undefined') {
throw new InvalidArgumentError(`InMemoryDataSource: value can't be undefined`);
Expand All @@ -77,45 +44,14 @@ export class InMemoryDataSource<T> implements DataSource<T> {
}
}

/**
* @deprecated please use put with an array type instead
*/
public async putAll(values: T[] | undefined, query: Query): Promise<T[]> {
console.warn('putAll is deprecated. Please use put instead');

if (typeof values === 'undefined') {
throw new InvalidArgumentError(`InMemoryDataSource: values can't be undefined`);
}

if (query instanceof IdsQuery) {
if (values.length !== query.keys.length) {
throw new InvalidArgumentError(`InMemoryDataSource: values & query.keys have different length`);
}

query.keys.forEach((key, idx) => {
this.objects.set(key, values[idx]);
});

return values;
} else if (query instanceof KeyQuery) {
this.arrays.set(query.key, values);
return values;
} else {
throw new QueryNotSupportedError();
}
}

public async delete(query: Query): Promise<void> {
if (query instanceof IdsQuery) {
for (const key of query.keys) {
this.arrays.delete(key);
this.objects.delete(key);
}
} else if (query instanceof KeyQuery) {
this.arrays.delete(query.key);
this.objects.delete(query.key);
} else if (query instanceof AllObjectsQuery) {
this.arrays.clear();
this.objects.clear();
} else {
throw new QueryNotSupportedError();
Expand Down
15 changes: 1 addition & 14 deletions packages/core/src/repository/data-source/mock.data-source.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { DataSource } from './data-source';
import { Query } from '..';
import { DeviceConsoleLogger, Logger } from '../../helpers';

export class MockDataSource<T> implements DataSource<T> {
constructor(
private readonly one: T,
private readonly many: T[],
private readonly logger: Logger = new DeviceConsoleLogger(),
) {}
constructor(private readonly one: T) {}

public async get(_query: Query): Promise<T> {
return this.one;
}

public async getAll(_query: Query): Promise<T[]> {
return this.many;
}

public async put(_value: T | undefined, _query: Query): Promise<T> {
return this.one;
}

public async putAll(_values: T[] | undefined, _query: Query): Promise<T[]> {
return this.many;
}

public async delete(_query: Query): Promise<void> {
return;
}
Expand Down
13 changes: 4 additions & 9 deletions packages/core/src/repository/data-source/network.data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,12 @@ export class NetworkDataSource implements DataSource<unknown> {

return request;
}

public async getAll(_query: Query): Promise<unknown[]> {
throw new MethodNotImplementedError();
}

public async putAll(_values: unknown[] | undefined, _query: Query): Promise<unknown[]> {
throw new MethodNotImplementedError();
}
}

export function provideDefaultNetworkDataSource<T extends unknown | void>(requestService: ApiRequestService, type?: Type<T>): DataSource<T> {
export function provideDefaultNetworkDataSource<T extends unknown | void>(
requestService: ApiRequestService,
type?: Type<T>,
): DataSource<T> {
const dataSource = new NetworkDataSource(requestService);
return new DataSourceMapper(
dataSource,
Expand Down
Loading