Skip to content

Commit 12c880c

Browse files
committed
added support for >, <, >=, <= operators
1 parent 964316b commit 12c880c

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ console.log(resp);
6969
- [ ] MAX
7070
- [ ] Operators
7171
- [x] =
72-
- [ ] !=
73-
- [ ] >
74-
- [ ] <
75-
- [ ] > =
76-
- [ ] <=
72+
- [x] !=
73+
- [x] >
74+
- [x] <
75+
- [x] >=
76+
- [x] <=
7777
- [ ] AND
7878
- [ ] OR
7979
- [ ] NOT

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sql-to-nosql",
3-
"version": "0.1.0",
3+
"version": "0.1.01",
44
"description": "Run SQL quries on your Mongodb database.",
55
"publishConfig": {
66
"registry": "https://www.npmjs.com/package/sql-to-nosql"

src/config/mapping.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
export const sqlToMongoDBcommandsMapping = {
2-
select: "find",
2+
select: "find", // s!
33
insert: "insertMany",
44
update: "updateMany",
55
delete: "deleteMany",
66
} as const;
77

88
export const sqlToMongoDBoperatorsMapping = {
9-
"=": "$eq",
9+
"=": "$eq", // s!
10+
"!=": "$ne", // s!
1011
">": "$gt",
1112
"<": "$lt",
1213
">=": "$gte",

src/index.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export class SqlToNoSql {
1515
throw new Error("missing query!");
1616
}
1717

18-
// TODO: add better validation!
19-
if ([";", "(", ")"].some((char) => query.includes(char))) {
20-
throw new Error("Invalid query, your query contains invalid characters!");
21-
}
18+
// // TODO: add better validation!
19+
// if ([";", "(", ")"].some((char) => query.includes(char))) {
20+
// throw new Error("Invalid query, your query contains invalid characters!");
21+
// }
2222

2323
const q = parseQuery(query);
2424

src/utils/parser.mts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,32 @@ export const parseQuery = (query: string): ParsedSqlType => {
1010
filters: null,
1111
};
1212

13-
const [command, ...rest] = query.toLocaleLowerCase().split(" ");
13+
const [command, ...rest] = query.split(" ");
1414

15-
if (command !== "select") {
15+
const lowerCaseCommand = command.toLowerCase();
16+
if (lowerCaseCommand !== "select") {
1617
throw new Error("Only select queries are supported");
1718
}
18-
parsedQuery.command = command;
19+
// parsedQuery.command = command;
1920

20-
const fromIndex = rest.indexOf("from");
21+
const fromIndex = rest.findIndex((word) => word.toLowerCase() === "from");
2122
if (fromIndex === -1) {
2223
throw new Error("Invalid query, missing FROM keyword");
2324
}
2425
parsedQuery.table = rest[fromIndex + 1];
2526
parsedQuery.columns = rest.slice(0, fromIndex);
2627

27-
const whereIndex = rest.indexOf("where");
28+
const whereIndex = rest.findIndex((word) => word.toLowerCase() === "where");
2829
if (whereIndex !== -1) {
2930
parsedQuery.filters = [
3031
{
3132
column: rest[whereIndex + 1],
32-
// FIXME: you know what to do here!
3333
operator: rest[whereIndex + 2] as "=",
34-
value: rest[whereIndex + 3],
34+
// to handle string and number values
35+
value:
36+
Number(rest[whereIndex + 3]) ||
37+
// remove quotes from string values
38+
String(rest[whereIndex + 3]).replace(/^'(.*)'$/, "$1"),
3539
},
3640
];
3741
}

0 commit comments

Comments
 (0)