diff --git a/packages/port-data/deno.jsonc b/packages/port-data/deno.jsonc index d98e5be0..30e3367c 100644 --- a/packages/port-data/deno.jsonc +++ b/packages/port-data/deno.jsonc @@ -4,13 +4,9 @@ "cache": "deno cache --lock=deno.lock --lock-write mod.test.ts" }, "fmt": { - "files": { - "include": ["./"] - }, - "options": { - "lineWidth": 100, - "singleQuote": true, - "semiColons": false - } + "include": ["./"], + "lineWidth": 100, + "singleQuote": true, + "semiColons": false } } diff --git a/packages/port-data/mod.test.ts b/packages/port-data/mod.test.ts index 4b5fa2ea..124739f2 100644 --- a/packages/port-data/mod.test.ts +++ b/packages/port-data/mod.test.ts @@ -12,9 +12,7 @@ const impl: DataPort = { removeDocument: ({ db, id }) => Promise.resolve({ ok: true, id }), listDocuments: ({ db, limit, startkey, endkey, keys, descending }) => Promise.resolve({ ok: true, docs: [] }), - queryDocuments: ({ db, query }) => ( - console.log(query), Promise.resolve({ ok: true, docs: [] }) - ), + queryDocuments: ({ db, query }) => Promise.resolve({ ok: true, docs: [] }), indexDocuments: ({ db, name, fields }) => Promise.resolve({ ok: true }), bulkDocuments: ({ db, docs }) => Promise.resolve({ ok: true, results: [{ ok: true, id: '1' }] }), } @@ -408,7 +406,20 @@ Deno.test('data', async (t) => { query: { selector: { name: { $gt: 'mike' } }, fields: ['name'], - sort: ['ASC' as const], + sort: ['name'], + limit: 1000, + use_index: 'idx-name', + }, + }), + ) + + assert( + await adapter.queryDocuments({ + db: 'foo', + query: { + selector: { name: { $gt: 'mike' } }, + fields: ['name'], + sort: [{ name: 'ASC' }], limit: 1000, use_index: 'idx-name', }, @@ -431,7 +442,7 @@ Deno.test('data', async (t) => { query: { selector: { name: { $gt: 'mike' } }, fields: ['name'], - sort: ['ASC' as const], + sort: ['name'], limit: 1000, use_index: 'idx-name', }, @@ -445,7 +456,7 @@ Deno.test('data', async (t) => { // @ts-ignore selector: 123, fields: ['name'], - sort: ['ASC' as const], + sort: ['name'], limit: 1000, use_index: 'idx-name', }, @@ -459,7 +470,7 @@ Deno.test('data', async (t) => { selector: { name: { $gt: 'mike' } }, // @ts-ignore fields: [123], - sort: ['ASC' as const], + sort: ['name'], limit: 1000, use_index: 'idx-name', }, @@ -473,7 +484,7 @@ Deno.test('data', async (t) => { selector: { name: { $gt: 'mike' } }, fields: ['name'], // @ts-ignore - sort: ['FOO' as const], + sort: ['name', { foo: 'ASC' }], limit: 1000, use_index: 'idx-name', }, @@ -625,6 +636,23 @@ Deno.test('data', async (t) => { }), ) + assert( + await adapter.indexDocuments({ + db: 'foo', + fields: [{ name: 'ASC' }, { foo: 'ASC' }], + name: 'idx-name', + }), + ) + + await assertRejects(() => + adapter.indexDocuments({ + db: 'foo', + // @ts-ignore + fields: ['name', { foo: 'ASC' }], + name: 'idx-name', + }) + ) + await assertRejects(() => // @ts-ignore adapter.indexDocuments({ db: 123, fields: ['name'], name: 'idx-name' }) diff --git a/packages/port-data/port.ts b/packages/port-data/port.ts index 82558638..4d574bee 100644 --- a/packages/port-data/port.ts +++ b/packages/port-data/port.ts @@ -42,24 +42,21 @@ const SortEnum = z.enum(['ASC', 'DESC']) const doc = z.record(z.any()) -const maybeNumber = z.preprocess( - (val) => { - if (typeof val === 'number') return val - /** - * Let zod do its job and reject the value - */ - if (typeof val !== 'string') return val +const maybeNumber = z.preprocess((val) => { + if (typeof val === 'number') return val + /** + * Let zod do its job and reject the value + */ + if (typeof val !== 'string') return val - const parsed = parseFloat(val) - /** - * The string could not be parsed into a number - * so let zod do its job and reject the value - */ - if (isNaN(parsed)) return val - return parsed - }, - z.number(), -) + const parsed = parseFloat(val) + /** + * The string could not be parsed into a number + * so let zod do its job and reject the value + */ + if (isNaN(parsed)) return val + return parsed +}, z.number()) export const port = z.object({ createDatabase: z @@ -110,7 +107,9 @@ export const port = z.object({ // TODO: enforce selector api selector: z.record(z.any()).optional(), fields: z.array(z.string()).optional(), - sort: z.array(z.union([SortEnum, z.record(SortEnum)])).optional(), + sort: z + .union([z.array(z.string()), z.array(z.record(SortEnum))]) + .optional(), limit: maybeNumber.optional(), use_index: z.string().optional(), }), @@ -123,7 +122,7 @@ export const port = z.object({ z.object({ db: z.string(), name: z.string(), - fields: z.array(z.string()), + fields: z.union([z.array(z.string()), z.array(z.record(SortEnum))]), }), ) .returns(z.promise(hyperResponse({}))),