add generic repository

This commit is contained in:
Peter Maquiran
2024-08-07 16:31:31 +01:00
parent b61b87ce6c
commit 75a8822e57
6 changed files with 59 additions and 59 deletions
@@ -4,8 +4,8 @@ import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
// Define a type for the Result of repository operations
type RepositoryResult<T> = Result<T, Error>;
export class DexieRepository<ISchema, T> {
private table: EntityTable<ISchema, any>;
export class DexieRepository<T> {
private table: EntityTable<any, any>;
constructor(table: EntityTable<any, any>) {
this.table = table as any
@@ -56,39 +56,39 @@ export class DexieRepository<ISchema, T> {
}
}
// async find(filter: any) {
// try {
// const documents = await this.table.where(filter).toArray();
// return ok(documents);
// } catch (error) {
// return err(new Error('Failed to find documents: ' + error.message));
// }
// }
async find(filter: Object) {
try {
const documents = await this.table.where(filter).toArray();
return ok(documents);
} catch (error) {
return err(new Error('Failed to find documents: ' + error.message));
}
}
// async findOne(filter: any): Promise<RepositoryResult<T | undefined>> {
// try {
// const document = await this.table.where(filter).first();
// return ok(document);
// } catch (error) {
// return err(new Error('Failed to find document: ' + error.message));
// }
// }
async findOne(filter: Object): Promise<RepositoryResult<T | undefined>> {
try {
const document = await this.table.where(filter).first();
return ok(document);
} catch (error) {
return err(new Error('Failed to find document: ' + error.message));
}
}
// async findAll(): Promise<RepositoryResult<T[]>> {
// try {
// const documents = await this.table.toArray();
// return ok(documents);
// } catch (error) {
// return err(new Error('Failed to retrieve all documents: ' + error.message));
// }
// }
async findAll(): Promise<RepositoryResult<T[]>> {
try {
const documents = await this.table.toArray();
return ok(documents);
} catch (error) {
return err(new Error('Failed to retrieve all documents: ' + error.message));
}
}
// async count(filter?: any): Promise<RepositoryResult<number>> {
// try {
// const count = filter ? await this.table.where(filter).count() : await this.table.count();
// return ok(count);
// } catch (error) {
// return err(new Error('Failed to count documents: ' + error.message));
// }
// }
async count(filter?: Object): Promise<RepositoryResult<number>> {
try {
const count = filter ? await this.table.where(filter).count() : await this.table.count();
return ok(count);
} catch (error) {
return err(new Error('Failed to count documents: ' + error.message));
}
}
}