@@ -4,6 +4,7 @@ import { PGliteWorker } from '@electric-sql/pglite/worker'
4
4
import { Message as AiMessage , ToolInvocation } from 'ai'
5
5
import { codeBlock } from 'common-tags'
6
6
import { nanoid } from 'nanoid'
7
+ import { downloadFileFromUrl } from '../util'
7
8
8
9
export type Database = {
9
10
id : string
@@ -47,7 +48,7 @@ export class DbManager {
47
48
/**
48
49
* Creates a PGlite instance that runs in a web worker
49
50
*/
50
- static async createPGlite ( options ?: PGliteOptions ) : Promise < PGliteInterface > {
51
+ static async createPGlite ( options ?: PGliteOptions & { id ?: string } ) {
51
52
if ( typeof window === 'undefined' ) {
52
53
throw new Error ( 'PGlite worker instances are only available in the browser' )
53
54
}
@@ -59,7 +60,7 @@ export class DbManager {
59
60
new Worker ( new URL ( './worker.ts' , import . meta. url ) , { type : 'module' } ) ,
60
61
{
61
62
// Opt out of PGlite worker leader election / shared DBs
62
- id : nanoid ( ) ,
63
+ id : options ?. id ?? nanoid ( ) ,
63
64
...options ,
64
65
}
65
66
)
@@ -274,7 +275,7 @@ export class DbManager {
274
275
return metaDb . sql `insert into databases (id, name, created_at, is_hidden) values ${ join ( values , ',' ) } on conflict (id) do nothing`
275
276
}
276
277
277
- async getDbInstance ( id : string , loadDataDir ?: Blob | File ) {
278
+ async getDbInstance ( id : string , loadDataDir ?: Blob | File ) : Promise < PGliteInterface > {
278
279
const openDatabasePromise = this . databaseConnections . get ( id )
279
280
280
281
if ( openDatabasePromise ) {
@@ -292,7 +293,7 @@ export class DbManager {
292
293
293
294
await this . handleUnsupportedPGVersion ( dbPath )
294
295
295
- const db = await DbManager . createPGlite ( { dataDir : `idb://${ dbPath } ` , loadDataDir } )
296
+ const db = await DbManager . createPGlite ( { dataDir : `idb://${ dbPath } ` , loadDataDir, id } )
296
297
await runMigrations ( db , migrations )
297
298
298
299
return db
0 commit comments