Skip to content

Commit

Permalink
Fix condition deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Feb 15, 2024
1 parent 0a6e1c5 commit 5da4015
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ export class Condition implements ISequelizable, ISerializable {
);
}
}
static deserialize(value: Condition): Condition {
static deserialize(value: Condition | string): Condition | null {
if (typeof value === "string") {
try {
return Condition.fromJSON(JSON.parse(value));
} catch {
return null;
}
}
return value;
// console.log("??", value);
// if (typeof value === "string") {
Expand Down
18 changes: 14 additions & 4 deletions src/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@ export type ExpressionValue =

export class ExpressionBase implements ISerializable, ISequelizable {
static deserialize(value: ExpressionValue): ExpressionBase {
if (typeof value === "string" && ValueExpression.isValueString(value)) {
const valueIsString = typeof value === "string";
if (valueIsString && ValueExpression.isValueString(value)) {
return ValueExpression.deserialize(value);
}
if (typeof value === "string" && FunctionExpression.isValidString(value)) {
if (valueIsString && FunctionExpression.isValidString(value)) {
return FunctionExpression.deserialize(value);
}
if (typeof value === "string" && OperationExpression.isValidString(value)) {
if (valueIsString && OperationExpression.isValidString(value)) {
return OperationExpression.deserialize(value);
}
if (typeof value === "string" || typeof value === "number") {
if (valueIsString || typeof value === "number") {
return new Expression(value);
}
if (value instanceof Condition) {
return Condition.deserialize(value);
}
if (
valueIsString ||
(value instanceof Expression && typeof value.value === "string")
) {
const condition = Condition.deserialize(value.value || value);
if (condition !== null) {
return condition;
}
}
return value;
}
static deserializeValue(value: ExpressionValue): ValueExpression {
Expand Down
27 changes: 27 additions & 0 deletions src/Function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe("Expression", () => {
).toEqual(
'SUM(IF(`tax_date` BETWEEN "2020-01-01" AND "2020-01-31",`amount`,0))'
);

expect(
Fn.sum(
Fn.if(
Cond.between(`tax_date`, ["2020-01-01", "2020-01-31"]),
"price_last_year",
Fn.string(`0`)
)
).toSQL(flavor)
).toEqual(
'SUM(IF(`tax_date` BETWEEN "2020-01-01" AND "2020-01-31",`price_last_year`,"0"))'
);
});
it("should support serialization for functions", () => {
const serialized =
Expand All @@ -70,5 +82,20 @@ describe("Expression", () => {
// console.log("serialized test:", JSON.stringify(fn.serialize()));

expect(fn2.toSQL(flavor)).toEqual(fn.toSQL(flavor));

const fn3 = Q.select().addField(
Fn.sum(
Fn.if(
Cond.between(`tax_date`, ["2020-01-01", "2020-01-31"]),
"price_last_year",
Fn.string(`0`)
)
),
"blah"
);

expect(Q.deserialize(fn3.serialize()).toSQL(flavor)).toEqual(
fn3.toSQL(flavor)
);
});
});

0 comments on commit 5da4015

Please sign in to comment.