mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
set member to admin
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { Result, ok, err, ResultAsync } from 'neverthrow';
|
||||
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>;
|
||||
|
||||
constructor(table: EntityTable<any, any>) {
|
||||
this.table = table as any
|
||||
}
|
||||
|
||||
async insert(document: T): Promise<RepositoryResult<number>> {
|
||||
try {
|
||||
const id = await this.table.add(document as any);
|
||||
return ok(id);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to insert document: ' + error.message));
|
||||
}
|
||||
}
|
||||
|
||||
async insertMany(documents: T[]): Promise<RepositoryResult<number[]>> {
|
||||
try {
|
||||
const ids = await this.table.bulkAdd(documents as any);
|
||||
return ok(ids);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to insert multiple documents: ' + error.message));
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: any, updatedDocument: Partial<T>) {
|
||||
try {
|
||||
const updatedCount = await this.table.update(id, updatedDocument as any);
|
||||
return ok(updatedCount);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to update document: ' + error.message));
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: any): Promise<RepositoryResult<void>> {
|
||||
try {
|
||||
await this.table.delete(id);
|
||||
return ok(undefined);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to delete document: ' + error.message));
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: any) {
|
||||
try {
|
||||
const document = await this.table.get(id);
|
||||
return ok(document);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to find document by ID: ' + error.message));
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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 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));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user