Simplify generics

This commit is contained in:
Aleksandar Stanisic
2020-10-26 21:32:04 +01:00
parent 9130c1b0b8
commit c30a5fcb0c

View File

@@ -26,7 +26,7 @@ import getDefaultValue from '../utils/get-default-value';
import { InvalidPayloadException } from '../exceptions';
import { ForbiddenException } from '../exceptions';
export class ItemsService<Item extends AnyItem> implements AbstractService {
export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractService {
collection: string;
knex: Knex;
accountability: Accountability | null;
@@ -46,11 +46,9 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
return this;
}
async create<T extends AnyItem = Partial<Item>>(data: T[]): Promise<PrimaryKey[]>;
async create<T extends AnyItem = Partial<Item>>(data: T): Promise<PrimaryKey>;
async create<T extends AnyItem = Partial<Item>>(
data: T | T[]
): Promise<PrimaryKey | PrimaryKey[]> {
async create(data: Partial<Item>[]): Promise<PrimaryKey[]>;
async create(data: Partial<Item>): Promise<PrimaryKey>;
async create(data: Partial<Item> | Partial<Item>[]): Promise<PrimaryKey | PrimaryKey[]> {
const primaryKeyField = await this.schemaInspector.primary(this.collection);
const columns = await this.schemaInspector.columns(this.collection);
@@ -196,7 +194,7 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
return Array.isArray(data) ? savedPrimaryKeys : savedPrimaryKeys[0];
}
async readByQuery<T extends AnyItem = Item>(query: Query): Promise<null | T | T[]> {
async readByQuery(query: Query): Promise<null | Partial<Item> | Partial<Item>[]> {
const authorizationService = new AuthorizationService({
accountability: this.accountability,
knex: this.knex,
@@ -212,24 +210,24 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
}
const records = await runAST(ast, { knex: this.knex });
return records as T | T[] | null;
return records as Partial<Item> | Partial<Item>[] | null;
}
readByKey<T extends AnyItem = Item>(
readByKey(
keys: PrimaryKey[],
query?: Query,
action?: PermissionsAction
): Promise<null | T[]>;
readByKey<T extends AnyItem = Item>(
): Promise<null | Partial<Item>[]>;
readByKey(
key: PrimaryKey,
query?: Query,
action?: PermissionsAction
): Promise<null | T>;
async readByKey<T extends AnyItem = Item>(
): Promise<null | Partial<Item>>;
async readByKey(
key: PrimaryKey | PrimaryKey[],
query: Query = {},
action: PermissionsAction = 'read'
): Promise<null | T | T[]> {
): Promise<null | Partial<Item> | Partial<Item>[]> {
query = clone(query);
const primaryKeyField = await this.schemaInspector.primary(this.collection);
const keys = toArray(key);
@@ -266,14 +264,14 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
if (result === null) throw new ForbiddenException();
return result as T | T[] | null;
return result as Partial<Item> | Partial<Item>[] | null;
}
update<T extends AnyItem = Partial<Item>>(data: T, keys: PrimaryKey[]): Promise<PrimaryKey[]>;
update<T extends AnyItem = Partial<Item>>(data: T, key: PrimaryKey): Promise<PrimaryKey>;
update<T extends AnyItem = Partial<Item>>(data: T[]): Promise<PrimaryKey[]>;
async update<T extends AnyItem = Partial<Item>>(
data: T | T[],
update(data: Partial<Item>, keys: PrimaryKey[]): Promise<PrimaryKey[]>;
update(data: Partial<Item>, key: PrimaryKey): Promise<PrimaryKey>;
update(data: Partial<Item>[]): Promise<PrimaryKey[]>;
async update(
data: Partial<Item> | Partial<Item>[],
key?: PrimaryKey | PrimaryKey[]
): Promise<PrimaryKey | PrimaryKey[]> {
const primaryKeyField = await this.schemaInspector.primary(this.collection);
@@ -434,10 +432,7 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
return keys;
}
async updateByQuery<T extends AnyItem = Partial<Item>>(
data: T,
query: Query
): Promise<PrimaryKey[]> {
async updateByQuery(data: Partial<Item>, query: Query): Promise<PrimaryKey[]> {
const primaryKeyField = await this.schemaInspector.primary(this.collection);
const readQuery = cloneDeep(query);
readQuery.fields = [primaryKeyField];
@@ -455,11 +450,9 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
return await this.update(data, keys);
}
upsert<T extends AnyItem = Partial<Item>>(data: T[]): Promise<PrimaryKey[]>;
upsert<T extends AnyItem = Partial<Item>>(data: T): Promise<PrimaryKey>;
async upsert<T extends AnyItem = Partial<Item>>(
data: T | T[]
): Promise<PrimaryKey | PrimaryKey[]> {
upsert(data: Partial<Item>[]): Promise<PrimaryKey[]>;
upsert(data: Partial<Item>): Promise<PrimaryKey>;
async upsert(data: Partial<Item> | Partial<Item>[]): Promise<PrimaryKey | PrimaryKey[]> {
const primaryKeyField = await this.schemaInspector.primary(this.collection);
const payloads = toArray(data);
const primaryKeys: PrimaryKey[] = [];
@@ -561,11 +554,11 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
return await this.delete(keys);
}
async readSingleton<T extends AnyItem = Item>(query: Query): Promise<T> {
async readSingleton(query: Query): Promise<Partial<Item>> {
query = clone(query);
query.single = true;
const record = (await this.readByQuery<T>(query)) as T;
const record = (await this.readByQuery(query)) as Partial<Item>;
if (!record) {
const columns = await this.schemaInspector.columnInfo(this.collection);
@@ -575,13 +568,13 @@ export class ItemsService<Item extends AnyItem> implements AbstractService {
defaults[column.name] = getDefaultValue(column);
}
return defaults as T;
return defaults as Partial<Item>;
}
return record;
}
async upsertSingleton<T extends AnyItem = Partial<Item>>(data: T) {
async upsertSingleton(data: Partial<Item>) {
const primaryKeyField = await this.schemaInspector.primary(this.collection);
const record = await this.knex