interface IStorage<T, K>
class Storage<T, K> implements IStorage<T, K>
class MatchStorage extends Storage<Match, number>
const matchStorageConfig: IStorageConfig = {
storageName: StorageName.Matches,
oftype: MatchStorage, // MatchStorage - это класс из примера выше
const instance = new oftype(тут аргументы);
interface IStorageConfig {
oftype: ??????? как описать тип под такую задачу
export interface IStorage<T, K> {
save(data: T | T[]): Promise<DbOperationResult<T>[]>;
private createStorageInstances(connection: IDBDatabase) {
this.storages = new Map();
this.config.storages.forEach(s => {
if (s.oftype) {
this.storages.set(s.storageName, new s.oftype(connection, s.storageName)); // ТУТъ
}
});
}
public getStorage(storageName: StorageName) {
return this.storages.get(storageName);
}
public read(key?: K | K[]): Promise<DbOperationResult<T>> {
export class MatchStorage extends Storage<Match, number> {
public constructor(database: IDBDatabase, storageName: string) {
super(database, storageName);
}
}
export const matchStorageConfig: IStorageConfig = {
storageName: StorageName.Matches,
oftype: MatchStorage, // !!!!!!!! ВОТъ
options: {
keyPath: "id"
},
indexes: [
{
storageIndexName: "lobby_type_index",
keyPath: "lobbyType",
},
{
storageIndexName: "start_date_index",
keyPath: "startDateTime",
}
]
};
....
export const defaultDbConfig: IDbConfig = {
version: 1,
dbname: 'test123',
storages: [
matchStorageConfig,
]
}
interface IStorageConfig<T, K> {
oftype: new () => IStorage<T, K>;
}
const matchStorageConfig: IStorageConfig<Match, number> = {
oftype: MatchStorage
}
const s = new matchStorageConfig.oftype();
IStorageConfig<T, K>
export interface IDbConfig {
dbname: string;
version: number;
storages: IStorageConfig[]; // ЗДЕСЬ
}
export interface IStorageConfig {
storageName: StorageName;
oftype: new (database: IDBDatabase, storageName: string) => IStorage<any, any>;
В плане того, что any в данном случае это не моветон? Как раз как будто бы логично выглядит, что тип данных и ключ хранилища может быть любой.
export interface IStorage {
save(data: unknown): Promise<DbOperationResult<unknown>[]>;
}
export interface IDbConfig {
dbname: string;
version: number;
storages: IStorageConfig<any, any>[]; // Закрываю типы тут
}
export interface IStorageConfig<T, K> {
storageName: StorageName;
oftype: new (database: IDBDatabase, storageName: StorageName) => IStorage<T, K>;
export const matchStorageConfig: IStorageConfig<Match, number> = {
storageName: StorageName.Matches,
oftype: MatchStorage,
private storages: Map<StorageName, IStorage<any, any>>;
const matchStorage = database.getStorage(StorageName.Matches);
const ms = new MatchService(matchStorage);
Правильно ли я понимаю, что ваше решение базируется на том, что обязательно надо вводить дженерик на классе Database и иначе никак?
public getStorage<T>(storageName: StorageName): T {
return this.storages.get(storageName) as T;
}
const matchStorage = database.getStorage<MatchStorage>(StorageName.Matches);
database.getStorage<MatchStorage>(StorageName.Matches);
database.getStorage(StorageName.Matches) as MatchStorage;
, она не должна вводить в заблуждение.В классе MyDatabase я использую синглтон паттерн