mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
|
|
import { Injectable } from '@angular/core';
|
||
|
|
import { z } from 'zod';
|
||
|
|
import { Dexie, EntityTable, liveQuery } from 'Dexie';
|
||
|
|
|
||
|
|
const UserProfilePicture = z.object({
|
||
|
|
base64: z.string(),
|
||
|
|
id: z.number().optional(),
|
||
|
|
})
|
||
|
|
|
||
|
|
const UserTable = z.object({
|
||
|
|
ChunksBase64: z.number(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export type UserProfilePicture = z.infer<typeof UserProfilePicture>
|
||
|
|
|
||
|
|
// Database declaration (move this to its own module also)
|
||
|
|
const session = new Dexie('session') as Dexie & {
|
||
|
|
profilePicture: EntityTable<UserProfilePicture, 'id'>;
|
||
|
|
};
|
||
|
|
|
||
|
|
session.version(1).stores({
|
||
|
|
profilePicture: '++id, base64'
|
||
|
|
});
|
||
|
|
|
||
|
|
@Injectable({
|
||
|
|
providedIn: 'root'
|
||
|
|
})
|
||
|
|
export class UserLocalRepositoryService {
|
||
|
|
|
||
|
|
constructor() { }
|
||
|
|
|
||
|
|
|
||
|
|
async addProfilePicture(data: UserProfilePicture) {
|
||
|
|
try {
|
||
|
|
await session.transaction('rw', session.profilePicture, async () => {
|
||
|
|
// Clear existing records from myTable
|
||
|
|
await session.profilePicture.clear();
|
||
|
|
await session.profilePicture.add(data);
|
||
|
|
});
|
||
|
|
console.log('Clear and add operations completed within transaction.');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error performing transaction:', error, data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
getLiveProfilePicture() {
|
||
|
|
return liveQuery(() =>
|
||
|
|
session.profilePicture.orderBy('id').reverse().first()
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|