Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(port-data): allow passing index sort order on indexDocuments #485 #601

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/port-data/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
44 changes: 36 additions & 8 deletions packages/port-data/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }] }),
}
Expand Down Expand Up @@ -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',
},
Expand All @@ -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',
},
Expand All @@ -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',
},
Expand All @@ -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',
},
Expand All @@ -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',
},
Expand Down Expand Up @@ -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' })
Expand Down
37 changes: 18 additions & 19 deletions packages/port-data/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
}),
Expand All @@ -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({}))),
Expand Down