diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index b474371c..f59cbe8b 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -25,6 +25,7 @@ export default defineNuxtConfig({ nitro: { // preset: 'cloudflare-durable', experimental: { + database: true, openAPI: true, websocket: true } diff --git a/playground/server/api/cached.ts b/playground/server/api/tests/cached.ts similarity index 100% rename from playground/server/api/cached.ts rename to playground/server/api/tests/cached.ts diff --git a/playground/server/api/db-test.ts b/playground/server/api/tests/db.ts similarity index 77% rename from playground/server/api/db-test.ts rename to playground/server/api/tests/db.ts index 7733ebdd..76bb76e5 100644 --- a/playground/server/api/db-test.ts +++ b/playground/server/api/tests/db.ts @@ -1,6 +1,20 @@ import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core' export default defineEventHandler(async () => { + const db0 = useDatabase() + + const _tables = await db0.sql` + SELECT + name, + type + FROM + sqlite_schema + WHERE + type = 'table' AND + name NOT LIKE 'sqlite_%' and name NOT LIKE '_litestream_%' and name NOT LIKE '__drizzle%' + ;` + + console.log(_tables.rows) const db = useDrizzle() const tables = await db.all(sql` diff --git a/playground/server/api/tests/kv.ts b/playground/server/api/tests/kv.ts new file mode 100644 index 00000000..2414f0e7 --- /dev/null +++ b/playground/server/api/tests/kv.ts @@ -0,0 +1,12 @@ +export default eventHandler(async () => { + const kv = hubKV() + + await kv.set('vue', { year: 2014 }) + await kv.set('vue:nuxt', { year: 2016 }) + await kv.set('vue:quasar', { version: 2015 }) + await kv.set('react', { version: 2013 }) + await kv.set('react:next', { version: 2016 }) + await kv.set('react:gatsby', { version: 2015 }) + + return kv.keys() +}) diff --git a/src/features.ts b/src/features.ts index e2be0af6..08642215 100644 --- a/src/features.ts +++ b/src/features.ts @@ -216,6 +216,17 @@ export async function setupDatabase(nuxt: Nuxt, hub: HubConfig) { // Add Server scanning addServerScanDir(resolve('./runtime/database/server')) addServerImportsDir(resolve('./runtime/database/server/utils')) + // Bind `useDatabase()` to `hubDatabase()` if experimental.database is true + if (nuxt.options.nitro.experimental?.database) { + // @ts-expect-error cannot respect the typed database configs + nuxt.options.nitro.database = defu(nuxt.options.nitro.database, { + default: { + connector: 'cloudflare-d1', + options: { bindingName: 'DB' } + } + }) + } + // Handle migrations nuxt.hook('modules:done', async () => { // Call hub:database:migrations:dirs hook await nuxt.callHook('hub:database:migrations:dirs', hub.databaseMigrationsDirs!)