Skip to content

Commit

Permalink
feat(db): support Nitro useDatabase()
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Mar 7, 2024
1 parent 2059138 commit 2e16536
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
41 changes: 0 additions & 41 deletions playground/server/api/test.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
12 changes: 12 additions & 0 deletions playground/server/api/tests/kv.ts
Original file line number Diff line number Diff line change
@@ -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()
})
11 changes: 11 additions & 0 deletions src/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ export default defineNuxtModule<ModuleOptions>({
}
})

// Bind `useDatabase()` to `hubDatabase()`
nuxt.options.nitro.experimental = defu(nuxt.options.nitro.experimental, {
database: true
})
nuxt.options.nitro.database = defu(nuxt.options.nitro.database, {
default: {
connector: 'cloudflare-d1',
options: { bindingName: 'DB' }
}
})

// nuxt prepare or production mode, stop here
if (nuxt.options._prepare || !nuxt.options.dev) {
return
Expand Down
5 changes: 4 additions & 1 deletion src/module/runtime/bindings.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ async function getBindingsProxy() {
persist: { path: wranglerConfig.persistDir },
})

// @ts-ignore
globalThis.__cf_env__ = proxy.env

Object.keys(proxy.env).forEach((key) => {
// @ts-ignore
if (!globalThis[key]) {
// @ts-ignore
// @ts-ignore
globalThis[key] = proxy.env[key]
}
})
Expand Down

0 comments on commit 2e16536

Please sign in to comment.