Skip to content

Commit 9308656

Browse files
committed
v0fix: fixed non-default pglite db name not actually changing the db used
Removed default clientPgLitePath from the runtimeConfig, it is now a runtime only option and has been changed to a function (so it can be passed the db name).
1 parent f17ec76 commit 9308656

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ There are several things to keep in mind when using the client side db:
187187

188188
- While the resolved migrationJson location is added to the public runtime config, it cannot be used to import it dynamically since dynamic imports don't work with variables.
189189
- **The client options are exposed to the public runtime config.** There is no such thing as the private runtimeConfig client side.
190-
- `migrate` will try to skip migrations if at all possible. Doing a drizzle migration, even if nothing needs to be done, is expensive (~1500ms), so `migrate` stores a localstorage key `db:lastMigrationHash` (configurable) to prevent unnecessary calls to drizzle's migrate. If the database is configured to use indexedDb, it exists, and the last known hash matches the last migration hash, migration is skipped, reducing the time for non-migrations to around 4-5ms.
190+
- `migrate` will try to skip migrations if at all possible. Doing a drizzle migration, even if nothing needs to be done, is expensive (~1500ms), so `migrate` stores a localstorage key `db:lastMigrationHash:[NAME (if provided)]` (configurable) to prevent unnecessary calls to drizzle's migrate. If the database is configured to use indexedDb, it exists, and the last known hash matches the last migration hash, migration is skipped, reducing the time for non-migrations to around 4-5ms.
191191

192192
## Using a Local Server Database for Testing
193193

@@ -201,15 +201,15 @@ The client db can be used in electron or other contexts that support it. You wil
201201

202202
Electron example using [@witchcraft/nuxt-electron](TODO):
203203
```ts
204-
const db = await useClientDb("name", {
204+
const db = await useClientDb("NAME", {
205205
schema,
206206
clientMigrationOptions: {
207207
migrationJson,
208208
migrationsLogger: useElectronLogger(),
209209
},
210210
...STATIC.ELECTRON_RUNTIME_CONFIG.postgres,
211211
// we need to override the filepath so it can write to disk
212-
clientPgLitePath: path.join(userDataDir, "db.pglite"),
212+
clientPgLitePath: (name) => path.join(userDataDir, `${name}.pglite`),
213213
}, {
214214
logger: useElectronLogger(),
215215
// import.meta.client is not defined

src/module.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ export type ClientPostgresOptions = {
115115
* @default false
116116
*/
117117
useClientDb: boolean
118-
/**
119-
* Where to store the client side PGlite database.
120-
*
121-
* @default "idb://[NAME]" (indexeddb)
122-
*/
123-
clientPgLitePath: string
124118

125119
/**
126120
* Whether to generate a client migrations json file for use with the client side PGlite database.
@@ -208,7 +202,6 @@ export default defineNuxtModule<ModuleOptions>({
208202
generateDrizzleClientMigrationsJson: undefined as any as boolean,
209203
useClientDb: false,
210204
devAutoGenerateMigrations: false,
211-
clientPgLitePath: "idb://local-pglite",
212205
autoMigrateClientDb: true,
213206
eventContextKeyName: "$postgres",
214207
aliasServerImport: "~~/server/postgres"
@@ -250,7 +243,6 @@ export default defineNuxtModule<ModuleOptions>({
250243
delete privateOptions.useClientDb
251244
delete privateOptions.useWebWorker
252245
delete privateOptions.generateClientMigrationsJson
253-
delete privateOptions.clientPgLitePath
254246

255247
const serverConfig = nuxt.options.runtimeConfig.postgres.serverMigrationConfig
256248
serverConfig.migrationsFolder = path.relative(nuxt.options.rootDir, resolveAlias(serverConfig.migrationsFolder, nuxt.options.alias))
@@ -263,7 +255,6 @@ export default defineNuxtModule<ModuleOptions>({
263255
nuxt.options.runtimeConfig.public.postgres as any,
264256
{
265257
clientMigrationConfig: options.clientMigrationConfig,
266-
clientPgLitePath: options.clientPgLitePath,
267258
autoMigrateClientDb: options.autoMigrateClientDb
268259
}
269260
)

src/runtime/utils/clientDatabaseManager.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ type AllOptions = Partial<Omit<ClientPostgresOptions, "clientMigrationConfig"> &
6161
clientPgliteOptions: ClientPostgresOptions["clientPgliteOptions"] & {
6262
extensions?: PGliteOptions["extensions"]
6363
}
64+
/**
65+
* A function that return the filepath for the PGlite database. See {@link https://pglite.dev/docs/filesystems}.
66+
*
67+
* @default "idb://[NAME]" (indexeddb)
68+
*/
69+
clientPgLitePath?: ((name: string) => string)
6470
/** Custom proxy function, in this case a drizzleProxy is created instead of a client, and the database manager passes a wrapper so you can also get the name of the database you're proxying to, the rest of the params are just like the ones in the drizzle proxy docs. */
6571
drizzleProxy: (name: string, sql: string, params: any[], method: "all" | "run" | "get" | "values") => Promise<any>
6672
/** The schema to use for the client side database. This must be defined unless a proxy is being used. */
@@ -118,18 +124,19 @@ export class ClientDatabaseManager {
118124
console.warn("No schema for a client side database was provided. This is not recommended. Drizzle will not be able to do db.query type queries. Schema can only safely not be defined if using drizzleProxy (as it would be defined on the real instance).")
119125
}
120126

127+
const dbPath = opts.clientPgLitePath?.(name) ?? `idb://${name}`
121128
const client = opts.drizzleProxy
122129
? undefined
123130
: (opts.useWebWorker
124131
? new PGliteWorker(
125132
new Worker(opts.webWorkerUrl ?? new URL("./../worker.js", import.meta.url), { type: "module" }),
126133
{
127-
dataDir: opts.clientPgLitePath ?? `idb:// ${name}`,
134+
dataDir: dbPath,
128135
meta: { options: clientPgliteOptions }
129136
// extensions
130137
}
131138
)
132-
: new PGlite(opts.clientPgLitePath ?? `idb://${name}`, clientPgliteOptions))
139+
: new PGlite(dbPath, clientPgliteOptions))
133140

134141
const migrationOptions = opts.clientMigrationOptions ?? {}
135142

@@ -144,7 +151,7 @@ export class ClientDatabaseManager {
144151
...ClientDatabaseManager.defaultMigrationState,
145152
storage: migrationOptions.storage ?? ClientDatabaseManager.useDefaultStorage()
146153
},
147-
path: opts?.clientPgLitePath ?? `idb://${name}`
154+
path: dbPath
148155

149156
}
150157
this.databases.set(name, entry)

0 commit comments

Comments
 (0)