claude drizzle integration
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
import { openDB, DBSchema, IDBPDatabase } from "idb";
|
||||
import { type CalendarEvent } from "./types";
|
||||
|
||||
interface ICalDB extends DBSchema {
|
||||
events: {
|
||||
key: string;
|
||||
value: CalendarEvent;
|
||||
};
|
||||
}
|
||||
|
||||
let dbPromise: Promise<IDBPDatabase<ICalDB>> | null = null;
|
||||
|
||||
async function initDB() {
|
||||
return openDB<ICalDB>("icalPWA", 1, {
|
||||
upgrade(db) {
|
||||
if (!db.objectStoreNames.contains("events")) {
|
||||
db.createObjectStore("events", { keyPath: "id" });
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Get the database in a browser-safe way
|
||||
export async function getDB() {
|
||||
if (typeof window === "undefined") return null;
|
||||
if (!dbPromise) {
|
||||
dbPromise = initDB();
|
||||
}
|
||||
return dbPromise;
|
||||
}
|
||||
|
||||
// CRUD operations — all SSR-safe
|
||||
export async function getAllEvents() {
|
||||
const db = await getDB();
|
||||
if (!db) return [];
|
||||
return db.getAll("events");
|
||||
}
|
||||
|
||||
export async function addEvent(event: ICalDB["events"]["value"]) {
|
||||
const db = await getDB();
|
||||
if (!db) return;
|
||||
return db.put("events", event);
|
||||
}
|
||||
|
||||
export async function deleteEvent(id: string) {
|
||||
const db = await getDB();
|
||||
if (!db) return;
|
||||
return db.delete("events", id);
|
||||
}
|
||||
|
||||
export async function clearEvents() {
|
||||
const db = await getDB();
|
||||
if (!db) return;
|
||||
return db.clear("events");
|
||||
}
|
||||
62
src/lib/events-db.ts
Normal file
62
src/lib/events-db.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { openDB, type IDBPDatabase } from 'idb';
|
||||
import type { CalendarEvent } from '@/lib/types';
|
||||
|
||||
const DB_NAME = 'LocalCalEvents';
|
||||
const DB_VERSION = 1;
|
||||
const EVENTS_STORE = 'events';
|
||||
|
||||
let dbPromise: Promise<IDBPDatabase> | null = null;
|
||||
|
||||
function getDB() {
|
||||
if (!dbPromise) {
|
||||
dbPromise = openDB(DB_NAME, DB_VERSION, {
|
||||
upgrade(db) {
|
||||
if (!db.objectStoreNames.contains(EVENTS_STORE)) {
|
||||
const store = db.createObjectStore(EVENTS_STORE, { keyPath: 'id' });
|
||||
store.createIndex('start', 'start');
|
||||
store.createIndex('title', 'title');
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
return dbPromise;
|
||||
}
|
||||
|
||||
export async function saveEvent(event: CalendarEvent): Promise<void> {
|
||||
const db = await getDB();
|
||||
await db.put(EVENTS_STORE, event);
|
||||
}
|
||||
|
||||
export async function getEvents(): Promise<CalendarEvent[]> {
|
||||
const db = await getDB();
|
||||
return db.getAll(EVENTS_STORE);
|
||||
}
|
||||
|
||||
export async function getEvent(id: string): Promise<CalendarEvent | undefined> {
|
||||
const db = await getDB();
|
||||
return db.get(EVENTS_STORE, id);
|
||||
}
|
||||
|
||||
export async function deleteEvent(id: string): Promise<void> {
|
||||
const db = await getDB();
|
||||
await db.delete(EVENTS_STORE, id);
|
||||
}
|
||||
|
||||
export async function updateEvent(event: CalendarEvent): Promise<void> {
|
||||
const db = await getDB();
|
||||
await db.put(EVENTS_STORE, event);
|
||||
}
|
||||
|
||||
export async function getEventsByDateRange(startDate: string, endDate: string): Promise<CalendarEvent[]> {
|
||||
const db = await getDB();
|
||||
const tx = db.transaction(EVENTS_STORE, 'readonly');
|
||||
const index = tx.store.index('start');
|
||||
const events = await index.getAll(IDBKeyRange.bound(startDate, endDate));
|
||||
await tx.done;
|
||||
return events;
|
||||
}
|
||||
|
||||
export async function clearEvents(): Promise<void> {
|
||||
const db = await getDB();
|
||||
await db.clear(EVENTS_STORE);
|
||||
}
|
||||
Reference in New Issue
Block a user