refactor(database): migrate to new modular database layer and unify data access
- Remove legacy database.ts, LocalDataSource.ts, and MessageRepository.ts - Create new src/database/ module with messageRepository, userCacheRepository, conversationRepository, and groupCacheRepository - Update all consumers to import from @/database instead of services/database - Add web platform blur handling for modal components to fix focus issues - Flatten SystemMessageItem and NotificationsScreen styles for consistent design - Add draggable slider in ChatSettingsScreen and dynamic font size support - Introduce 9 new chat color themes - Add profile screens for about, terms, and privacy policy with navigation routes - Add policy links to login and registration screens - Fix post share URL format from /posts/ to /post/
This commit is contained in:
44
src/database/repositories/PostCacheRepository.ts
Normal file
44
src/database/repositories/PostCacheRepository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { localDataSource } from '../LocalDataSource';
|
||||
import type { ILocalDataSource } from '@/data/datasources/interfaces';
|
||||
|
||||
export interface IPostCacheRepository {
|
||||
save(id: string, data: any): Promise<void>;
|
||||
get(id: string): Promise<any | null>;
|
||||
getAll(): Promise<any[]>;
|
||||
delete(id: string): Promise<void>;
|
||||
}
|
||||
|
||||
export class PostCacheRepository implements IPostCacheRepository {
|
||||
constructor(private dataSource: ILocalDataSource = localDataSource) {}
|
||||
|
||||
async save(id: string, data: any): Promise<void> {
|
||||
await this.dataSource.enqueueWrite(async () => {
|
||||
await this.dataSource.run(
|
||||
`INSERT OR REPLACE INTO posts_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
|
||||
[String(id), JSON.stringify(data), new Date().toISOString()]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async get(id: string): Promise<any | null> {
|
||||
const r = await this.dataSource.getFirst<{ data: string }>(
|
||||
`SELECT data FROM posts_cache WHERE id = ?`, [String(id)]
|
||||
);
|
||||
return r?.data ? JSON.parse(r.data) : null;
|
||||
}
|
||||
|
||||
async getAll(): Promise<any[]> {
|
||||
const rows = await this.dataSource.query<{ data: string }>(
|
||||
`SELECT data FROM posts_cache ORDER BY updatedAt DESC`
|
||||
);
|
||||
return rows.map(r => JSON.parse(r.data));
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.dataSource.enqueueWrite(async () => {
|
||||
await this.dataSource.run(`DELETE FROM posts_cache WHERE id = ?`, [String(id)]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const postCacheRepository = new PostCacheRepository();
|
||||
Reference in New Issue
Block a user