Skip to content

Commit

Permalink
Refactor type checks and field name casting
Browse files Browse the repository at this point in the history
Changed conditions in 'anyWhere' method to check for function types instead of string. Also updated instances of field identifier use to ensure type safety by casting identifier to string. This improves error messaging and overall type consistency across
  • Loading branch information
dennisofficial committed Jun 10, 2024
1 parent 4972f71 commit c593dbe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
18 changes: 9 additions & 9 deletions lib/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createHash } from 'crypto'
import { ulid } from 'ulid'
import {createHash} from 'crypto'
import {ulid} from 'ulid'

import { Entity, EntityKeys } from "../entity"
import {Entity, EntityKeys} from "../entity"

import { IdStrategy, DataStructure, StopWordOptions, SchemaOptions } from './options'
import {DataStructure, IdStrategy, SchemaOptions, StopWordOptions} from './options'

import { FieldDefinition, SchemaDefinition } from './definitions'
import { Field } from './field'
import { InvalidSchema } from '../error'
import {FieldDefinition, SchemaDefinition} from './definitions'
import {Field} from './field'
import {InvalidSchema} from '../error'


/**
Expand Down Expand Up @@ -85,7 +85,7 @@ export class Schema<T extends Entity = Record<string, any>> {
* @param name The name of the {@link Field} in this Schema.
* @returns The {@link Field}, or null of not found.
*/
fieldByName(name: string): Field | null {
fieldByName(name: EntityKeys<T>): Field | null {
return this.#fieldsByName[name] ?? null
}

Expand Down Expand Up @@ -145,7 +145,7 @@ export class Schema<T extends Entity = Record<string, any>> {
#createFields() {
const entries = Object.entries(this.#definition) as [EntityKeys<T>, FieldDefinition][];
return entries.forEach(([fieldName, fieldDef]) => {
const field = new Field(fieldName, fieldDef)
const field = new Field(String(fieldName), fieldDef)
this.#validateField(field)
this.#fieldsByName[fieldName] = field
})
Expand Down
55 changes: 30 additions & 25 deletions lib/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { SearchOptions } from "redis"

import { Client, SearchResults } from "../client"
import { Entity, EntityKeys } from '../entity'
import { Schema } from "../schema"

import { Where } from './where'
import { WhereAnd } from './where-and'
import { WhereOr } from './where-or'
import { WhereField } from './where-field'
import { WhereStringArray } from './where-string-array'
import { WhereHashBoolean, WhereJsonBoolean } from './where-boolean'
import { WhereNumber } from './where-number'
import { WherePoint } from './where-point'
import { WhereString } from './where-string'
import { WhereText } from './where-text'

import { extractCountFromSearchResults, extractEntitiesFromSearchResults, extractEntityIdsFromSearchResults, extractKeyNamesFromSearchResults } from "./results-converter"
import { FieldNotInSchema, RedisOmError, SearchError } from "../error"
import { WhereDate } from "./where-date"
import {SearchOptions} from "redis"

import {Client, SearchResults} from "../client"
import {Entity, EntityKeys} from '../entity'
import {Schema} from "../schema"

import {Where} from './where'
import {WhereAnd} from './where-and'
import {WhereOr} from './where-or'
import {WhereField} from './where-field'
import {WhereStringArray} from './where-string-array'
import {WhereHashBoolean, WhereJsonBoolean} from './where-boolean'
import {WhereNumber} from './where-number'
import {WherePoint} from './where-point'
import {WhereString} from './where-string'
import {WhereText} from './where-text'

import {
extractCountFromSearchResults,
extractEntitiesFromSearchResults,
extractEntityIdsFromSearchResults,
extractKeyNamesFromSearchResults
} from "./results-converter"
import {FieldNotInSchema, RedisOmError, SearchError} from "../error"
import {WhereDate} from "./where-date"

/**
* A function that takes a {@link Search} and returns a {@link Search}. Used in nested queries.
Expand Down Expand Up @@ -98,7 +103,7 @@ export abstract class AbstractSearch<T extends Entity = Record<string, any>> {
const dataStructure = this.schema.dataStructure

if (!field) {
const message = `'sortBy' was called on field '${fieldName}' which is not defined in the Schema.`
const message = `'sortBy' was called on field '${String(fieldName)}' which is not defined in the Schema.`
console.error(message)
throw new RedisOmError(message)
}
Expand Down Expand Up @@ -557,10 +562,10 @@ export class Search<T extends Entity = Record<string, any>> extends AbstractSear
}

private anyWhere(ctor: AndOrConstructor, fieldOrFn: EntityKeys<T> | SubSearchFunction<T>): WhereField<T> | Search<T> {
if (typeof fieldOrFn === 'string') {
return this.anyWhereForField(ctor, fieldOrFn)
} else {
if (typeof fieldOrFn === 'function') {
return this.anyWhereForFunction(ctor, fieldOrFn)
} else {
return this.anyWhereForField(ctor, fieldOrFn)
}
}

Expand Down Expand Up @@ -596,7 +601,7 @@ export class Search<T extends Entity = Record<string, any>> extends AbstractSear
private createWhere(fieldName: EntityKeys<T>): WhereField<T> {
const field = this.schema.fieldByName(fieldName)

if (field === null) throw new FieldNotInSchema(fieldName)
if (field === null) throw new FieldNotInSchema(String(fieldName))

if (field.type === 'boolean' && this.schema.dataStructure === 'HASH') return new WhereHashBoolean(this, field)
if (field.type === 'boolean' && this.schema.dataStructure === 'JSON') return new WhereJsonBoolean(this, field)
Expand Down

0 comments on commit c593dbe

Please sign in to comment.