Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 23 additions & 16 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export type WhereInput<
: FieldIsArray<Schema, Model, Key> extends true
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
: // primitive
PrimitiveFilter<GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
} & {
$expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
} & {
Expand All @@ -249,47 +249,52 @@ type ArrayFilter<T extends string> = {
isEmpty?: boolean;
};

type PrimitiveFilter<T extends string, Nullable extends boolean> = T extends 'String'
? StringFilter<Nullable>
type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean> = T extends 'String'
? StringFilter<Schema, Nullable>
: T extends 'Int' | 'Float' | 'Decimal' | 'BigInt'
? NumberFilter<T, Nullable>
? NumberFilter<Schema, T, Nullable>
: T extends 'Boolean'
? BooleanFilter<Nullable>
: T extends 'DateTime'
? DateTimeFilter<Nullable>
? DateTimeFilter<Schema, Nullable>
: T extends 'Bytes'
? BytesFilter<Nullable>
: T extends 'Json'
? 'Not implemented yet' // TODO: Json filter
: never;

type CommonPrimitiveFilter<DataType, T extends BuiltinType, Nullable extends boolean> = {
type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean> = {
equals?: NullableIf<DataType, Nullable>;
in?: DataType[];
notIn?: DataType[];
lt?: DataType;
lte?: DataType;
gt?: DataType;
gte?: DataType;
not?: PrimitiveFilter<T, Nullable>;
not?: PrimitiveFilter<Schema, T, Nullable>;
};

export type StringFilter<Nullable extends boolean> =
export type StringFilter<Schema extends SchemaDef, Nullable extends boolean> =
| NullableIf<string, Nullable>
| (CommonPrimitiveFilter<string, 'String', Nullable> & {
| (CommonPrimitiveFilter<Schema, string, 'String', Nullable> & {
contains?: string;
startsWith?: string;
endsWith?: string;
mode?: 'default' | 'insensitive';
});
} & (ProviderSupportsCaseSensitivity<Schema> extends true
? {
mode?: 'default' | 'insensitive';
}
: {}));

export type NumberFilter<T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean> =
| NullableIf<number | bigint, Nullable>
| CommonPrimitiveFilter<number, T, Nullable>;
export type NumberFilter<
Schema extends SchemaDef,
T extends 'Int' | 'Float' | 'Decimal' | 'BigInt',
Nullable extends boolean,
> = NullableIf<number | bigint, Nullable> | CommonPrimitiveFilter<Schema, number, T, Nullable>;

export type DateTimeFilter<Nullable extends boolean> =
export type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean> =
| NullableIf<Date | string, Nullable>
| CommonPrimitiveFilter<Date | string, 'DateTime', Nullable>;
| CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable>;

export type BytesFilter<Nullable extends boolean> =
| NullableIf<Uint8Array | Buffer, Nullable>
Expand Down Expand Up @@ -1192,4 +1197,6 @@ type HasToManyRelations<Schema extends SchemaDef, Model extends GetModels<Schema
? false
: true;

type ProviderSupportsCaseSensitivity<Schema extends SchemaDef> = Schema['provider'] extends 'postgresql' ? true : false;

// #endregion
61 changes: 38 additions & 23 deletions packages/runtime/src/client/crud/dialects/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
recurse: (value: unknown) => Expression<SqlBool>,
throwIfInvalid = false,
onlyForKeys: string[] | undefined = undefined,
excludeKeys: string[] = [],
) {
if (payload === null || !isPlainObject(payload)) {
return {
Expand All @@ -472,6 +473,9 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
if (onlyForKeys && !onlyForKeys.includes(op)) {
continue;
}
if (excludeKeys.includes(op)) {
continue;
}
const rhs = Array.isArray(value) ? value.map(getRhs) : getRhs(value);
const condition = match(op)
.with('equals', () => (rhs === null ? eb(lhs, 'is', null) : eb(lhs, '=', rhs)))
Expand Down Expand Up @@ -513,20 +517,23 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return { conditions, consumedKeys };
}

private buildStringFilter(eb: ExpressionBuilder<any, any>, fieldRef: Expression<any>, payload: StringFilter<true>) {
let insensitive = false;
if (payload && typeof payload === 'object' && 'mode' in payload && payload.mode === 'insensitive') {
insensitive = true;
fieldRef = eb.fn('lower', [fieldRef]);
private buildStringFilter(
eb: ExpressionBuilder<any, any>,
fieldRef: Expression<any>,
payload: StringFilter<Schema, true>,
) {
let mode: 'default' | 'insensitive' | undefined;
if (payload && typeof payload === 'object' && 'mode' in payload) {
mode = payload.mode;
}

const { conditions, consumedKeys } = this.buildStandardFilter(
eb,
'String',
payload,
fieldRef,
(value) => this.prepStringCasing(eb, value, insensitive),
(value) => this.buildStringFilter(eb, fieldRef, value as StringFilter<true>),
mode === 'insensitive' ? eb.fn('lower', [fieldRef]) : fieldRef,
(value) => this.prepStringCasing(eb, value, mode),
(value) => this.buildStringFilter(eb, fieldRef, value as StringFilter<Schema, true>),
);

if (payload && typeof payload === 'object') {
Expand All @@ -538,19 +545,19 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {

const condition = match(key)
.with('contains', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`%${value}%`))
: eb(fieldRef, 'like', sql.lit(`%${value}%`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`%${value}%`))
: eb(fieldRef, 'like', sql.val(`%${value}%`)),
)
.with('startsWith', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`${value}%`))
: eb(fieldRef, 'like', sql.lit(`${value}%`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`${value}%`))
: eb(fieldRef, 'like', sql.val(`${value}%`)),
)
.with('endsWith', () =>
insensitive
? eb(fieldRef, 'ilike', sql.lit(`%${value}`))
: eb(fieldRef, 'like', sql.lit(`%${value}`)),
mode === 'insensitive'
? eb(fieldRef, 'ilike', sql.val(`%${value}`))
: eb(fieldRef, 'like', sql.val(`%${value}`)),
)
.otherwise(() => {
throw new Error(`Invalid string filter key: ${key}`);
Expand All @@ -565,13 +572,21 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
return this.and(eb, ...conditions);
}

private prepStringCasing(eb: ExpressionBuilder<any, any>, value: unknown, toLower: boolean = true): any {
private prepStringCasing(
eb: ExpressionBuilder<any, any>,
value: unknown,
mode: 'default' | 'insensitive' | undefined,
): any {
if (!mode || mode === 'default') {
return value === null ? value : sql.val(value);
}

if (typeof value === 'string') {
return toLower ? eb.fn('lower', [sql.lit(value)]) : sql.lit(value);
return eb.fn('lower', [sql.val(value)]);
} else if (Array.isArray(value)) {
return value.map((v) => this.prepStringCasing(eb, v, toLower));
return value.map((v) => this.prepStringCasing(eb, v, mode));
} else {
return value === null ? null : sql.lit(value);
return value === null ? null : sql.val(value);
}
}

Expand Down Expand Up @@ -613,15 +628,15 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
private buildDateTimeFilter(
eb: ExpressionBuilder<any, any>,
fieldRef: Expression<any>,
payload: DateTimeFilter<true>,
payload: DateTimeFilter<Schema, true>,
) {
const { conditions } = this.buildStandardFilter(
eb,
'DateTime',
payload,
fieldRef,
(value) => this.transformPrimitive(value, 'DateTime', false),
(value) => this.buildDateTimeFilter(eb, fieldRef, value as DateTimeFilter<true>),
(value) => this.buildDateTimeFilter(eb, fieldRef, value as DateTimeFilter<Schema, true>),
true,
);
return this.and(eb, ...conditions);
Expand Down
Loading