Skip to content

Commit

Permalink
feat: first commit 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroJayat committed Dec 26, 2023
0 parents commit 510b063
Show file tree
Hide file tree
Showing 28 changed files with 3,998 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build

on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js 20.x (LTS)
uses: actions/setup-node@v2
with:
node-version: 20.x
- name: Install and build
run: |
yarn
yarn build
18 changes: 18 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test

on:
push:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js 20.x (LTS)
uses: actions/setup-node@v2
with:
node-version: 20.x
- name: Install and test
run: |
yarn
yarn test
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
bin
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Lautaro Jayat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
142 changes: 142 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Mongo To SQL!

![Testing](https://github.com/lautarojayat/mongo-to-sql/actions/workflows/test.yaml/badge.svg)
![Build](https://github.com/lautarojayat/mongo-to-sql/actions/workflows/build.yaml/badge.svg)



A simple mongo to sql translator powered by Nodejs

![header](assets/header.png)



* [How to start](#how-to-start)
* [1. Clone this repo](#1-clone-this-repo)
* [2. Install yarn](#2-install-yarn)
* [3. Install all dependencies](#3-install-all-dependencies)
* [4. Transpile the project](#4-transpile-the-project)
* [5. Install the package as a cmd utility](#5-install-the-package-as-a-cmd-utility)
* [6. Try to translate a query](#6-try-to-translate-a-query)
* [Usage](#usage)
* [Observations](#observations)
* [String Values](#string-values)
* [Command Line tips](#command-line-tips)
* [Bash escaping](#bash-escaping)
* [Uninstall](#uninstall)

## How to start

### 1. Clone this repo
Clone the project and then go to the root folder
```bash
git clone https://github.com/LautaroJayat/mongo-to-sql.git

cd mongo-to-sql
```

### 2. Install yarn
if you have NPM installed:
```bash
npm install --global yarn
```

if not, you can follow these alternative steps:
https://classic.yarnpkg.com/lang/en/docs/install

### 3. Install all dependencies
```bash
yarn
```

### 4. Transpile the project

```bash
yarn build
```

### 5. Install package as a command line utility
```bash
yarn install:bin

# under the hood it does
# chmod +x bin/cli/index.js && npm i -g .
```

### 6. Translate a query

```bash
mongoToSql "db.myCollection.find({some: 'args'}, { fieldToProject: 1 })"
```
## Usage

### `mongoToSql [options] [ command | arguments ] `

Options:
* `--cool`: prints a pretty title before the output
```bash
mongoToSql --cool "db.myCollection.find({})"
```

Commands:
* `help` : prints a help dialog with all the options
```bash
mongoToSql help
```

Arguments:
* A mongo query that will be used as input
```bash
mongoToSql "db.myCollection.find({my: 'field'})"
```




## Observations

### String Values
When using string values, remember to use single quotes. If you try to quote using double quotes or backticks you will have an error
```js
// this is ok
db.users.find({name: 'smith'})
// but this no
db.users.find({name: "smith"})
```

### Command Line tips
You can use this CLI in three ways:

1. You can introduce a a string containing a command as the first argument:

```1.ash
mongoToSql "db.myCollection.find({some: 'args'}, { fieldToProject: 1 })"
```

2. You can interpolate the value of the first argument by using a subshell:

```bash
mongoToSql $(cat query.js)
```

3. You can pipe a string directly to the command
```bash
cat query.js | mongoToSql
```

### Bash escaping
Mongodb uses `$` as a prefix for their operators, but the same character is an actual operator in bash.
To avoid unexpected errors, if you are providing a string directly into the shell, remember to escape any special character:
```bash
# this will work
mongoToSql "db.users.find({age: { \$gte: 25 }, name: 'smith' })"
# this will interpret 'gre' as an env and will try to interpolate its value
mongoToSql "db.users.find({age: { $gte: 25 }, name: 'smith' })"
```

## Uninstall
Just run:
```bash
yarn remove:bin
```
Binary file added assets/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "mongo-to-sql",
"version": "0.1.0",
"description": "A simple mongo to sql translator",
"main": "bin/cli/index.js",
"repository": "https://github.com/LautaroJayat/mongo-to-sql",
"author": "Lautaro Jayat",
"license": "MIT",
"scripts": {
"build": "tsc",
"test": "jest --verbose",
"translate": "ts-node src/cli/index.ts",
"install:bin": "chmod +x bin/cli/index.js && npm i -g .",
"remove:bin": "npm uninstall -g mongo-to-sql"
},
"bin":{
"mongoToSql": "bin/cli/index.js"
},
"dependencies": {
"@types/node": "^20.10.5",
"figlet": "^1.7.0",
"typescript": "^5.3.3"
},
"devDependencies": {
"@types/figlet": "^1.5.8",
"@types/jest": "^29.5.11",
"add": "^2.0.6",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"yarn": "^1.22.21"
}
}
Loading

0 comments on commit 510b063

Please sign in to comment.