fix typing to allow PrimaryFieldType as Primitive in where

This commit is contained in:
dswbx
2025-03-05 08:14:15 +01:00
parent ef629321ab
commit dda02807c1
3 changed files with 6 additions and 18 deletions

View File

@@ -1,5 +1,4 @@
import { afterAll, describe, expect, test } from "bun:test"; import { describe, expect, test } from "bun:test";
import { _jsonp } from "../../../src/core/utils";
import { import {
Entity, Entity,
EntityManager, EntityManager,
@@ -10,7 +9,7 @@ import {
WithBuilder, WithBuilder,
} from "../../../src/data"; } from "../../../src/data";
import * as proto from "../../../src/data/prototype"; import * as proto from "../../../src/data/prototype";
import { compileQb, prettyPrintQb, schemaToEm } from "../../helper"; import { schemaToEm } from "../../helper";
import { getDummyConnection } from "../helper"; import { getDummyConnection } from "../helper";
const { dummyConnection } = getDummyConnection(); const { dummyConnection } = getDummyConnection();
@@ -30,7 +29,7 @@ describe("[data] WithBuilder", async () => {
); );
const em = schemaToEm(schema); const em = schemaToEm(schema);
expect(WithBuilder.validateWiths(em, "posts", undefined)).toBe(0); expect(WithBuilder.validateWiths(em, "posts", undefined as any)).toBe(0);
expect(WithBuilder.validateWiths(em, "posts", {})).toBe(0); expect(WithBuilder.validateWiths(em, "posts", {})).toBe(0);
expect(WithBuilder.validateWiths(em, "posts", { users: {} })).toBe(1); expect(WithBuilder.validateWiths(em, "posts", { users: {} })).toBe(1);
expect( expect(

View File

@@ -1,4 +1,6 @@
export type Primitive = string | number | boolean; import type { PrimaryFieldType } from "core";
export type Primitive = PrimaryFieldType | string | number | boolean;
export function isPrimitive(value: any): value is Primitive { export function isPrimitive(value: any): value is Primitive {
return ["string", "number", "boolean"].includes(typeof value); return ["string", "number", "boolean"].includes(typeof value);
} }
@@ -63,7 +65,6 @@ function _convert<Exps extends Expressions>(
expressions: Exps, expressions: Exps,
path: string[] = [], path: string[] = [],
): FilterQuery<Exps> { ): FilterQuery<Exps> {
//console.log("-----------------");
const ExpressionConditionKeys = expressions.map((e) => e.key); const ExpressionConditionKeys = expressions.map((e) => e.key);
const keys = Object.keys($query); const keys = Object.keys($query);
const operands = [OperandOr] as const; const operands = [OperandOr] as const;
@@ -132,8 +133,6 @@ function _build<Exps extends Expressions>(
): ValidationResults { ): ValidationResults {
const $query = options.convert ? _convert<Exps>(_query, expressions) : _query; const $query = options.convert ? _convert<Exps>(_query, expressions) : _query;
//console.log("-----------------", { $query });
//const keys = Object.keys($query);
const result: ValidationResults = { const result: ValidationResults = {
$and: [], $and: [],
$or: [], $or: [],
@@ -150,22 +149,16 @@ function _build<Exps extends Expressions>(
if (!exp.valid(expected)) { if (!exp.valid(expected)) {
throw new Error(`Invalid expected value at "${[...path, $op].join(".")}": ${expected}`); throw new Error(`Invalid expected value at "${[...path, $op].join(".")}": ${expected}`);
} }
//console.log("found exp", { key: exp.key, expected, actual });
return exp.validate(expected, actual, options.exp_ctx); return exp.validate(expected, actual, options.exp_ctx);
} }
// check $and // check $and
//console.log("$and entries", Object.entries($and));
for (const [key, value] of Object.entries($and)) { for (const [key, value] of Object.entries($and)) {
//console.log("$op/$v", Object.entries(value));
for (const [$op, $v] of Object.entries(value)) { for (const [$op, $v] of Object.entries(value)) {
const objValue = options.value_is_kv ? key : options.object[key]; const objValue = options.value_is_kv ? key : options.object[key];
//console.log("--check $and", { key, value, objValue, v_i_kv: options.value_is_kv });
//console.log("validate", { $op, $v, objValue, key });
result.$and.push(__validate($op, $v, objValue, [key])); result.$and.push(__validate($op, $v, objValue, [key]));
result.keys.add(key); result.keys.add(key);
} }
//console.log("-", { key, value });
} }
// check $or // check $or
@@ -173,14 +166,11 @@ function _build<Exps extends Expressions>(
const objValue = options.value_is_kv ? key : options.object[key]; const objValue = options.value_is_kv ? key : options.object[key];
for (const [$op, $v] of Object.entries(value)) { for (const [$op, $v] of Object.entries(value)) {
//console.log("validate", { $op, $v, objValue });
result.$or.push(__validate($op, $v, objValue, [key])); result.$or.push(__validate($op, $v, objValue, [key]));
result.keys.add(key); result.keys.add(key);
} }
//console.log("-", { key, value });
} }
//console.log("matches", matches);
return result; return result;
} }

View File

@@ -2,7 +2,6 @@ import {
type BooleanLike, type BooleanLike,
type FilterQuery, type FilterQuery,
type Primitive, type Primitive,
type TExpression,
exp, exp,
isBooleanLike, isBooleanLike,
isPrimitive, isPrimitive,