Skip to content

Commit 7e7130b

Browse files
committed
added inital suppport for Select command with where operation
1 parent 119f963 commit 7e7130b

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.env
22
node_modules
33
build
4-
dist
4+
dist
5+
TODO.md

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ npm insall sql-to-nosql
1717
### Roadmap
1818

1919
- [ ] Database
20-
- [ ] MongoDB
20+
- [x] MongoDB
2121
- [ ] Commands
22-
- [ ] SELECT
22+
- [x] SELECT
2323
- [ ] INSERT
2424
- [ ] DELETE
2525
- [ ] UPDATE
2626
- [ ] Clauses
27-
- [ ] WHERE
27+
- [x] WHERE
2828
- [ ] ORDER BY
2929
- [ ] LIMIT
3030
- [ ] OFFSET
@@ -41,7 +41,7 @@ npm insall sql-to-nosql
4141
- [ ] MIN
4242
- [ ] MAX
4343
- [ ] Operators
44-
- [ ] =
44+
- [x] =
4545
- [ ] !=
4646
- [ ] >
4747
- [ ] <

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sql-to-nosql",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Run SQL quries on your Mongodb database.",
55
"repository": {
66
"type": "git",
@@ -25,4 +25,4 @@
2525
"sql-to-nosql": "^0.0.3"
2626
},
2727
"type": "module"
28-
}
28+
}

src/config/mapping.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export const sqlToMongoDBcommandsMapping = {
22
select: "find",
3-
insert: "insertOne",
4-
update: "updateOne",
5-
delete: "deleteOne",
6-
};
3+
insert: "insertMany",
4+
update: "updateMany",
5+
delete: "deleteMany",
6+
} as const;
77

88
export const sqlToMongoDBoperatorsMapping = {
99
"=": "$eq",
@@ -12,7 +12,7 @@ export const sqlToMongoDBoperatorsMapping = {
1212
">=": "$gte",
1313
"<=": "$lte",
1414
"<>": "$ne",
15-
};
15+
} as const;
1616

1717
export const mappings = {
1818
mongodb: {

src/index.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { MongoClient } from "mongodb";
2+
13
import { mappings } from "./config/mapping.js";
24
import { parseQuery } from "./utils/parser.js";
35
import { connect } from "./utils/database.js";
46
import { SqlToNoSqlType } from "./types/index.js";
57

68
export class SqlToNoSql {
7-
constructor(private config: SqlToNoSqlType) {
8-
connect(this.config.connection);
9-
}
9+
client: MongoClient | undefined;
10+
11+
constructor(private config: SqlToNoSqlType) {}
1012

11-
run(query: string) {
13+
async run(query: string) {
1214
if (!query) {
1315
throw new Error("missing query!");
1416
}
@@ -19,6 +21,7 @@ export class SqlToNoSql {
1921
}
2022

2123
const q = parseQuery(query);
24+
this.client = await connect(this.config.connection);
2225

2326
const filters: {
2427
[key: string]: {
@@ -43,15 +46,18 @@ export class SqlToNoSql {
4346
query: filters,
4447
};
4548

46-
return mongoQuery;
49+
try {
50+
await this.client.connect();
51+
const db = this.client.db();
52+
const collection = db.collection(mongoQuery.collection);
53+
const data = await collection[mongoQuery[q.command]](
54+
mongoQuery.query,
55+
).toArray();
56+
57+
return data;
58+
} catch (err) {
59+
console.error(err);
60+
throw Error("Something went wrong!");
61+
}
4762
}
4863
}
49-
50-
const runner = new SqlToNoSql({
51-
srcDBtype: "postgresql",
52-
destDBtype: "mongodb",
53-
connection: "mongodb://localhost:27017",
54-
});
55-
56-
console.log(runner.run("SELECT * FROM users"));
57-
console.log(runner.run("select * from users where id = 1"));

src/utils/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const connect = async (url: string) => {
44
try {
55
const client = new MongoClient(url);
66
await client.connect();
7+
return client;
78
} catch (error) {
89
throw new Error("Could not connect to Database!");
910
}

test/test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
import { greetings } from "sql-to-nosql";
1+
const runner = new SqlToNoSql({
2+
srcDBtype: "postgresql",
3+
destDBtype: "mongodb",
4+
connection: process.env.TEST_MONGODB_URI,
5+
});
26

3-
console.log(greetings());
7+
console.log(runner.run("select * from users where role = admin"));

0 commit comments

Comments
 (0)