Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CORS error when fetching pglite resources #49

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions src/utils/vector-db/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
sql,
} from 'drizzle-orm'
import { PgliteDatabase, drizzle } from 'drizzle-orm/pglite'
import { App } from 'obsidian'
import { App, requestUrl } from 'obsidian'

import migrations from '../../db/migrations.json'
import { InsertVector, SelectVector, vectorTables } from '../../db/schema'
Expand Down Expand Up @@ -169,7 +169,7 @@
const similaritySearchResult = await this.db
.select({
...(() => {
const { embedding, ...rest } = getTableColumns(vectors)

Check warning on line 172 in src/utils/vector-db/repository.ts

View workflow job for this annotation

GitHub Actions / check

'embedding' is assigned a value but never used
return rest
})(),
similarity,
Expand Down Expand Up @@ -213,6 +213,12 @@

private async loadExistingDatabase(): Promise<PgliteDatabase | null> {
try {
const databaseFileExists = await this.app.vault.adapter.exists(
this.dbPath,
)
if (!databaseFileExists) {
return null
}
const fileBuffer = await this.app.vault.adapter.readBinary(this.dbPath)
const fileBlob = new Blob([fileBuffer], { type: 'application/x-gzip' })
const { fsBundle, wasmModule, vectorExtensionBundlePath } =
Expand All @@ -233,14 +239,19 @@
}

private async migrateDatabase(): Promise<void> {
// Workaround for running Drizzle migrations in a browser environment
// This method uses an undocumented API to perform migrations
// See: https://github.com/drizzle-team/drizzle-orm/discussions/2532#discussioncomment-10780523
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await this.db.dialect.migrate(migrations, this.db.session, {
migrationsTable: 'drizzle_migrations',
})
try {
// Workaround for running Drizzle migrations in a browser environment
// This method uses an undocumented API to perform migrations
// See: https://github.com/drizzle-team/drizzle-orm/discussions/2532#discussioncomment-10780523
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await this.db.dialect.migrate(migrations, this.db.session, {
migrationsTable: 'drizzle_migrations',
})
} catch (error) {
console.error('Error migrating database:', error)
throw error
}
}

// TODO: This function is a temporary workaround chosen due to the difficulty of bundling postgres.wasm and postgres.data from node_modules into a single JS file. The ultimate goal is to bundle everything into one JS file in the future.
Expand All @@ -249,28 +260,24 @@
wasmModule: WebAssembly.Module
vectorExtensionBundlePath: URL
}> {
const [fsBundleResponse, wasmResponse] = await Promise.all([
fetch('https://unpkg.com/@electric-sql/pglite/dist/postgres.data'),
fetch('https://unpkg.com/@electric-sql/pglite/dist/postgres.wasm'),
])

if (!fsBundleResponse.ok || !wasmResponse.ok) {
throw new Error('Failed to fetch PGlite resources')
}

const [fsBundleArrayBuffer, wasmArrayBuffer] = await Promise.all([
fsBundleResponse.arrayBuffer(),
wasmResponse.arrayBuffer(),
])
try {
const [fsBundleResponse, wasmResponse] = await Promise.all([
requestUrl('https://unpkg.com/@electric-sql/pglite/dist/postgres.data'),
requestUrl('https://unpkg.com/@electric-sql/pglite/dist/postgres.wasm'),
])

const fsBundle = new Blob([fsBundleArrayBuffer], {
type: 'application/octet-stream',
})
const wasmModule = await WebAssembly.compile(wasmArrayBuffer)
const vectorExtensionBundlePath = new URL(
'https://unpkg.com/@electric-sql/pglite/dist/vector.tar.gz',
)
const fsBundle = new Blob([fsBundleResponse.arrayBuffer], {
type: 'application/octet-stream',
})
const wasmModule = await WebAssembly.compile(wasmResponse.arrayBuffer)
const vectorExtensionBundlePath = new URL(
'https://unpkg.com/@electric-sql/pglite/dist/vector.tar.gz',
)

return { fsBundle, wasmModule, vectorExtensionBundlePath }
return { fsBundle, wasmModule, vectorExtensionBundlePath }
} catch (error) {
console.error('Error loading PGlite resources:', error)
throw error
}
}
}
Loading