Skip to content

Commit 5fd350f

Browse files
authored
Merge pull request #16 from ariflogs/dev
Docs update for installation & contributing
2 parents 2d3ed3a + b9b469d commit 5fd350f

File tree

9 files changed

+106
-26
lines changed

9 files changed

+106
-26
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ src
22
test
33
.github
44
.gitignore
5-
TODO.md
5+
TODO.md
6+
CONTRIBUTING.md

CONTRIBUTING.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Before You Contribute
2+
3+
### Raise an Issue First
4+
5+
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.
6+
7+
### Avoid Dependencies
8+
9+
We want to keep the project as lightweight as possible. So, please avoid adding any new dependencies unless it's absolutely necessary.
10+
11+
---
12+
13+
## Contributing
14+
15+
- Fork the repository on GitHub to your personal account.
16+
- Clone your forked repository to your local development environment.
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`
20+
- Make changes to your local repository.
21+
- Commit your changes and push them to your forked repository.
22+
- Open a pull request from your forked repository to the **dev branch** of this repository.
23+
24+
## Code Licensing
25+
26+
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.
27+
28+
## Thanks for your contribution!

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ The project is under active development and is not yet ready to use in productio
1212

1313
```bash
1414
npm insall sql-to-nosql
15+
16+
# or
17+
18+
yarn add sql-to-nosql
19+
20+
# or
21+
22+
pnpm add sql-to-nosql
1523
```
1624

1725
## Usage
@@ -83,3 +91,7 @@ console.log(resp);
8391
- [ ] IS NULL
8492
- [ ] IS NOT NULL
8593
- [ ] Typescript Support
94+
95+
## Contributing
96+
97+
Read the [contributing guide](./CONTRIBUTING.md) to learn how you can contribute to this project.

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.1.01",
3+
"version": "0.1.02",
44
"description": "Run SQL quries on your Mongodb database.",
55
"repository": {
66
"type": "git",
@@ -13,7 +13,7 @@
1313
"start": "tsc && node dist/index.mjs",
1414
"check": "npx prettier . --check",
1515
"pretty": "npx prettier . --write",
16-
"build": "tsc",
16+
"build": "tsc && npm run check",
1717
"test": "echo \"Error: no test specified\" && exit 1"
1818
},
1919
"author": {

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)