From aa0bb2f85aae0f17b5eb0c0c8990f38d5d0e0bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Ara=C3=BAjo?= Date: Sun, 22 Dec 2024 16:15:05 -0300 Subject: [PATCH] Use interpreter term --- src/index.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3c70896..693e817 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)) { @@ -30,49 +30,49 @@ 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); @@ -80,7 +80,7 @@ const and: CompoundOperator = (conditions, table) => { ); }; -const or: CompoundOperator = (conditions, table) => { +const or: CompoundInterpreter = (conditions, table) => { return drizzle.or( ...conditions.value.map((condition) => { return generateSQL(condition, table); @@ -88,7 +88,7 @@ const or: CompoundOperator = (conditions, table) => { ); }; -const operators: Record = { +const interpreters: Record = { eq, ne, gt, @@ -107,7 +107,7 @@ export function generateSQL( ): drizzle.SQL | undefined { const { operator } = condition; - const op = operators[operator]; + const op = interpreters[operator]; if (!op) { throw new Error(`Unsupported operator: ${operator}`);