From ec7e13f9c47fbfdd8992c4b8c444bce80ca572b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 26 Feb 2024 23:28:18 +0100 Subject: [PATCH] chore: rename use to hub prefix to avoid collision with Nitro In order to better integrate in the future --- README.md | 6 +++--- docs/content/docs/1.getting-started/1.index.md | 6 +++--- docs/content/docs/2.storage/1.database.md | 6 +++--- docs/content/docs/2.storage/2.kv.md | 10 +++++----- docs/content/docs/2.storage/3.blob.md | 12 ++++++------ docs/content/docs/3.recipes/1.hooks.md | 4 ++-- docs/content/docs/3.recipes/2.drizzle.md | 4 ++-- docs/content/index.yml | 10 +++++----- playground/server/api/blob/[...pathname].delete.ts | 2 +- playground/server/api/blob/[...pathname].get.ts | 2 +- playground/server/api/blob/index.get.ts | 2 +- playground/server/api/blob/index.put.ts | 2 +- playground/server/api/kv/[key].delete.ts | 2 +- playground/server/api/kv/index.get.ts | 2 +- playground/server/api/kv/index.put.ts | 2 +- playground/server/api/test.ts | 2 +- playground/server/utils/db.ts | 2 +- .../server/api/_hub/blob/[...pathname].delete.ts | 4 ++-- .../server/api/_hub/blob/[...pathname].get.ts | 4 ++-- .../server/api/_hub/blob/[...pathname].head.ts | 4 ++-- .../server/api/_hub/blob/[...pathname].put.ts | 4 ++-- src/runtime/server/api/_hub/blob/index.get.ts | 4 ++-- .../server/api/_hub/database/[command].post.ts | 4 ++-- src/runtime/server/api/_hub/kv/[...path].ts | 4 ++-- src/runtime/server/api/_hub/manifest.get.ts | 12 ++++++------ src/runtime/server/utils/blob.ts | 2 +- src/runtime/server/utils/database.ts | 2 +- src/runtime/server/utils/kv.ts | 2 +- 28 files changed, 61 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 2075c10e..be4c7f3f 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ Build full-stack applications with Nuxt on CloudFlare, with zero configuration. ## Features -- Query an SQLite database with `useDatabase()` -- Access key-value storage with `useKV()` -- Store files with `useBlob()` +- Query an SQLite database with `hubDatabase()` +- Access key-value storage with `hubKV()` +- Store files with `hubBlob()` Read more on https://hub.nuxt.com diff --git a/docs/content/docs/1.getting-started/1.index.md b/docs/content/docs/1.getting-started/1.index.md index 507352c6..d7fbca29 100644 --- a/docs/content/docs/1.getting-started/1.index.md +++ b/docs/content/docs/1.getting-started/1.index.md @@ -12,9 +12,9 @@ It is **currently made to be deployed on [Cloudflare Pages](https://pages.cloudf ## Storage NuxtHub provides different storage to help you build full-stack applications: -- **SQL database** to store your application's data with [`useDatabase()`](/docs/storage/database) -- **Key-Value** to store JSON data accessible globally with low-latency with [`useKV()`](/docs/storage/kv) -- **Blob storage** to store static assets, such as images, video and more with [`useBlob()`](/docs/storage/blob) +- **SQL database** to store your application's data with [`hubDatabase()`](/docs/storage/database) +- **Key-Value** to store JSON data accessible globally with low-latency with [`hubKV()`](/docs/storage/kv) +- **Blob storage** to store static assets, such as images, video and more with [`hubBlob()`](/docs/storage/blob) Each storage utils is auto-imported and ready to be used in your [Nuxt's server directory](https://nuxt.com/docs/guide/directory-structure/server). diff --git a/docs/content/docs/2.storage/1.database.md b/docs/content/docs/2.storage/1.database.md index c86be621..5286b9d0 100644 --- a/docs/content/docs/2.storage/1.database.md +++ b/docs/content/docs/2.storage/1.database.md @@ -7,12 +7,12 @@ NuxtHub Database is a layer on top of [Cloudflare D1](https://developers.cloudfl Once properly configured, NuxtHub module exposes a server composable to the application. -## `useDatabase()` +## `hubDatabase()` Server composable that returns a [D1 database client](https://developers.cloudflare.com/d1/build-databases/query-databases/). ```ts -const db = useDatabase() +const db = hubDatabase() ``` ::callout @@ -183,7 +183,7 @@ Executes one or more queries directly without prepared statements or parameters If an error occurs, an exception is thrown with the query and error messages, execution stops and further statements are not executed. ```ts -const result = await useDatabase().exec(`CREATE TABLE IF NOT EXISTS frameworks (id INTEGER PRIMARY KEY, name TEXT NOT NULL, year INTEGER NOT NULL DEFAULT 0)`) +const result = await hubDatabase().exec(`CREATE TABLE IF NOT EXISTS frameworks (id INTEGER PRIMARY KEY, name TEXT NOT NULL, year INTEGER NOT NULL DEFAULT 0)`) console.log(result) /* { diff --git a/docs/content/docs/2.storage/2.kv.md b/docs/content/docs/2.storage/2.kv.md index 0c5ac8b1..489f639b 100644 --- a/docs/content/docs/2.storage/2.kv.md +++ b/docs/content/docs/2.storage/2.kv.md @@ -9,7 +9,7 @@ NuxtHub KV is a layer to [Cloudflare Workers KV](https://developers.cloudflare.c Once properly configured, NuxtHub module exposes a server composable to the application. -## `useKV()` +## `hubKV()` Server composable that returns a [Storage](https://unstorage.unjs.io/getting-started/usage#interface){target=_blank}. @@ -19,7 +19,7 @@ Retrieves all keys from the storage. ```ts[/api/kv/index.get.ts] export default eventHandler(async () => { - return await useKV().getKeys() + return await hubKV().getKeys() }) ``` @@ -31,7 +31,7 @@ Retrieves an item from the storage. export default eventHandler(async () => { const { key } = getRouterParams(event) - return await useKV().getItem(key) + return await hubKV().getItem(key) }) ``` @@ -43,7 +43,7 @@ Puts an item in the storage. export default eventHandler(async () => { const { key, value } = await readBody(event) - return await useKV().setItem(key, value) + return await hubKV().setItem(key, value) }) ``` @@ -55,7 +55,7 @@ Deletes an item from the storage. export default eventHandler(async (event) => { const { key } = getRouterParams(event) - await useKV().removeItem(key) + await hubKV().removeItem(key) return { key } }) diff --git a/docs/content/docs/2.storage/3.blob.md b/docs/content/docs/2.storage/3.blob.md index d24568e6..93208cb6 100644 --- a/docs/content/docs/2.storage/3.blob.md +++ b/docs/content/docs/2.storage/3.blob.md @@ -7,7 +7,7 @@ NuxtHub Blob is a layer to [Cloudflare R2](https://developers.cloudflare.com/r2) Once properly configured, NuxtHub module exposes a server composable to the application. -## `useBlob()` +## `hubBlob()` Server composable that returns a set of methods to manipulate the blob storage. @@ -17,7 +17,7 @@ Returns a paginated list of blobs. ```ts [server/api/files.get.ts] export default eventHandler(async () => { - return useBlob().list() + return hubBlob().list() }) ``` @@ -50,7 +50,7 @@ Returns a blob's data. export default eventHandler(async (event) => { const { pathname } = getRouterParams(event) - return useBlob().serve(event, pathname) + return hubBlob().serve(event, pathname) }) ``` @@ -82,7 +82,7 @@ If you are fetching an image with a server route similar to the one above, you m Returns a blob's metadata. ```ts -const blob = await useBlob().head(pathname) +const blob = await hubBlob().head(pathname) ``` #### Params @@ -113,7 +113,7 @@ export default eventHandler(async (event) => { ensureBlob(file, { maxSize: '1MB', types: ['image' ]}) - return useBlob().put(`images/${file.name}`, file, { addRandomSuffix: false }) + return hubBlob().put(`images/${file.name}`, file, { addRandomSuffix: false }) }) ``` @@ -152,7 +152,7 @@ Deletes a blob. export default eventHandler(async (event) => { const { pathname } = getRouterParams(event) - await useBlob().delete(pathname) + await hubBlob().delete(pathname) return sendNoContent(event) }) diff --git a/docs/content/docs/3.recipes/1.hooks.md b/docs/content/docs/3.recipes/1.hooks.md index 58f895fe..b044fc11 100644 --- a/docs/content/docs/3.recipes/1.hooks.md +++ b/docs/content/docs/3.recipes/1.hooks.md @@ -18,7 +18,7 @@ export default defineNitroPlugin(() => { // Only run migrations in development if (import.meta.dev) { onHubReady(async () => { - await useDatabase().exec(` + await hubDatabase().exec(` CREATE TABLE IF NOT EXISTS todos ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, @@ -55,7 +55,7 @@ You can use the `hubHooks` object to listen to the `bindings:ready` event in you export default defineNitroPlugin(() => { hubHooks.hook('bindings:ready', () => { console.log('NuxtHub bindings are ready!') - const db = useDatabase() + const db = hubDatabase() }) }) ``` diff --git a/docs/content/docs/3.recipes/2.drizzle.md b/docs/content/docs/3.recipes/2.drizzle.md index c20f4475..de1c7274 100644 --- a/docs/content/docs/3.recipes/2.drizzle.md +++ b/docs/content/docs/3.recipes/2.drizzle.md @@ -104,7 +104,7 @@ export default defineNitroPlugin(async () => { let migrationFiles = await migrationsStorage.getKeys() migrationFiles = migrationFiles.filter(key => key.endsWith('.sql')) - const database = useDatabase() + const database = hubDatabase() for (const file of migrationFiles) { consola.info(`Applying database migrations from ${file}...`) @@ -140,7 +140,7 @@ import * as schema from '../database/schema' export const tables = schema export function useDrizzle() { - return drizzle(useDatabase(), { schema }) + return drizzle(hubDatabase(), { schema }) } export type User = typeof schema.users.$inferSelect diff --git a/docs/content/index.yml b/docs/content/index.yml index 5e9470d2..9d720ca3 100644 --- a/docs/content/index.yml +++ b/docs/content/index.yml @@ -11,7 +11,7 @@ hero: trailing: true to: '/docs/getting-started' size: lg - - label: Open on GitHub + - label: Star on GitHub icon: i-simple-icons-github size: lg color: gray @@ -20,9 +20,9 @@ hero: code: | ```ts [server/api/hello.ts] export default eventHandler(async (event) => { - const database = useDatabase() - const blob = useBlob() - const kv = useKV() + const database = hubDatabase() + const blob = hubBlob() + const kv = hubKV() // Do your magic here return { hello: 'world' } @@ -37,7 +37,7 @@ features: trailing: true to: '/docs/getting-started' size: lg - - label: Open on GitHub + - label: Star on GitHub icon: i-simple-icons-github size: lg color: gray diff --git a/playground/server/api/blob/[...pathname].delete.ts b/playground/server/api/blob/[...pathname].delete.ts index 1697a138..822ed688 100644 --- a/playground/server/api/blob/[...pathname].delete.ts +++ b/playground/server/api/blob/[...pathname].delete.ts @@ -3,5 +3,5 @@ export default eventHandler(async (event) => { pathname: z.string().min(1) }).parse) - return useBlob().delete(pathname) + return hubBlob().delete(pathname) }) diff --git a/playground/server/api/blob/[...pathname].get.ts b/playground/server/api/blob/[...pathname].get.ts index 8eda3edc..4685cbfe 100644 --- a/playground/server/api/blob/[...pathname].get.ts +++ b/playground/server/api/blob/[...pathname].get.ts @@ -3,5 +3,5 @@ export default eventHandler(async (event) => { pathname: z.string().min(1) }).parse) - return useBlob().serve(event, pathname) + return hubBlob().serve(event, pathname) }) diff --git a/playground/server/api/blob/index.get.ts b/playground/server/api/blob/index.get.ts index 88d5c0fe..d1fdfed9 100644 --- a/playground/server/api/blob/index.get.ts +++ b/playground/server/api/blob/index.get.ts @@ -1,3 +1,3 @@ export default eventHandler(async () => { - return useBlob().list() + return hubBlob().list() }) diff --git a/playground/server/api/blob/index.put.ts b/playground/server/api/blob/index.put.ts index 9c4dba2f..50ceb779 100644 --- a/playground/server/api/blob/index.put.ts +++ b/playground/server/api/blob/index.put.ts @@ -5,7 +5,7 @@ export default eventHandler(async (event) => { throw createError({ statusCode: 400, message: 'Missing files' }) } - const { put } = useBlob() + const { put } = hubBlob() const objects: BlobObject[] = [] try { for (const file of files) { diff --git a/playground/server/api/kv/[key].delete.ts b/playground/server/api/kv/[key].delete.ts index 25af41f0..c2bfda30 100644 --- a/playground/server/api/kv/[key].delete.ts +++ b/playground/server/api/kv/[key].delete.ts @@ -4,7 +4,7 @@ export default eventHandler(async (event) => { }).parse) // Delete entry for the current user - await useKV().removeItem(key) + await hubKV().removeItem(key) return { key } }) diff --git a/playground/server/api/kv/index.get.ts b/playground/server/api/kv/index.get.ts index 1dad4321..ee3c041c 100644 --- a/playground/server/api/kv/index.get.ts +++ b/playground/server/api/kv/index.get.ts @@ -1,6 +1,6 @@ export default eventHandler(async () => { // List entries for the current user - const storage = useKV() + const storage = hubKV() const keys = await storage.getKeys() // const items = await storage.getItems(keys) diff --git a/playground/server/api/kv/index.put.ts b/playground/server/api/kv/index.put.ts index 6d341595..00828e8c 100644 --- a/playground/server/api/kv/index.put.ts +++ b/playground/server/api/kv/index.put.ts @@ -5,7 +5,7 @@ export default eventHandler(async (event) => { }).parse) // Set entry for the current user - await useKV().setItem(key, value) + await hubKV().setItem(key, value) return { key, value } }) diff --git a/playground/server/api/test.ts b/playground/server/api/test.ts index 4732b480..76c52da0 100644 --- a/playground/server/api/test.ts +++ b/playground/server/api/test.ts @@ -1,5 +1,5 @@ export default eventHandler(async () => { - const db = useDatabase() + const db = hubDatabase() // return useProjectKV(projectUrl).getKeys() // return await db.prepare('SELECT * from todos').all() // return await db.prepare("SELECT * from todos").first() diff --git a/playground/server/utils/db.ts b/playground/server/utils/db.ts index bb983f5b..c2bf69b6 100644 --- a/playground/server/utils/db.ts +++ b/playground/server/utils/db.ts @@ -14,5 +14,5 @@ export const tables = { } export function useDrizzle() { - return drizzle(useDatabase(), { schema: tables }) + return drizzle(hubDatabase(), { schema: tables }) } diff --git a/src/runtime/server/api/_hub/blob/[...pathname].delete.ts b/src/runtime/server/api/_hub/blob/[...pathname].delete.ts index 299432f1..23dd4f00 100644 --- a/src/runtime/server/api/_hub/blob/[...pathname].delete.ts +++ b/src/runtime/server/api/_hub/blob/[...pathname].delete.ts @@ -1,13 +1,13 @@ import { eventHandler, getValidatedRouterParams } from 'h3' import { z } from 'zod' -import { useBlob } from '../../../utils/blob' +import { hubBlob } from '../../../utils/blob' export default eventHandler(async (event) => { const { pathname } = await getValidatedRouterParams(event, z.object({ pathname: z.string().min(1) }).parse) - await useBlob().delete(pathname) + await hubBlob().delete(pathname) return sendNoContent(event) }) diff --git a/src/runtime/server/api/_hub/blob/[...pathname].get.ts b/src/runtime/server/api/_hub/blob/[...pathname].get.ts index 0339ae82..e0ea68f4 100644 --- a/src/runtime/server/api/_hub/blob/[...pathname].get.ts +++ b/src/runtime/server/api/_hub/blob/[...pathname].get.ts @@ -1,6 +1,6 @@ import { eventHandler, getValidatedRouterParams } from 'h3' import { z } from 'zod' -import { useBlob } from '../../../utils/blob' +import { hubBlob } from '../../../utils/blob' export default eventHandler(async (event) => { // TODO: handle caching in production @@ -8,5 +8,5 @@ export default eventHandler(async (event) => { pathname: z.string().min(1) }).parse) - return useBlob().serve(event, pathname) + return hubBlob().serve(event, pathname) }) diff --git a/src/runtime/server/api/_hub/blob/[...pathname].head.ts b/src/runtime/server/api/_hub/blob/[...pathname].head.ts index 6b171fef..2ce5fae9 100644 --- a/src/runtime/server/api/_hub/blob/[...pathname].head.ts +++ b/src/runtime/server/api/_hub/blob/[...pathname].head.ts @@ -1,13 +1,13 @@ import { eventHandler, getValidatedRouterParams, setHeader, sendNoContent } from 'h3' import { z } from 'zod' -import { useBlob } from '../../../utils/blob' +import { hubBlob } from '../../../utils/blob' export default eventHandler(async (event) => { const { pathname } = await getValidatedRouterParams(event, z.object({ pathname: z.string().min(1) }).parse) - const blob = await useBlob().head(pathname) + const blob = await hubBlob().head(pathname) setHeader(event, 'x-blob', JSON.stringify(blob)) diff --git a/src/runtime/server/api/_hub/blob/[...pathname].put.ts b/src/runtime/server/api/_hub/blob/[...pathname].put.ts index 78f66b43..aac8dc1c 100644 --- a/src/runtime/server/api/_hub/blob/[...pathname].put.ts +++ b/src/runtime/server/api/_hub/blob/[...pathname].put.ts @@ -1,6 +1,6 @@ import { eventHandler, getValidatedRouterParams, getHeader, getRequestWebStream } from 'h3' import { z } from 'zod' -import { useBlob } from '../../../utils/blob' +import { hubBlob } from '../../../utils/blob' async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) { const result = new Uint8Array(streamSize) @@ -34,5 +34,5 @@ export default eventHandler(async (event) => { const stream = getRequestWebStream(event)! const body = await streamToArrayBuffer(stream, contentLength) - return useBlob().put(pathname, body, options) + return hubBlob().put(pathname, body, options) }) diff --git a/src/runtime/server/api/_hub/blob/index.get.ts b/src/runtime/server/api/_hub/blob/index.get.ts index 2e73e464..774dfc0a 100644 --- a/src/runtime/server/api/_hub/blob/index.get.ts +++ b/src/runtime/server/api/_hub/blob/index.get.ts @@ -1,6 +1,6 @@ import { eventHandler } from 'h3' -import { useBlob } from '../../../utils/blob' +import { hubBlob } from '../../../utils/blob' export default eventHandler(async () => { - return useBlob().list() + return hubBlob().list() }) diff --git a/src/runtime/server/api/_hub/database/[command].post.ts b/src/runtime/server/api/_hub/database/[command].post.ts index daee922d..fa3932c0 100644 --- a/src/runtime/server/api/_hub/database/[command].post.ts +++ b/src/runtime/server/api/_hub/database/[command].post.ts @@ -1,6 +1,6 @@ import { eventHandler, getValidatedRouterParams, readValidatedBody } from 'h3' import { z } from 'zod' -import { useDatabase } from '../../../utils/database' +import { hubDatabase } from '../../../utils/database' const statementValidation = z.object({ query: z.string().min(1).max(1e6).trim(), @@ -12,7 +12,7 @@ export default eventHandler(async (event) => { const { command } = await getValidatedRouterParams(event, z.object({ command: z.enum(['first', 'all', 'raw', 'run', 'dump', 'exec', 'batch']) }).parse) - const db = useDatabase() + const db = hubDatabase() if (command === 'exec') { const { query } = await readValidatedBody(event, z.object({ diff --git a/src/runtime/server/api/_hub/kv/[...path].ts b/src/runtime/server/api/_hub/kv/[...path].ts index cf7f11ad..c90ec8e8 100644 --- a/src/runtime/server/api/_hub/kv/[...path].ts +++ b/src/runtime/server/api/_hub/kv/[...path].ts @@ -1,9 +1,9 @@ import { eventHandler } from 'h3' import { createH3StorageHandler } from 'unstorage/server' -import { useKV } from '../../../utils/kv' +import { hubKV } from '../../../utils/kv' export default eventHandler(async (event) => { - const storage = useKV() + const storage = hubKV() return createH3StorageHandler(storage, { resolvePath(event) { return event.context.params!.path || '' diff --git a/src/runtime/server/api/_hub/manifest.get.ts b/src/runtime/server/api/_hub/manifest.get.ts index ae15d539..b5b3359f 100644 --- a/src/runtime/server/api/_hub/manifest.get.ts +++ b/src/runtime/server/api/_hub/manifest.get.ts @@ -1,15 +1,15 @@ import { eventHandler } from 'h3' -import { useDatabase } from '../../utils/database' -import { useBlob } from '../../utils/blob' -import { useKV } from '../../utils/kv' +import { hubDatabase } from '../../utils/database' +import { hubBlob } from '../../utils/blob' +import { hubKV } from '../../utils/kv' import { useRuntimeConfig } from '#imports' export default eventHandler(async () => { const { version } = useRuntimeConfig().hub const [ dbCheck, kvCheck, blobCheck ] = await Promise.all([ - falseIfFail(() => useDatabase().exec('PRAGMA table_list')), - falseIfFail(() => useKV().getKeys('__check__')), - falseIfFail(() => useBlob().list({ prefix: '__check__' })) + falseIfFail(() => hubDatabase().exec('PRAGMA table_list')), + falseIfFail(() => hubKV().getKeys('__check__')), + falseIfFail(() => hubBlob().list({ prefix: '__check__' })) ]) return { diff --git a/src/runtime/server/utils/blob.ts b/src/runtime/server/utils/blob.ts index 6b383ce2..1b8d9ba9 100644 --- a/src/runtime/server/utils/blob.ts +++ b/src/runtime/server/utils/blob.ts @@ -52,7 +52,7 @@ function _useBucket() { throw createError(`Missing Cloudflare ${name} binding (R2)`) } -export function useBlob() { +export function hubBlob() { const hub = useRuntimeConfig().hub if (import.meta.dev && hub.projectUrl) { return useProxyBlob(hub.projectUrl, hub.projectSecretKey || hub.userToken) diff --git a/src/runtime/server/utils/database.ts b/src/runtime/server/utils/database.ts index a3fa6612..f18608c8 100644 --- a/src/runtime/server/utils/database.ts +++ b/src/runtime/server/utils/database.ts @@ -7,7 +7,7 @@ import { useRuntimeConfig } from '#imports' let _db: D1Database -export function useDatabase(): D1Database { +export function hubDatabase(): D1Database { if (_db) { return _db } diff --git a/src/runtime/server/utils/kv.ts b/src/runtime/server/utils/kv.ts index 59e7e59f..588575e9 100644 --- a/src/runtime/server/utils/kv.ts +++ b/src/runtime/server/utils/kv.ts @@ -8,7 +8,7 @@ import { useRuntimeConfig } from '#imports' let _kv: Storage -export function useKV(): Storage { +export function hubKV(): Storage { if (_kv) { return _kv }