skrivert/src/db/main.ts

71 lines
1.8 KiB
TypeScript

import Dexie, { type Table } from "dexie";
import { importDefaultShortforms } from "./import";
import { LogicalSize } from "@tauri-apps/api/dpi";
export interface SyncInfo {
key: string;
last_sync: Date;
}
export interface List {
id?: number;
name: string;
type: number;
updated: Date;
}
export interface Shortform {
id?: number;
listid: number;
shortform: string;
phrase: string;
used: Date;
uses: number;
remind: number, // negative if disabled
updated: Date;
}
export class SkrivertDB extends Dexie {
syncInfo!: Table<SyncInfo>;
shortforms!: Table<Shortform>;
lists!: Table<List>;
constructor() {
super("skrivertdb");
this.version(1).stores({
syncInfo: "key, last_sync",
shortforms: "++id, listid, shortform, phrase, last_use, uses, remind, updated",
lists: "++id, name, type, updated",
});
this.on("ready", async (db) => {
console.log("Do ready stuff...") // Check if we have any local data
const localListsCount = await db.table("lists").count();
const lastSync = await db.table("syncInfo").get("lastFullSync");
const lastSyncTime = lastSync?.lastSync || 0;
if (localListsCount === 0) {
// No local data - perform full download
importDefaultShortforms()
console.log('No local data found. Prompt user to import data or use standard list...');
}
})
};
}
export function createShortformList(l: List): number {
db.lists.add(l)
.then(result => {
console.log(result)
})
}
export async function deleteShortformList(id: number) {
await db.shortforms.where("listid").equals(id).delete()
await db.lists.where("id").equals(id).delete()
}
export const db = new SkrivertDB();