Skip to content

Commit

Permalink
Merge pull request #16 from ariflogs/dev
Browse files Browse the repository at this point in the history
Docs update for installation & contributing
  • Loading branch information
ariflogs authored Oct 15, 2023
2 parents 2d3ed3a + b9b469d commit 5fd350f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ src
test
.github
.gitignore
TODO.md
TODO.md
CONTRIBUTING.md
28 changes: 28 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Before You Contribute

### Raise an Issue First

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.

### Avoid Dependencies

We want to keep the project as lightweight as possible. So, please avoid adding any new dependencies unless it's absolutely necessary.

---

## Contributing

- Fork the repository on GitHub to your personal account.
- Clone your forked repository to your local development environment.
- Create a new branch for your feature or bug fix: `git checkout -b your-branch-name`
- Install the project dependencies: `pnpm i`
- Run the project in development mode: `pnpm dev`
- Make changes to your local repository.
- Commit your changes and push them to your forked repository.
- Open a pull request from your forked repository to the **dev branch** of this repository.

## Code Licensing

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.

## Thanks for your contribution!
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ The project is under active development and is not yet ready to use in productio

```bash
npm insall sql-to-nosql

# or

yarn add sql-to-nosql

# or

pnpm add sql-to-nosql
```

## Usage
Expand Down Expand Up @@ -83,3 +91,7 @@ console.log(resp);
- [ ] IS NULL
- [ ] IS NOT NULL
- [ ] Typescript Support

## Contributing

Read the [contributing guide](./CONTRIBUTING.md) to learn how you can contribute to this project.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sql-to-nosql",
"version": "0.1.01",
"version": "0.1.02",
"description": "Run SQL quries on your Mongodb database.",
"repository": {
"type": "git",
Expand All @@ -13,7 +13,7 @@
"start": "tsc && node dist/index.mjs",
"check": "npx prettier . --check",
"pretty": "npx prettier . --write",
"build": "tsc",
"build": "tsc && npm run check",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
Expand Down
6 changes: 3 additions & 3 deletions src/config/mapping.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export const sqlToMongoDBcommandsMapping = {
select: "find", // s!
select: "find",
insert: "insertMany",
update: "updateMany",
delete: "deleteMany",
} as const;

export const sqlToMongoDBoperatorsMapping = {
"=": "$eq", // s!
"!=": "$ne", // s!
"=": "$eq",
"!=": "$ne",
">": "$gt",
"<": "$lt",
">=": "$gte",
Expand Down
47 changes: 31 additions & 16 deletions src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mappings } from "./config/mapping.mjs";
import { parseQuery } from "./utils/parser.mjs";
import { connect } from "./utils/database.mjs";
import { SqlToNoSqlType } from "./types/index.mjs";
import { MongoFindOperationType } from "types/nosql.mjs";

export class SqlToNoSql {
client: MongoClient | undefined;
Expand All @@ -22,29 +23,43 @@ export class SqlToNoSql {

const q = parseQuery(query);

const filters: {
[key: string]: {
[operator: string]: string | number;
};
} = {};
if (q.command !== "select") {
throw new Error("Only select queries are supported");
}

const mongoQuery: MongoFindOperationType = {
collection: q.table,
[q.command]: mappings["mongodb"]["commands"][q.command],
query: {},
fields: {
// coz mongodb by default returns _id
_id: 0,
},
};

// Convert parsed columns to document fields
q.columns?.forEach((column) => {
if (column === "*") {
return;
}
if (column === "_id") {
mongoQuery.fields["_id"] = 1;
return;
}
mongoQuery.fields[column] = 1;
});

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

if (!filters[column]) {
filters[column] = {
if (!mongoQuery.query[column]) {
mongoQuery.query[column] = {
[mappings["mongodb"]["operators"][operator]]: value,
};
}
});

const mongoQuery = {
collection: q.table,
[q.command]: mappings["mongodb"]["commands"][q.command],
query: filters,
};

try {
if (!this.client) {
this.client = await connect(this.config.connection);
Expand All @@ -54,9 +69,9 @@ export class SqlToNoSql {
const db = this.client.db();
const collection = db.collection(mongoQuery.collection);

const data = await collection[mongoQuery[q.command]](
mongoQuery.query,
).toArray();
const data = await collection[mongoQuery[q.command]](mongoQuery.query, {
projection: mongoQuery.fields,
}).toArray();

return data;
} catch (err) {
Expand Down
21 changes: 21 additions & 0 deletions src/types/nosql.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { mappings } from "config/mapping.mjs";
import { SqlCommandOptions } from "./sql.mjs";

export type MongoCommandOptions = "find" | "insertMany";

export interface MongoQueryType {
[key: string]: {
[operator: string]: string | number;
};
}

export interface MongoFieldSelectionType {
[key: string]: 1 | 0;
}

export interface MongoFindOperationType {
select: (typeof mappings)["mongodb"]["commands"]["select"]; // sql -> mongodb
collection: string;
query: MongoQueryType;
fields: MongoFieldSelectionType;
}
8 changes: 5 additions & 3 deletions src/types/sql.mts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
interface filterType {
export type SqlCommandOptions = "select" | "insert";

interface FilterType {
column: string;
operator: "=";
value: string | number;
}

export interface ParsedSqlType {
command: "select";
command: SqlCommandOptions;
table: string;
columns: string[];
filters: filterType[] | null;
filters: FilterType[] | null;
}
3 changes: 2 additions & 1 deletion src/utils/parser.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const parseQuery = (query: string): ParsedSqlType => {
filters: null,
};

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

const lowerCaseCommand = command.toLowerCase();
if (lowerCaseCommand !== "select") {
Expand Down

0 comments on commit 5fd350f

Please sign in to comment.