mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
upload attachment
This commit is contained in:
@@ -1,26 +1,47 @@
|
||||
import { Result, ok, err, ResultAsync } from 'neverthrow';
|
||||
import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
|
||||
import { ZodError, ZodObject, ZodSchema } from 'zod';
|
||||
|
||||
// Define a type for the Result of repository operations
|
||||
type RepositoryResult<T> = Result<T, Error>;
|
||||
type RepositoryResult<T, E> = Result<T, Error | ZodError<E>>;
|
||||
|
||||
|
||||
export class DexieRepository<T> {
|
||||
private table: EntityTable<any, any>;
|
||||
private ZodSchema: ZodSchema<T>
|
||||
private ZodPartialSchema: ZodSchema<T>
|
||||
|
||||
constructor(table: EntityTable<any, any>) {
|
||||
constructor(table: EntityTable<any, any>, ZodSchema: ZodSchema) {
|
||||
this.table = table as any
|
||||
this.ZodSchema = ZodSchema
|
||||
this.ZodPartialSchema = (ZodSchema as ZodObject<any>).partial() 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 insert(document: T): Promise<RepositoryResult<number, T>> {
|
||||
|
||||
const dataValidation = this.ZodSchema.safeParse(document)
|
||||
|
||||
if(dataValidation.success) {
|
||||
try {
|
||||
const id = await this.table.add(dataValidation.data);
|
||||
return ok(id);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to insert document: ' + error.message));
|
||||
}
|
||||
} else {
|
||||
return err((dataValidation as unknown as ZodError<T>))
|
||||
}
|
||||
}
|
||||
|
||||
async insertMany(documents: T[]): Promise<RepositoryResult<number[]>> {
|
||||
async insertMany(documents: T[]): Promise<RepositoryResult<number[], ZodError<T>>> {
|
||||
// Validate each document
|
||||
const schema = this.ZodSchema.array()
|
||||
|
||||
const validationResult = schema.safeParse(documents)
|
||||
if(!validationResult.success) {
|
||||
return err((validationResult as unknown as ZodError<T>))
|
||||
}
|
||||
|
||||
try {
|
||||
const ids = await this.table.bulkAdd(documents as any);
|
||||
return ok(ids);
|
||||
@@ -30,15 +51,22 @@ export class DexieRepository<T> {
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
const dataValidation = this.ZodPartialSchema.safeParse(document)
|
||||
|
||||
if(dataValidation.success) {
|
||||
try {
|
||||
const updatedCount = await this.table.update(id, dataValidation.data);
|
||||
return ok(updatedCount);
|
||||
} catch (error) {
|
||||
return err(new Error('Failed to update document: ' + error.message));
|
||||
}
|
||||
} else {
|
||||
return err((dataValidation as unknown as ZodError<T>))
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: any): Promise<RepositoryResult<void>> {
|
||||
async delete(id: any): Promise<RepositoryResult<void, T>> {
|
||||
try {
|
||||
await this.table.delete(id);
|
||||
return ok(undefined);
|
||||
@@ -65,7 +93,7 @@ export class DexieRepository<T> {
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(filter: Object): Promise<RepositoryResult<T | undefined>> {
|
||||
async findOne(filter: Object): Promise<RepositoryResult<T | undefined, T>> {
|
||||
try {
|
||||
const document = await this.table.where(filter).first();
|
||||
return ok(document);
|
||||
@@ -74,7 +102,7 @@ export class DexieRepository<T> {
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(): Promise<RepositoryResult<T[]>> {
|
||||
async findAll(): Promise<RepositoryResult<T[], T>> {
|
||||
try {
|
||||
const documents = await this.table.toArray();
|
||||
return ok(documents);
|
||||
@@ -83,7 +111,7 @@ export class DexieRepository<T> {
|
||||
}
|
||||
}
|
||||
|
||||
async count(filter?: Object): Promise<RepositoryResult<number>> {
|
||||
async count(filter?: Object): Promise<RepositoryResult<number, T>> {
|
||||
try {
|
||||
const count = filter ? await this.table.where(filter).count() : await this.table.count();
|
||||
return ok(count);
|
||||
|
||||
Reference in New Issue
Block a user