Files
doneit-web/src/app/infra/repository/adapter.ts
T
Peter Maquiran 9c8ecc182f fix chat
2024-10-08 13:16:01 +01:00

36 lines
1.3 KiB
TypeScript

import { Result } from 'neverthrow';
import { ZodError} from 'zod';
import { IDBError } from './types';
import Dexie, { EntityTable, Table } from 'Dexie';
// Define a type for the Result of repository operations
export type RepositoryResult<T, E> = Result<T, IDBError<E>>;
export abstract class IDexieRepository<T, R, I = EntityTable<any, any>> {
abstract createTransaction(callback: (table: I) => Promise<void>): Promise<void>
abstract insert(document: T): Promise<RepositoryResult<number, T>>
abstract insertMany(documents: T[]): Promise<RepositoryResult<number[], T[]>>
abstract update(id: any, updatedDocument: Partial<T>) : Promise<RepositoryResult<number, T>>
abstract delete(id: any): Promise<RepositoryResult<void, T>>
abstract findById(id: any) : Promise<RepositoryResult<R, any>>
abstract find(filter: Partial<T>): Promise<RepositoryResult<R[], T[]>>
abstract findOne(filter: Partial<T>): Promise<RepositoryResult<T & ILocalModel<T, R, I> | undefined, T>>
abstract findAll(): Promise<RepositoryResult<T[], T>>
abstract count(filter?: Object): Promise<RepositoryResult<number, T>>
abstract clear(): Promise<Result<any, any>>
}
export abstract class ILocalModel<T, R, I = EntityTable<any, any>>{
abstract save() : Promise<RepositoryResult<number, T>>
abstract delete()
}