Skip to content
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

feat(db): support Nitro useDatabase() #15

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -67,4 +67,4 @@
"vitest": "^1.4.0",
"wrangler": "^3.34.2"
}
}
}
41 changes: 0 additions & 41 deletions playground/server/api/test.ts

This file was deleted.

File renamed without changes.
@@ -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)
Atinux marked this conversation as resolved.
Show resolved Hide resolved
const db = useDrizzle()

const tables = await db.all(sql`
Expand Down
12 changes: 12 additions & 0 deletions 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()
})
12 changes: 12 additions & 0 deletions src/module/index.ts
Expand Up @@ -92,6 +92,18 @@ export default defineNuxtModule<ModuleOptions>({
}
})

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

// nuxt prepare, stop here
if (nuxt.options._prepare) {
return
Expand Down