Skip to content

Commit

Permalink
Merge pull request #16 from sandros94/dev
Browse files Browse the repository at this point in the history
Refactoring Database preset handling and SurrealDB 2.0 support
  • Loading branch information
sandros94 authored Sep 13, 2024
2 parents 6b6bd64 + e0701be commit f724e3d
Show file tree
Hide file tree
Showing 24 changed files with 2,284 additions and 1,911 deletions.
38 changes: 3 additions & 35 deletions docs/database-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineNuxtConfig({
},

website: {
// This preset will inherit `host`, `ws` and `NS` from the default preset above
DB: 'website',
},

Expand All @@ -27,16 +28,11 @@ export default defineNuxtConfig({
ws: 'wss://crm.example.com',
NS: 'demo',
DB: 'crm',
// The following bearer auth is exposed client side!
// The following bearer token is exposed client side!
auth: 'mySuperLongBearerToken',
},

shop: {
host: '', // initialize any property that will be set via `.env`
ws: '',
NS: '',
DB: '',
},
shop: {}, // initialize the preset and edit it via `.env`
},
},
})
Expand All @@ -50,10 +46,6 @@ NUXT_PUBLIC_SURREALDB_DATABASES_SHOP_DB="docs"
NUXT_PUBLIC_SURREALDB_DATABASES_SHOP_SC="user"
```

::: tip
When editing variables via `.env` to a custom preset, like the `shop` preset above, it is important to initialize any parameter as an empty string inside your `nuxt.config.ts`.
:::

### Database Presets for development

If you want to use different Database Presets between development and production, please use Nuxt's native [`$development` and `$production` properties](https://nuxt.com/docs/getting-started/configuration#environment-overrides) within your `nuxt.config.ts` like in the example below.
Expand Down Expand Up @@ -86,30 +78,6 @@ export default defineNuxtConfig({
})
```

## Set a Preset as the Default Database

It is also possible to set a particular Preset as the Default Database, with the ability to set a different one between client and server side.

```ts
export default defineNuxtConfig({
modules: ['nuxt-surrealdb'],
surrealdb: {
defaultDatabase: 'website',
databases: {
default: {
host: 'https://example.com',
ws: 'wss://example.com',
NS: 'production',
DB: 'example',
},
website: {
DB: 'website',
}
}
}
})
```

## Server-Side Database Presets

It is also possible to expand or change preset properties to be available only 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. Server-Side Presets are availbale under `surrealdb.server.databases` (or `runtimeConfig.surrealdb.databases`) inside your `nuxt.config.ts`.
Expand Down
1 change: 1 addition & 0 deletions docs/rpc-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Here the full list:
const {
authenticate, // $authenticate
create, // $create
delete, // $delete
info, // $info
insert, // $insert
invalidate, // $invalidate
Expand Down
5 changes: 3 additions & 2 deletions docs/ws-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const {
close,
create,
data,
set, // Surreal's `let`
set, // alias for `let`
info,
insert,
invalidate,
kill,
let,
live,
merge,
open,
Expand All @@ -27,7 +28,7 @@ const {
send,
signin,
signup,
sql, // alias for query
sql, // alias for `query`
status,
unset,
update,
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ export default createConfigForNuxt({
max: 3,
},
}],

// Project Specific
'@typescript-eslint/no-empty-object-type': 'off',
},
})
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@
"docs:preview": "vitepress preview docs"
},
"dependencies": {
"@nuxt/kit": "^3.13.0",
"@vueuse/core": "^10.11.1",
"@nuxt/kit": "^3.13.1",
"@vueuse/core": "^11.0.3",
"undio": "^0.2.0"
},
"devDependencies": {
"@nuxt/devtools": "^1.3.14",
"@nuxt/eslint-config": "^0.3.13",
"@nuxt/devtools": "^1.4.2",
"@nuxt/eslint-config": "^0.5.7",
"@nuxt/module-builder": "^0.8.4",
"@nuxt/schema": "^3.13.0",
"@nuxt/test-utils": "^3.14.1",
"@types/node": "^20.14.10",
"@nuxt/schema": "^3.13.1",
"@nuxt/test-utils": "^3.14.2",
"changelogen": "^0.5.5",
"eslint": "^9.9.1",
"nuxt": "^3.13.0",
"vite": "^5.4.2",
"eslint": "^9.10.0",
"nuxt": "^3.13.1",
"vite": "^5.4.4",
"vitepress": "^1.3.4",
"vitest": "^1.6.0",
"vue-tsc": "^2.0.29"
"vitest": "^2.0.5",
"vue-tsc": "^2.1.6"
},
"resolutions": {
"nuxt-surrealdb": "workspace:*"
Expand Down
14 changes: 3 additions & 11 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
export default defineNuxtConfig({
modules: ['nuxt-surrealdb'],
modules: ['../src/module'],
surrealdb: {
auth: {
database: 'staging',
},
databases: {
staging: {
host: '',
ws: '',
NS: '',
DB: '',
SC: '',
},
staging: {},
nasa: {
host: 'https://surrealdb.s94.dev',
ws: 'wss://surrealdb.s94.dev',
NS: 'demo',
DB: 'nasa',
},
example: {
DB: '',
},
example: {},
},
server: {
databases: {
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
},
"dependencies": {
"nuxt-surrealdb": "latest",
"nuxt": "^3.11.2"
"nuxt": "^3.13.1"
}
}
5 changes: 4 additions & 1 deletion playground/pages/fetch-examples.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ interface Product {
const { create, remove, select, sql } = useSurrealDB({
database: 'staging',
token: false,
})
const search = ref('products')
const { data, error, execute } = await select<Product[]>(search, {
watch: [search],
})
error.value && console.error('error', error.value)
if (error.value) {
console.error('error', error.value)
}
const newProduct = reactive<Partial<Product>>({})
const { data: dataCreate, execute: executeCreate } = await create<Product>('products', newProduct)
Expand Down
Loading

0 comments on commit f724e3d

Please sign in to comment.