Skip to content

Commit 44676f1

Browse files
committed
added mongo mapper for select quires
1 parent 7c2db3a commit 44676f1

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/config/mapping.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const sqlToMongoDBcommandsMapping = {
2+
select: "find",
3+
insert: "insertOne",
4+
update: "updateOne",
5+
delete: "deleteOne",
6+
};
7+
8+
export const sqlToMongoDBoperatorsMapping = {
9+
"=": "$eq",
10+
">": "$gt",
11+
"<": "$lt",
12+
">=": "$gte",
13+
"<=": "$lte",
14+
"<>": "$ne",
15+
};
16+
17+
export const mappings = {
18+
mongodb: {
19+
commands: sqlToMongoDBcommandsMapping,
20+
operators: sqlToMongoDBoperatorsMapping,
21+
},
22+
};

src/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mappings } from "./config/mapping.js";
12
import { parseQuery } from "./utils/parser.js";
23

34
export const SqlToNoSql = (query: string) => {
@@ -10,7 +11,30 @@ export const SqlToNoSql = (query: string) => {
1011
}
1112

1213
const q = parseQuery(query);
13-
return q;
14+
15+
const filters: {
16+
[key: string]: {
17+
[operator: string]: string | number;
18+
};
19+
} = {};
20+
21+
q.filters?.forEach((filter) => {
22+
const { column, operator, value } = filter;
23+
24+
if (!filters[column]) {
25+
filters[column] = {
26+
[mappings["mongodb"]["operators"][operator]]: value,
27+
};
28+
}
29+
});
30+
31+
const mongoQuery = {
32+
collection: q.table,
33+
[q.command]: mappings["mongodb"]["commands"][q.command],
34+
query: filters,
35+
};
36+
37+
return mongoQuery;
1438
};
1539

1640
console.log(SqlToNoSql("SELECT * FROM users"));

0 commit comments

Comments
 (0)