document deployment

This commit is contained in:
2025-07-19 00:55:13 +02:00
parent 051c291b8c
commit 178f5c517d
12 changed files with 643 additions and 71 deletions

118
src/db.ts
View File

@ -10,7 +10,21 @@ type Space = {
notes: Note[]
}
class Database {
type StorageMode = 'indexeddb' | 'database'
interface StorageAdapter {
init(): Promise<void>
getSpace(id: string): Promise<Space | undefined>
saveSpace(space: Space): Promise<void>
createSpace(id: string): Promise<Space>
addNoteToSpace(spaceId: string, note: Note): Promise<void>
updateNoteInSpace(spaceId: string, noteId: string, updates: Partial<Note>): Promise<void>
deleteNoteFromSpace(spaceId: string, noteId: string): Promise<void>
getAllSpaceIds(): Promise<string[]>
deleteSpace(id: string): Promise<void>
}
class IndexedDBAdapter implements StorageAdapter {
private db: IDBDatabase | null = null
async init() {
@ -121,4 +135,104 @@ class Database {
}
}
export const db = new Database()
class DatabaseAdapter implements StorageAdapter {
private apiUrl: string
constructor(apiUrl?: string) {
this.apiUrl = apiUrl || this.getApiUrl()
}
private getApiUrl(): string {
if (typeof window !== 'undefined' && window.location.hostname === 'palace.raphaelforment.fr') {
return 'https://palace.raphaelforment.fr:3001/api'
}
return '/api'
}
async init(): Promise<void> {
// Database connection is handled per request
}
async getSpace(id: string): Promise<Space | undefined> {
const response = await fetch(`${this.apiUrl}/spaces/${id}`)
if (response.status === 404) return undefined
if (!response.ok) throw new Error(`Failed to get space: ${response.statusText}`)
return await response.json()
}
async saveSpace(space: Space): Promise<void> {
const response = await fetch(`${this.apiUrl}/spaces/${space.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(space)
})
if (!response.ok) throw new Error(`Failed to save space: ${response.statusText}`)
}
async createSpace(id: string): Promise<Space> {
const space: Space = { id, notes: [] }
await this.saveSpace(space)
return space
}
async addNoteToSpace(spaceId: string, note: Note): Promise<void> {
const response = await fetch(`${this.apiUrl}/spaces/${spaceId}/notes`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(note)
})
if (!response.ok) throw new Error(`Failed to add note: ${response.statusText}`)
}
async updateNoteInSpace(spaceId: string, noteId: string, updates: Partial<Note>): Promise<void> {
const response = await fetch(`${this.apiUrl}/spaces/${spaceId}/notes/${noteId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updates)
})
if (!response.ok) throw new Error(`Failed to update note: ${response.statusText}`)
}
async deleteNoteFromSpace(spaceId: string, noteId: string): Promise<void> {
const response = await fetch(`${this.apiUrl}/spaces/${spaceId}/notes/${noteId}`, {
method: 'DELETE'
})
if (!response.ok) throw new Error(`Failed to delete note: ${response.statusText}`)
}
async getAllSpaceIds(): Promise<string[]> {
const response = await fetch(`${this.apiUrl}/spaces`)
if (!response.ok) throw new Error(`Failed to get spaces: ${response.statusText}`)
const spaces = await response.json()
return spaces.map((space: Space) => space.id)
}
async deleteSpace(id: string): Promise<void> {
const response = await fetch(`${this.apiUrl}/spaces/${id}`, {
method: 'DELETE'
})
if (!response.ok) throw new Error(`Failed to delete space: ${response.statusText}`)
}
}
function getStorageMode(): StorageMode {
return (localStorage.getItem('palace-storage-mode') as StorageMode) || 'indexeddb'
}
function setStorageMode(mode: StorageMode): void {
localStorage.setItem('palace-storage-mode', mode)
}
function createStorageAdapter(): StorageAdapter {
const mode = getStorageMode()
switch (mode) {
case 'database':
return new DatabaseAdapter()
case 'indexeddb':
default:
return new IndexedDBAdapter()
}
}
export const db = createStorageAdapter()
export { type Note, type Space, type StorageMode, getStorageMode, setStorageMode }