-
-
Notifications
You must be signed in to change notification settings - Fork 745
fix(nuxthub): handle object form of hub.db
#3822
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
Open
benjamincanac
wants to merge
5
commits into
main
Choose a base branch
from
fix/nuxthub-db-object-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
β18
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c80e16c
fix(nuxthub): handle object form of hub.db
benjamincanac 425b590
fix(nuxthub): normalize legacy sqlite and postgres driver aliases
benjamincanac 985308f
fix(nuxthub): reject database configs missing required connection values
benjamincanac 8d1f631
fix(nuxthub): validate connection value types in database mapping
benjamincanac 2744e23
test(nuxthub): cover string form of hub.db in setupNitro
benjamincanac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { describe, expect, test, vi } from 'vitest' | ||
| import type { Nuxt } from '@nuxt/schema' | ||
| import type { Resolver } from '@nuxt/kit' | ||
| import type { NitroConfig } from 'nitropack' | ||
| import nuxthubPreset, { hubDatabaseToContentDatabase } from '../../src/presets/nuxthub' | ||
| import type { ModuleOptions } from '../../src/types/module' | ||
| import type { Manifest } from '../../src/types/manifest' | ||
|
|
||
| // `addTemplate` and `addServerHandler` require a live Nuxt context | ||
| vi.mock('@nuxt/kit', async (importOriginal) => { | ||
| const original = await importOriginal<typeof import('@nuxt/kit')>() | ||
| return { | ||
| ...original, | ||
| addTemplate: vi.fn(() => ({ dst: '' })), | ||
| addServerHandler: vi.fn(), | ||
| } | ||
| }) | ||
|
|
||
| const resolver = { resolve: (p: string) => p } as unknown as Resolver | ||
| const manifest = { collections: [], dump: {} } as unknown as Manifest | ||
| const opts = { resolver, manifest } | ||
|
|
||
| function createNuxt(hub: Record<string, unknown>, runtimeHub: Record<string, unknown>): Nuxt { | ||
| return { | ||
| options: { | ||
| dev: false, | ||
| hub, | ||
| nitro: { preset: 'node-server' }, | ||
| runtimeConfig: { hub: runtimeHub }, | ||
| }, | ||
| } as unknown as Nuxt | ||
| } | ||
|
|
||
| describe('hubDatabaseToContentDatabase', () => { | ||
| test('maps d1 driver to d1 database', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'd1' })).toEqual({ type: 'd1', bindingName: 'DB' }) | ||
| }) | ||
|
|
||
| test('maps postgres drivers to postgresql database', () => { | ||
| for (const driver of ['node-postgres', 'postgres-js', 'neon-http', 'postgres', 'postgresql']) { | ||
| expect(hubDatabaseToContentDatabase({ driver, connection: { url: 'postgres://localhost' } })) | ||
| .toEqual({ type: 'postgresql', url: 'postgres://localhost' }) | ||
| } | ||
| }) | ||
|
|
||
| test('maps sqlite drivers to sqlite database', () => { | ||
| for (const driver of ['sqlite', 'better-sqlite3']) { | ||
| expect(hubDatabaseToContentDatabase({ driver, connection: { url: 'file:.data/hub/db/sqlite.db' } })) | ||
| .toEqual({ type: 'sqlite', filename: '.data/hub/db/sqlite.db' }) | ||
| } | ||
| }) | ||
|
|
||
| test('keeps an explicit sqlite filename', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'sqlite', connection: { filename: './contents.sqlite' } })) | ||
| .toEqual({ type: 'sqlite', filename: './contents.sqlite' }) | ||
| }) | ||
|
|
||
| test('maps libsql driver with its connection', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'libsql', connection: { url: 'file:.data/hub/db/sqlite.db' } })) | ||
| .toEqual({ type: 'libsql', url: 'file:.data/hub/db/sqlite.db' }) | ||
| }) | ||
|
|
||
| test('maps pglite driver with its connection', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'pglite', connection: { dataDir: '.data/hub/db/pglite' } })) | ||
| .toEqual({ type: 'pglite', dataDir: '.data/hub/db/pglite' }) | ||
| }) | ||
|
|
||
| test('returns undefined for unsupported drivers', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'mysql2', connection: { uri: 'mysql://localhost' } })).toBeUndefined() | ||
| }) | ||
|
|
||
| test('returns undefined when required connection values are missing', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'postgres-js' })).toBeUndefined() | ||
| expect(hubDatabaseToContentDatabase({ driver: 'postgres-js', connection: { url: '' } })).toBeUndefined() | ||
| expect(hubDatabaseToContentDatabase({ driver: 'sqlite' })).toBeUndefined() | ||
| expect(hubDatabaseToContentDatabase({ driver: 'sqlite', connection: {} })).toBeUndefined() | ||
| }) | ||
|
|
||
| test('returns undefined for non-string connection values', () => { | ||
| expect(hubDatabaseToContentDatabase({ driver: 'postgres-js', connection: { url: 123 as unknown as string } })).toBeUndefined() | ||
| expect(hubDatabaseToContentDatabase({ driver: 'sqlite', connection: { filename: true, url: 123 as unknown as string } })).toBeUndefined() | ||
| expect(hubDatabaseToContentDatabase({ driver: 'sqlite', connection: { filename: true, url: 'file:.data/hub/db/sqlite.db' } })) | ||
| .toEqual({ type: 'sqlite', filename: '.data/hub/db/sqlite.db' }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('nuxthub preset setup', () => { | ||
| const resolvedSqliteDb = { driver: 'libsql', connection: { url: 'file:.data/hub/db/sqlite.db' } } | ||
|
|
||
| test('maps the string form of hub.db', async () => { | ||
| const options = {} as ModuleOptions | ||
| await nuxthubPreset.setup!(options, createNuxt({ db: 'sqlite' }, { db: resolvedSqliteDb }), opts) | ||
|
|
||
| expect(options.database).toEqual({ type: 'libsql', url: 'file:.data/hub/db/sqlite.db' }) | ||
| }) | ||
|
|
||
| test('maps the object form of hub.db', async () => { | ||
| const options = {} as ModuleOptions | ||
| await nuxthubPreset.setup!(options, createNuxt({ db: { dialect: 'sqlite', applyMigrationsDuringBuild: false } }, { db: resolvedSqliteDb }), opts) | ||
|
|
||
| expect(options.database).toEqual({ type: 'libsql', url: 'file:.data/hub/db/sqlite.db' }) | ||
| }) | ||
|
|
||
| test('does not override an explicitly configured database', async () => { | ||
| const options = { database: { type: 'libsql', url: 'file:/tmp/sqlite.db' } } as ModuleOptions | ||
| await nuxthubPreset.setup!(options, createNuxt({ db: { dialect: 'sqlite' } }, { db: resolvedSqliteDb }), opts) | ||
|
|
||
| expect(options.database).toEqual({ type: 'libsql', url: 'file:/tmp/sqlite.db' }) | ||
| }) | ||
|
|
||
| test('leaves the database unset for unsupported drivers', async () => { | ||
| const options = {} as ModuleOptions | ||
| await nuxthubPreset.setup!(options, createNuxt({ db: { dialect: 'mysql' } }, { db: { driver: 'mysql2', connection: { uri: 'mysql://localhost' } } }), opts) | ||
|
|
||
| expect(options.database).toBeUndefined() | ||
| }) | ||
|
|
||
| test('uses d1 with NuxtHub <= 0.9', async () => { | ||
| const options = {} as ModuleOptions | ||
| await nuxthubPreset.setup!(options, createNuxt({ database: true }, { database: true }), opts) | ||
|
|
||
| expect(options.database).toEqual({ type: 'd1', bindingName: 'DB' }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('nuxthub preset setupNitro', () => { | ||
| test('maps the object form of hub.db and rewrites local libsql to /tmp', async () => { | ||
| const nuxt = createNuxt( | ||
| { db: { dialect: 'sqlite', applyMigrationsDuringBuild: false } }, | ||
| { db: { driver: 'libsql', connection: { url: 'file:.data/hub/db/sqlite.db' }, applyMigrationsDuringBuild: false } }, | ||
| ) | ||
| const nitroConfig = { runtimeConfig: { content: {} }, rootDir: '/' } as unknown as NitroConfig | ||
| await nuxthubPreset.setupNitro(nitroConfig, { ...opts, moduleOptions: {} as ModuleOptions, nuxt }) | ||
|
|
||
| expect(nitroConfig.runtimeConfig!.content!.database).toEqual({ type: 'libsql', url: 'file:/tmp/sqlite.db' }) | ||
| expect(nitroConfig.runtimeConfig!.content!.integrityCheck).toBe(true) | ||
| }) | ||
|
|
||
| test('maps the string form of hub.db to the same database', async () => { | ||
| const nuxt = createNuxt( | ||
| { db: 'sqlite' }, | ||
| { db: { driver: 'libsql', connection: { url: 'file:.data/hub/db/sqlite.db' }, applyMigrationsDuringBuild: false } }, | ||
| ) | ||
| const nitroConfig = { runtimeConfig: { content: {} }, rootDir: '/' } as unknown as NitroConfig | ||
| await nuxthubPreset.setupNitro(nitroConfig, { ...opts, moduleOptions: {} as ModuleOptions, nuxt }) | ||
|
|
||
| expect(nitroConfig.runtimeConfig!.content!.database).toEqual({ type: 'libsql', url: 'file:/tmp/sqlite.db' }) | ||
| expect(nitroConfig.runtimeConfig!.content!.integrityCheck).toBe(true) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.