feat(frontend): connect client to local SurrealDB
Add a SurrealDB service with a local IndexedDB-backed (indxdb://) connection for private, local-first data and an optional remote connection for sync. Initialize the local connection on app start and document the required VITE_* variables in .env.example. Bump the client version to 0.0.1.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { createApp } from 'vue'
|
||||
import vuetify from './plugins/vuetify'
|
||||
import { connectLocalSurreal } from './services/surreal'
|
||||
import './style.css'
|
||||
import './styles/a11y.css'
|
||||
import App from './App.vue'
|
||||
|
||||
void connectLocalSurreal().catch((error: unknown) => {
|
||||
console.error('Nie udało się połączyć z lokalną bazą SurrealDB', error)
|
||||
})
|
||||
|
||||
createApp(App).use(vuetify).mount('#app')
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { Surreal, createRemoteEngines } from 'surrealdb'
|
||||
import { createWasmEngines } from '@surrealdb/wasm'
|
||||
|
||||
const namespace = import.meta.env.VITE_SURREAL_NAMESPACE ?? 'hubmi'
|
||||
const database = import.meta.env.VITE_SURREAL_DATABASE ?? 'hubmi'
|
||||
|
||||
/**
|
||||
* A) Lokalna baza SurrealDB w przeglądarce.
|
||||
* Persistencja przez IndexedDB (`indxdb://`) — localStorage jest zbyt mały.
|
||||
* Dane prywatne użytkownika zostają tutaj i nie są wysyłane automatycznie.
|
||||
*/
|
||||
const localDatabase = import.meta.env.VITE_SURREAL_LOCAL_DATABASE ?? 'tarninahub'
|
||||
const localUrl = import.meta.env.VITE_SURREAL_LOCAL_URL ?? `indxdb://${localDatabase}`
|
||||
|
||||
export const localSurreal = new Surreal({
|
||||
engines: {
|
||||
...createRemoteEngines(),
|
||||
...createWasmEngines(),
|
||||
},
|
||||
})
|
||||
|
||||
let localConnection: Promise<void> | null = null
|
||||
|
||||
export function connectLocalSurreal(): Promise<void> {
|
||||
localConnection ??= localSurreal
|
||||
.connect(localUrl, { namespace, database })
|
||||
.then(() => undefined)
|
||||
|
||||
return localConnection
|
||||
}
|
||||
|
||||
/**
|
||||
* B) Zdalna baza backendu na dedykowanym porcie (tylko dla SurrealDB),
|
||||
* używana do synchronizacji danych uznanych za nadające się do wysłania.
|
||||
*/
|
||||
const remoteUrl =
|
||||
import.meta.env.VITE_SURREAL_REMOTE_URL ?? 'ws://127.0.0.1:8010/rpc'
|
||||
const username = import.meta.env.VITE_SURREAL_USERNAME as string | undefined
|
||||
const password = import.meta.env.VITE_SURREAL_PASSWORD as string | undefined
|
||||
|
||||
export const remoteSurreal = new Surreal()
|
||||
|
||||
let remoteConnection: Promise<void> | null = null
|
||||
|
||||
export function connectRemoteSurreal(): Promise<void> {
|
||||
remoteConnection ??= remoteSurreal
|
||||
.connect(remoteUrl, {
|
||||
namespace,
|
||||
database,
|
||||
...(username && password
|
||||
? { authentication: { username, password } }
|
||||
: {}),
|
||||
})
|
||||
.then(() => undefined)
|
||||
|
||||
return remoteConnection
|
||||
}
|
||||
|
||||
export { namespace, database }
|
||||
Reference in New Issue
Block a user