From 7abbf984455cb8e21555ec565f615238e39ead23 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Thu, 1 Jun 2023 10:37:54 -0400 Subject: [PATCH] feat(connect): add support for sort syntax on data.index #485 --- packages/connect/deno/services/data.ts | 33 +++++++++++++++--------- packages/connect/deno/tests/data.test.ts | 6 +++++ packages/connect/deno/types.ts | 3 +++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/connect/deno/services/data.ts b/packages/connect/deno/services/data.ts index d63fde7e..333b40a6 100644 --- a/packages/connect/deno/services/data.ts +++ b/packages/connect/deno/services/data.ts @@ -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 @@ -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 }) diff --git a/packages/connect/deno/tests/data.test.ts b/packages/connect/deno/tests/data.test.ts index 35a6a673..8f7db0c2 100644 --- a/packages/connect/deno/tests/data.test.ts +++ b/packages/connect/deno/tests/data.test.ts @@ -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 () => { diff --git a/packages/connect/deno/types.ts b/packages/connect/deno/types.ts index 5de1ef16..7d8ae80b 100644 --- a/packages/connect/deno/types.ts +++ b/packages/connect/deno/types.ts @@ -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',