mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
add generic repository
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user