SQL-in-MongoDB
This tools can convert common sql query to mongodb query. Support node version >= 12
:star: Why we need this tool
If we have a complex query condition, use mongodb query we should write complex json object, and sometime we often missing the brackets, but use this tools, we just need write an simple sql query, and it can convert it to mongodb query.
:rocket: How to use
1. install dependency
npm install sql-in-mongodb
or
yarn add sql-in-mongodb
2. Code example
import SQLParser from 'sql-in-mongodb';
const sqlQuery = `where a = 1`;
const parser = new SQLParser();
const data = await TestModel.find(parser.parseSql(sqlQuery));
console.log(JSON.stringify(data));
If you want select some fields
import SQLParser from 'sql-in-mongodb';
const sqlQuery = `select a,b from test where a = 1`;
const parser = new SQLParser();
const mongoQuery = parser.parseSql(sqlQuery);
const selectedFileds = parser.getSelectedFeilds(sqlQuery);
const data = await TestModel.findOne(mongoQuery, selectedFileds);
console.log(JSON.stringify(data));
:question: SQLParser() Option parameters
export type Option = {
likeOpsCaseSensitive?: boolean,
multipleLineSql?: boolean,
database?: Database,
}
const DEFAULT_OPTIONS: Option = {
likeOpsCaseSensitive: false,
multipleLineSql: false,
database: 'mysql',
}
:eyes: Todo
- Support cli
- Support each database advanced query statement, only suppport common sql query now