Skip to content

Commit

Permalink
refactor: use literal string union instead of enum for filter operato…
Browse files Browse the repository at this point in the history
…rs (#47)
  • Loading branch information
fahchen authored Aug 15, 2024
1 parent db3a2fe commit 3381034
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
3 changes: 1 addition & 2 deletions examples/delete_movie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LogicalFilterOperator } from '../types/mod.ts'
import type { DeleteAction } from '../types/persistence_schema/mod.ts'
import moviesSchema from './schemas/movies.ts'

Expand All @@ -11,7 +10,7 @@ const deleteMovieAction: DeleteAction = {
},
},
filter: {
operator: LogicalFilterOperator.EQ,
operator: 'eq',
operands: [
{ type: 'schema', value: '/id' },
{ type: 'data', value: '/id' },
Expand Down
10 changes: 3 additions & 7 deletions examples/delete_movies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
ConditionalFilterOperator,
LogicalFilterOperator,
} from '../types/mod.ts'
import type { BulkDeleteAction } from '../types/persistence_schema/mod.ts'
import moviesSchema from './schemas/movies.ts'

Expand All @@ -14,17 +10,17 @@ const deleteMovieAction: BulkDeleteAction = {
},
},
filter: {
operator: ConditionalFilterOperator.AND,
operator: 'and',
operands: [
{
operator: LogicalFilterOperator.LT,
operator: 'lt',
operands: [
{ type: 'schema', value: '/release_date' },
{ type: 'data', value: '/release_date' },
],
},
{
operator: LogicalFilterOperator.IS_NULL,
operator: 'is_null',
operands: [{ type: 'schema', value: '/likes' }],
},
],
Expand Down
3 changes: 1 addition & 2 deletions examples/update_movie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LogicalFilterOperator } from '../types/mod.ts'
import type { UpdateAction } from '../types/persistence_schema/mod.ts'
import moviesSchema from './schemas/movies.ts'

Expand All @@ -14,7 +13,7 @@ const updateMovieAction: UpdateAction = {
required: ['id'],
},
filter: {
operator: LogicalFilterOperator.EQ,
operator: 'eq',
operands: [
{ type: 'schema', value: '/id' },
{ type: 'data', value: '/id' },
Expand Down
3 changes: 1 addition & 2 deletions examples/update_movie_with_characters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LogicalFilterOperator } from '../types/mod.ts'
import type { UpdateAction } from '../types/persistence_schema/mod.ts'
import moviesSchema from './schemas/movies.ts'

Expand Down Expand Up @@ -27,7 +26,7 @@ const updateMovieWithCharactersAction: UpdateAction = {
required: ['id'],
},
filter: {
operator: LogicalFilterOperator.EQ,
operator: 'eq',
operands: [
{ type: 'schema', value: '/id' },
{ type: 'data', value: '/id' },
Expand Down
14 changes: 5 additions & 9 deletions examples/update_movies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
ConditionalFilterOperator,
LogicalFilterOperator,
} from '../types/mod.ts'
import type { BulkUpdateAction } from '../types/persistence_schema/mod.ts'
import moviesSchema from './schemas/movies.ts'

Expand All @@ -14,24 +10,24 @@ const updateMoviesAction: BulkUpdateAction = {
},
},
filter: {
operator: ConditionalFilterOperator.AND,
operator: 'and',
operands: [
{
operator: LogicalFilterOperator.EQ,
operator: 'eq',
operands: [
{ type: 'schema', value: '/created_at' },
{ type: 'schema', value: '/updated_at' },
],
},
{
operator: ConditionalFilterOperator.OR,
operator: 'or',
operands: [
{
operator: LogicalFilterOperator.IS_NULL,
operator: 'is_null',
operands: [{ type: 'schema', value: '/likes' }],
},
{
operator: LogicalFilterOperator.IS_NULL,
operator: 'is_null',
operands: [{ type: 'schema', value: '/release_date' }],
},
],
Expand Down
10 changes: 5 additions & 5 deletions types/persistence_schema/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ export enum LogicalFilterOperator {
}

export interface ConditionalFilter {
operator: ConditionalFilterOperator
operator: `${ConditionalFilterOperator}`
operands: Array<ConditionalFilter | LogicalFilter>
}

export type LogicalFilter = COFilter | EQFilter | LTFilter | IsNullFilter

export interface COFilter {
operator: LogicalFilterOperator.CO
operator: `${LogicalFilterOperator.CO}`
operands: [Operand, Operand]
}

export interface EQFilter {
operator: LogicalFilterOperator.EQ
operator: `${LogicalFilterOperator.EQ}`
operands: [Operand, Operand]
}

export interface LTFilter {
operator: LogicalFilterOperator.LT
operator: `${LogicalFilterOperator.LT}`
operands: [Operand, Operand]
}

export interface IsNullFilter {
operator: LogicalFilterOperator.IS_NULL
operator: `${LogicalFilterOperator.IS_NULL}`
operands: [Operand]
}

Expand Down

0 comments on commit 3381034

Please sign in to comment.