Skip to content

Commit

Permalink
docs: add social card
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 3, 2024
1 parent e8dee46 commit c84d316
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
155 changes: 78 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![nuxt-surrealdb](/docs/public/nuxt-surrealdb-social-card.png)

# Nuxt SurrealDB

[![npm version][npm-version-src]][npm-version-href]
Expand All @@ -16,16 +18,90 @@ A Nuxt module aimed to simplify the use of [SurrealDB](https://surrealdb.com).
This module is still under development and not suitable for production use, proceed at your own risk. Expect breaking changes!
There are no docs atm, so please refer to the [playground](/playground/app.vue) or the [source code](/src/).

## Quick Setup

Install the module to your Nuxt application with one command:

```bash
npx nuxi module add nuxt-surrealdb
```

That's it! You can now edit your [default Database Preset](https://github.com/Sandros94/nuxt-surrealdb?tab=readme-ov-file#database-presets) and use Nuxt SurrealDB in your Nuxt app ✨

## Features

<!-- Highlight some of the features your module provide here -->
- 🚀&nbsp;Custom built-in `$surrealFetch` and `useSurrealFetch` (based on `$fetch` and `useFetch` respectively).
- 📦&nbsp;Custom Database Presets, to be able to use multiple Databases on a per-function/composable basis.
- 📦&nbsp;Custom Database Presets, to be able to use multiple Databases on a composable/per-function basis.
- ⚡️&nbsp;Built-in support for [RPC endpoint](https://surrealdb.com/docs/surrealdb/integration/rpc) via `$surrealRPC` and `useSurrealRPC`.
- 🏗️&nbsp;Built-in Nuxt server `useSurrealRPC` util with server-side private DB Presets for a private network communication with SurrealDB.
- 💡&nbsp;Each RPC method is mapped to a `useSurrealDB` exported function.
- 🌟&nbsp;Built-in support for Websocket communication with RPC methods using the `useSurrealWS` composable.

### Database Presets

It is possible to customize the `default` preset or define your own Database presets either via `nuxt.config.ts` or via `.env`.

> [!NOTE]
> When passing variables to a custom preset like `production` below, it is important to initialize it as an empty object inside `nuxt.config.ts`
```dotenv
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_HOST="https://example.com"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_WS="wss://example.com"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_NS="surrealdb"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_DB="docs"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_SC="user"
# For auth
# user and pass separated by a colon
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH="root:root"
# Or as a Bearer
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH="Bearer mySuperLongBearerToken"
# Or as an object
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH_USER="root"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH_PASS="root"
```

```ts
export default defineNuxtConfig({
modules: ['nuxt-surrealdb'],
surrealdb: {
databases: {
staging: {
host: 'https://staging.example.com',
ws: 'wss://staging.example.com',
NS: 'staging',
DB: 'demo',

// Auth examples
auth: 'root:root',
auth: 'Bearer mySuperLongBearerToken',
auth: {
user: 'root',
pass: 'root'
}
},
production: {
host: '', // initialize any property that will be set via `.env`
ws: '',
NS: '',
DB: ''
},
},
server: {
databases: {
production: {
auth: '', // NUXT_SURREALDB_DATABASES_PRODUCTION_AUTH='root:root'
}
}
}
},
// ...
})
```

It is also possible to expand or change database properties (like `server.databases.production.auth` above) to be available only on Nuxt server-side. This becomes particularly useful for a more traditional database auth approach without exposing credentials client-side or to use a different `host` address in a private network.

### RPC functions

The main `useSurrealDB` exports a number of functions that directly communicate with the RPC endpoint. Each function has two variants, one starts with `$` and one without. The first is based on `$surrealRPC`, that provides the plain function, while the latter uses `useSurrealRPC`, taking advantage of `useSurrealFetch` (and thus, [`useFetch`](https://nuxt.com/docs/api/composables/use-fetch)).
Expand Down Expand Up @@ -94,85 +170,10 @@ const {
```

> [!WARNING]
> Currently while the `signin` and `signup` functions are avaible, they are limited to that Websocket connection. Therefore if data is required outside of that composable it is advised to use the main `useSurrealDB` composable for `SCOPE` user authentication.
### Database Presets

It is possible to customize the `default` preset or define your own Database presets either via `nuxt.config.ts` or `.env`.

> [!NOTE]
> When passing variables to a custom preset like `production` below, it is important to initialize it as an empty object inside `nuxt.config.ts`
```dotenv
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_HOST="https://example.com"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_WS="wss://example.com"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_NS="surrealdb"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_DB="docs"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_SC="user"
# For auth
# user and pass separated by a colon
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH="root:root"
# Or as a Bearer
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH="Bearer mySuperLongBearerToken"
# Or as an object
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH_USER="root"
NUXT_PUBLIC_SURREALDB_DATABASES_PRODUCTION_AUTH_PASS="root"
```

```ts
export default defineNuxtConfig({
modules: ['nuxt-surrealdb'],
surrealdb: {
databases: {
staging: {
host: 'https://staging.example.com',
ws: 'wss://staging.example.com',
NS: 'staging',
DB: 'demo',

// Auth examples
auth: 'root:root',
auth: 'Bearer mySuperLongBearerToken',
auth: {
user: 'root',
pass: 'root'
}
},
production: {
host: '', // initialize any property that will be set via `.env`
ws: '',
NS: '',
DB: ''
},
},
server: {
databases: {
production: {
auth: '', // NUXT_SURREALDB_DATABASES_PRODUCTION_AUTH='root:root'
}
}
}
},
// ...
})
```

It is also possible to expand or change database properties (like `server.databases.production.auth` above) to be available only on Nuxt server-side. This becomes particularly useful for a more traditional database auth approach without exposing credentials client-side or to use a different `host` address in a private network.
> Currently while the `signin` and `signup` functions are avaible, they are limited to that Websocket connection. Therefore if auth is required outside of that websocket connection it is advised to use the main `useSurrealAuth` composable for `SCOPE` user authentication.
---

## Quick Setup

Install the module to your Nuxt application with one command:

```bash
npx nuxi module add nuxt-surrealdb
```

That's it! You can now use Nuxt SurrealDB in your Nuxt app ✨


## Contribution

<details>
Expand Down
Binary file added docs/public/nuxt-surrealdb-social-card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c84d316

Please sign in to comment.