add sqlite
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { getDatabase } from '../client';
|
||||
import { FILE_COLUMNS_SQL } from '../schema';
|
||||
import type { FileEntry, FileRow, StoredFile } from '../types';
|
||||
|
||||
export async function saveFile(file: FileEntry, folderUri: string): Promise<void> {
|
||||
const db = await getDatabase();
|
||||
const exists = file.exists ? 1 : 0;
|
||||
|
||||
await db.runAsync(
|
||||
`INSERT INTO files (uri, name, folder_uri, extension, size, "type", "exists", last_modified, added_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(uri) DO UPDATE SET
|
||||
name = excluded.name, folder_uri = excluded.folder_uri, extension = excluded.extension,
|
||||
size = excluded.size, "type" = excluded."type", "exists" = excluded."exists",
|
||||
last_modified = excluded.last_modified`,
|
||||
file.uri,
|
||||
file.name,
|
||||
folderUri,
|
||||
file.extension ?? null,
|
||||
file.size,
|
||||
file.type ?? null,
|
||||
exists,
|
||||
file.lastModified,
|
||||
Date.now(),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getFiles(folderUri?: string): Promise<StoredFile[]> {
|
||||
const db = await getDatabase();
|
||||
const rows = folderUri
|
||||
? await db.getAllAsync<FileRow>(
|
||||
`SELECT ${FILE_COLUMNS_SQL} FROM files WHERE folder_uri = ? ORDER BY name ASC`,
|
||||
folderUri,
|
||||
)
|
||||
: await db.getAllAsync<FileRow>(
|
||||
`SELECT ${FILE_COLUMNS_SQL} FROM files ORDER BY name ASC`,
|
||||
);
|
||||
return rows.map(toStoredFile);
|
||||
}
|
||||
|
||||
export async function removeFile(uri: string): Promise<void> {
|
||||
const db = await getDatabase();
|
||||
await db.runAsync('DELETE FROM files WHERE uri = ?', uri);
|
||||
}
|
||||
|
||||
function toStoredFile(row: FileRow): StoredFile {
|
||||
return {
|
||||
uri: row.uri,
|
||||
name: row.name,
|
||||
isDirectory: false,
|
||||
extension: row.extension ?? '',
|
||||
exists: row.exists === 1,
|
||||
size: row.size,
|
||||
type: row.type ?? '',
|
||||
lastModified: row.last_modified,
|
||||
folderUri: row.folder_uri,
|
||||
syncStatus: row.sync_status,
|
||||
addedAt: row.added_at,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { getDatabase } from '../client';
|
||||
import { FOLDER_COLUMNS_SQL } from '../schema';
|
||||
import type { Folder, FolderContext, FolderRow, SyncStatus } from '../types';
|
||||
|
||||
type SaveFolderOptions = {
|
||||
parentUri?: string;
|
||||
syncStatus?: SyncStatus;
|
||||
};
|
||||
|
||||
export async function saveFolder(
|
||||
folder: Folder,
|
||||
options?: SaveFolderOptions,
|
||||
): Promise<void> {
|
||||
const db = await getDatabase();
|
||||
const exists = folder.exists === undefined ? null : folder.exists ? 1 : 0;
|
||||
|
||||
await db.runAsync(
|
||||
`INSERT INTO folders (uri, name, "exists", sync_status, added_at, parent_uri)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(uri) DO UPDATE SET
|
||||
name = excluded.name, "exists" = excluded."exists", sync_status = excluded.sync_status`,
|
||||
folder.uri,
|
||||
folder.name,
|
||||
exists,
|
||||
options?.syncStatus ?? 'local',
|
||||
Date.now(),
|
||||
options?.parentUri ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
export async function saveDirectory(folder: Folder): Promise<boolean> {
|
||||
await saveFolder(folder);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getFolders(): Promise<FolderContext[]> {
|
||||
const db = await getDatabase();
|
||||
const rows = await db.getAllAsync<FolderRow>(
|
||||
`SELECT ${FOLDER_COLUMNS_SQL} FROM folders ORDER BY added_at ASC, name ASC`,
|
||||
);
|
||||
return rows.map(toFolderContext);
|
||||
}
|
||||
|
||||
export async function getFolderFolders(parentUri: string): Promise<FolderContext[]> {
|
||||
const db = await getDatabase();
|
||||
const rows = await db.getAllAsync<FolderRow>(
|
||||
`SELECT ${FOLDER_COLUMNS_SQL} FROM folders WHERE parent_uri = ? ORDER BY name ASC`,
|
||||
parentUri,
|
||||
);
|
||||
return rows.map(toFolderContext);
|
||||
}
|
||||
|
||||
export async function removeFolder(uri: string): Promise<void> {
|
||||
const db = await getDatabase();
|
||||
await db.runAsync('DELETE FROM folders WHERE uri = ?', uri);
|
||||
}
|
||||
|
||||
function toFolderContext(row: FolderRow): FolderContext {
|
||||
return {
|
||||
syncStatus: row.sync_status,
|
||||
addedAt: row.added_at,
|
||||
folder: {
|
||||
uri: row.uri,
|
||||
name: row.name,
|
||||
isDirectory: true,
|
||||
exists: row.exists === null ? undefined : row.exists === 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export {
|
||||
saveFolder,
|
||||
saveDirectory,
|
||||
getFolders,
|
||||
getFolderFolders,
|
||||
removeFolder,
|
||||
} from './folders';
|
||||
export { saveFile, getFiles, removeFile } from './files';
|
||||
export { saveUserPreferences, getUserPreferences } from './preferences';
|
||||
@@ -0,0 +1,33 @@
|
||||
import { getDatabase } from '../client';
|
||||
import { PREFERENCES_KEY } from '../schema';
|
||||
import type { UserPreferences } from '../types';
|
||||
|
||||
const DEFAULT_PREFERENCES: UserPreferences = {
|
||||
syncMode: 'full',
|
||||
};
|
||||
|
||||
export async function saveUserPreferences(preferences: UserPreferences): Promise<void> {
|
||||
const db = await getDatabase();
|
||||
await db.runAsync(
|
||||
`INSERT INTO user_preferences ("key", "value", updated_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT("key") DO UPDATE SET "value" = excluded."value", updated_at = excluded.updated_at`,
|
||||
PREFERENCES_KEY,
|
||||
JSON.stringify(preferences),
|
||||
Date.now(),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getUserPreferences(): Promise<UserPreferences> {
|
||||
const db = await getDatabase();
|
||||
const row = await db.getFirstAsync<{ value: string }>(
|
||||
'SELECT "value" FROM user_preferences WHERE "key" = ?',
|
||||
PREFERENCES_KEY,
|
||||
);
|
||||
if (!row) return DEFAULT_PREFERENCES;
|
||||
|
||||
try {
|
||||
return { ...DEFAULT_PREFERENCES, ...JSON.parse(row.value) } satisfies UserPreferences;
|
||||
} catch {
|
||||
return DEFAULT_PREFERENCES;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user