Skip to content

Commit b9b469d

Browse files
committed
added support for searching specific columns/fields πŸš€
1 parent 2ec105c commit b9b469d

File tree

8 files changed

+70
-28
lines changed

8 files changed

+70
-28
lines changed

β€ŽCONTRIBUTING.mdβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
## Before You Contribute
22

33
### Raise an Issue First
4+
45
Before you invest a significant amount of time on a change, please create a issue describing your proposal. This will help us to make sure that the change is in line with the project's goals, roadmap and avoid duplicate work.
56

67
### Avoid Dependencies
8+
79
We want to keep the project as lightweight as possible. So, please avoid adding any new dependencies unless it's absolutely necessary.
810

911
---
@@ -12,9 +14,9 @@ We want to keep the project as lightweight as possible. So, please avoid adding
1214

1315
- Fork the repository on GitHub to your personal account.
1416
- Clone your forked repository to your local development environment.
15-
- Create a new branch for your feature or bug fix: ```git checkout -b your-branch-name```
16-
- Install the project dependencies: ```pnpm i```
17-
- Run the project in development mode: ```pnpm dev```
17+
- Create a new branch for your feature or bug fix: `git checkout -b your-branch-name`
18+
- Install the project dependencies: `pnpm i`
19+
- Run the project in development mode: `pnpm dev`
1820
- Make changes to your local repository.
1921
- Commit your changes and push them to your forked repository.
2022
- Open a pull request from your forked repository to the **dev branch** of this repository.
@@ -23,4 +25,4 @@ We want to keep the project as lightweight as possible. So, please avoid adding
2325

2426
Your contributions are subject to the project's open-source(MIT) license. By submitting a PR, you agree to release your code under this license.
2527

26-
## Thanks for your contribution!
28+
## Thanks for your contribution!

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@ console.log(resp);
9393
- [ ] Typescript Support
9494

9595
## Contributing
96+
9697
Read the [contributing guide](./CONTRIBUTING.md) to learn how you can contribute to this project.

β€Ž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.01",
3+
"version": "0.1.02",
44
"description": "Run SQL quries on your Mongodb database.",
55
"repository": {
66
"type": "git",

β€Žsrc/config/mapping.mtsβ€Ž

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

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

β€Žsrc/index.mtsβ€Ž

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { mappings } from "./config/mapping.mjs";
44
import { parseQuery } from "./utils/parser.mjs";
55
import { connect } from "./utils/database.mjs";
66
import { SqlToNoSqlType } from "./types/index.mjs";
7+
import { MongoFindOperationType } from "types/nosql.mjs";
78

89
export class SqlToNoSql {
910
client: MongoClient | undefined;
@@ -22,29 +23,43 @@ export class SqlToNoSql {
2223

2324
const q = parseQuery(query);
2425

25-
const filters: {
26-
[key: string]: {
27-
[operator: string]: string | number;
28-
};
29-
} = {};
26+
if (q.command !== "select") {
27+
throw new Error("Only select queries are supported");
28+
}
29+
30+
const mongoQuery: MongoFindOperationType = {
31+
collection: q.table,
32+
[q.command]: mappings["mongodb"]["commands"][q.command],
33+
query: {},
34+
fields: {
35+
// coz mongodb by default returns _id
36+
_id: 0,
37+
},
38+
};
39+
40+
// Convert parsed columns to document fields
41+
q.columns?.forEach((column) => {
42+
if (column === "*") {
43+
return;
44+
}
45+
if (column === "_id") {
46+
mongoQuery.fields["_id"] = 1;
47+
return;
48+
}
49+
mongoQuery.fields[column] = 1;
50+
});
3051

3152
// Convert parsed filters to MongoDB query
3253
q.filters?.forEach((filter) => {
3354
const { column, operator, value } = filter;
3455

35-
if (!filters[column]) {
36-
filters[column] = {
56+
if (!mongoQuery.query[column]) {
57+
mongoQuery.query[column] = {
3758
[mappings["mongodb"]["operators"][operator]]: value,
3859
};
3960
}
4061
});
4162

42-
const mongoQuery = {
43-
collection: q.table,
44-
[q.command]: mappings["mongodb"]["commands"][q.command],
45-
query: filters,
46-
};
47-
4863
try {
4964
if (!this.client) {
5065
this.client = await connect(this.config.connection);
@@ -54,9 +69,9 @@ export class SqlToNoSql {
5469
const db = this.client.db();
5570
const collection = db.collection(mongoQuery.collection);
5671

57-
const data = await collection[mongoQuery[q.command]](
58-
mongoQuery.query,
59-
).toArray();
72+
const data = await collection[mongoQuery[q.command]](mongoQuery.query, {
73+
projection: mongoQuery.fields,
74+
}).toArray();
6075

6176
return data;
6277
} catch (err) {

β€Žsrc/types/nosql.mtsβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { mappings } from "config/mapping.mjs";
2+
import { SqlCommandOptions } from "./sql.mjs";
3+
4+
export type MongoCommandOptions = "find" | "insertMany";
5+
6+
export interface MongoQueryType {
7+
[key: string]: {
8+
[operator: string]: string | number;
9+
};
10+
}
11+
12+
export interface MongoFieldSelectionType {
13+
[key: string]: 1 | 0;
14+
}
15+
16+
export interface MongoFindOperationType {
17+
select: (typeof mappings)["mongodb"]["commands"]["select"]; // sql -> mongodb
18+
collection: string;
19+
query: MongoQueryType;
20+
fields: MongoFieldSelectionType;
21+
}

β€Žsrc/types/sql.mtsβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
interface filterType {
1+
export type SqlCommandOptions = "select" | "insert";
2+
3+
interface FilterType {
24
column: string;
35
operator: "=";
46
value: string | number;
57
}
68

79
export interface ParsedSqlType {
8-
command: "select";
10+
command: SqlCommandOptions;
911
table: string;
1012
columns: string[];
11-
filters: filterType[] | null;
13+
filters: FilterType[] | null;
1214
}

β€Žsrc/utils/parser.mtsβ€Ž

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

13-
const [command, ...rest] = query.split(" ");
13+
// for splliting by comma and space
14+
const [command, ...rest] = query.split(/, |,| /);
1415

1516
const lowerCaseCommand = command.toLowerCase();
1617
if (lowerCaseCommand !== "select") {

0 commit comments

Comments
Β (0)