Skip to content

Commit 119f963

Browse files
committed
Ok, OOP it is... 😬
1 parent 44676f1 commit 119f963

File tree

5 files changed

+294
-29
lines changed

5 files changed

+294
-29
lines changed

‎package-lock.json‎

Lines changed: 233 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"typescript": "^5.2.2"
2222
},
2323
"dependencies": {
24+
"mongodb": "^6.1.0",
2425
"sql-to-nosql": "^0.0.3"
2526
},
2627
"type": "module"

‎src/index.ts‎

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
import { mappings } from "./config/mapping.js";
22
import { parseQuery } from "./utils/parser.js";
3+
import { connect } from "./utils/database.js";
4+
import { SqlToNoSqlType } from "./types/index.js";
35

4-
export const SqlToNoSql = (query: string) => {
5-
if (!query) {
6-
throw new Error("missing query!");
6+
export class SqlToNoSql {
7+
constructor(private config: SqlToNoSqlType) {
8+
connect(this.config.connection);
79
}
810

9-
if ([";", "(", ")"].some((char) => query.includes(char))) {
10-
throw new Error("Invalid query, your query contains invalid characters!");
11-
}
12-
13-
const q = parseQuery(query);
11+
run(query: string) {
12+
if (!query) {
13+
throw new Error("missing query!");
14+
}
1415

15-
const filters: {
16-
[key: string]: {
17-
[operator: string]: string | number;
18-
};
19-
} = {};
16+
// TODO: add better validation!
17+
if ([";", "(", ")"].some((char) => query.includes(char))) {
18+
throw new Error("Invalid query, your query contains invalid characters!");
19+
}
2020

21-
q.filters?.forEach((filter) => {
22-
const { column, operator, value } = filter;
21+
const q = parseQuery(query);
2322

24-
if (!filters[column]) {
25-
filters[column] = {
26-
[mappings["mongodb"]["operators"][operator]]: value,
23+
const filters: {
24+
[key: string]: {
25+
[operator: string]: string | number;
2726
};
28-
}
29-
});
27+
} = {};
28+
29+
// Convert parsed filters to MongoDB query
30+
q.filters?.forEach((filter) => {
31+
const { column, operator, value } = filter;
32+
33+
if (!filters[column]) {
34+
filters[column] = {
35+
[mappings["mongodb"]["operators"][operator]]: value,
36+
};
37+
}
38+
});
39+
40+
const mongoQuery = {
41+
collection: q.table,
42+
[q.command]: mappings["mongodb"]["commands"][q.command],
43+
query: filters,
44+
};
3045

31-
const mongoQuery = {
32-
collection: q.table,
33-
[q.command]: mappings["mongodb"]["commands"][q.command],
34-
query: filters,
35-
};
46+
return mongoQuery;
47+
}
48+
}
3649

37-
return mongoQuery;
38-
};
50+
const runner = new SqlToNoSql({
51+
srcDBtype: "postgresql",
52+
destDBtype: "mongodb",
53+
connection: "mongodb://localhost:27017",
54+
});
3955

40-
console.log(SqlToNoSql("SELECT * FROM users"));
41-
console.log(SqlToNoSql("select * from users where id = 1"));
56+
console.log(runner.run("SELECT * FROM users"));
57+
console.log(runner.run("select * from users where id = 1"));

‎src/types/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface SqlToNoSqlType {
2+
srcDBtype: "postgresql";
3+
destDBtype: "mongodb";
4+
connection: string;
5+
}

0 commit comments

Comments
 (0)