Browse Source

Update function signature for query conversion

Use key-value to ensure less duplicates in coercing query param values.
master
TheoryOfNekomata 7 months ago
parent
commit
c0b298bab0
2 changed files with 46 additions and 17 deletions
  1. +35
    -13
      packages/core/src/backend/data-source/queries.ts
  2. +11
    -4
      packages/core/test/features/query.test.ts

+ 35
- 13
packages/core/src/backend/data-source/queries.ts View File

@@ -27,27 +27,47 @@ export interface DataSourceQueryAndGrouping {
expressions: DataSourceQueryOrGrouping[];
}

export const COERCE_VALUES_TYPES = [
'number',
'string',
'boolean',
] as const;
interface ProcessEntryBase {
type: string;
}

export interface ProcessEntryString extends ProcessEntryBase {
type: 'string';
startsWith?: boolean;
endsWith?: boolean;
caseInsensitive?: boolean;
includes?: boolean;
}

export interface ProcessEntryNumber extends ProcessEntryBase {
type: 'number';
}

export type CoerceValuesType = typeof COERCE_VALUES_TYPES[number];
export interface ProcessEntryBoolean extends ProcessEntryBase {
type: 'boolean';
truthyStrings?: string[];
}

type CoerceValues = Partial<Record<CoerceValuesType, string[]>>;
export type ProcessEntry = ProcessEntryString | ProcessEntryNumber | ProcessEntryBoolean;

interface ConvertToDataSourceQueryCollectionOptions {
coerceValues: CoerceValues;
processEntries: Record<string, ProcessEntry>;
//coerceValues: CoerceValues;
//stringSearch: StringSearch;
}

const normalizeRhs = (lhs: string, rhs: string, coerceValues: CoerceValues) => {
if (coerceValues?.number?.includes(lhs)) {
const normalizeRhs = (rhs: string, coerceValues?: ProcessEntry) => {
if (coerceValues?.type === 'number') {
return Number(rhs);
}

if (coerceValues?.boolean?.includes(lhs)) {
return rhs.trim().toLowerCase() === 'true';
if (coerceValues?.type === 'boolean') {
const truthyStrings = [
...(coerceValues.truthyStrings ?? []).map((s) => s.trim().toLowerCase()),
'true',
];

return truthyStrings.includes(rhs);
}

return rhs?.toString() ?? '';
@@ -67,7 +87,7 @@ export const convertToDataSourceQueryCollection = (
expressions: [],
};
const existingLhs = existingOr.expressions.find((ex) => ex.lhs === key);
const rhs = normalizeRhs(key, value, options.coerceValues);
const rhs = normalizeRhs(value, options.processEntries?.[key]);

if (typeof existingLhs === 'undefined') {
return {
@@ -115,3 +135,5 @@ export const convertToDataSourceQueryCollection = (

// all the queries are to be treated as "AND", as suggested by the & character to separate the query param entries
};

// TODO add more operators

+ 11
- 4
packages/core/test/features/query.test.ts View File

@@ -16,8 +16,10 @@ describe('query', () => {
const collection = convertToDataSourceQueryCollection(
q,
{
coerceValues: {
number: ['attr'],
processEntries: {
attr: {
type: 'number'
}
},
},
);
@@ -58,8 +60,13 @@ describe('query', () => {
const collection = convertToDataSourceQueryCollection(
q,
{
coerceValues: {
boolean: ['attr', 'attr2'],
processEntries: {
attr: {
type: 'boolean',
},
attr2: {
type: 'boolean',
}
},
},
);


Loading…
Cancel
Save