@@ -4,6 +4,7 @@ import { PGliteWorker } from '@electric-sql/pglite/worker'
44import { Message as AiMessage , ToolInvocation } from 'ai'
55import { codeBlock } from 'common-tags'
66import { nanoid } from 'nanoid'
7+ import { downloadFileFromUrl } from '../util'
78
89export type Database = {
910 id : string
@@ -47,7 +48,7 @@ export class DbManager {
4748 /**
4849 * Creates a PGlite instance that runs in a web worker
4950 */
50- static async createPGlite ( options ?: PGliteOptions ) : Promise < PGliteInterface > {
51+ static async createPGlite ( options ?: PGliteOptions & { id ?: string } ) {
5152 if ( typeof window === 'undefined' ) {
5253 throw new Error ( 'PGlite worker instances are only available in the browser' )
5354 }
@@ -59,7 +60,7 @@ export class DbManager {
5960 new Worker ( new URL ( './worker.ts' , import . meta. url ) , { type : 'module' } ) ,
6061 {
6162 // Opt out of PGlite worker leader election / shared DBs
62- id : nanoid ( ) ,
63+ id : options ?. id ?? nanoid ( ) ,
6364 ...options ,
6465 }
6566 )
@@ -274,7 +275,7 @@ export class DbManager {
274275 return metaDb . sql `insert into databases (id, name, created_at, is_hidden) values ${ join ( values , ',' ) } on conflict (id) do nothing`
275276 }
276277
277- async getDbInstance ( id : string , loadDataDir ?: Blob | File ) {
278+ async getDbInstance ( id : string , loadDataDir ?: Blob | File ) : Promise < PGliteInterface > {
278279 const openDatabasePromise = this . databaseConnections . get ( id )
279280
280281 if ( openDatabasePromise ) {
@@ -292,7 +293,7 @@ export class DbManager {
292293
293294 await this . handleUnsupportedPGVersion ( dbPath )
294295
295- const db = await DbManager . createPGlite ( { dataDir : `idb://${ dbPath } ` , loadDataDir } )
296+ const db = await DbManager . createPGlite ( { dataDir : `idb://${ dbPath } ` , loadDataDir, id } )
296297 await runMigrations ( db , migrations )
297298
298299 return db
0 commit comments