Skip to content

Commit

Permalink
feat: handle default database param merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jul 11, 2024
1 parent 2faf8e3 commit bf9a6c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ export default defineNuxtModule<ModuleOptions>({
ws: '',
NS: '',
DB: '',
SC: '',
KV: '',
},
},
server: {
Expand Down
25 changes: 25 additions & 0 deletions src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { AsyncData } from 'nuxt/app'
import type { FetchError } from 'ofetch'
import { hash } from 'ohash'
import { createDefu, defu } from 'defu'

import type {
DatabasePreset,
DatabasePresetKeys,
KeysOf,
PickFrom,
UseSurrealFetchOptions,
Expand All @@ -21,6 +24,7 @@ import {
toValue,
useFetch,
useNuxtApp,
useRuntimeConfig,
} from '#imports'

export function useSurrealFetch<
Expand Down Expand Up @@ -92,3 +96,24 @@ export function useSurrealRPC<
key: _key,
})
}

export function useSurrealDatabases(): {
[key in DatabasePresetKeys]: DatabasePreset
} {
const {
databases: publicDatabases,
defaultDatabase,
} = useRuntimeConfig().public.surrealdb
const defaultDB = publicDatabases[defaultDatabase as DatabasePresetKeys]

const defuDatabases = createDefu((obj, key, value) => {
obj[key] = defu(value, obj[key], defaultDB)
return true
})

const databases = defuDatabases(publicDatabases, {
[defaultDatabase]: defaultDB,
})

return databases
}
27 changes: 21 additions & 6 deletions src/runtime/server/utils/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { textToBase64 } from 'undio'
import type { H3Event } from 'h3'
import { ofetch } from 'ofetch'
import { getCookie } from 'h3'
import { defu } from 'defu'
import { createDefu, defu } from 'defu'

import type {
DatabasePreset,
DatabasePresetKeys,
DatabasePresetServerKeys,
RpcRequest,
ServerOverrides,
Expand All @@ -33,22 +34,36 @@ export function useSurrealDatabases(event?: H3Event): {
const {
surrealdb: {
databases: privateDatabases,
defaultDatabase: defaultPrivateDatabase,
},
public: {
surrealdb: {
databases: publicDatabases,
databases: _publicDatabases,
defaultDatabase: defaultPublicDatabase,
},
},
} = useRuntimeConfig(event)
const defaultPrivateDB = privateDatabases[defaultPrivateDatabase as DatabasePresetKeys]
const defaultPublicDB = _publicDatabases[defaultPublicDatabase as DatabasePresetKeys]

const databases = defu<
const defuPublicDatabases = createDefu((obj, key, value) => {
obj[key] = defu(value, obj[key], defaultPublicDB)
return true
})
const publicDatabases = defuPublicDatabases(_publicDatabases, {
[defaultPublicDatabase]: defaultPublicDB,
})
const defuDatabases = createDefu((obj, key, value) => {
obj[key] = defu(value, obj[key], defaultPrivateDB)
return true
})

const databases = defuDatabases<
RuntimeConfig['surrealdb']['databases'],
PublicRuntimeConfig['surrealdb']['databases'][]
>(privateDatabases, publicDatabases)

return {
...databases,
}
return databases
}

export function useSurrealFetch<
Expand Down

0 comments on commit bf9a6c6

Please sign in to comment.