Skip to content

Commit

Permalink
fix: fix select contains any of
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 21, 2024
1 parent 62793f8 commit 4255fef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,24 @@
<span class="flex w-full items-center truncate">
{#if internalValue && internalValue.start}
{#if internalValue.end}
{formatter(internalValue.start.toDate())} - {formatter(internalValue.end.toDate())}
<span class="flex items-center gap-1">
<span class="rounded-sm border bg-gray-50 px-1 py-0.5">
{formatter(internalValue.start.toDate())}
</span>
<span class="mx-1 text-xs text-gray-500"> - </span>
<span class="rounded-sm border bg-gray-50 px-1 py-0.5">
{formatter(internalValue.end.toDate())}
</span>
</span>
{:else}
{formatter(internalValue.start.toDate())}
<span class="rounded-sm border bg-gray-50 px-1 py-0.5">
{formatter(internalValue.start.toDate())}
</span>
{/if}
{:else if value?.[0]}
{formatter(new Date(value[0]))}
<span class="rounded-sm border bg-gray-50 px-1 py-0.5">
{formatter(new Date(value[0]))}
</span>
{/if}
</span>
</Popover.Trigger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
FormulaGTE,
FormulaLT,
FormulaLTE,
ID_TYPE,
JsonContains,
LongTextEqual,
PercentageEqual,
Expand Down Expand Up @@ -52,7 +51,6 @@ import {
type SelectContainsAnyOf,
type SelectEmpty,
type SelectEqual,
type SelectField,
type StringContains,
type StringEmpty,
type StringEndsWith,
Expand All @@ -64,7 +62,7 @@ import {
type UserEmpty,
type UserEqual,
} from "@undb/table"
import { sql, type QueryCreator } from "kysely"
import { type QueryCreator } from "kysely"
import type { IRecordQueryBuilder } from "../qb"

export class RecordQuerySpecCreatorVisitor implements IRecordVisitor {
Expand Down Expand Up @@ -128,16 +126,7 @@ export class RecordQuerySpecCreatorVisitor implements IRecordVisitor {
currencyLT(s: CurrencyLT): void {}
currencyLTE(s: CurrencyLTE): void {}
durationEqual(s: DurationEqual): void {}
selectContainsAnyOf(spec: SelectContainsAnyOf): void {
const field = this.table.schema.getFieldById(spec.fieldId).expect("No field found") as SelectField
if (field.isMultiple) {
this.#creator = (this.#creator || this.qb).with(spec.fieldId.value, (db) =>
db
.selectFrom([this.table.id.value, sql.raw(`json_each(${this.getFieldId(spec)})`).as("json_each")])
.select([`${this.table.id.value}.${ID_TYPE}`, `json_each.value as ${spec.fieldId.value}`]),
)
}
}
selectContainsAnyOf(spec: SelectContainsAnyOf): void {}
selectEmpty(spec: SelectEmpty): void {}
ratingEqual(s: RatingEqual): void {}
emailEqual(s: EmailEqual): void {}
Expand Down Expand Up @@ -176,6 +165,7 @@ export class RecordQuerySpecCreatorVisitor implements IRecordVisitor {
return this
}
not(spec: RecordComositeSpecification): this {
spec.accept(this)
return this
}
clone(): this {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
FormulaGTE,
FormulaLT,
FormulaLTE,
ID_TYPE,
IdEqual,
IdIn,
JsonContains,
Expand Down Expand Up @@ -118,11 +117,7 @@ export class RecordSpecReferenceVisitor implements IRecordVisitor {
currencyLT(s: CurrencyLT): void {}
currencyLTE(s: CurrencyLTE): void {}
durationEqual(s: DurationEqual): void {}
selectContainsAnyOf(spec: SelectContainsAnyOf): void {
this.qb = this.qb
.innerJoin(spec.fieldId.value, `${this.table.id.value}.${ID_TYPE}`, `${spec.fieldId.value}.${ID_TYPE}`)
.groupBy(`${this.table.id.value}.${ID_TYPE}`)
}
selectContainsAnyOf(spec: SelectContainsAnyOf): void {}
selectEmpty(spec: SelectEmpty): void {}
ratingEqual(s: RatingEqual): void {}
emailEqual(s: EmailEqual): void {}
Expand Down Expand Up @@ -151,6 +146,7 @@ export class RecordSpecReferenceVisitor implements IRecordVisitor {
return this
}
not(spec: ISpecification): this {
spec.accept(this)
return this
}
clone(): this {
Expand Down
9 changes: 7 additions & 2 deletions packages/persistence/src/record/record.filter-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import {
startOfTomorrow,
startOfYesterday,
} from "date-fns"
import type { ExpressionBuilder } from "kysely"
import { sql, type ExpressionBuilder } from "kysely"
import { isString, unique } from "radash"
import { AbstractQBVisitor } from "../abstract-qb.visitor"
import { getDateRangeFieldName } from "../underlying/underlying-table.util"
Expand Down Expand Up @@ -394,7 +394,12 @@ export class RecordFilterVisitor extends AbstractQBVisitor<RecordDO> implements
const cond = this.eb.eb(this.getFieldId(spec), "in", spec.value)
this.addCond(cond)
} else {
const cond = this.eb.eb(`${spec.fieldId.value}.${spec.fieldId.value}`, "in", spec.value)
const cond = this.eb.exists(
this.eb
.selectFrom(sql.raw(`json_each(${this.getFieldId(spec)})`).as("json_each"))
.select(["1"])
.where("json_each.value", "in", spec.value),
)
this.addCond(cond)
}
}
Expand Down

0 comments on commit 4255fef

Please sign in to comment.