refactor: move db and repository into page.svelte
This commit is contained in:
committed by
didierfranc
parent
fb22d3fd18
commit
8ec77dc7ef
@@ -1,4 +1,3 @@
|
||||
import { dev } from '$app/environment';
|
||||
import { MIGRATIONS } from '$lib/migrations';
|
||||
import { IndexedDBCache } from '@agnosticeng/cache';
|
||||
import { MigrationManager } from '@agnosticeng/migrate';
|
||||
@@ -10,7 +9,7 @@ const DB_NAME = 'sqlite-storage';
|
||||
const STORE_NAME = 'sqlite-data';
|
||||
const CACHE_KEY = 'db';
|
||||
|
||||
class Database {
|
||||
export class Database {
|
||||
private db = new SQLite();
|
||||
private cache = new IndexedDBCache({ dbName: DB_NAME, storeName: STORE_NAME });
|
||||
private migration = new MigrationManager(this.db);
|
||||
@@ -39,11 +38,3 @@ class Database {
|
||||
return this.db.exec(sql, bind);
|
||||
}
|
||||
}
|
||||
|
||||
export type { Database };
|
||||
export const db = new Database();
|
||||
|
||||
if (dev) {
|
||||
// @ts-ignore
|
||||
window.db = db;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { db, type Database } from '$lib/database';
|
||||
import type { Database } from '$lib/database';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
|
||||
@@ -17,7 +17,7 @@ export interface HistoryRepository {
|
||||
delete(id: HistoryEntry['id']): Promise<void>;
|
||||
}
|
||||
|
||||
class SQLiteHistoryRepository implements HistoryRepository {
|
||||
export class SQLiteHistoryRepository implements HistoryRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
async getAll(): Promise<HistoryEntry[]> {
|
||||
@@ -63,5 +63,3 @@ class SQLiteHistoryRepository implements HistoryRepository {
|
||||
await this.db.exec('DELETE FROM history WHERE id = ?', [id]);
|
||||
}
|
||||
}
|
||||
|
||||
export const historyRepository: HistoryRepository = new SQLiteHistoryRepository(db);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { db, type Database } from '$lib/database';
|
||||
import type { Database } from '$lib/database';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
|
||||
@@ -21,7 +21,7 @@ export interface QueryRepository {
|
||||
delete(id: number): Promise<void>;
|
||||
}
|
||||
|
||||
class SQLiteQueryRepository implements QueryRepository {
|
||||
export class SQLiteQueryRepository implements QueryRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
async getAll(): Promise<Query[]> {
|
||||
@@ -75,5 +75,3 @@ function row_to_query(row: Awaited<ReturnType<Database['exec']>>[number]): Query
|
||||
.toDate()
|
||||
};
|
||||
}
|
||||
|
||||
export const queryRepository: QueryRepository = new SQLiteQueryRepository(db);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { db, type Database } from '$lib/database';
|
||||
import type { Database } from '$lib/database';
|
||||
|
||||
export interface Tab {
|
||||
id: string;
|
||||
@@ -12,7 +12,7 @@ export interface TabRepository {
|
||||
save(tabs: Tab[], activeIndex: number): Promise<void>;
|
||||
}
|
||||
|
||||
class SQLiteTabRepository implements TabRepository {
|
||||
export class SQLiteTabRepository implements TabRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
async get(): Promise<[tabs: Tab[], activeIndex: number]> {
|
||||
@@ -51,5 +51,3 @@ function row_to_tab(row: Awaited<ReturnType<Database['exec']>>[number]): Tab {
|
||||
query_id: row.query_id as number | undefined
|
||||
};
|
||||
}
|
||||
|
||||
export const tabRepository: TabRepository = new SQLiteTabRepository(db);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
import TabComponent from '$lib/components/Tab.svelte';
|
||||
import TimeCounter from '$lib/components/TimeCounter.svelte';
|
||||
import { setAppContext } from '$lib/context';
|
||||
import { Database } from '$lib/database';
|
||||
import { FileDropEventManager } from '$lib/FileDropEventManager';
|
||||
import Bars3 from '$lib/icons/Bars3.svelte';
|
||||
import Bold from '$lib/icons/Bold.svelte';
|
||||
@@ -25,15 +26,28 @@
|
||||
import type { Table } from '$lib/olap-engine';
|
||||
import { engine, type OLAPResponse } from '$lib/olap-engine';
|
||||
import { PanelState } from '$lib/PanelState.svelte';
|
||||
import { historyRepository, type HistoryEntry } from '$lib/repositories/history';
|
||||
import { queryRepository, type Query } from '$lib/repositories/queries';
|
||||
import { tabRepository, type Tab } from '$lib/repositories/tabs';
|
||||
import {
|
||||
SQLiteHistoryRepository,
|
||||
type HistoryEntry,
|
||||
type HistoryRepository
|
||||
} from '$lib/repositories/history';
|
||||
import {
|
||||
SQLiteQueryRepository,
|
||||
type Query,
|
||||
type QueryRepository
|
||||
} from '$lib/repositories/queries';
|
||||
import { SQLiteTabRepository, type Tab, type TabRepository } from '$lib/repositories/tabs';
|
||||
import { IndexedDBCache } from '@agnosticeng/cache';
|
||||
import { SplitPane } from '@rich_harris/svelte-split-pane';
|
||||
import debounce from 'p-debounce';
|
||||
import { format } from 'sql-formatter';
|
||||
import { tick, type ComponentProps } from 'svelte';
|
||||
|
||||
const db = new Database();
|
||||
const historyRepository: HistoryRepository = new SQLiteHistoryRepository(db);
|
||||
const queryRepository: QueryRepository = new SQLiteQueryRepository(db);
|
||||
const tabRepository: TabRepository = new SQLiteTabRepository(db);
|
||||
|
||||
let response = $state.raw<OLAPResponse>();
|
||||
let loading = $state(false);
|
||||
let counter = $state<ReturnType<typeof TimeCounter>>();
|
||||
|
||||
Reference in New Issue
Block a user