fix message

This commit is contained in:
Peter Maquiran
2024-09-05 11:45:54 +01:00
parent 106267aee9
commit b0e1dd74cd
16 changed files with 237 additions and 78 deletions
@@ -7,11 +7,11 @@ export const AttachmentTableSchema = z.object({
$id: z.number().optional(), // local id
$messageId: z.string(),
attachmentId: z.string().optional(),
file: z.instanceof(Blob),
file: z.instanceof(Blob).optional(),
base64: zodDataUrlSchema.nullable().optional(),
//
fileType: z.nativeEnum(MessageAttachmentFileType),
source: z.nativeEnum(MessageAttachmentSource),
fileType: z.nativeEnum(MessageAttachmentFileType).optional(),
source: z.nativeEnum(MessageAttachmentSource).optional(),
fileName: z.string().optional(),
applicationId: z.number().optional(),
docId: z.string().optional(),
@@ -49,5 +49,5 @@ export const MessageTableSchema = z.object({
export type MessageTable = z.infer<typeof MessageTableSchema>
export type DexieMessageTable = EntityTable<MessageTable, '$id'>;
export const messageTableColumn = '$id, id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock'
export const messageTableColumn = '$id, id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock, sending'
+3 -1
View File
@@ -1,11 +1,13 @@
import { Result } from 'neverthrow';
import { ZodError} from 'zod';
import { IDBError } from './types';
import Dexie, { EntityTable, Table } from 'Dexie';
// Define a type for the Result of repository operations
export type RepositoryResult<T, E> = Result<T, IDBError<E>>;
export abstract class IDexieRepository<T, R> {
export abstract class IDexieRepository<T, R, I = EntityTable<any, any>> {
abstract createTransaction(callback: (table: I) => Promise<void>): Promise<void>
abstract insert(document: T): Promise<RepositoryResult<number, T>>
abstract insertMany(documents: T[]): Promise<RepositoryResult<number[], T[]>>
@@ -1,5 +1,5 @@
import { Result, ok, err } from 'neverthrow';
import { EntityTable } from 'Dexie';
import Dexie, { EntityTable, Table } from 'Dexie';
import { ZodError, ZodObject, ZodSchema } from 'zod';
import { Logger } from 'src/app/services/logger/main/service';
import { IDexieRepository, RepositoryResult } from '../adapter'
@@ -30,17 +30,37 @@ class DBError<T> extends Error implements IDBError<T> {
}
}
export class DexieRepository<T, R> implements IDexieRepository<T, R> {
export class DexieRepository<T, R, I = EntityTable<any, any>> implements IDexieRepository<T, R, I> {
private table: EntityTable<any, any>;
private ZodSchema: ZodSchema<T>
private ZodPartialSchema: ZodSchema<T>
private db: Dexie
constructor(table: EntityTable<any, any>, ZodSchema: ZodSchema) {
constructor(table: EntityTable<any, any>, ZodSchema: ZodSchema, db?:Dexie) {
this.table = table as any
this.ZodSchema = ZodSchema
this.ZodPartialSchema = (ZodSchema as ZodObject<any>).partial() as any;
this.db = db
}
// Method to create a transaction and use the callback
async createTransaction(callback: (table:I) => Promise<void>): Promise<void> {
return this.db.transaction('rw', this.table, async () => {
try {
// Execute the callback function
await callback(this.table as any);
} catch (error) {
// Transactions are automatically rolled back on error
throw error;
}
}).then(() => {
console.log('Transaction completed successfully');
}).catch((error) => {
console.error('Transaction failed: ' + error);
});
}
async insert(document: T): Promise<RepositoryResult<any, T>> {
const dataValidation = this.ZodSchema.safeParse(document)