Skip to content

Commit

Permalink
chore: rename use to hub prefix to avoid collision with Nitro
Browse files Browse the repository at this point in the history
In order to better integrate in the future
  • Loading branch information
atinux committed Feb 26, 2024
1 parent 394a044 commit ec7e13f
Show file tree
Hide file tree
Showing 28 changed files with 61 additions and 61 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/content/docs/1.getting-started/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
6 changes: 3 additions & 3 deletions docs/content/docs/2.storage/1.database.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
/*
{
Expand Down
10 changes: 5 additions & 5 deletions docs/content/docs/2.storage/2.kv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}.

Expand All @@ -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()
})
```

Expand All @@ -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)
})
```

Expand All @@ -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)
})
```

Expand All @@ -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 }
})
Expand Down
12 changes: 6 additions & 6 deletions docs/content/docs/2.storage/3.blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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()
})
```

Expand Down Expand Up @@ -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)
})
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 })
})
```

Expand Down Expand Up @@ -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)
})
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/3.recipes/1.hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
})
})
```
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/3.recipes/2.drizzle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}...`)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions docs/content/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' }
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion playground/server/api/blob/[...pathname].delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export default eventHandler(async (event) => {
pathname: z.string().min(1)
}).parse)

return useBlob().delete(pathname)
return hubBlob().delete(pathname)
})
2 changes: 1 addition & 1 deletion playground/server/api/blob/[...pathname].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
2 changes: 1 addition & 1 deletion playground/server/api/blob/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default eventHandler(async () => {
return useBlob().list()
return hubBlob().list()
})
2 changes: 1 addition & 1 deletion playground/server/api/blob/index.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion playground/server/api/kv/[key].delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
2 changes: 1 addition & 1 deletion playground/server/api/kv/index.get.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion playground/server/api/kv/index.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
2 changes: 1 addition & 1 deletion playground/server/api/test.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion playground/server/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const tables = {
}

export function useDrizzle() {
return drizzle(useDatabase(), { schema: tables })
return drizzle(hubDatabase(), { schema: tables })
}
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/blob/[...pathname].delete.ts
Original file line number Diff line number Diff line change
@@ -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)
})
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/blob/[...pathname].get.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

return useBlob().serve(event, pathname)
return hubBlob().serve(event, pathname)
})
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/blob/[...pathname].head.ts
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/blob/[...pathname].put.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
})
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/blob/index.get.ts
Original file line number Diff line number Diff line change
@@ -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()
})
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/database/[command].post.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/api/_hub/kv/[...path].ts
Original file line number Diff line number Diff line change
@@ -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 || ''
Expand Down
Loading

0 comments on commit ec7e13f

Please sign in to comment.