mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 13:02:56 +00:00
fix chat bold
This commit is contained in:
@@ -42,7 +42,8 @@ export const MessageTableSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
description: z.string().nullable().optional(),
|
||||
mimeType: z.string().optional()
|
||||
})).optional()
|
||||
})).optional(),
|
||||
origin: z.enum(['history', 'local', 'incoming']).optional()
|
||||
})
|
||||
|
||||
export type MessageTable = z.infer<typeof MessageTableSchema>
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
import { Result } from 'neverthrow';
|
||||
import { ZodError} from 'zod';
|
||||
import { IDBError } from './types';
|
||||
|
||||
// Define a type for the Result of repository operations
|
||||
export type RepositoryResult<T, E> = Result<T, Error | ZodError<E>>;
|
||||
|
||||
export type RepositoryResultNew<T, E> = Result<T, IDBError<E>>;
|
||||
export abstract class IDexieRepository<T, R> {
|
||||
|
||||
abstract insert(document: T): Promise<RepositoryResult<number, T>>
|
||||
abstract insert(document: T): Promise<RepositoryResultNew<number, T>>
|
||||
|
||||
abstract insertMany(documents: T[]): Promise<RepositoryResult<number[], ZodError<T>>>
|
||||
|
||||
abstract update(id: any, updatedDocument: Partial<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 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 find(filter: Partial<T>): Promise<RepositoryResult<R[], T[]>>
|
||||
|
||||
abstract findOne(filter: Partial<T>): Promise<RepositoryResult<T | undefined, T>>
|
||||
abstract findOne(filter: Partial<T>): Promise<RepositoryResult<T | undefined, T>>
|
||||
|
||||
abstract findAll(): Promise<RepositoryResult<T[], T>>
|
||||
abstract findAll(): Promise<RepositoryResult<T[], T>>
|
||||
|
||||
abstract count(filter?: Object): Promise<RepositoryResult<number, T>>
|
||||
abstract count(filter?: Object): Promise<RepositoryResult<number, T>>
|
||||
}
|
||||
|
||||
@@ -2,9 +2,30 @@ import { Result, ok, err } from 'neverthrow';
|
||||
import { EntityTable } from 'Dexie';
|
||||
import { ZodError, ZodObject, ZodSchema } from 'zod';
|
||||
import { Logger } from 'src/app/services/logger/main/service';
|
||||
import { IDexieRepository } from '../adapter'
|
||||
import { IDexieRepository, RepositoryResultNew } from '../adapter'
|
||||
import { IDBError } from '../types';
|
||||
|
||||
// Define a type for the Result of repository operations
|
||||
|
||||
class DBError<T> extends Error implements IDBError<T> {
|
||||
zodError?: ZodError<T>;
|
||||
parameters: T;
|
||||
error?: any;
|
||||
|
||||
constructor(data: IDBError<T>) {
|
||||
super(data.message);
|
||||
this.zodError = data.zodError;
|
||||
this.parameters = data.parameters;
|
||||
this.error = data.error;
|
||||
|
||||
// // Manually capture the stack trace if needed
|
||||
// if (Error.captureStackTrace) {
|
||||
// Error.captureStackTrace(this, DBError);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type RepositoryResult<T, E> = Result<T, Error | ZodError<E>>;
|
||||
|
||||
export class DexieRepository<T, R> implements IDexieRepository<T, R> {
|
||||
@@ -18,7 +39,7 @@ export class DexieRepository<T, R> implements IDexieRepository<T, R> {
|
||||
this.ZodPartialSchema = (ZodSchema as ZodObject<any>).partial() as any;
|
||||
}
|
||||
|
||||
async insert(document: T): Promise<RepositoryResult<number, T>> {
|
||||
async insert(document: T): Promise<RepositoryResultNew<number, T>> {
|
||||
|
||||
const dataValidation = this.ZodSchema.safeParse(document)
|
||||
|
||||
@@ -28,14 +49,22 @@ export class DexieRepository<T, R> implements IDexieRepository<T, R> {
|
||||
return ok(id);
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return err(new Error('Failed to insert document: ' + error));
|
||||
return err(new DBError({
|
||||
message: `dexie.js failed to insert into ${this.table.name}`,
|
||||
parameters: document,
|
||||
error: error
|
||||
}))
|
||||
}
|
||||
} else {
|
||||
Logger.error(`dexie.js failed to insert into ${this.table.name}, invalid data`, {
|
||||
data: document,
|
||||
zodError: dataValidation.error.issues
|
||||
});
|
||||
return err((dataValidation as unknown as ZodError<T>))
|
||||
return err(new DBError({
|
||||
message: `dexie.js failed to insert into ${this.table.name}, invalid data`,
|
||||
parameters: document,
|
||||
zodError: dataValidation.error
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
// upsertedCount: number;
|
||||
// };
|
||||
|
||||
import { ZodError } from "zod"
|
||||
|
||||
// export type RemovedModel = {
|
||||
// deletedCount: number;
|
||||
// deleted: boolean;
|
||||
@@ -34,3 +36,11 @@
|
||||
// value: unknown[];
|
||||
// command: DatabaseOperationEnum;
|
||||
// };
|
||||
|
||||
|
||||
export type IDBError<T> = {
|
||||
message: string,
|
||||
zodError?: ZodError<T>,
|
||||
parameters?: T,
|
||||
error?: any
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user