Skip to content

Commit

Permalink
feat: defaultDatabase selection
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 2, 2024
1 parent 7568bde commit c8b11d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import { defu } from 'defu'

import type { DatabasePreset } from './runtime/types'

type PublicDatabases = PublicRuntimeConfig['surrealdb']['databases']

// Module options TypeScript interface definition
export interface ModuleOptions {
auth?: {
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | false
database?: keyof PublicDatabases | false
sessionName?: string
cookieName?: string
sameSite?: boolean | 'strict' | 'lax' | 'none'
maxAge?: number
}
defaultDatabase?: keyof PublicDatabases
databases?: {
default?: DatabasePreset
[key: string]: DatabasePreset | undefined
}
server?: {
defaultDatabase?: keyof PublicRuntimeConfig['surrealdb']['databases'] | keyof RuntimeConfig['surrealdb']['databases']
defaultDatabase?: keyof PublicDatabases | keyof RuntimeConfig['surrealdb']['databases']
databases?: {
[key: string]: DatabasePreset | undefined
}
Expand All @@ -38,6 +41,7 @@ export default defineNuxtModule<ModuleOptions>({
sameSite: 'lax',
maxAge: 60 * 60 * 24 * 7,
},
defaultDatabase: 'default',
databases: {
default: {
host: '',
Expand All @@ -63,6 +67,7 @@ export default defineNuxtModule<ModuleOptions>({
{
auth: options.auth,
databases: options.databases,
defaultDatabase: options.defaultDatabase,
},
)
// Private RuntimeConfig
Expand All @@ -72,8 +77,8 @@ export default defineNuxtModule<ModuleOptions>({
>(
nuxt.options.runtimeConfig.surrealdb,
{
defaultDatabase: options.server?.defaultDatabase,
databases: options.server?.databases,
defaultDatabase: options.server?.defaultDatabase,
},
)

Expand Down
15 changes: 8 additions & 7 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type { DatabasePreset, Overrides, RpcRequest, RpcResponse, SurrealFetchOp
import { createError, defineNuxtPlugin, useSurrealAuth } from '#imports'

export default defineNuxtPlugin(async ({ $config }) => {
const { databases, auth: { database: authDatabase } } = $config.public.surrealdb
const { databases, defaultDatabase, auth: { database: authDatabase } } = $config.public.surrealdb
const defaultDB = databases[defaultDatabase as keyof PublicRuntimeConfig['surrealdb']['databases']]
const { token: userAuth, session } = useSurrealAuth()

const authToken = authTokenFn(databases.default.auth)
const authToken = authTokenFn(defaultDB.auth)

function authTokenFn(dbAuth: DatabasePreset['auth']) {
if (!dbAuth) return undefined
Expand All @@ -35,19 +36,19 @@ export default defineNuxtPlugin(async ({ $config }) => {
}

const surrealFetch = $fetch.create({
baseURL: databases.default.host,
baseURL: defaultDB.host,
onRequest({ options }) {
options.headers = options.headers || {}

// @ts-expect-error NS header type missing
if (databases.default.NS && options.headers.NS === undefined) {
if (defaultDB.NS && options.headers.NS === undefined) {
// @ts-expect-error NS header type missing
options.headers.NS = databases.default.NS
options.headers.NS = defaultDB.NS
}
// @ts-expect-error DB header type missing
if (databases.default.DB && options.headers.DB === undefined) {
if (defaultDB.DB && options.headers.DB === undefined) {
// @ts-expect-error DB header type missing
options.headers.DB = databases.default.DB
options.headers.DB = defaultDB.DB
}
// @ts-expect-error Authorization header type missing
if (authToken && !userAuth.value && !options.headers.Authorization) {
Expand Down

0 comments on commit c8b11d9

Please sign in to comment.