Skip to content

Commit 6f6aa97

Browse files
authored
Merge pull request #8 from ariflogs/dev
added support for >, <, >=, <= operators
2 parents b20f3df + d31f4d4 commit 6f6aa97

File tree

9 files changed

+48
-34
lines changed

9 files changed

+48
-34
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ src
22
test
33
.github
44
.gitignore
5+
TODO.md

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
### SQL-to-NOSQL
1+
## SQL-to-NOSQL
22

33
As the name suggests, **sql-to-nosql** lets you run SQL scripts on your NoSQL database.
44

55
### Status
66

77
The project is under active development and is not yet ready to use in production. I'm aiming to launch _v-1.0_ by November 2023 with some of the most common sql operations. 🤞
88

9-
### Installation
9+
---
10+
11+
## Installation
1012

1113
```bash
1214
npm insall sql-to-nosql
1315
```
1416

15-
### Usage
17+
## Usage
1618

1719
```js
1820
import { SqlToNoSql } from "sql-to-nosql";
@@ -39,7 +41,7 @@ console.log(resp);
3941

4042
---
4143

42-
### Roadmap
44+
## Roadmap
4345

4446
- [ ] Database
4547
- [x] MongoDB
@@ -67,11 +69,11 @@ console.log(resp);
6769
- [ ] MAX
6870
- [ ] Operators
6971
- [x] =
70-
- [ ] !=
71-
- [ ] >
72-
- [ ] <
73-
- [ ] > =
74-
- [ ] <=
72+
- [x] !=
73+
- [x] >
74+
- [x] <
75+
- [x] > =
76+
- [x] <=
7577
- [ ] AND
7678
- [ ] OR
7779
- [ ] NOT

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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.",
5+
"publishConfig": {
6+
"registry": "https://www.npmjs.com/package/sql-to-nosql"
7+
},
58
"repository": {
69
"type": "git",
710
"url": "https://github.com/ariflogs/SQL-to-NoSQL"
811
},
912
"homepage": "https://github.com/ariflogs/SQL-to-NoSQL",
10-
"main": "dist/index.js",
13+
"main": "dist/index.mjs",
1114
"scripts": {
1215
"dev": "tsc -w",
13-
"start": "tsc && node dist/index.js",
16+
"start": "tsc && node dist/index.mjs",
1417
"build": "npx prettier . --check && tsc",
1518
"test": "echo \"Error: no test specified\" && exit 1",
1619
"pretty": "npx prettier . --write"
@@ -33,6 +36,5 @@
3336
},
3437
"dependencies": {
3538
"mongodb": "^6.1.0"
36-
},
37-
"type": "module"
39+
}
3840
}

src/config/mapping.ts renamed to 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.ts renamed to src/index.mts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { MongoClient } from "mongodb";
22

3-
import { mappings } from "./config/mapping.js";
4-
import { parseQuery } from "./utils/parser.js";
5-
import { connect } from "./utils/database.js";
6-
import { SqlToNoSqlType } from "./types/index.js";
3+
import { mappings } from "./config/mapping.mjs";
4+
import { parseQuery } from "./utils/parser.mjs";
5+
import { connect } from "./utils/database.mjs";
6+
import { SqlToNoSqlType } from "./types/index.mjs";
77

88
export class SqlToNoSql {
99
client: MongoClient | undefined;
@@ -15,13 +15,12 @@ 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);
24-
this.client = await connect(this.config.connection);
2524

2625
const filters: {
2726
[key: string]: {
@@ -47,9 +46,14 @@ export class SqlToNoSql {
4746
};
4847

4948
try {
50-
await this.client.connect();
49+
if (!this.client) {
50+
this.client = await connect(this.config.connection);
51+
await this.client.connect();
52+
}
53+
5154
const db = this.client.db();
5255
const collection = db.collection(mongoQuery.collection);
56+
5357
const data = await collection[mongoQuery[q.command]](
5458
mongoQuery.query,
5559
).toArray();
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/utils/parser.ts renamed to src/utils/parser.mts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// select * from users where id = 1;
22

3-
import { ParsedSqlType } from "types/sql";
3+
import { ParsedSqlType } from "types/sql.mjs";
44

55
export const parseQuery = (query: string): ParsedSqlType => {
66
const parsedQuery: ParsedSqlType = {
@@ -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)