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

Use interpreter term #2

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
30 changes: 15 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export type TableWithColumns<
[Key in keyof T["columns"]]: T["columns"][Key];
};

type FieldOperator = (
type FieldInterpreter = (
condition: FieldCondition,
table: TableWithColumns,
) => drizzle.SQL | undefined;

type CompoundOperator = (
type CompoundInterpreter = (
conditions: CompoundCondition,
table: TableWithColumns,
) => drizzle.SQL | undefined;

const eq: FieldOperator = (condition, table) => {
const eq: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

if (isRegExp(condition.value)) {
Expand All @@ -30,65 +30,65 @@ const eq: FieldOperator = (condition, table) => {
return drizzle.eq(column, condition.value);
};

const ne: FieldOperator = (condition, table) => {
const ne: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

return drizzle.ne(column, condition.value);
};

const gt: FieldOperator = (condition, table) => {
const gt: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

return drizzle.gt(column, condition.value);
};

const gte: FieldOperator = (condition, table) => {
const gte: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

return drizzle.gte(column, condition.value);
};

const lt: FieldOperator = (condition, table) => {
const lt: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

return drizzle.lt(column, condition.value);
};

const lte: FieldOperator = (condition, table) => {
const lte: FieldInterpreter = (condition, table) => {
const column = table[condition.field];

return drizzle.lte(column, condition.value);
};

// we can't use `in` as const, so we're using `inArray` as alias
// inside the `operators` object we rename it to `in` to avoid confusion
const inArray: FieldOperator = (condition, table) => {
// inside the `interpreters` object we rename it to `in` to avoid confusion
const inArray: FieldInterpreter = (condition, table) => {
const column = table[condition.field];
return drizzle.inArray(column, condition.value as unknown[]);
};

const notInArray: FieldOperator = (condition, table) => {
const notInArray: FieldInterpreter = (condition, table) => {
const column = table[condition.field];
return drizzle.notInArray(column, condition.value as unknown[]);
};

const and: CompoundOperator = (conditions, table) => {
const and: CompoundInterpreter = (conditions, table) => {
return drizzle.and(
...conditions.value.map((condition) => {
return generateSQL(condition, table);
}),
);
};

const or: CompoundOperator = (conditions, table) => {
const or: CompoundInterpreter = (conditions, table) => {
return drizzle.or(
...conditions.value.map((condition) => {
return generateSQL(condition, table);
}),
);
};

const operators: Record<string, FieldOperator | CompoundOperator> = {
const interpreters: Record<string, FieldInterpreter | CompoundInterpreter> = {
eq,
ne,
gt,
Expand All @@ -107,7 +107,7 @@ export function generateSQL<T extends drizzle.TableConfig>(
): drizzle.SQL | undefined {
const { operator } = condition;

const op = operators[operator];
const op = interpreters[operator];

if (!op) {
throw new Error(`Unsupported operator: ${operator}`);
Expand Down
Loading