Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Feb 21, 2024
1 parent 196109c commit cbd48b8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
24 changes: 22 additions & 2 deletions docs/content/2.usage/1.database.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@ title: Database
description: How to create a database and store entries with NuxtHub.
---

NuxtHub Database is a layer to [Cloudflare D1](https://developers.cloudflare.com/d1), serverless SQL databases.
NuxtHub Database is a layer to [Cloudflare D1](https://developers.cloudflare.com/d1){target=_blank}, serverless SQL databases.

<!-- TODO: config, binding ? -->

Once properly configured, NuxtHub module exposes a server composable to the application.

## `useDatabase()`

Server composable that returns a set of methods from [D1Database](https://developers.cloudflare.com/d1/build-databases/query-databases/).
Server composable that returns a set of methods from [D1Database](https://developers.cloudflare.com/d1/build-databases/query-databases/){target=_blank}.

::callout{icon="i-heroicons-light-bulb"}
This composable exposes low level API, so we recommand using our pattern with [`useDB()`](/recipes/drizzle#usedb) incombination to [Drizzle](https://orm.drizzle.team/docs/overview){target=_blank} to enhance your DX.
::

### `exec()`

Executes one or more queries directly without prepared statements or parameters binding.

[Read more on Cloudflare](https://developers.cloudflare.com/d1/build-databases/query-databases/#await-dbexec){target=_blank}.

### `dump()`

Dumps the entire D1 database to an SQLite compatible file inside an ArrayBuffer.

[Read more on Cloudflare](https://developers.cloudflare.com/d1/build-databases/query-databases/#await-dbdump){target=_blank}.

### `prepare()`

Generates a prepared statement to be used later.

[Read more on Cloudflare](https://developers.cloudflare.com/d1/build-databases/query-databases/#prepared-and-static-statements){target=_blank}.

### `batch()`

Sends a list of prepared statements and get the results in the same order.

[Read more on Cloudflare](https://developers.cloudflare.com/d1/build-databases/query-databases/#dbbatch){target=_blank}.
4 changes: 2 additions & 2 deletions docs/content/2.usage/2.kv.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ title: KV
description: How to use key-value data storage with NuxtHub.
---

NuxtHub KV is a layer to [Cloudflare Workers KV](https://developers.cloudflare.com/kv), a global, low-latency, key-value data storage.
NuxtHub KV is a layer to [Cloudflare Workers KV](https://developers.cloudflare.com/kv){target=_blank}, a global, low-latency, key-value data storage.

<!-- TODO: config, binding ? -->

Once properly configured, NuxtHub module exposes a server composable to the application.

## `useKV()`

Server composable that returns a [Storage](https://unstorage.unjs.io/getting-started/usage#interface).
Server composable that returns a [Storage](https://unstorage.unjs.io/getting-started/usage#interface){target=_blank}.
4 changes: 2 additions & 2 deletions docs/content/2.usage/3.blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Blob
description: How to store objects with NuxtHub.
---

NuxtHub Blob is a layer to [Cloudflare R2](https://developers.cloudflare.com/r2), allowing to store large amounts of unstructured data.
NuxtHub Blob is a layer to [Cloudflare R2](https://developers.cloudflare.com/r2){target=_blank}, allowing to store large amounts of unstructured data.

<!-- TODO: config, binding ? -->

Expand Down Expand Up @@ -45,7 +45,7 @@ export default eventHandler(async (event) => {

#### Params

- `event`: [`H3Event`](https://github.com/unjs/h3)
- `event`: [`H3Event`](https://github.com/unjs/h3){target=_blank}
- `pathname`: `string`

#### Return
Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.usage/4.analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Analytics
description: How to track events with NuxtHub.
---

NuxtHub Analytics is a layer to [Cloudflare Workers Analytics Engine](https://developers.cloudflare.com/analytics/analytics-engine/), allowing to get analytics about anything.
NuxtHub Analytics is a layer to [Cloudflare Workers Analytics Engine](https://developers.cloudflare.com/analytics/analytics-engine/){target=_blank}, allowing to get analytics about anything.

<!-- TODO: config, binding ? -->

Expand Down
32 changes: 32 additions & 0 deletions docs/content/3.recipes/1.validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ description:

## Blob

To enhance your development experience with blobs, here are some utils:

### `ensureBlob()`

`ensureBlob()` is a handy util to validate a `Blob` by checking its size and type:

```ts
export default eventHandler(async (event) => {
const form = await readFormData(event)
const file = form.get('file') as Blob

if (!file || !file.size) {
throw createError({ statusCode: 400, message: 'No file provided' })
}

ensureBlob(file, { maxSize: '1MB', types: ['image' ]})

// ...
})
```

#### Params

- `file`: `Blob`
- `options`: `Object`
- `maxSize`: `string`
- `types`: `string[]`

#### Return

Returns nothing.

Throws an error if `file` doesn't meet the requirements.

<!-- TODO -->
11 changes: 10 additions & 1 deletion docs/content/3.recipes/2.hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ description:

## Hooks

### `onHubReady`
### `onHubReady()`

Use `onHubReady()` to ensure the execution of some code once NuxtHub environment bindings are set.

```ts
onHubReady(async () => {
const myConfig = useKV().getItem('my-config')
// ...
})
```

<!-- TODO -->
33 changes: 33 additions & 0 deletions docs/content/3.recipes/3.drizzle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Drizzle
description:
---

## Database

To enhance your development experience with databases, we recommand creating this type of composable:

### `useDB()`

```ts
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'
import { drizzle } from 'drizzle-orm/d1'
export { sql } from 'drizzle-orm'

const todos = sqliteTable('todos', {
id: integer('id').primaryKey(),
title: text('title').notNull(),
completed: integer('completed').notNull().default(0),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
})

export const tables = {
todos
}

export function useDB() {
return drizzle(useDatabase(), { schema: tables })
}
```

This allows you to conveniently reference your tables and interact directly with the [Drizzle API](https://orm.drizzle.team/docs/overview).

0 comments on commit cbd48b8

Please sign in to comment.