-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
138 additions
and
235 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains 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,3 @@ | ||
export default defineNuxtConfig({ | ||
|
||
}) |
This file contains 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,27 @@ | ||
import { z } from 'zod' | ||
|
||
export default eventHandler(async (event) => { | ||
const { sql, params, method } = await readValidatedBody(event, z.object({ | ||
sql: z.string(), | ||
params: z.array(z.any()), | ||
method: z.enum(['all', 'get', 'run', 'values']) | ||
}).parse) | ||
|
||
// Make sure the client is initialized first | ||
useDatabase() | ||
const client = useDatabaseClient() | ||
|
||
try { | ||
if (method === 'run') | ||
return client.prepare(sql).run(params) | ||
if (method === 'all' || method === 'values') | ||
return client.prepare(sql).raw().all(params) | ||
// method = 'get' | ||
return client.prepare(sql).raw().get(params) | ||
} catch (e: any) { | ||
throw createError({ | ||
statusCode: 500, | ||
message: `Database error: ${e.message}` | ||
}) | ||
} | ||
}) |
This file contains 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,56 @@ | ||
import { drizzle as drizzleD1, DrizzleD1Database } from 'drizzle-orm/d1' | ||
import { drizzle } from 'drizzle-orm/better-sqlite3' | ||
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' | ||
import { drizzle as drizzleHTTP } from 'drizzle-orm/sqlite-proxy' | ||
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy' | ||
import { ofetch } from 'ofetch' | ||
// @ts-ignore | ||
import Database from 'better-sqlite3' | ||
import { join } from 'pathe' | ||
|
||
// export * as tables from '~/server/database/schema' | ||
|
||
let _db: DrizzleD1Database | BetterSQLite3Database | SqliteRemoteDatabase | null = null | ||
let _client: any = null | ||
|
||
export const useDatabase = () => { | ||
if (!_db) { | ||
const isDev = process.env.NODE_ENV === 'development' | ||
if (process.env.DB) { | ||
// d1 in production | ||
_client = process.env.DB | ||
_db = drizzleD1(_client) | ||
} else if (isDev && process.env.NUXT_HUB_URL) { | ||
console.log('Using D1 remote database...') | ||
_db = drizzleHTTP(async (sql, params, method) => { | ||
// https://orm.drizzle.team/docs/get-started-sqlite#http-proxy | ||
try { | ||
const rows = await ofetch('/api/db/query', { | ||
baseURL: process.env.NUXT_HUB_URL, | ||
method: 'POST', | ||
body: { sql, params, method } | ||
}) | ||
return { rows } | ||
} catch (err) { | ||
console.error('Error from remote database', err) | ||
return { rows: [] } | ||
} | ||
}) | ||
} else if (isDev) { | ||
// local sqlite in development | ||
console.log('Using local database...') | ||
_client = new Database(join(process.cwd(), './db.sqlite')) | ||
_db = drizzle(_client) | ||
} else { | ||
throw new Error('No database configured for production') | ||
} | ||
} | ||
return _db | ||
} | ||
|
||
export const useDatabaseClient = () => { | ||
if (!_client) { | ||
throw new Error('No client configured') | ||
} | ||
return _client | ||
} |
This file contains 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 |
---|---|---|
@@ -1,22 +1,6 @@ | ||
export default defineAppConfig({ | ||
ui: { | ||
primary: 'emerald', | ||
container: { | ||
constrained: 'max-w-2xl' | ||
}, | ||
card: { | ||
header: { | ||
base: 'flex flex-wrap items-center justify-between' | ||
}, | ||
body: { | ||
base: 'space-y-4' | ||
} | ||
}, | ||
dropdown: { | ||
width: 'w-full', | ||
popper: { | ||
strategy: 'absolute' | ||
} | ||
} | ||
primary: 'lime', | ||
gray: 'neutral' | ||
} | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,70 +1,31 @@ | ||
<script setup> | ||
const { loggedIn } = useUserSession() | ||
<script setup lang="ts"> | ||
const colorMode = useColorMode() | ||
watch(loggedIn, () => { | ||
if (!loggedIn.value) { | ||
navigateTo('/') | ||
} | ||
}) | ||
function toggleColorMode() { | ||
colorMode.preference = colorMode.preference === 'dark' ? 'light' : 'dark' | ||
} | ||
const color = computed(() => colorMode.value === 'dark' ? '#111827' : 'white') | ||
useHead({ | ||
htmlAttrs: { lang: 'en' }, | ||
link: [{ rel: 'icon', href: '/icon.png' }], | ||
}) | ||
useSeoMeta({ | ||
viewport: 'width=device-width, initial-scale=1, maximum-scale=1', | ||
title: 'Nuxt Todos Edge', | ||
description: | ||
'A Nuxt demo hosted with Edge-side rendering, authentication and queyring a SQLite database', | ||
ogImage: '/social-image.png', | ||
twitterImage: '/social-image.png', | ||
twitterCard: 'summary_large_image', | ||
meta: [ | ||
{ charset: 'utf-8' }, | ||
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }, | ||
{ key: 'theme-color', name: 'theme-color', content: color } | ||
], | ||
link: [ | ||
{ rel: 'icon', href: '/favicon.ico' } | ||
], | ||
htmlAttrs: { | ||
lang: 'en' | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<UContainer class="min-h-screen flex flex-col justify-center"> | ||
<div class="mb-2 text-right"> | ||
<UButton | ||
square | ||
variant="ghost" | ||
color="black" | ||
:icon="$colorMode.preference === 'dark' ? 'i-heroicons-moon' : 'i-heroicons-sun'" | ||
@click="toggleColorMode" | ||
/> | ||
</div> | ||
|
||
<NuxtPage /> | ||
<div> | ||
<NuxtLoadingIndicator /> | ||
|
||
<footer class="text-center mt-2"> | ||
<NuxtLink | ||
href="https://github.com/atinux/nuxt-todos-edge" | ||
target="_blank" | ||
class="text-sm text-gray-500 hover:text-gray-700" | ||
> | ||
GitHub | ||
</NuxtLink> | ||
· | ||
<NuxtLink | ||
href="https://twitter.com/Atinux" | ||
target="_blank" | ||
class="text-sm text-gray-500 hover:text-gray-700" | ||
> | ||
</NuxtLink> | ||
</footer> | ||
</UContainer> | ||
<UNotifications /> | ||
</template> | ||
<NuxtLayout> | ||
<NuxtPage /> | ||
</NuxtLayout> | ||
|
||
<style lang="postcss"> | ||
body { | ||
@apply font-sans text-gray-950 bg-gray-50 dark:bg-gray-950 dark:text-gray-50; | ||
} | ||
</style> | ||
<UNotifications /> | ||
</div> | ||
</template> |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,18 @@ | ||
import { sql } from 'drizzle-orm' | ||
|
||
export default defineEventHandler(async (event) => { | ||
const db = useDatabase() | ||
|
||
const rows = await db.all(sql` | ||
SELECT | ||
name | ||
FROM | ||
sqlite_schema | ||
WHERE | ||
type = 'table' AND | ||
name NOT LIKE 'sqlite_%' and name NOT LIKE '_litestream_%' and name NOT LIKE '__drizzle%' | ||
; | ||
`) | ||
|
||
return rows | ||
}) |
This file contains 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 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 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 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
Oops, something went wrong.