Skip to content

Commit

Permalink
refactor(useSurrealDB)!: follow ws param structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 10, 2024
1 parent b984905 commit 9489044
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
17 changes: 8 additions & 9 deletions playground/pages/fetch-examples.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,20 @@ const { data, error, execute } = await select<Product[]>(search, {
error.value && console.error('error', error.value)
const newProduct = reactive<Partial<Product>>({})
const { data: dataCreate, execute: executeCreate } = await create<Product>('products', {
data: newProduct,
})
const { data: dataCreate, execute: executeCreate } = await create<Product>('products', newProduct)
const removeProduct = ref('')
const { execute: executeRemove } = await remove(removeProduct)
const { error: forcedError } = await sql<Product[]>('SELECT * ROM products;', {
server: false,
})
const { error: forcedError } = await sql<Product[]>(
'SELECT * ROM products;',
undefined,
{ server: false },
)
const { signin, signout, session, refreshInfo } = useSurrealAuth()
interface UserCredentials {
const credentials = reactive<{
username?: string
password?: string
}
const credentials = reactive<UserCredentials>({})
}>({})
</script>
56 changes: 32 additions & 24 deletions src/runtime/composables/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ export function useSurrealDB(overrides?: Overrides) {
// create [ thing, data? ]
async function $create<T = any>(
thing: MROGParam<T, 'create', 0>,
options?: Overrides & { data?: MROGParam<T, 'create', 1> },
data?: MROGParam<T, 'create', 1>,
ovr?: Overrides,
) {
const { data, ...ovr } = options || {}
return $surrealRPC<T>({ method: 'create', params: [toValue(thing), toValue(data)] }, ovr || overrides)
}
async function create<T = any>(
thing: MROGParam<T, 'create', 0>,
options?: UseSurrealRpcOptions<T> & { data?: MROGParam<T, 'create', 1> },
data?: MROGParam<T, 'create', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { data, database, immediate, key, token, watch, ...opts } = options || {}
const { database, immediate, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'create'>['params']>(() => ([toValue(thing), toValue(data)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'create', toValue(params)])
Expand Down Expand Up @@ -104,16 +105,17 @@ export function useSurrealDB(overrides?: Overrides) {
// insert [ thing, data? ]
async function $insert<T = any>(
thing: MROGParam<T, 'insert', 0>,
options?: Overrides & { data?: MROGParam<T, 'insert', 1> },
data?: MROGParam<T, 'insert', 1>,
ovr?: Overrides,
) {
const { data, ...ovr } = options || {}
return $surrealRPC<T>({ method: 'insert', params: [toValue(thing), toValue(data)] }, ovr || overrides)
}
async function insert<T = any>(
thing: MROGParam<T, 'insert', 0>,
options?: UseSurrealRpcOptions<T> & { data?: MROGParam<T, 'insert', 1> },
data?: MROGParam<T, 'insert', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { data, database, immediate, key, token, watch, ...opts } = options || {}
const { database, immediate, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'insert'>['params']>(() => ([toValue(thing), toValue(data)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'insert', toValue(params)])
Expand Down Expand Up @@ -154,16 +156,17 @@ export function useSurrealDB(overrides?: Overrides) {
// merge [ thing, data ]
async function $merge<T = any>(
thing: MROGParam<T, 'merge', 0>,
options: Overrides & { data: MROGParam<T, 'merge', 1> },
data: MROGParam<T, 'merge', 1>,
ovr?: Overrides,
) {
const { data, ...ovr } = options
return $surrealRPC<T>({ method: 'merge', params: [toValue(thing), toValue(data)] }, ovr || overrides)
}
async function merge<T = any>(
thing: MROGParam<T, 'merge', 0>,
options: UseSurrealRpcOptions<T> & { data: MROGParam<T, 'merge', 1> },
data: MROGParam<T, 'merge', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { data, database, immediate, key, token, watch, ...opts } = options
const { database, immediate, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'merge'>['params']>(() => ([toValue(thing), toValue(data)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'merge', toValue(params)])
Expand All @@ -181,16 +184,19 @@ export function useSurrealDB(overrides?: Overrides) {
// patch [ thing, patches, diff? ]
async function $patch<T = any>(
thing: MROGParam<T, 'patch', 0>,
options: Overrides & { patches: MROGParam<T, 'patch', 1>, diff?: MROGParam<T, 'patch', 2> },
patches: MROGParam<T, 'patch', 1>,
diff?: MROGParam<T, 'patch', 2>,
ovr?: Overrides,
) {
const { diff, patches, ...ovr } = options
return $surrealRPC<T>({ method: 'patch', params: [toValue(thing), toValue(patches), toValue(diff)] }, ovr || overrides)
}
async function patch<T = any>(
thing: MROGParam<T, 'patch', 0>,
options: UseSurrealRpcOptions<T> & { patches: MROGParam<T, 'patch', 1>, diff?: MROGParam<T, 'patch', 2> },
patches: MROGParam<T, 'patch', 1>,
diff?: MROGParam<T, 'patch', 2>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { database, diff, immediate, key, patches, token, watch, ...opts } = options
const { database, immediate, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'patch'>['params']>(() => ([toValue(thing), toValue(patches), toValue(diff)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'patch', toValue(params)])
Expand All @@ -208,16 +214,17 @@ export function useSurrealDB(overrides?: Overrides) {
// query [ sql, vars? ]
async function $query<T = any>(
sql: MROGParam<T, 'query', 0>,
options?: Overrides & { vars?: MROGParam<T, 'query', 1> },
vars?: MROGParam<T, 'query', 1>,
ovr?: Overrides,
) {
const { vars, ...ovr } = options || {}
return $surrealRPC<T>({ method: 'query', params: [toValue(sql), toValue(vars)] }, ovr || overrides)
}
async function query<T = any>(
sql: MROGParam<T, 'query', 0>,
options?: UseSurrealRpcOptions<T> & { vars?: MROGParam<T, 'query', 1> },
vars?: MROGParam<T, 'query', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { database, key, vars, token, watch, ...opts } = options || {}
const { database, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'query'>['params']>(() => ([toValue(sql), toValue(vars)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'query', toValue(params)])
Expand Down Expand Up @@ -337,16 +344,17 @@ export function useSurrealDB(overrides?: Overrides) {
// update [ thing, data? ]
async function $update<T = any>(
thing: MROGParam<T, 'update', 0>,
options?: Overrides & { data?: MROGParam<T, 'update', 1> },
data?: MROGParam<T, 'update', 1>,
ovr?: Overrides,
) {
const { data, ...ovr } = options || {}
return $surrealRPC<T>({ method: 'update', params: [toValue(thing), toValue(data)] }, ovr || overrides)
}
async function update<T = any>(
thing: MROGParam<T, 'update', 0>,
options?: UseSurrealRpcOptions<T> & { data?: MROGParam<T, 'update', 1> },
data?: MROGParam<T, 'update', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { data, database, immediate, key, token, watch, ...opts } = options || {}
const { database, immediate, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'update'>['params']>(() => ([toValue(thing), toValue(data)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'update', toValue(params)])
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/utils/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export function useSurrealDB(event: H3Event, overrides?: ServerOverrides) {
// query [ sql, vars ]
async function query<T = any>(
sql: RpcParams<T, 'query'>[0],
options?: ServerOverrides & { vars?: RpcParams<T, 'query'>[1] },
vars?: RpcParams<T, 'query'>[1],
ovr?: ServerOverrides,
) {
const { vars, ...ovr } = options || {}
return useSurrealRPC<T>(event, { method: 'query', params: [sql, vars] }, ovr || overrides)
}

Expand Down

0 comments on commit 9489044

Please sign in to comment.