Skip to content

Commit

Permalink
feat(connect): add support for sort syntax on data.index #485
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Jun 1, 2023
1 parent cd5d7ca commit 7abbf98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/connect/deno/services/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { toDataQuery } from '../utils/hyper-query.ts'

import { Action, HyperRequestFunction, ListOptions, Method, QueryOptions } from '../types.ts'
import {
Action,
HyperRequestFunction,
IndexFieldOptions,
ListOptions,
Method,
QueryOptions,
} from '../types.ts'
import { HYPER_LEGACY_GET_HEADER } from '../utils/hyper-request.ts'

const service = 'data' as const
Expand Down Expand Up @@ -38,17 +45,19 @@ export const query = (selector: unknown, options?: QueryOptions) => (hyper: Hype
})
export const bulk = (docs: unknown[]) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, action: Action.BULK, body: docs })
export const index = (indexName: string, fields: string[]) => (hyper: HyperRequestFunction) =>
hyper({
service,
method: Method.POST,
action: Action.INDEX,
body: {
fields,
name: indexName,
type: 'JSON',
},
})
export const index =
(indexName: string, fields: string[] | { [k: string]: IndexFieldOptions }[]) =>
(hyper: HyperRequestFunction) =>
hyper({
service,
method: Method.POST,
action: Action.INDEX,
body: {
fields,
name: indexName,
type: 'JSON',
},
})

export const create = () => (hyper: HyperRequestFunction) => hyper({ service, method: Method.PUT })

Expand Down
6 changes: 6 additions & 0 deletions packages/connect/deno/tests/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ test('data.index', async () => {
const body = await result.json()
assertEquals(body.name, 'foo')
assertEquals(body.fields[0], 'type')

const withSort = await index('foo', [{ type: 'ASC' }, { bar: 'ASC' }])(mockRequest)
const bodyWithSort = await withSort.json()
assertEquals(bodyWithSort.name, 'foo')
assertEquals(bodyWithSort.fields[0], { type: 'ASC' })
assertEquals(bodyWithSort.fields[1], { bar: 'ASC' })
})

test('data.create', async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/connect/deno/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const SortOptions = {
ASC: 'ASC',
} as const

export type IndexFieldOptions = SortOptions
export const IndexFieldOptions = SortOptions

export type Method = typeof Method[keyof typeof Method]
export const Method = {
GET: 'GET',
Expand Down

0 comments on commit 7abbf98

Please sign in to comment.