Skip to content

Commit

Permalink
fix: add field casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Pomar committed May 3, 2024
1 parent 59db3de commit d1bcaae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Field = {
name: string;
db: string;
cast?: (value: any) => any;
};
25 changes: 15 additions & 10 deletions src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@ export const create_semantics = (
const s = grammar.createSemantics();
s.addAttribute("asMongo", {
FieldExpr(name, operator, value) {
// Valid field
const token = name.asMongo;
const field_def = valid_fields.find((f) => f.name == token);
if (!field_def) {
throw new FaableQLError(`${token} is not a valid field to query`);
}

const ops = {
":": "$eq",
"!:": "$ne",
};
const op = ops[operator.asMongo];
let val;

const set_value = field_def.cast
? field_def.cast(value.asMongo)
: value.asMongo;
let mdb_operator;
if (op == "$eq") {
val = value.asMongo;
mdb_operator = set_value;
} else {
val = { [op]: value.asMongo };
mdb_operator = { [op]: set_value };
}

// Valid field
const token = name.asMongo;
const field_def = valid_fields.find((f) => f.name == token);
if (!field_def) {
throw new FaableQLError(`${token} is not a valid field to query`);
}
const fieldname = field_def.db;
return { [fieldname]: val };
return { [fieldname]: mdb_operator };
},
TextExpr(text) {
return {
Expand Down

0 comments on commit d1bcaae

Please sign in to comment.