looda3131's picture
ู‡ู„ ู‡ู†ุงูƒ ู…ู„ูุงุช ู…ู‡ู…ู‡ ุงุฎุฑูŠุŸ
bbb691f
raw
history blame contribute delete
999 Bytes
// This file should only be imported on the client side.
'use client';
import { openDB, type IDBPDatabase } from 'idb';
import type { PostWithUIState } from './types';
const DB_NAME = 'AIWorldDB';
const DB_VERSION = 1;
const POSTS_STORE_NAME = 'posts';
interface AIWorldDB {
posts: {
key: string;
value: PostWithUIState;
indexes: { createdAt: string };
};
}
let dbPromise: Promise<IDBPDatabase<AIWorldDB>> | null = null;
function getDb(): Promise<IDBPDatabase<AIWorldDB>> {
if (typeof window === 'undefined') {
return Promise.reject(new Error('IndexedDB cannot be used on the server.'));
}
if (!dbPromise) {
dbPromise = openDB<AIWorldDB>(DB_NAME, DB_VERSION, {
upgrade(db) {
if (!db.objectStoreNames.contains(POSTS_STORE_NAME)) {
const store = db.createObjectStore(POSTS_STORE_NAME, {
keyPath: 'id',
});
store.createIndex('createdAt', 'createdAt');
}
},
});
}
return dbPromise;
}