This commit is contained in:
Peter Maquiran
2024-08-14 15:29:16 +01:00
parent d7eb6a552b
commit ea4ca5c34c
19 changed files with 270 additions and 80 deletions
@@ -14,20 +14,20 @@ export class DexieRepository<T> {
constructor(table: EntityTable<any, any>, ZodSchema: ZodSchema) {
this.table = table as any
this.ZodSchema = ZodSchema
this.ZodPartialSchema = (ZodSchema as ZodObject<any>).partial() as any;
this.ZodPartialSchema = (ZodSchema as ZodObject<any>).partial() as any;
}
async insert(document: T): Promise<RepositoryResult<number, T>> {
const dataValidation = this.ZodSchema.safeParse(document)
if(dataValidation.success) {
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>))
}
@@ -52,7 +52,7 @@ export class DexieRepository<T> {
async update(id: any, updatedDocument: Partial<T>) {
const dataValidation = this.ZodPartialSchema.safeParse(document)
const dataValidation = this.ZodPartialSchema.safeParse(updatedDocument)
if(dataValidation.success) {
try {
@@ -84,9 +84,10 @@ export class DexieRepository<T> {
}
}
async find(filter: Object) {
async find(filter: Object): Promise<RepositoryResult<T[], T[]>> {
try {
const documents = await this.table.where(filter).toArray();
const documents: any = await this.table.where(filter).toArray();
console.log('documents', {documents})
return ok(documents);
} catch (error) {
return err(new Error('Failed to find documents: ' + error.message));
@@ -104,7 +105,8 @@ export class DexieRepository<T> {
async findAll(): Promise<RepositoryResult<T[], T>> {
try {
const documents = await this.table.toArray();
console.log(this.table)
const documents = await this.table.toArray()
return ok(documents);
} catch (error) {
return err(new Error('Failed to retrieve all documents: ' + error.message));